@solidnative-dev/fabric-host 0.0.0-stage → 0.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +87 -2
  3. package/dist/fatal-error-handler.d.ts +27 -0
  4. package/dist/fatal-error-handler.d.ts.map +1 -0
  5. package/dist/fatal-error-handler.js +106 -0
  6. package/dist/fatal-error-handler.js.map +1 -0
  7. package/dist/index.d.ts +7 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +7 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/native-binding.d.ts +183 -0
  12. package/dist/native-binding.d.ts.map +1 -0
  13. package/dist/native-binding.js +554 -0
  14. package/dist/native-binding.js.map +1 -0
  15. package/dist/native-fabric-host.d.ts +32 -0
  16. package/dist/native-fabric-host.d.ts.map +1 -0
  17. package/dist/native-fabric-host.js +275 -0
  18. package/dist/native-fabric-host.js.map +1 -0
  19. package/dist/native-transaction.d.ts +17 -0
  20. package/dist/native-transaction.d.ts.map +1 -0
  21. package/dist/native-transaction.js +124 -0
  22. package/dist/native-transaction.js.map +1 -0
  23. package/dist/react-native-0.87.d.ts +61 -0
  24. package/dist/react-native-0.87.d.ts.map +1 -0
  25. package/dist/react-native-0.87.js +77 -0
  26. package/dist/react-native-0.87.js.map +1 -0
  27. package/dist/react-native-0.88.d.ts +77 -0
  28. package/dist/react-native-0.88.d.ts.map +1 -0
  29. package/dist/react-native-0.88.js +79 -0
  30. package/dist/react-native-0.88.js.map +1 -0
  31. package/dist/surface-readiness.d.ts +18 -0
  32. package/dist/surface-readiness.d.ts.map +1 -0
  33. package/dist/surface-readiness.js +43 -0
  34. package/dist/surface-readiness.js.map +1 -0
  35. package/native/README.md +502 -0
  36. package/native/SolidNativeFabric.podspec +84 -0
  37. package/native/android/SolidNativeBindingsInstaller.cpp +4218 -0
  38. package/native/android/SolidNativeBindingsInstaller.h +8 -0
  39. package/native/android/SolidNativeRuntime.cmake +61 -0
  40. package/native/android/java/dev/solidnative/runtime/SolidNativeBindingsInstaller.kt +239 -0
  41. package/native/android/java/dev/solidnative/runtime/SolidNativeDebugModule.kt +191 -0
  42. package/native/android/java/dev/solidnative/runtime/SolidNativePackage.kt +43 -0
  43. package/native/android/java/dev/solidnative/runtime/SolidNativePlatformModule.kt +81 -0
  44. package/native/android/java/dev/solidnative/runtime/SolidNativeStartupFailureView.kt +93 -0
  45. package/native/android/java/dev/solidnative/runtime/SolidNativeSurface.kt +423 -0
  46. package/native/android/solid-native.gradle +90 -0
  47. package/native/apple/SolidNativeDebugRequest.mm +421 -0
  48. package/native/fabric/SolidNativeDebugRequest.h +12 -0
  49. package/native/fabric/SolidNativeDirtyNodes.h +26 -0
  50. package/native/fabric/SolidNativeFabricApi.cpp +224 -0
  51. package/native/fabric/SolidNativeFabricApi.h +107 -0
  52. package/native/fabric/SolidNativeFabricApplication.h +32 -0
  53. package/native/fabric/SolidNativeFabricApplication.mm +373 -0
  54. package/native/fabric/SolidNativeFabricJSIBinding.h +43 -0
  55. package/native/fabric/SolidNativeFabricJSIBinding.mm +4491 -0
  56. package/native/fabric/SolidNativeFabricSmokeApplication.h +26 -0
  57. package/native/fabric/SolidNativeFabricSmokeApplication.mm +331 -0
  58. package/native/fabric/SolidNativeFabricSurface.h +63 -0
  59. package/native/fabric/SolidNativeFabricSurface.mm +549 -0
  60. package/native/fabric/SolidNativeFabricTransactionCoordinator.cpp +1275 -0
  61. package/native/fabric/SolidNativeFabricTransactionCoordinator.h +90 -0
  62. package/native/fabric/SolidNativeFabricTransactionCoordinatorApple.mm +97 -0
  63. package/native/fabric/SolidNativeRootViewController.h +20 -0
  64. package/native/fabric/SolidNativeRootViewController.mm +83 -0
  65. package/native/fabric/SolidNativeStagedNodeMap.h +104 -0
  66. package/native/fabric/SolidNativeStartupFailureView.h +17 -0
  67. package/native/fabric/SolidNativeStartupFailureView.mm +77 -0
  68. package/native/react-native-boundary.json +106 -0
  69. package/native/worklets/SolidNativeUIWorklet.cpp +1080 -0
  70. package/native/worklets/SolidNativeUIWorklet.h +245 -0
  71. package/package.json +41 -3
  72. package/scripts/verify-react-native-boundary.mjs +282 -0
