From 8dede75fe35a56fe31969c4829afc96010e174b0 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Fri, 10 Apr 2026 02:45:35 -0700 Subject: [PATCH] Simplify jsi::Function storage in UIManagerBinding::startViewTransition (#56403) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56403 Document the threading contract on UIManagerViewTransitionDelegate::startViewTransition and simplifying how we track the jsi::Function Changelog: [Internal] Reviewed By: zeyap Differential Revision: D98717252 fbshipit-source-id: 6e24728efa8f316fda0b4eb9e8f2291d5bcb8fda --- .../renderer/uimanager/UIManagerBinding.cpp | 38 ++++++++++--------- .../UIManagerViewTransitionDelegate.h | 5 +++ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp index c9d08a9d80b..1aa9fdf2ab2 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp @@ -1064,10 +1064,8 @@ jsi::Value UIManagerBinding::get( auto promiseConstructor = runtime.global().getPropertyAsFunction(runtime, "Promise"); - auto readyResolveFunc = - std::make_shared>(); - auto finishedResolveFunc = - std::make_shared>(); + std::shared_ptr readyResolveFunc; + std::shared_ptr finishedResolveFunc; auto mutationFunc = std::make_shared( arguments[0].asObject(runtime).asFunction(runtime)); @@ -1078,14 +1076,13 @@ jsi::Value UIManagerBinding::get( runtime, jsi::PropNameID::forAscii(runtime, "readyExecutor"), 2, - [readyResolveFunc]( + [&readyResolveFunc]( jsi::Runtime& runtime, const jsi::Value& /*thisValue*/, const jsi::Value* args, size_t /*count*/) -> jsi::Value { - auto onReadyFunc = std::make_shared( + readyResolveFunc = std::make_shared( args[0].asObject(runtime).asFunction(runtime)); - *readyResolveFunc = onReadyFunc; return jsi::Value::undefined(); })); @@ -1095,19 +1092,21 @@ jsi::Value UIManagerBinding::get( runtime, jsi::PropNameID::forAscii(runtime, "finishedExecutor"), 2, - [finishedResolveFunc, viewTransitionDelegate]( + [&finishedResolveFunc, viewTransitionDelegate]( jsi::Runtime& rt, const jsi::Value& /*thisValue*/, const jsi::Value* args, - size_t /*count*/) -> jsi::Value { + size_t /*count*/) mutable -> jsi::Value { auto onCompleteFunc = std::make_shared( args[0].asObject(rt).asFunction(rt)); - *finishedResolveFunc = std::make_shared( + finishedResolveFunc = std::make_shared( jsi::Function::createFromHostFunction( rt, jsi::PropNameID::forAscii(rt, "finishedResolve"), 0, - [onCompleteFunc, viewTransitionDelegate]( + [onCompleteFunc = std::move(onCompleteFunc), + viewTransitionDelegate = + std::move(viewTransitionDelegate)]( jsi::Runtime& runtime, const jsi::Value& /*thisValue*/, const jsi::Value* /*args*/, @@ -1125,20 +1124,25 @@ jsi::Value UIManagerBinding::get( viewTransitionDelegate->startViewTransition( [&runtime, mutationFunc = std::move(mutationFunc)]() { + // mutationCallback is called synchronously by the + // delegate during startViewTransition, so &runtime is + // valid (it comes from the enclosing host function). mutationFunc->call(runtime); }, [readyResolveFunc = std::move(readyResolveFunc), &runtime]() { - if (*readyResolveFunc) { - (*readyResolveFunc)->call(runtime); + // onReadyCallback is called synchronously on the JS + // thread by the delegate, so &runtime is valid. + if (readyResolveFunc) { + readyResolveFunc->call(runtime); } }, [finishedResolveFunc = std::move(finishedResolveFunc), - uiManager]() { - uiManager->runtimeExecutor_( + runtimeExecutor = uiManager->runtimeExecutor_]() { + runtimeExecutor( [finishedResolveFunc = std::move(finishedResolveFunc)]( jsi::Runtime& rt) mutable { - if (*finishedResolveFunc) { - (*finishedResolveFunc)->call(rt); + if (finishedResolveFunc) { + finishedResolveFunc->call(rt); } }); }); diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerViewTransitionDelegate.h b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerViewTransitionDelegate.h index 702ec141a27..9d4d83637f4 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerViewTransitionDelegate.h +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerViewTransitionDelegate.h @@ -27,6 +27,11 @@ class UIManagerViewTransitionDelegate { virtual void restoreViewTransitionName(const ShadowNode &shadowNode) {} + /* + * Start a view transition. mutationCallback and onReadyCallback MUST be + * called from a thread with access to the jsi::Runtime. + * onCompleteCallback may be called from any thread. + */ virtual void startViewTransition( std::function mutationCallback, std::function onReadyCallback,