diff --git a/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp b/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp index a96f875e2fd..291cfa2caa4 100644 --- a/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp +++ b/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp @@ -27,6 +27,24 @@ namespace facebook::react { +namespace { + +std::shared_ptr createRuntimeScheduler( + RuntimeExecutor runtimeExecutor, + RuntimeSchedulerTaskErrorHandler taskErrorHandler) { + std::shared_ptr scheduler = + std::make_shared( + runtimeExecutor, RuntimeSchedulerClock::now, taskErrorHandler); + scheduler->setPerformanceEntryReporter( + // FIXME: Move creation of PerformanceEntryReporter to here and + // guarantee that its lifetime is the same as the runtime. + PerformanceEntryReporter::getInstance().get()); + + return scheduler; +} + +} // namespace + ReactInstance::ReactInstance( std::unique_ptr runtime, std::shared_ptr jsMessageQueueThread, @@ -93,13 +111,34 @@ ReactInstance::ReactInstance( if (parentInspectorTarget_) { auto executor = parentInspectorTarget_->executorFromThis(); - auto runtimeExecutorThatWaitsForInspectorSetup = + auto bufferedRuntimeExecutorThatWaitsForInspectorSetup = std::make_shared(runtimeExecutor); + auto runtimeExecutorThatExecutesAfterInspectorSetup = + [bufferedRuntimeExecutorThatWaitsForInspectorSetup]( + std::function&& callback) { + bufferedRuntimeExecutorThatWaitsForInspectorSetup->execute( + std::move(callback)); + }; + + runtimeScheduler_ = createRuntimeScheduler( + runtimeExecutorThatExecutesAfterInspectorSetup, + [jsErrorHandler = jsErrorHandler_]( + jsi::Runtime& runtime, jsi::JSError& error) { + jsErrorHandler->handleError(runtime, error, true); + }); + + auto runtimeExecutorThatGoesThroughRuntimeScheduler = + [runtimeScheduler = runtimeScheduler_.get()]( + std::function&& callback) { + runtimeScheduler->scheduleWork(std::move(callback)); + }; // This code can execute from any thread, so we need to make sure we set up // the inspector logic in the right one. The callback executes immediately // if we are already in the right thread. - executor([this, runtimeExecutor, runtimeExecutorThatWaitsForInspectorSetup]( + executor([this, + runtimeExecutorThatGoesThroughRuntimeScheduler, + bufferedRuntimeExecutorThatWaitsForInspectorSetup]( jsinspector_modern::HostTarget& hostTarget) { // Callbacks scheduled through the page target executor are generally // not guaranteed to run (e.g.: if the page target is destroyed) @@ -110,32 +149,19 @@ ReactInstance::ReactInstance( // creation task to finish before starting the destruction. inspectorTarget_ = &hostTarget.registerInstance(*this); runtimeInspectorTarget_ = &inspectorTarget_->registerRuntime( - runtime_->getRuntimeTargetDelegate(), runtimeExecutor); - runtimeExecutorThatWaitsForInspectorSetup->flush(); + runtime_->getRuntimeTargetDelegate(), + runtimeExecutorThatGoesThroughRuntimeScheduler); + bufferedRuntimeExecutorThatWaitsForInspectorSetup->flush(); }); - - // We decorate the runtime executor used everywhere else to wait for the - // inspector to finish its setup. - runtimeExecutor = - [runtimeExecutorThatWaitsForInspectorSetup]( - std::function&& callback) { - runtimeExecutorThatWaitsForInspectorSetup->execute( - std::move(callback)); - }; + } else { + runtimeScheduler_ = createRuntimeScheduler( + runtimeExecutor, + [jsErrorHandler = jsErrorHandler_]( + jsi::Runtime& runtime, jsi::JSError& error) { + jsErrorHandler->handleError(runtime, error, true); + }); } - runtimeScheduler_ = std::make_shared( - runtimeExecutor, - RuntimeSchedulerClock::now, - [jsErrorHandler = jsErrorHandler_]( - jsi::Runtime& runtime, jsi::JSError& error) { - jsErrorHandler->handleError(runtime, error, true); - }); - runtimeScheduler_->setPerformanceEntryReporter( - // FIXME: Move creation of PerformanceEntryReporter to here and guarantee - // that its lifetime is the same as the runtime. - PerformanceEntryReporter::getInstance().get()); - bufferedRuntimeExecutor_ = std::make_shared( [runtimeScheduler = runtimeScheduler_.get()]( std::function&& callback) {