@@ -0,0 +1,549 @@
1
+ #import "SolidNativeFabricSurface.h"
2
+
3
+ #import <React/RCTConstants.h>
4
+ #import <React/RCTFabricSurface.h>
5
+ #import <React/RCTScheduler.h>
6
+ #import <React/RCTSurfacePresenter.h>
7
+ #import <React/RCTSurfaceView.h>
8
+ #import <React/RCTUtils.h>
9
+ #import <ReactCommon/RCTHost.h>
10
+
11
+ #include <exception>
12
+ #include <memory>
13
+ #include <stdexcept>
14
+
15
+ #include <react/renderer/scheduler/SurfaceHandler.h>
16
+ #include <react/renderer/mounting/MountingCoordinator.h>
17
+
18
+ #include "SolidNativeFabricTransactionCoordinator.h"
19
+
20
+ using solid_native::fabric::react_native::NativeResourceResolver;
21
+
22
+ namespace {
23
+
24
+ constexpr NSInteger MaxSurfaceStartAttempts = 2000;
25
+ constexpr int64_t SurfaceStartRetryNanoseconds = 5 * NSEC_PER_MSEC;
26
+
27
+ NSError *makeError(NSString *message) {
28
+ return [NSError errorWithDomain:@"SolidNativeFabricSurface"
29
+ code:1
30
+ userInfo:@{NSLocalizedDescriptionKey : message}];
31
+ }
32
+
33
+ void finishOnMainQueue(
34
+ SolidNativeFabricTransactionCompletion completion,
35
+ NSDictionary<NSString *, id> *result,
36
+ NSError *error) {
37
+ dispatch_async(dispatch_get_main_queue(), ^{
38
+ completion(result, error);
39
+ });
40
+ }
41
+
42
+ BOOL isCommandTransaction(NSDictionary<NSString *, id> *transaction) {
43
+ id mutationsValue = transaction[@"mutations"];
44
+ if (![mutationsValue isKindOfClass:NSArray.class] ||
45
+ [(NSArray *)mutationsValue count] != 1) {
46
+ return NO;
47
+ }
48
+ id mutationValue = [(NSArray *)mutationsValue firstObject];
49
+ return [mutationValue isKindOfClass:NSDictionary.class] &&
50
+ [((NSDictionary *)mutationValue)[@"type"] isEqual:@"command"];
51
+ }
52
+
53
+ } // namespace
54
+
55
+ @interface SolidNativeFabricSurface ()
56
+ - (void)synchronizeSurfaceView;
57
+ - (void)updateLayoutConstraintsForView:(UIView *)view;
58
+ - (nullable NSDictionary<NSString *, id> *)
59
+ applyTransactionSynchronously:(NSDictionary<NSString *, id> *)transaction
60
+ nodeIdentities:
61
+ (const solid_native::fabric::react_native::NodeIdentityMap *)
62
+ nodeIdentities
63
+ runtime:(facebook::jsi::Runtime *)runtime
64
+ resourceResolver:
65
+ (const NativeResourceResolver *)resourceResolver
66
+ error:(NSError *_Nullable *_Nullable)error;
67
+ - (void)addSolidNativeEventListener:
68
+ (const std::shared_ptr<facebook::react::EventListener> &)listener;
69
+ - (void)removeSolidNativeEventListener:
70
+ (const std::shared_ptr<facebook::react::EventListener> &)listener;
71
+ - (nullable NSDictionary<NSString *, id> *)
72
+ measureNodeSynchronously:(int64_t)node
73
+ afterSequence:(int64_t)afterSequence
74
+ error:(NSError *_Nullable *_Nullable)error;
75
+ - (BOOL)hasNoLogicalNodes;
76
+ - (int64_t)mountedFabricRevision;
77
+ - (nullable UIView *)findComponentViewWithTag:(facebook::react::Tag)tag;
78
+ @end
79
+
80
+ // RCTFabricSurface does not observe the frame of an arbitrary embedding view.
81
+ // Keep the package-owned view as the UIKit lifecycle boundary and forward each
82
+ // size or window-position change into Fabric's root layout constraints.
83
+ @interface SolidNativeFabricContainerView : UIView
84
+ @property(nonatomic, weak) SolidNativeFabricSurface *surface;
85
+ @end
86
+
87
+ @implementation SolidNativeFabricContainerView
88
+
89
+ - (void)layoutSubviews {
90
+ [super layoutSubviews];
91
+ [self.surface updateLayoutConstraintsForView:self];
92
+ }
93
+
94
+ - (void)didMoveToWindow {
95
+ [super didMoveToWindow];
96
+ [self.surface updateLayoutConstraintsForView:self];
97
+ }
98
+
99
+ - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
100
+ [super traitCollectionDidChange:previousTraitCollection];
101
+ if (RCTSharedApplication().applicationState == UIApplicationStateBackground) {
102
+ return;
103
+ }
104
+ [[NSNotificationCenter defaultCenter]
105
+ postNotificationName:RCTUserInterfaceStyleDidChangeNotification
106
+ object:self
107
+ userInfo:@{
108
+ RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey :
109
+ self.traitCollection,
110
+ }];
111
+ }
112
+
113
+ @end
114
+
115
+ @implementation SolidNativeFabricSurface {
116
+ RCTHost *_host;
117
+ RCTFabricSurface *_surface;
118
+ UIView *_view;
119
+ UIView *_surfaceView;
120
+ CGSize _lastLayoutSize;
121
+ CGPoint _lastViewportOffset;
122
+ BOOL _hasLayoutConstraints;
123
+ std::unique_ptr<solid_native::fabric::react_native::TransactionCoordinator>
124
+ _coordinator;
125
+ }
126
+
127
+ - (instancetype)initWithHost:(RCTHost *)host size:(CGSize)size {
128
+ if (self = [super init]) {
129
+ NSAssert(
130
+ NSThread.isMainThread,
131
+ @"The Fabric surface must be created on the main thread.");
132
+ _host = host;
133
+ // RCTHost's convenience factory queues a second `start` after bundle
134
+ // evaluation. Solid Native must start its empty surface before bundle
135
+ // completion, and RCTFabricSurface 0.87 only guards the status before its
136
+ // asynchronous start block. Two queued starts can therefore race inside
137
+ // animation-driver setup. Constructing through the public surface API
138
+ // leaves exactly one start owner while retaining presenter registration.
139
+ _surface = [[RCTFabricSurface alloc]
140
+ initWithSurfacePresenter:host.surfacePresenter
141
+ moduleName:@""
142
+ initialProperties:@{}];
143
+ [_surface setMinimumSize:size
144
+ maximumSize:size
145
+ viewportOffset:CGPointZero];
146
+ _lastLayoutSize = size;
147
+ _lastViewportOffset = CGPointZero;
148
+ _hasLayoutConstraints = YES;
149
+ auto containerView = [[SolidNativeFabricContainerView alloc]
150
+ initWithFrame:CGRectMake(0, 0, size.width, size.height)];
151
+ containerView.surface = self;
152
+ _view = containerView;
153
+ _view.autoresizingMask =
154
+ UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
155
+ [self synchronizeSurfaceView];
156
+ _coordinator = std::make_unique<
157
+ solid_native::fabric::react_native::TransactionCoordinator>(
158
+ _surface.surfaceHandler.getSurfaceId());
159
+ }
160
+ return self;
161
+ }
162
+
163
+ - (UIView *)view {
164
+ return _view;
165
+ }
166
+
167
+ - (void)updateLayoutConstraintsForView:(UIView *)view {
168
+ NSAssert(
169
+ NSThread.isMainThread,
170
+ @"Fabric surface layout constraints must update on the main thread.");
171
+ if (_surface == nil || view != _view) return;
172
+
173
+ const auto size = view.bounds.size;
174
+ if (!std::isfinite(size.width) || !std::isfinite(size.height) ||
175
+ size.width <= 0 || size.height <= 0) {
176
+ return;
177
+ }
178
+ const auto viewportOffset = view.window == nil
179
+ ? CGPointZero
180
+ : [view convertRect:view.bounds toView:view.window].origin;
181
+ if (_hasLayoutConstraints &&
182
+ CGSizeEqualToSize(_lastLayoutSize, size) &&
183
+ CGPointEqualToPoint(_lastViewportOffset, viewportOffset)) {
184
+ return;
185
+ }
186
+
187
+ [_surface setMinimumSize:size
188
+ maximumSize:size
189
+ viewportOffset:viewportOffset];
190
+ _lastLayoutSize = size;
191
+ _lastViewportOffset = viewportOffset;
192
+ _hasLayoutConstraints = YES;
193
+ }
194
+
195
+ - (void)synchronizeSurfaceView {
196
+ NSAssert(
197
+ NSThread.isMainThread,
198
+ @"The Fabric surface view must be synchronized on the main thread.");
199
+ auto surfaceView = _surface.view;
200
+ if (_surfaceView == surfaceView) return;
201
+ [_surfaceView removeFromSuperview];
202
+ _surfaceView = surfaceView;
203
+ _surfaceView.frame = _view.bounds;
204
+ _surfaceView.autoresizingMask =
205
+ UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
206
+ [_view addSubview:_surfaceView];
207
+ }
208
+
209
+ - (NSInteger)surfaceId {
210
+ return _surface.surfaceHandler.getSurfaceId();
211
+ }
212
+
213
+ - (int64_t)lastSequence {
214
+ return _coordinator ? _coordinator->lastSequence() : 0;
215
+ }
216
+
217
+ - (BOOL)hasNoLogicalNodes {
218
+ return !_coordinator || _coordinator->empty();
219
+ }
220
+
221
+ - (int64_t)mountedFabricRevision {
222
+ if (_surface == nil) return 0;
223
+ auto mountingCoordinator =
224
+ _surface.surfaceHandler.getMountingCoordinator();
225
+ return mountingCoordinator == nullptr
226
+ ? 0
227
+ : mountingCoordinator->getBaseRevision().number;
228
+ }
229
+
230
+ - (nullable UIView *)findComponentViewWithTag:(facebook::react::Tag)tag {
231
+ NSAssert(
232
+ NSThread.isMainThread,
233
+ @"Fabric component views must be resolved on the main thread.");
234
+ return [_host.surfacePresenter
235
+ findComponentViewWithTag_DO_NOT_USE_DEPRECATED:tag];
236
+ }
237
+
238
+ - (void)addSolidNativeEventListener:
239
+ (const std::shared_ptr<facebook::react::EventListener> &)listener {
240
+ [_host.surfacePresenter.scheduler addEventListener:listener];
241
+ }
242
+
243
+ - (void)removeSolidNativeEventListener:
244
+ (const std::shared_ptr<facebook::react::EventListener> &)listener {
245
+ [_host.surfacePresenter.scheduler removeEventListener:listener];
246
+ }
247
+
248
+ - (NSDictionary<NSString *, id> *)
249
+ applyTransactionSynchronously:(NSDictionary<NSString *, id> *)transaction
250
+ error:(NSError **)error {
251
+ return [self applyTransactionSynchronously:transaction
252
+ nodeIdentities:nullptr
253
+ runtime:nullptr
254
+ resourceResolver:nullptr
255
+ error:error];
256
+ }
257
+
258
+ - (NSDictionary<NSString *, id> *)
259
+ applyTransactionSynchronously:(NSDictionary<NSString *, id> *)transaction
260
+ nodeIdentities:
261
+ (const solid_native::fabric::react_native::NodeIdentityMap *)
262
+ nodeIdentities
263
+ runtime:(facebook::jsi::Runtime *)runtime
264
+ resourceResolver:
265
+ (const NativeResourceResolver *)resourceResolver
266
+ error:(NSError **)error {
267
+ if (_surface.surfaceHandler.getStatus() !=
268
+ facebook::react::SurfaceHandler::Status::Running) {
269
+ if (error != nullptr) {
270
+ *error = makeError(@"The Fabric surface is not running.");
271
+ }
272
+ return nil;
273
+ }
274
+
275
+ @try {
276
+ try {
277
+ auto scheduler = _host.surfacePresenter.scheduler;
278
+ auto uiManager = scheduler.uiManager;
279
+ if (!uiManager) {
280
+ throw std::runtime_error(
281
+ "React Native did not expose a Fabric UIManager.");
282
+ }
283
+ if (nodeIdentities != nullptr &&
284
+ (runtime == nullptr || resourceResolver == nullptr)) {
285
+ throw std::invalid_argument(
286
+ "Application transactions require their owning JSI runtime and "
287
+ "resource resolver.");
288
+ }
289
+ const auto sequence = nodeIdentities == nullptr
290
+ ? _coordinator->apply(*uiManager, transaction)
291
+ : _coordinator->apply(
292
+ *uiManager,
293
+ transaction,
294
+ *runtime,
295
+ *nodeIdentities,
296
+ *resourceResolver);
297
+ NSMutableDictionary<NSString *, id> *result = [@{
298
+ @"surface" : @(self.surfaceId),
299
+ @"sequence" : @(sequence),
300
+ @"mounted" : @YES
301
+ } mutableCopy];
302
+ if (!isCommandTransaction(transaction)) {
303
+ result[@"fabricRevision"] = @(_coordinator->lastFabricRevision());
304
+ }
305
+ return result;
306
+ } catch (const std::exception &exception) {
307
+ if (error != nullptr) {
308
+ *error = makeError([NSString stringWithUTF8String:exception.what()]);
309
+ }
310
+ return nil;
311
+ }
312
+ } @catch (NSException *exception) {
313
+ if (error != nullptr) *error = makeError(exception.reason);
314
+ return nil;
315
+ }
316
+ }
317
+
318
+ - (NSDictionary<NSString *, id> *)
319
+ measureNodeSynchronously:(int64_t)node
320
+ afterSequence:(int64_t)afterSequence
321
+ error:(NSError **)error {
322
+ if (_surface.surfaceHandler.getStatus() !=
323
+ facebook::react::SurfaceHandler::Status::Running) {
324
+ if (error != nullptr) {
325
+ *error = makeError(@"The Fabric surface is not running.");
326
+ }
327
+ return nil;
328
+ }
329
+
330
+ @try {
331
+ try {
332
+ auto uiManager = _host.surfacePresenter.scheduler.uiManager;
333
+ if (!uiManager) {
334
+ throw std::runtime_error(
335
+ "React Native did not expose a Fabric UIManager.");
336
+ }
337
+ const auto measurement =
338
+ _coordinator->measure(*uiManager, node, afterSequence);
339
+ return @{
340
+ @"x" : @(measurement.x),
341
+ @"y" : @(measurement.y),
342
+ @"width" : @(measurement.width),
343
+ @"height" : @(measurement.height),
344
+ @"pageX" : @(measurement.pageX),
345
+ @"pageY" : @(measurement.pageY),
346
+ @"observedSequence" : @(measurement.observedSequence)
347
+ };
348
+ } catch (const std::exception &exception) {
349
+ if (error != nullptr) {
350
+ *error = makeError([NSString stringWithUTF8String:exception.what()]);
351
+ }
352
+ return nil;
353
+ }
354
+ } @catch (NSException *exception) {
355
+ if (error != nullptr) *error = makeError(exception.reason);
356
+ return nil;
357
+ }
358
+ }
359
+
360
+ - (void)applyTransaction:(NSDictionary<NSString *, id> *)transaction
361
+ completion:(SolidNativeFabricTransactionCompletion)completion {
362
+ NSDictionary<NSString *, id> *snapshot = [transaction copy];
363
+ [self waitForRunningSurfaceWithAttempt:0
364
+ transaction:snapshot
365
+ completion:completion];
366
+ }
367
+
368
+ - (void)mountText:(NSString *)text
369
+ completion:(SolidNativeFabricCompletion)completion {
370
+ NSDictionary<NSString *, id> *transaction = @{
371
+ @"contractVersion" : @1,
372
+ @"surface" : @(self.surfaceId),
373
+ @"sequence" : @1,
374
+ @"priority" : @"normal",
375
+ @"mutations" : @[
376
+ @{
377
+ @"type" : @"create-element",
378
+ @"node" : @1,
379
+ @"component" : @"RootView",
380
+ @"props" : @{ @"style" : @{ @"flex" : @1 } }
381
+ },
382
+ @{
383
+ @"type" : @"create-element",
384
+ @"node" : @2,
385
+ @"component" : @"Text",
386
+ @"props" : @{}
387
+ },
388
+ @{ @"type" : @"create-text", @"node" : @3, @"text" : text },
389
+ @{ @"type" : @"insert-child", @"parent" : @2, @"child" : @3 },
390
+ @{ @"type" : @"insert-child", @"parent" : @1, @"child" : @2 }
391
+ ]
392
+ };
393
+ [self applyTransaction:transaction
394
+ completion:^(NSDictionary<NSString *, id> *result,
395
+ NSError *error) {
396
+ completion(error);
397
+ }];
398
+ }
399
+
400
+ - (void)updateText:(NSString *)text
401
+ completion:(SolidNativeFabricCompletion)completion {
402
+ NSDictionary<NSString *, id> *transaction = @{
403
+ @"contractVersion" : @1,
404
+ @"surface" : @(self.surfaceId),
405
+ @"sequence" : @2,
406
+ @"priority" : @"normal",
407
+ @"mutations" : @[
408
+ @{ @"type" : @"update-text", @"node" : @3, @"text" : text }
409
+ ]
410
+ };
411
+ [self applyTransaction:transaction
412
+ completion:^(NSDictionary<NSString *, id> *result,
413
+ NSError *error) {
414
+ completion(error);
415
+ }];
416
+ }
417
+
418
+ - (void)prepareEmptySurfaceForJSIOwnershipWithCompletion:
419
+ (SolidNativeFabricCompletion)completion {
420
+ // RCTHost buffers ordinary application-surface startup until the JavaScript
421
+ // bundle has executed because a named surface calls AppRegistry. Solid
422
+ // Native deliberately owns an empty surface, whose start path never enters
423
+ // AppRegistry. Start it here so JavaScript bundle loading and native surface
424
+ // readiness cannot wait on one another. RCTHost's later buffered start is
425
+ // idempotent because RCTFabricSurface ignores an already-running surface.
426
+ // A reload also clears RCTFabricSurface's cached RCTSurfaceView. Keep the
427
+ // package-owned outer view stable while replacing that internal child before
428
+ // Fabric attaches the restarted surface.
429
+ [self synchronizeSurfaceView];
430
+ [_surface start];
431
+ NSDictionary<NSString *, id> *transaction = @{
432
+ @"contractVersion" : @1,
433
+ @"surface" : @(self.surfaceId),
434
+ @"sequence" : @1,
435
+ @"priority" : @"normal",
436
+ @"mutations" : @[]
437
+ };
438
+ __weak SolidNativeFabricSurface *weakSelf = self;
439
+ [self applyTransaction:transaction
440
+ completion:^(NSDictionary<NSString *, id> *result,
441
+ NSError *error) {
442
+ SolidNativeFabricSurface *strongSelf = weakSelf;
443
+ if (error == nil && strongSelf != nil &&
444
+ strongSelf->_coordinator) {
445
+ strongSelf->_coordinator->reset();
446
+ }
447
+ completion(error);
448
+ }];
449
+ }
450
+
451
+ - (void)resetForJSRuntimeReload {
452
+ if (_coordinator) _coordinator->reset();
453
+ }
454
+
455
+ - (void)rebindToCurrentHostAfterJSRuntimeReload {
456
+ NSAssert(
457
+ NSThread.isMainThread,
458
+ @"The Fabric surface must rebind on the main thread.");
459
+ [_surface resetWithSurfacePresenter:_host.surfacePresenter];
460
+ [self synchronizeSurfaceView];
461
+ }
462
+
463
+ - (void)prepareForJSIOwnershipWithCompletion:
464
+ (SolidNativeFabricCompletion)completion {
465
+ NSDictionary<NSString *, id> *transaction = @{
466
+ @"contractVersion" : @1,
467
+ @"surface" : @(self.surfaceId),
468
+ @"sequence" : @2,
469
+ @"priority" : @"normal",
470
+ @"mutations" : @[
471
+ @{ @"type" : @"remove-child", @"parent" : @2, @"child" : @3 },
472
+ @{ @"type" : @"remove-child", @"parent" : @1, @"child" : @2 },
473
+ @{ @"type" : @"delete-node", @"node" : @3 },
474
+ @{ @"type" : @"delete-node", @"node" : @2 },
475
+ @{ @"type" : @"delete-node", @"node" : @1 }
476
+ ]
477
+ };
478
+ __weak SolidNativeFabricSurface *weakSelf = self;
479
+ [self applyTransaction:transaction
480
+ completion:^(NSDictionary<NSString *, id> *result,
481
+ NSError *error) {
482
+ SolidNativeFabricSurface *strongSelf = weakSelf;
483
+ if (error == nil && strongSelf != nil &&
484
+ strongSelf->_coordinator) {
485
+ strongSelf->_coordinator->reset();
486
+ }
487
+ completion(error);
488
+ }];
489
+ }
490
+
491
+ - (void)stop {
492
+ if (_coordinator) {
493
+ _coordinator->reset();
494
+ }
495
+ if (_surface != nil &&
496
+ _surface.surfaceHandler.getStatus() ==
497
+ facebook::react::SurfaceHandler::Status::Running) {
498
+ [_surface stop];
499
+ }
500
+ }
501
+
502
+ - (void)dealloc {
503
+ [self stop];
504
+ }
505
+
506
+ - (void)waitForRunningSurfaceWithAttempt:(NSInteger)attempt
507
+ transaction:
508
+ (NSDictionary<NSString *, id> *)transaction
509
+ completion:
510
+ (SolidNativeFabricTransactionCompletion)
511
+ completion {
512
+ __weak SolidNativeFabricSurface *weakSelf = self;
513
+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
514
+ SolidNativeFabricSurface *strongSelf = weakSelf;
515
+ if (strongSelf == nil) {
516
+ finishOnMainQueue(
517
+ completion, nil, makeError(@"The Fabric surface was released."));
518
+ return;
519
+ }
520
+
521
+ if (strongSelf->_surface.surfaceHandler.getStatus() !=
522
+ facebook::react::SurfaceHandler::Status::Running) {
523
+ if (attempt >= MaxSurfaceStartAttempts) {
524
+ finishOnMainQueue(
525
+ completion,
526
+ nil,
527
+ makeError(
528
+ @"The empty Fabric surface did not start within ten seconds."));
529
+ return;
530
+ }
531
+ dispatch_after(
532
+ dispatch_time(DISPATCH_TIME_NOW, SurfaceStartRetryNanoseconds),
533
+ dispatch_get_main_queue(),
534
+ ^{
535
+ [strongSelf waitForRunningSurfaceWithAttempt:attempt + 1
536
+ transaction:transaction
537
+ completion:completion];
538
+ });
539
+ return;
540
+ }
541
+
542
+ NSError *error = nil;
543
+ auto result =
544
+ [strongSelf applyTransactionSynchronously:transaction error:&error];
545
+ finishOnMainQueue(completion, result, error);
546
+ });
547
+ }
548
+
549
+ @end