RCTSurfacePresenter.mm
9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTSurfacePresenter.h"
#import <objc/runtime.h>
#import <mutex>
#import <jsi/jsi.h>
#import <cxxreact/MessageQueueThread.h>
#import <React/RCTAssert.h>
#import <React/RCTBridge+Private.h>
#import <React/RCTComponentViewRegistry.h>
#import <React/RCTFabricSurface.h>
#import <React/RCTImageLoader.h>
#import <React/RCTMountingManager.h>
#import <React/RCTMountingManagerDelegate.h>
#import <React/RCTScheduler.h>
#import <React/RCTSurfaceRegistry.h>
#import <React/RCTSurfaceView.h>
#import <React/RCTSurfaceView+Internal.h>
#import <React/RCTUtils.h>
#import <react/core/LayoutContext.h>
#import <react/core/LayoutConstraints.h>
#import <react/imagemanager/ImageManager.h>
#import <react/uimanager/ContextContainer.h>
#import "MainRunLoopEventBeat.h"
#import "RuntimeEventBeat.h"
#import "RCTConversions.h"
using namespace facebook::react;
@interface RCTBridge ()
- (std::shared_ptr<facebook::react::MessageQueueThread>)jsMessageThread;
@end
@interface RCTSurfacePresenter () <RCTSchedulerDelegate, RCTMountingManagerDelegate>
@end
@implementation RCTSurfacePresenter {
std::mutex _schedulerMutex;
RCTScheduler *_Nullable _scheduler; // Thread-safe. Mutation of the instance variable is protected by `_schedulerMutex`.
RCTMountingManager *_mountingManager; // Thread-safe.
RCTSurfaceRegistry *_surfaceRegistry; // Thread-safe.
RCTBridge *_bridge; // Unsafe. We are moving away from Bridge.
RCTBridge *_batchedBridge;
}
- (instancetype)initWithBridge:(RCTBridge *)bridge
{
if (self = [super init]) {
_bridge = bridge;
_batchedBridge = [_bridge batchedBridge] ?: _bridge;
_surfaceRegistry = [[RCTSurfaceRegistry alloc] init];
_mountingManager = [[RCTMountingManager alloc] init];
_mountingManager.delegate = self;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleBridgeWillReloadNotification:)
name:RCTBridgeWillReloadNotification
object:_bridge];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleJavaScriptDidLoadNotification:)
name:RCTJavaScriptDidLoadNotification
object:_bridge];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Internal Surface-dedicated Interface
- (void)registerSurface:(RCTFabricSurface *)surface
{
[_surfaceRegistry registerSurface:surface];
[self _startSurface:surface];
}
- (void)unregisterSurface:(RCTFabricSurface *)surface
{
[self _stopSurface:surface];
[_surfaceRegistry unregisterSurface:surface];
}
- (void)setProps:(NSDictionary *)props
surface:(RCTFabricSurface *)surface
{
// This implementation is suboptimal indeed but still better than nothing for now.
[self _stopSurface:surface];
[self _startSurface:surface];
}
- (RCTFabricSurface *)surfaceForRootTag:(ReactTag)rootTag
{
return [_surfaceRegistry surfaceForRootTag:rootTag];
}
- (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize
maximumSize:(CGSize)maximumSize
surface:(RCTFabricSurface *)surface
{
LayoutContext layoutContext = {
.pointScaleFactor = RCTScreenScale()
};
LayoutConstraints layoutConstraints = {
.minimumSize = RCTSizeFromCGSize(minimumSize),
.maximumSize = RCTSizeFromCGSize(maximumSize)
};
return [self._scheduler measureSurfaceWithLayoutConstraints:layoutConstraints
layoutContext:layoutContext
surfaceId:surface.rootTag];
}
- (void)setMinimumSize:(CGSize)minimumSize
maximumSize:(CGSize)maximumSize
surface:(RCTFabricSurface *)surface
{
LayoutContext layoutContext = {
.pointScaleFactor = RCTScreenScale()
};
LayoutConstraints layoutConstraints = {
.minimumSize = RCTSizeFromCGSize(minimumSize),
.maximumSize = RCTSizeFromCGSize(maximumSize)
};
[self._scheduler constraintSurfaceLayoutWithLayoutConstraints:layoutConstraints
layoutContext:layoutContext
surfaceId:surface.rootTag];
}
#pragma mark - Private
- (RCTScheduler *)_scheduler
{
std::lock_guard<std::mutex> lock(_schedulerMutex);
if (_scheduler) {
return _scheduler;
}
auto contextContainer = std::make_shared<ContextContainer>();
auto messageQueueThread = _batchedBridge.jsMessageThread;
auto runtime = (facebook::jsi::Runtime *)((RCTCxxBridge *)_batchedBridge).runtime;
RuntimeExecutor runtimeExecutor =
[runtime, messageQueueThread](std::function<void(facebook::jsi::Runtime &runtime)> &&callback) {
messageQueueThread->runOnQueue([runtime, callback = std::move(callback)]() {
callback(*runtime);
});
};
EventBeatFactory synchronousBeatFactory = [runtimeExecutor]() {
return std::make_unique<MainRunLoopEventBeat>(runtimeExecutor);
};
EventBeatFactory asynchronousBeatFactory = [runtimeExecutor]() {
return std::make_unique<RuntimeEventBeat>(runtimeExecutor);
};
contextContainer->registerInstance<EventBeatFactory>(synchronousBeatFactory, "synchronous");
contextContainer->registerInstance<EventBeatFactory>(asynchronousBeatFactory, "asynchronous");
contextContainer->registerInstance(runtimeExecutor, "runtime-executor");
contextContainer->registerInstance(std::make_shared<ImageManager>((__bridge void *)[_bridge imageLoader]));
_scheduler = [[RCTScheduler alloc] initWithContextContainer:contextContainer];
_scheduler.delegate = self;
return _scheduler;
}
- (void)_startSurface:(RCTFabricSurface *)surface
{
[_mountingManager.componentViewRegistry dequeueComponentViewWithName:@"Root" tag:surface.rootTag];
LayoutContext layoutContext = {
.pointScaleFactor = RCTScreenScale()
};
LayoutConstraints layoutConstraints = {
.minimumSize = RCTSizeFromCGSize(surface.minimumSize),
.maximumSize = RCTSizeFromCGSize(surface.maximumSize)
};
[self._scheduler startSurfaceWithSurfaceId:surface.rootTag
moduleName:surface.moduleName
initailProps:surface.properties
layoutConstraints:layoutConstraints
layoutContext:layoutContext];
}
- (void)_stopSurface:(RCTFabricSurface *)surface
{
[self._scheduler stopSurfaceWithSurfaceId:surface.rootTag];
UIView<RCTComponentViewProtocol> *rootView = [_mountingManager.componentViewRegistry componentViewByTag:surface.rootTag];
[_mountingManager.componentViewRegistry enqueueComponentViewWithName:@"Root" tag:surface.rootTag componentView:rootView];
[surface _unsetStage:(RCTSurfaceStagePrepared | RCTSurfaceStageMounted)];
}
- (void)_startAllSurfaces
{
for (RCTFabricSurface *surface in _surfaceRegistry.enumerator) {
[self _startSurface:surface];
}
}
- (void)_stopAllSurfaces
{
for (RCTFabricSurface *surface in _surfaceRegistry.enumerator) {
[self _stopSurface:surface];
}
}
#pragma mark - RCTSchedulerDelegate
- (void)schedulerDidFinishTransaction:(facebook::react::ShadowViewMutationList)mutations
rootTag:(ReactTag)rootTag
{
RCTFabricSurface *surface = [_surfaceRegistry surfaceForRootTag:rootTag];
[surface _setStage:RCTSurfaceStagePrepared];
[_mountingManager performTransactionWithMutations:mutations
rootTag:rootTag];
}
- (void)schedulerDidRequestPreliminaryViewAllocationWithComponentName:(NSString *)componentName
{
[_mountingManager preliminaryCreateComponentViewWithName:componentName];
}
#pragma mark - RCTMountingManagerDelegate
- (void)mountingManager:(RCTMountingManager *)mountingManager willMountComponentsWithRootTag:(ReactTag)rootTag
{
RCTAssertMainQueue();
// Does nothing.
}
- (void)mountingManager:(RCTMountingManager *)mountingManager didMountComponentsWithRootTag:(ReactTag)rootTag
{
RCTAssertMainQueue();
RCTFabricSurface *surface = [_surfaceRegistry surfaceForRootTag:rootTag];
RCTSurfaceStage stage = surface.stage;
if (stage & RCTSurfaceStagePrepared) {
// We have to progress the stage only if the preparing phase is done.
if ([surface _setStage:RCTSurfaceStageMounted]) {
UIView *rootComponentView = [_mountingManager.componentViewRegistry componentViewByTag:rootTag];
surface.view.rootView = (RCTSurfaceRootView *)rootComponentView;
}
}
}
#pragma mark - Bridge events
- (void)handleBridgeWillReloadNotification:(NSNotification *)notification
{
{
std::lock_guard<std::mutex> lock(_schedulerMutex);
if (!_scheduler) {
// Seems we are already in the realoding process.
return;
}
}
[self _stopAllSurfaces];
{
std::lock_guard<std::mutex> lock(_schedulerMutex);
_scheduler = nil;
}
}
- (void)handleJavaScriptDidLoadNotification:(NSNotification *)notification
{
RCTBridge *bridge = notification.userInfo[@"bridge"];
if (bridge != _batchedBridge) {
_batchedBridge = bridge;
[self _startAllSurfaces];
}
}
@end
@implementation RCTSurfacePresenter (Deprecated)
- (RCTBridge *)bridge_DO_NOT_USE
{
return _bridge;
}
@end
@implementation RCTBridge (Deprecated)
- (void)setSurfacePresenter:(RCTSurfacePresenter *)surfacePresenter
{
objc_setAssociatedObject(self, @selector(surfacePresenter), surfacePresenter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (RCTSurfacePresenter *)surfacePresenter
{
return objc_getAssociatedObject(self, @selector(surfacePresenter));
}
@end