Skip to content

Commit aa83b5a

Browse files
sherginfacebook-github-bot
authored andcommittedNov 12, 2017
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
1 parent e75bd87 commit aa83b5a

4 files changed

+146
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import <ComponentKit/CKComponent.h>
11+
#import <ComponentKit/CKCompositeComponent.h>
12+
#import <RCTSurfaceHostingComponent/RCTSurfaceHostingComponentOptions.h>
13+
14+
@class RCTBridge;
15+
16+
/**
17+
* ComponentKit component represents a React Native Surface created
18+
* (and stored in the state) with given `bridge`, `moduleName`,
19+
* and `properties`.
20+
*/
21+
@interface RCTSurfaceBackedComponent : CKCompositeComponent
22+
23+
+ (instancetype)newWithBridge:(RCTBridge *)bridge
24+
moduleName:(NSString *)moduleName
25+
properties:(NSDictionary *)properties
26+
options:(RCTSurfaceHostingComponentOptions)options;
27+
28+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "RCTSurfaceBackedComponent.h"
11+
12+
#import <UIKit/UIKit.h>
13+
14+
#import <ComponentKit/CKComponentSubclass.h>
15+
#import <ComponentKit/CKOverlayLayoutComponent.h>
16+
#import <RCTSurfaceHostingComponent/RCTSurfaceHostingComponent.h>
17+
#import <React/RCTSurface.h>
18+
19+
#import "RCTSurfaceBackedComponentState.h"
20+
21+
@implementation RCTSurfaceBackedComponent
22+
23+
+ (id)initialState
24+
{
25+
return [RCTSurfaceBackedComponentState new];
26+
}
27+
28+
+ (instancetype)newWithBridge:(RCTBridge *)bridge
29+
moduleName:(NSString *)moduleName
30+
properties:(NSDictionary *)properties
31+
options:(RCTSurfaceHostingComponentOptions)options
32+
{
33+
CKComponentScope scope(self, moduleName);
34+
35+
RCTSurfaceBackedComponentState *state = scope.state();
36+
37+
if (state.surface == nil || state.surface.bridge != bridge || ![state.surface.moduleName isEqualToString:moduleName]) {
38+
RCTSurface *surface =
39+
[[RCTSurface alloc] initWithBridge:bridge
40+
moduleName:moduleName
41+
initialProperties:properties];
42+
43+
state = [RCTSurfaceBackedComponentState newWithSurface:surface];
44+
45+
CKComponentScope::replaceState(scope, state);
46+
}
47+
else {
48+
if (![state.surface.properties isEqualToDictionary:properties]) {
49+
state.surface.properties = properties;
50+
}
51+
}
52+
53+
RCTSurfaceHostingComponent *surfaceHostingComponent =
54+
[RCTSurfaceHostingComponent newWithSurface:state.surface
55+
options:options];
56+
57+
CKComponent *component;
58+
if (options.activityIndicatorComponentFactory == nil || state.surface.stage & RCTSurfaceStageSurfaceDidInitialLayout) {
59+
component = surfaceHostingComponent;
60+
} else {
61+
component = [CKOverlayLayoutComponent newWithComponent:surfaceHostingComponent
62+
overlay:options.activityIndicatorComponentFactory()];
63+
}
64+
65+
return [super newWithComponent:component];
66+
}
67+
68+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import <UIKit/UIKit.h>
11+
12+
@class RCTSurface;
13+
14+
@interface RCTSurfaceBackedComponentState: NSObject
15+
16+
@property (atomic, readonly, strong) RCTSurface *surface;
17+
18+
+ (instancetype)newWithSurface:(RCTSurface *)surface;
19+
20+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "RCTSurfaceBackedComponentState.h"
11+
12+
#import <React/RCTSurface.h>
13+
14+
@implementation RCTSurfaceBackedComponentState
15+
16+
+ (instancetype)newWithSurface:(RCTSurface *)surface
17+
{
18+
return [[self alloc] initWithSurface:surface];
19+
}
20+
21+
- (instancetype)initWithSurface:(RCTSurface *)surface
22+
{
23+
if (self == [super init]) {
24+
_surface = surface;
25+
}
26+
27+
return self;
28+
}
29+
30+
@end

0 commit comments

Comments
 (0)
Please sign in to comment.