Skip to content

Commit 7df58e2

Browse files
sherginfacebook-github-bot
authored andcommitted
Introducing RCTSurface, new experimental thread-safe interop layer
Summary: RCTSurface instance represents React Native-powered piece of a user interface which can be a full-screen app, separate modal view controller, or even small widget. It is called "Surface". The RCTSurface instance is completely thread-safe by design; it can be created on any thread, and any its method can be called from any thread (if the opposite is not mentioned explicitly). The primary goals of the RCTSurface are: - ability to measure and layout the surface in a thread-safe and synchronous manner; - ability to create a UIView instance on demand (later); - ability to communicate the current stage of the surface granularly. Differential Revision: D6202576 fbshipit-source-id: 8e644c87fcaad2b6a9c9304b58384d7192747556
1 parent be6976d commit 7df58e2

15 files changed

+1198
-3
lines changed

React/Base/Surface/RCTSurface.h

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 <Foundation/Foundation.h>
11+
12+
#import <React/RCTSurfaceStage.h>
13+
14+
NS_ASSUME_NONNULL_BEGIN
15+
16+
@class RCTBridge;
17+
@class RCTSurfaceView;
18+
@protocol RCTSurfaceDelegate;
19+
20+
/**
21+
* RCTSurface instance represents React Native-powered piece of a user interface
22+
* which can be a full-screen app, separate modal view controller,
23+
* or even small widget.
24+
* It is called "Surface".
25+
*
26+
* The RCTSurface instance is completely thread-safe by design;
27+
* it can be created on any thread, and any its method can be called from
28+
* any thread (if the opposite is not mentioned explicitly).
29+
*
30+
* The primary goals of the RCTSurface are:
31+
* * ability to measure and layout the surface in a thread-safe
32+
* and synchronous manner;
33+
* * ability to create a UIView instance on demand (later);
34+
* * ability to communicate the current stage of the surface granularly.
35+
*/
36+
@interface RCTSurface : NSObject
37+
38+
@property (atomic, readonly) RCTSurfaceStage stage;
39+
@property (atomic, readonly) RCTBridge *bridge;
40+
@property (atomic, readonly) NSString *moduleName;
41+
@property (atomic, readonly) NSNumber *rootViewTag;
42+
43+
@property (atomic, readwrite, weak, nullable) id<RCTSurfaceDelegate> delegate;
44+
45+
@property (atomic, copy, readwrite) NSDictionary *properties;
46+
47+
- (instancetype)initWithBridge:(RCTBridge *)bridge
48+
moduleName:(NSString *)moduleName
49+
initialProperties:(NSDictionary *)initialProperties NS_DESIGNATED_INITIALIZER;
50+
51+
#pragma mark - Dealing with UIView representation, the Main thread only access
52+
53+
/**
54+
* Creates (if needed) and returns `UIView` instance which represents the Surface.
55+
* The Surface will cache and *retain* this object.
56+
* Returning the UIView instance does not mean that the Surface is ready
57+
* to execute and layout. It can be just a handler which Surface will use later
58+
* to mount the actual views.
59+
* RCTSurface does not control (or influence in any way) the size or origin
60+
* of this view. Some superview (or another owner) must use other methods
61+
* of this class to setup proper layout and interop interactions with UIKit
62+
* or another UI framework.
63+
* This method must be called only from the main queue.
64+
*/
65+
- (RCTSurfaceView *)view;
66+
67+
#pragma mark - Layout: Setting the size constrains
68+
69+
/**
70+
* Sets `minimumSize` and `maximumSize` layout constraints for the Surface.
71+
*/
72+
- (void)setMinimumSize:(CGSize)minimumSize
73+
maximumSize:(CGSize)maximumSize;
74+
75+
/**
76+
* Previously set `minimumSize` layout constraint.
77+
* Defaults to `{0, 0}`.
78+
*/
79+
@property (atomic, assign, readonly) CGSize minimumSize;
80+
81+
/**
82+
* Previously set `maximumSize` layout constraint.
83+
* Defaults to `{INFINITY, INFINITY}`.
84+
*/
85+
@property (atomic, assign, readonly) CGSize maximumSize;
86+
87+
88+
/**
89+
* Simple shortcut to `-[RCTSurface setMinimumSize:size maximumSize:size]`.
90+
*/
91+
- (void)setSize:(CGSize)size;
92+
93+
#pragma mark - Layout: Measuring
94+
95+
/**
96+
* Measures the Surface with given constraints.
97+
* This method does not cause any side effects on the surface object.
98+
*/
99+
- (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize
100+
maximumSize:(CGSize)maximumSize;
101+
102+
/**
103+
* Return the current size of the root view based on (but not clamp by) current
104+
* size constraints.
105+
*/
106+
@property (atomic, assign, readonly) CGSize intrinsicSize;
107+
108+
#pragma mark - Synchronous waiting
109+
110+
/**
111+
* Synchronously blocks the current thread up to given `timeout` until
112+
* the Surface will not have given `stage`.
113+
* Do nothing, if called from the main or `UIManager` queue.
114+
*/
115+
- (BOOL)synchronouslyWaitForStage:(RCTSurfaceStage)stage timeout:(NSTimeInterval)timeout;
116+
117+
@end
118+
119+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)