From db798c18d688feffde5ae9bac0f2779d4f04acf9 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 5 Dec 2024 06:00:34 -0800 Subject: [PATCH] Fabric: Post RCTInstanceDidLoadBundle notification after bundle loaded (#48082) Summary: Fixes https://github.com/facebook/react-native/issues/47949 ## Changelog: [IOS] [FIXED] - Fabric: Post RCTInstanceDidLoadBundle notification after bundle loaded Pull Request resolved: https://github.com/facebook/react-native/pull/48082 Test Plan: Post RCTInstanceDidLoadBundle notification after bundle loaded Reviewed By: philIip Differential Revision: D66754060 Pulled By: cipolleschi fbshipit-source-id: d30f0ed73e127936082e6f91e137b9b4013c6651 --- .../react/runtime/ReactInstance.cpp | 74 ++++++++++--------- .../ReactCommon/react/runtime/ReactInstance.h | 3 +- .../platform/ios/ReactCommon/RCTInstance.mm | 5 +- 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp b/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp index 2a695fc28b2ff4..7e83f7412caef8 100644 --- a/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp +++ b/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp @@ -236,47 +236,51 @@ std::string simpleBasename(const std::string& path) { */ void ReactInstance::loadScript( std::unique_ptr script, - const std::string& sourceURL) { + const std::string& sourceURL, + std::function&& completion) { auto buffer = std::make_shared(std::move(script)); std::string scriptName = simpleBasename(sourceURL); - runtimeScheduler_->scheduleWork( - [this, - scriptName, - sourceURL, - buffer = std::move(buffer), - weakBufferedRuntimeExecuter = std::weak_ptr( - bufferedRuntimeExecutor_)](jsi::Runtime& runtime) { - SystraceSection s("ReactInstance::loadScript"); - bool hasLogger(ReactMarker::logTaggedMarkerBridgelessImpl); - if (hasLogger) { - ReactMarker::logTaggedMarkerBridgeless( - ReactMarker::RUN_JS_BUNDLE_START, scriptName.c_str()); - } + runtimeScheduler_->scheduleWork([this, + scriptName, + sourceURL, + buffer = std::move(buffer), + weakBufferedRuntimeExecuter = + std::weak_ptr( + bufferedRuntimeExecutor_), + completion](jsi::Runtime& runtime) { + SystraceSection s("ReactInstance::loadScript"); + bool hasLogger(ReactMarker::logTaggedMarkerBridgelessImpl); + if (hasLogger) { + ReactMarker::logTaggedMarkerBridgeless( + ReactMarker::RUN_JS_BUNDLE_START, scriptName.c_str()); + } - runtime.evaluateJavaScript(buffer, sourceURL); + runtime.evaluateJavaScript(buffer, sourceURL); - /** - * TODO(T183610671): We need a safe/reliable way to enable the js - * pipeline from javascript. Remove this after we figure that out, or - * after we just remove the js pipeline. - */ - if (!jsErrorHandler_->hasHandledFatalError()) { - jsErrorHandler_->setRuntimeReady(); - } + /** + * TODO(T183610671): We need a safe/reliable way to enable the js + * pipeline from javascript. Remove this after we figure that out, or + * after we just remove the js pipeline. + */ + if (!jsErrorHandler_->hasHandledFatalError()) { + jsErrorHandler_->setRuntimeReady(); + } - if (hasLogger) { - ReactMarker::logTaggedMarkerBridgeless( - ReactMarker::RUN_JS_BUNDLE_STOP, scriptName.c_str()); - ReactMarker::logMarkerBridgeless( - ReactMarker::INIT_REACT_RUNTIME_STOP); - ReactMarker::logMarkerBridgeless(ReactMarker::APP_STARTUP_STOP); - } - if (auto strongBufferedRuntimeExecuter = - weakBufferedRuntimeExecuter.lock()) { - strongBufferedRuntimeExecuter->flush(); - } - }); + if (hasLogger) { + ReactMarker::logTaggedMarkerBridgeless( + ReactMarker::RUN_JS_BUNDLE_STOP, scriptName.c_str()); + ReactMarker::logMarkerBridgeless(ReactMarker::INIT_REACT_RUNTIME_STOP); + ReactMarker::logMarkerBridgeless(ReactMarker::APP_STARTUP_STOP); + } + if (auto strongBufferedRuntimeExecuter = + weakBufferedRuntimeExecuter.lock()) { + strongBufferedRuntimeExecuter->flush(); + } + if (completion) { + completion(runtime); + } + }); } /* diff --git a/packages/react-native/ReactCommon/react/runtime/ReactInstance.h b/packages/react-native/ReactCommon/react/runtime/ReactInstance.h index 0380e3185d698c..a1e40537ecc1e9 100644 --- a/packages/react-native/ReactCommon/react/runtime/ReactInstance.h +++ b/packages/react-native/ReactCommon/react/runtime/ReactInstance.h @@ -49,7 +49,8 @@ class ReactInstance final : private jsinspector_modern::InstanceTargetDelegate { void loadScript( std::unique_ptr script, - const std::string& sourceURL); + const std::string& sourceURL, + std::function&& completion = nullptr); void registerSegment(uint32_t segmentId, const std::string& segmentPath); 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 8dd279da7d61b1..eb55876baf9a71 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 @@ -472,8 +472,9 @@ - (void)_loadScriptFromSource:(RCTSource *)source auto script = std::make_unique(source.data); const auto *url = deriveSourceURL(source.url).UTF8String; - _reactInstance->loadScript(std::move(script), url); - [[NSNotificationCenter defaultCenter] postNotificationName:@"RCTInstanceDidLoadBundle" object:nil]; + _reactInstance->loadScript(std::move(script), url, [](jsi::Runtime &_) { + [[NSNotificationCenter defaultCenter] postNotificationName:@"RCTInstanceDidLoadBundle" object:nil]; + }); } - (void)_handleJSError:(const JsErrorHandler::ParsedError &)error withRuntime:(jsi::Runtime &)runtime