Introducing RCTSurfaceBackedComponent

Summary:
RCTSurfaceBackedComponent is ComponentKit component represents a React Native Surface created
(and stored in the state) with given `bridge`, `moduleName`, and `properties`.

Differential Revision: D6217103

fbshipit-source-id: 2849f68e1975562cd47851bda232e389705b4229
This commit is contained in:
Valentin Shergin 2017-11-11 21:15:20 -08:00 committed by Facebook Github Bot
parent e75bd87a76
commit aa83b5a0ca
4 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,28 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <ComponentKit/CKComponent.h>
#import <ComponentKit/CKCompositeComponent.h>
#import <RCTSurfaceHostingComponent/RCTSurfaceHostingComponentOptions.h>
@class RCTBridge;
/**
* ComponentKit component represents a React Native Surface created
* (and stored in the state) with given `bridge`, `moduleName`,
* and `properties`.
*/
@interface RCTSurfaceBackedComponent : CKCompositeComponent
+ (instancetype)newWithBridge:(RCTBridge *)bridge
moduleName:(NSString *)moduleName
properties:(NSDictionary *)properties
options:(RCTSurfaceHostingComponentOptions)options;
@end

View File

@ -0,0 +1,68 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTSurfaceBackedComponent.h"
#import <UIKit/UIKit.h>
#import <ComponentKit/CKComponentSubclass.h>
#import <ComponentKit/CKOverlayLayoutComponent.h>
#import <RCTSurfaceHostingComponent/RCTSurfaceHostingComponent.h>
#import <React/RCTSurface.h>
#import "RCTSurfaceBackedComponentState.h"
@implementation RCTSurfaceBackedComponent
+ (id)initialState
{
return [RCTSurfaceBackedComponentState new];
}
+ (instancetype)newWithBridge:(RCTBridge *)bridge
moduleName:(NSString *)moduleName
properties:(NSDictionary *)properties
options:(RCTSurfaceHostingComponentOptions)options
{
CKComponentScope scope(self, moduleName);
RCTSurfaceBackedComponentState *state = scope.state();
if (state.surface == nil || state.surface.bridge != bridge || ![state.surface.moduleName isEqualToString:moduleName]) {
RCTSurface *surface =
[[RCTSurface alloc] initWithBridge:bridge
moduleName:moduleName
initialProperties:properties];
state = [RCTSurfaceBackedComponentState newWithSurface:surface];
CKComponentScope::replaceState(scope, state);
}
else {
if (![state.surface.properties isEqualToDictionary:properties]) {
state.surface.properties = properties;
}
}
RCTSurfaceHostingComponent *surfaceHostingComponent =
[RCTSurfaceHostingComponent newWithSurface:state.surface
options:options];
CKComponent *component;
if (options.activityIndicatorComponentFactory == nil || state.surface.stage & RCTSurfaceStageSurfaceDidInitialLayout) {
component = surfaceHostingComponent;
} else {
component = [CKOverlayLayoutComponent newWithComponent:surfaceHostingComponent
overlay:options.activityIndicatorComponentFactory()];
}
return [super newWithComponent:component];
}
@end

View File

@ -0,0 +1,20 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <UIKit/UIKit.h>
@class RCTSurface;
@interface RCTSurfaceBackedComponentState: NSObject
@property (atomic, readonly, strong) RCTSurface *surface;
+ (instancetype)newWithSurface:(RCTSurface *)surface;
@end

View File

@ -0,0 +1,30 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTSurfaceBackedComponentState.h"
#import <React/RCTSurface.h>
@implementation RCTSurfaceBackedComponentState
+ (instancetype)newWithSurface:(RCTSurface *)surface
{
return [[self alloc] initWithSurface:surface];
}
- (instancetype)initWithSurface:(RCTSurface *)surface
{
if (self == [super init]) {
_surface = surface;
}
return self;
}
@end