Permalink
Browse files

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
  • Loading branch information...
shergin authored and facebook-github-bot committed Nov 12, 2017
1 parent e75bd87 commit aa83b5a0ca30736b2800833bcc6149dcbe8436fa
@@ -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
@@ -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
@@ -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
@@ -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

0 comments on commit aa83b5a

Please sign in to comment.