Skip to content

Commit

Permalink
Introducing RCTSurfaceBackedComponent
Browse files Browse the repository at this point in the history
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 aa83b5a
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponent.h
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponent.mm
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponentState.h
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponentState.mm
Original file line number Diff line number Diff line change
@@ -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.