diff --git a/packages/react-native/ReactCommon/react/renderer/animated/AnimatedModule.cpp b/packages/react-native/ReactCommon/react/renderer/animated/AnimatedModule.cpp index bc3285721a4..8a8b8edc79d 100644 --- a/packages/react-native/ReactCommon/react/renderer/animated/AnimatedModule.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animated/AnimatedModule.cpp @@ -27,17 +27,18 @@ void AnimatedModule::finishOperationBatch(jsi::Runtime& /*rt*/) { std::swap(preOperations_, preOperations); std::swap(operations_, operations); - if (nodesManager_) { + if (auto nodesManager = nodesManager_.lock()) { // TODO: nodesManager_ must exist at all times. But without this check // AnimatedProps-itest.js fails. - nodesManager_->scheduleOnUI([this, - preOperations = std::move(preOperations), - operations = std::move(operations)]() { + nodesManager->scheduleOnUI([this, + preOperations = std::move(preOperations), + operations = std::move(operations), + nodesManager = nodesManager_]() { for (auto& preOperation : preOperations) { - executeOperation(preOperation); + executeOperation(preOperation, nodesManager); } for (auto& operation : operations) { - executeOperation(operation); + executeOperation(operation, nodesManager); } }); } @@ -50,8 +51,8 @@ void AnimatedModule::createAnimatedNode( auto configDynamic = dynamicFromValue(rt, jsi::Value(rt, config)); if (auto it = configDynamic.find("disableBatchingForNativeCreate"); it != configDynamic.items().end() && it->second == true) { - if (nodesManager_) { - nodesManager_->createAnimatedNodeAsync(tag, configDynamic); + if (auto nodesManager = nodesManager_.lock()) { + nodesManager->createAnimatedNodeAsync(tag, configDynamic); } } else { operations_.emplace_back( @@ -220,79 +221,83 @@ void AnimatedModule::queueAndExecuteBatchedOperations( // TODO(T225953475): missing implementation } -void AnimatedModule::executeOperation(const Operation& operation) { - if (nodesManager_ == nullptr) { - return; - } - std::visit( - [&](const auto& op) { - using T = std::decay_t; +void AnimatedModule::executeOperation( + const Operation& operation, + std::weak_ptr nodesManagerWeak) { + if (auto nodesManager = nodesManagerWeak.lock()) { + std::visit( + [&](const auto& op) { + using T = std::decay_t; - if constexpr (std::is_same_v) { - nodesManager_->createAnimatedNode(op.tag, op.config); - } else if constexpr (std::is_same_v) { - auto animValue = nodesManager_->getValue(op.tag); - if (animValue) { - op.callback.call(animValue.value()); + if constexpr (std::is_same_v) { + nodesManager->createAnimatedNode(op.tag, op.config); + } else if constexpr (std::is_same_v) { + auto animValue = nodesManager->getValue(op.tag); + if (animValue) { + op.callback.call(animValue.value()); + } + } else if constexpr (std::is_same_v< + T, + StartListeningToAnimatedNodeValueOp>) { + nodesManager->startListeningToAnimatedNodeValue( + op.tag, [this, tag = op.tag](double value) { + emitDeviceEvent( + "onAnimatedValueUpdate", + [tag, value]( + jsi::Runtime& rt, std::vector& args) { + auto arg = jsi::Object(rt); + arg.setProperty(rt, "tag", jsi::Value(tag)); + arg.setProperty(rt, "value", jsi::Value(value)); + args.emplace_back(rt, arg); + }); + }); + } else if constexpr (std::is_same_v< + T, + StopListeningToAnimatedNodeValueOp>) { + nodesManager->stopListeningToAnimatedNodeValue(op.tag); + } else if constexpr (std::is_same_v) { + nodesManager->connectAnimatedNodes(op.parentTag, op.childTag); + } else if constexpr (std::is_same_v) { + nodesManager->disconnectAnimatedNodes(op.parentTag, op.childTag); + } else if constexpr (std::is_same_v) { + nodesManager->startAnimatingNode( + op.animationId, + op.nodeTag, + std::move(op.config), + std::move(op.endCallback)); + } else if constexpr (std::is_same_v) { + nodesManager->stopAnimation(op.animationId, false); + } else if constexpr (std::is_same_v) { + nodesManager->setAnimatedNodeValue(op.nodeTag, op.value); + } else if constexpr (std::is_same_v) { + nodesManager->setAnimatedNodeOffset(op.nodeTag, op.offset); + } else if constexpr (std::is_same_v) { + nodesManager->flattenAnimatedNodeOffset(op.nodeTag); + } else if constexpr (std::is_same_v) { + nodesManager->extractAnimatedNodeOffsetOp(op.nodeTag); + } else if constexpr (std::is_same_v) { + nodesManager->connectAnimatedNodeToView(op.nodeTag, op.viewTag); + } else if constexpr (std::is_same_v< + T, + DisconnectAnimatedNodeFromViewOp>) { + nodesManager->disconnectAnimatedNodeFromView( + op.nodeTag, op.viewTag); + } else if constexpr (std::is_same_v) { + nodesManager->restoreDefaultValues(op.nodeTag); + } else if constexpr (std::is_same_v) { + nodesManager->dropAnimatedNode(op.tag); + } else if constexpr (std::is_same_v) { + nodesManager->addAnimatedEventToView( + op.viewTag, op.eventName, op.eventMapping); + } else if constexpr (std::is_same_v< + T, + RemoveAnimatedEventFromViewOp>) { + nodesManager->removeAnimatedEventFromView( + op.viewTag, op.eventName, op.animatedNodeTag); } - } else if constexpr (std::is_same_v< - T, - StartListeningToAnimatedNodeValueOp>) { - nodesManager_->startListeningToAnimatedNodeValue( - op.tag, [this, tag = op.tag](double value) { - emitDeviceEvent( - "onAnimatedValueUpdate", - [tag, value]( - jsi::Runtime& rt, std::vector& args) { - auto arg = jsi::Object(rt); - arg.setProperty(rt, "tag", jsi::Value(tag)); - arg.setProperty(rt, "value", jsi::Value(value)); - args.emplace_back(rt, arg); - }); - }); - } else if constexpr (std::is_same_v< - T, - StopListeningToAnimatedNodeValueOp>) { - nodesManager_->stopListeningToAnimatedNodeValue(op.tag); - } else if constexpr (std::is_same_v) { - nodesManager_->connectAnimatedNodes(op.parentTag, op.childTag); - } else if constexpr (std::is_same_v) { - nodesManager_->disconnectAnimatedNodes(op.parentTag, op.childTag); - } else if constexpr (std::is_same_v) { - nodesManager_->startAnimatingNode( - op.animationId, - op.nodeTag, - std::move(op.config), - std::move(op.endCallback)); - } else if constexpr (std::is_same_v) { - nodesManager_->stopAnimation(op.animationId, false); - } else if constexpr (std::is_same_v) { - nodesManager_->setAnimatedNodeValue(op.nodeTag, op.value); - } else if constexpr (std::is_same_v) { - nodesManager_->setAnimatedNodeOffset(op.nodeTag, op.offset); - } else if constexpr (std::is_same_v) { - nodesManager_->flattenAnimatedNodeOffset(op.nodeTag); - } else if constexpr (std::is_same_v) { - nodesManager_->extractAnimatedNodeOffsetOp(op.nodeTag); - } else if constexpr (std::is_same_v) { - nodesManager_->connectAnimatedNodeToView(op.nodeTag, op.viewTag); - } else if constexpr (std::is_same_v< - T, - DisconnectAnimatedNodeFromViewOp>) { - nodesManager_->disconnectAnimatedNodeFromView(op.nodeTag, op.viewTag); - } else if constexpr (std::is_same_v) { - nodesManager_->restoreDefaultValues(op.nodeTag); - } else if constexpr (std::is_same_v) { - nodesManager_->dropAnimatedNode(op.tag); - } else if constexpr (std::is_same_v) { - nodesManager_->addAnimatedEventToView( - op.viewTag, op.eventName, op.eventMapping); - } else if constexpr (std::is_same_v) { - nodesManager_->removeAnimatedEventFromView( - op.viewTag, op.eventName, op.animatedNodeTag); - } - }, - operation); + }, + operation); + } } void AnimatedModule::installJSIBindingsWithRuntime(jsi::Runtime& runtime) { diff --git a/packages/react-native/ReactCommon/react/renderer/animated/AnimatedModule.h b/packages/react-native/ReactCommon/react/renderer/animated/AnimatedModule.h index 45614247448..a54254e6a6b 100644 --- a/packages/react-native/ReactCommon/react/renderer/animated/AnimatedModule.h +++ b/packages/react-native/ReactCommon/react/renderer/animated/AnimatedModule.h @@ -194,11 +194,11 @@ class AnimatedModule : public NativeAnimatedModuleCxxSpec, publi private: std::shared_ptr nodesManagerProvider_; - std::shared_ptr nodesManager_; + std::weak_ptr nodesManager_; std::vector preOperations_; std::vector operations_; - void executeOperation(const Operation &operation); + void executeOperation(const Operation &operation, std::weak_ptr nodesManagerWeak); void installJSIBindingsWithRuntime(jsi::Runtime &runtime) override; };