diff --git a/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp b/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp index b305ada5813264..041c774919c3ca 100644 --- a/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp +++ b/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp @@ -152,8 +152,8 @@ RuntimeExecutor ReactInstance::getUnbufferedRuntimeExecutor() noexcept { // This BufferedRuntimeExecutor ensures that the main JS bundle finished // execution before any JS queued into it from C++ are executed. Use -// getBufferedRuntimeExecutor() instead if you do not need the main JS bundle to -// have finished. e.g. setting global variables into JS runtime. +// getUnbufferedRuntimeExecutor() instead if you do not need the main JS bundle +// to have finished. e.g. setting global variables into JS runtime. RuntimeExecutor ReactInstance::getBufferedRuntimeExecutor() noexcept { return [weakBufferedRuntimeExecutor_ = std::weak_ptr(bufferedRuntimeExecutor_)]( diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm index 9fdac6566a5bed..914a8e8d037518 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm @@ -104,11 +104,6 @@ @implementation RCTHost { NSDictionary *_launchOptions; - // All the surfaces that need to be started after main bundle execution - NSMutableArray *_surfaceStartBuffer; - std::mutex _surfaceStartBufferMutex; - - RCTInstanceInitialBundleLoadCompletionBlock _onInitialBundleLoad; std::vector<__weak RCTFabricSurface *> _attachedSurfaces; RCTModuleRegistry *_moduleRegistry; @@ -152,7 +147,6 @@ - (instancetype)initWithBundleURLProvider:(RCTHostBundleURLProvider)provider if (self = [super init]) { _hostDelegate = hostDelegate; _turboModuleManagerDelegate = turboModuleManagerDelegate; - _surfaceStartBuffer = [NSMutableArray new]; _bundleManager = [RCTBundleManager new]; _moduleRegistry = [RCTModuleRegistry new]; _jsEngineProvider = [jsEngineProvider copy]; @@ -185,33 +179,6 @@ - (instancetype)initWithBundleURLProvider:(RCTHostBundleURLProvider)provider andSetter:bundleURLSetter andDefaultGetter:defaultBundleURLGetter]; - /** - * TODO (T108401473) Remove _onInitialBundleLoad, because it was initially - * introduced to start surfaces after the main JSBundle was fully executed. - * It is no longer needed because Fabric now dispatches all native-to-JS calls - * onto the JS thread, including JS calls to start Fabric surfaces. - * JS calls should be dispatched using the BufferedRuntimeExecutor, which can buffer - * JS calls before the main JSBundle finishes execution, and execute them after. - */ - _onInitialBundleLoad = ^{ - RCTHost *strongSelf = weakSelf; - if (!strongSelf) { - return; - } - - NSArray *unstartedSurfaces = @[]; - - { - std::lock_guard guard{strongSelf->_surfaceStartBufferMutex}; - unstartedSurfaces = [NSArray arrayWithArray:strongSelf->_surfaceStartBuffer]; - strongSelf->_surfaceStartBuffer = nil; - } - - for (RCTFabricSurface *surface in unstartedSurfaces) { - [surface start]; - } - }; - // Listen to reload commands dispatch_async(dispatch_get_main_queue(), ^{ RCTRegisterReloadCommandListener(self); @@ -261,7 +228,6 @@ - (void)start jsRuntimeFactory:[self _provideJSEngine] bundleManager:_bundleManager turboModuleManagerDelegate:_turboModuleManagerDelegate - onInitialBundleLoad:_onInitialBundleLoad moduleRegistry:_moduleRegistry parentInspectorTarget:_inspectorTarget.get() launchOptions:_launchOptions]; @@ -278,15 +244,7 @@ - (RCTFabricSurface *)createSurfaceWithModuleName:(NSString *)moduleName surface.surfaceHandler.setDisplayMode(displayMode); [self _attachSurface:surface]; - { - std::lock_guard guard{_surfaceStartBufferMutex}; - if (_surfaceStartBuffer) { - [_surfaceStartBuffer addObject:surface]; - return surface; - } - } - - [surface start]; + [_instance callFunctionOnBufferedRumtimeExecutor:[surface](facebook::jsi::Runtime &_) { [surface start]; }]; return surface; } @@ -320,17 +278,10 @@ - (void)didReceiveReloadCommand [self _setBundleURL:_bundleURLProvider()]; } - // Ensure all attached surfaces are restarted after reload - { - std::lock_guard guard{_surfaceStartBufferMutex}; - _surfaceStartBuffer = [NSMutableArray arrayWithArray:[self _getAttachedSurfaces]]; - } - _instance = [[RCTInstance alloc] initWithDelegate:self jsRuntimeFactory:[self _provideJSEngine] bundleManager:_bundleManager turboModuleManagerDelegate:_turboModuleManagerDelegate - onInitialBundleLoad:_onInitialBundleLoad moduleRegistry:_moduleRegistry parentInspectorTarget:_inspectorTarget.get() launchOptions:_launchOptions]; @@ -338,6 +289,7 @@ - (void)didReceiveReloadCommand for (RCTFabricSurface *surface in [self _getAttachedSurfaces]) { [surface resetWithSurfacePresenter:self.surfacePresenter]; + [_instance callFunctionOnBufferedRumtimeExecutor:[surface](facebook::jsi::Runtime &_) { [surface start]; }]; } } diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.h b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.h index 4f94b5700b5154..359bfa34743677 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.h +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.h @@ -46,8 +46,6 @@ RCT_EXTERN void RCTInstanceSetRuntimeDiagnosticFlags(NSString *_Nullable flags); @end -typedef void (^_Null_unspecified RCTInstanceInitialBundleLoadCompletionBlock)(); - /** * RCTInstance owns and manages most of the pieces of infrastructure required to display a screen powered by React * Native. RCTInstance should never be instantiated in product code, but rather accessed through RCTHost. The host @@ -59,12 +57,12 @@ typedef void (^_Null_unspecified RCTInstanceInitialBundleLoadCompletionBlock)(); jsRuntimeFactory:(std::shared_ptr)jsRuntimeFactory bundleManager:(RCTBundleManager *)bundleManager turboModuleManagerDelegate:(id)turboModuleManagerDelegate - onInitialBundleLoad:(RCTInstanceInitialBundleLoadCompletionBlock)onInitialBundleLoad moduleRegistry:(RCTModuleRegistry *)moduleRegistry parentInspectorTarget:(facebook::react::jsinspector_modern::HostTarget *)parentInspectorTarget launchOptions:(nullable NSDictionary *)launchOptions; - (void)callFunctionOnJSModule:(NSString *)moduleName method:(NSString *)method args:(NSArray *)args; +- (void)callFunctionOnBufferedRumtimeExecutor:(std::function &&)executor; - (void)registerSegmentWithId:(NSNumber *)segmentId path:(NSString *)path; diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm index ddeb1e9eff9437..6872139d876234 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm @@ -78,7 +78,6 @@ @implementation RCTInstance { RCTSurfacePresenter *_surfacePresenter; RCTPerformanceLogger *_performanceLogger; RCTDisplayLink *_displayLink; - RCTInstanceInitialBundleLoadCompletionBlock _onInitialBundleLoad; RCTTurboModuleManager *_turboModuleManager; std::mutex _invalidationMutex; std::atomic _valid; @@ -97,7 +96,6 @@ - (instancetype)initWithDelegate:(id)delegate jsRuntimeFactory:(std::shared_ptr)jsRuntimeFactory bundleManager:(RCTBundleManager *)bundleManager turboModuleManagerDelegate:(id)tmmDelegate - onInitialBundleLoad:(RCTInstanceInitialBundleLoadCompletionBlock)onInitialBundleLoad moduleRegistry:(RCTModuleRegistry *)moduleRegistry parentInspectorTarget:(jsinspector_modern::HostTarget *)parentInspectorTarget launchOptions:(nullable NSDictionary *)launchOptions @@ -111,7 +109,6 @@ - (instancetype)initWithDelegate:(id)delegate _jsRuntimeFactory = jsRuntimeFactory; _appTMMDelegate = tmmDelegate; _jsThreadManager = [RCTJSThreadManager new]; - _onInitialBundleLoad = onInitialBundleLoad; _bridgeModuleDecorator = [[RCTBridgeModuleDecorator alloc] initWithViewRegistry:[RCTViewRegistry new] moduleRegistry:moduleRegistry bundleManager:bundleManager @@ -393,6 +390,15 @@ - (void)_attachBridgelessAPIsToModule:(id)module } } +- (void)callFunctionOnBufferedRumtimeExecutor:(std::function &&)executor +{ + _reactInstance->getBufferedRuntimeExecutor()([=](jsi::Runtime &runtime) { + if (executor) { + executor(runtime); + } + }); +} + - (void)handleBundleLoadingError:(NSError *)error { if (!_valid) { @@ -467,11 +473,6 @@ - (void)_loadScriptFromSource:(RCTSource *)source const auto *url = deriveSourceURL(source.url).UTF8String; _reactInstance->loadScript(std::move(script), url); [[NSNotificationCenter defaultCenter] postNotificationName:@"RCTInstanceDidLoadBundle" object:nil]; - - if (_onInitialBundleLoad) { - _onInitialBundleLoad(); - _onInitialBundleLoad = nil; - } } - (void)_handleJSError:(const JsErrorHandler::ParsedError &)error diff --git a/packages/react-native/ReactCommon/react/test_utils/ios/Shims/ShimRCTInstance.mm b/packages/react-native/ReactCommon/react/test_utils/ios/Shims/ShimRCTInstance.mm index 2575f1453a2953..3b9947000e58db 100644 --- a/packages/react-native/ReactCommon/react/test_utils/ios/Shims/ShimRCTInstance.mm +++ b/packages/react-native/ReactCommon/react/test_utils/ios/Shims/ShimRCTInstance.mm @@ -23,8 +23,8 @@ - (instancetype)init [RCTInstance class], [ShimRCTInstance class], @selector(initWithDelegate: - jsRuntimeFactory:bundleManager:turboModuleManagerDelegate:onInitialBundleLoad:moduleRegistry - :parentInspectorTarget:launchOptions:)); + jsRuntimeFactory:bundleManager:turboModuleManagerDelegate:moduleRegistry:parentInspectorTarget + :launchOptions:)); RCTSwizzleInstanceSelector([RCTInstance class], [ShimRCTInstance class], @selector(invalidate)); RCTSwizzleInstanceSelector( [RCTInstance class], [ShimRCTInstance class], @selector(callFunctionOnJSModule:method:args:)); @@ -39,8 +39,8 @@ - (void)reset [RCTInstance class], [ShimRCTInstance class], @selector(initWithDelegate: - jsRuntimeFactory:bundleManager:turboModuleManagerDelegate:onInitialBundleLoad:moduleRegistry - :parentInspectorTarget:launchOptions:)); + jsRuntimeFactory:bundleManager:turboModuleManagerDelegate:moduleRegistry:parentInspectorTarget + :launchOptions:)); RCTSwizzleInstanceSelector([RCTInstance class], [ShimRCTInstance class], @selector(invalidate)); RCTSwizzleInstanceSelector( [RCTInstance class], [ShimRCTInstance class], @selector(callFunctionOnJSModule:method:args:)); @@ -52,7 +52,6 @@ - (instancetype)initWithDelegate:(id)delegate jsRuntimeFactory:(std::shared_ptr)jsRuntimeFactory bundleManager:(RCTBundleManager *)bundleManager turboModuleManagerDelegate:(id)tmmDelegate - onInitialBundleLoad:(RCTInstanceInitialBundleLoadCompletionBlock)onInitialBundleLoad moduleRegistry:(RCTModuleRegistry *)moduleRegistry parentInspectorTarget:(facebook::react::jsinspector_modern::HostTarget *)parentInspectorTarget launchOptions:(NSDictionary *)launchOptions