diff --git a/React/Fabric/RCTScheduler.mm b/React/Fabric/RCTScheduler.mm index d89af23b385..67afffa3b0f 100644 --- a/React/Fabric/RCTScheduler.mm +++ b/React/Fabric/RCTScheduler.mm @@ -114,7 +114,7 @@ class LayoutAnimationDelegateProxy : public LayoutAnimationStatusDelegate, publi if (_layoutAnimationsEnabled) { _layoutAnimationDelegateProxy = std::make_shared((__bridge void *)self); - _animationDriver = std::make_unique(_layoutAnimationDelegateProxy.get()); + _animationDriver = std::make_shared(_layoutAnimationDelegateProxy.get()); _uiRunLoopObserver = toolbox.mainRunLoopObserverFactory(RunLoopObserver::Activity::BeforeWaiting, _layoutAnimationDelegateProxy); _uiRunLoopObserver->setDelegate(_layoutAnimationDelegateProxy.get()); @@ -152,12 +152,7 @@ class LayoutAnimationDelegateProxy : public LayoutAnimationStatusDelegate, publi auto props = convertIdToFollyDynamic(initialProps); _scheduler->startSurface( - surfaceId, - RCTStringFromNSString(moduleName), - props, - layoutConstraints, - layoutContext, - (_animationDriver ? _animationDriver.get() : nullptr)); + surfaceId, RCTStringFromNSString(moduleName), props, layoutConstraints, layoutContext, _animationDriver); _scheduler->renderTemplateToSurface( surfaceId, props.getDefault("navigationConfig").getDefault("initialUITemplate", "").getString()); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp index 025d035502e..f78973739fc 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp @@ -73,10 +73,6 @@ std::shared_ptr Binding::getScheduler() { return scheduler_; } -LayoutAnimationDriver *Binding::getAnimationDriver() { - return (animationDriver_ ? animationDriver_.get() : nullptr); -} - void Binding::startSurface( jint surfaceId, jni::alias_ref moduleName, @@ -97,7 +93,7 @@ void Binding::startSurface( initialProps->consume(), {}, context, - getAnimationDriver()); + animationDriver_); } void Binding::startSurfaceWithConstraints( @@ -144,7 +140,7 @@ void Binding::startSurfaceWithConstraints( initialProps->consume(), constraints, context, - getAnimationDriver()); + animationDriver_); } void Binding::renderTemplateToSurface(jint surfaceId, jstring uiTemplate) { @@ -297,9 +293,10 @@ void Binding::installFabricUIManager( toolbox.asynchronousEventBeatFactory = asynchronousBeatFactory; if (enableLayoutAnimations_) { - animationDriver_ = std::make_unique(this); + animationDriver_ = std::make_shared(this); } - scheduler_ = std::make_shared(toolbox, getAnimationDriver(), this); + scheduler_ = std::make_shared( + toolbox, (animationDriver_ ? animationDriver_.get() : nullptr), this); } void Binding::uninstallFabricUIManager() { diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h index 504fc83b252..77b6842e47f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h @@ -110,7 +110,7 @@ class Binding : public jni::HybridClass, virtual void onAnimationStarted() override; virtual void onAllAnimationsComplete() override; LayoutAnimationDriver *getAnimationDriver(); - std::unique_ptr animationDriver_; + std::shared_ptr animationDriver_; std::shared_ptr scheduler_; std::mutex schedulerMutex_; diff --git a/ReactCommon/fabric/mounting/MountingCoordinator.cpp b/ReactCommon/fabric/mounting/MountingCoordinator.cpp index ad1552f1569..ee6b7440fe8 100644 --- a/ReactCommon/fabric/mounting/MountingCoordinator.cpp +++ b/ReactCommon/fabric/mounting/MountingCoordinator.cpp @@ -21,7 +21,7 @@ namespace react { MountingCoordinator::MountingCoordinator( ShadowTreeRevision baseRevision, - MountingOverrideDelegate *delegate) + std::weak_ptr delegate) : surfaceId_(baseRevision.getRootShadowNode().getSurfaceId()), baseRevision_(baseRevision), mountingOverrideDelegate_(delegate) { @@ -115,8 +115,10 @@ better::optional MountingCoordinator::pullTransaction() const { std::lock_guard lock(mutex_); - bool shouldOverridePullTransaction = mountingOverrideDelegate_ != nullptr && - mountingOverrideDelegate_->shouldOverridePullTransaction(); + auto mountingOverrideDelegate = mountingOverrideDelegate_.lock(); + + bool shouldOverridePullTransaction = mountingOverrideDelegate && + mountingOverrideDelegate->shouldOverridePullTransaction(); if (!shouldOverridePullTransaction && !lastRevision_.has_value()) { return {}; @@ -143,7 +145,7 @@ better::optional MountingCoordinator::pullTransaction() // even if there's no `lastRevision_`. Consider cases of animation frames // in between React tree updates. if (shouldOverridePullTransaction) { - transaction = mountingOverrideDelegate_->pullTransaction( + transaction = mountingOverrideDelegate->pullTransaction( surfaceId_, number_, telemetry, std::move(diffMutations)); } else if (lastRevision_.hasValue()) { transaction = MountingTransaction{ diff --git a/ReactCommon/fabric/mounting/MountingCoordinator.h b/ReactCommon/fabric/mounting/MountingCoordinator.h index 6976f35f48b..556258ef2eb 100644 --- a/ReactCommon/fabric/mounting/MountingCoordinator.h +++ b/ReactCommon/fabric/mounting/MountingCoordinator.h @@ -40,7 +40,7 @@ class MountingCoordinator final { */ MountingCoordinator( ShadowTreeRevision baseRevision, - MountingOverrideDelegate *delegate); + std::weak_ptr delegate); /* * Returns the id of the surface that the coordinator belongs to. @@ -102,7 +102,7 @@ class MountingCoordinator final { mutable better::optional lastRevision_{}; mutable MountingTransaction::Number number_{0}; mutable std::condition_variable signal_; - mutable MountingOverrideDelegate *mountingOverrideDelegate_{nullptr}; + std::weak_ptr mountingOverrideDelegate_; #ifdef RN_SHADOW_TREE_INTROSPECTION void validateTransactionAgainstStubViewTree( diff --git a/ReactCommon/fabric/mounting/ShadowTree.cpp b/ReactCommon/fabric/mounting/ShadowTree.cpp index d6a40be9170..a834310e1db 100644 --- a/ReactCommon/fabric/mounting/ShadowTree.cpp +++ b/ReactCommon/fabric/mounting/ShadowTree.cpp @@ -222,7 +222,7 @@ ShadowTree::ShadowTree( LayoutContext const &layoutContext, RootComponentDescriptor const &rootComponentDescriptor, ShadowTreeDelegate const &delegate, - MountingOverrideDelegate *mountingOverrideDelegate) + std::weak_ptr mountingOverrideDelegate) : surfaceId_(surfaceId), delegate_(delegate) { const auto noopEventEmitter = std::make_shared( nullptr, -1, std::shared_ptr()); diff --git a/ReactCommon/fabric/mounting/ShadowTree.h b/ReactCommon/fabric/mounting/ShadowTree.h index a4606ba9a88..5090aeafd70 100644 --- a/ReactCommon/fabric/mounting/ShadowTree.h +++ b/ReactCommon/fabric/mounting/ShadowTree.h @@ -40,7 +40,7 @@ class ShadowTree final { LayoutContext const &layoutContext, RootComponentDescriptor const &rootComponentDescriptor, ShadowTreeDelegate const &delegate, - MountingOverrideDelegate *mountingOverrideDelegate); + std::weak_ptr mountingOverrideDelegate); ~ShadowTree(); diff --git a/ReactCommon/fabric/mounting/tests/StateReconciliationTest.cpp b/ReactCommon/fabric/mounting/tests/StateReconciliationTest.cpp index 665f18bcf6c..eb6129f5bf4 100644 --- a/ReactCommon/fabric/mounting/tests/StateReconciliationTest.cpp +++ b/ReactCommon/fabric/mounting/tests/StateReconciliationTest.cpp @@ -104,7 +104,7 @@ TEST(StateReconciliationTest, testStateReconciliation) { LayoutContext{}, rootComponentDescriptor, shadowTreeDelegate, - nullptr}; + {}}; shadowTree.commit( [&](RootShadowNode::Shared const &oldRootShadowNode) { diff --git a/ReactCommon/fabric/scheduler/Scheduler.cpp b/ReactCommon/fabric/scheduler/Scheduler.cpp index d4dafd716b6..076d269e58c 100644 --- a/ReactCommon/fabric/scheduler/Scheduler.cpp +++ b/ReactCommon/fabric/scheduler/Scheduler.cpp @@ -173,7 +173,8 @@ void Scheduler::startSurface( const folly::dynamic &initialProps, const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext, - MountingOverrideDelegate *mountingOverrideDelegate) const { + std::weak_ptr mountingOverrideDelegate) + const { SystraceSection s("Scheduler::startSurface"); auto shadowTree = std::make_unique( diff --git a/ReactCommon/fabric/scheduler/Scheduler.h b/ReactCommon/fabric/scheduler/Scheduler.h index 9ee114ec21e..2693b821939 100644 --- a/ReactCommon/fabric/scheduler/Scheduler.h +++ b/ReactCommon/fabric/scheduler/Scheduler.h @@ -46,7 +46,8 @@ class Scheduler final : public UIManagerDelegate { const folly::dynamic &initialProps, const LayoutConstraints &layoutConstraints = {}, const LayoutContext &layoutContext = {}, - MountingOverrideDelegate *mountingOverrideDelegate = nullptr) const; + std::weak_ptr mountingOverrideDelegate = + {}) const; void renderTemplateToSurface( SurfaceId surfaceId,