From 358fe46969654932e73af94127c05e6ce5ed7049 Mon Sep 17 00:00:00 2001 From: Wojciech Lewicki Date: Fri, 14 Jun 2024 10:22:32 -0700 Subject: [PATCH] fix: make it possible to add multiple mountingOverrideDelegates (#44927) Summary: PR changing the single mountingOverrideDelegate to a vector of those, so other listeners can operate on the transaction. Used by `react-native-screens` in https://github.com/software-mansion/react-native-screens/pull/2134 and `react-native-reanimated` in https://github.com/software-mansion/react-native-reanimated/pull/6055. Till now, only one listener could be added there, meaning that e.g. `Layout Animations` from `react-native`, `Layout Animations` from `react-native-reanimated` and listening for `Screen` removal in `react-native-screens` could not operate at the same time. ## Changelog: [GENERAL] [FIXED] - Add option for multiple `mountingOverrideDelegates` Pull Request resolved: https://github.com/facebook/react-native/pull/44927 Test Plan: The code of `LayoutAnimations` inside `react-native` should work the same since it will add just one listener then. For other cases, different libraries can read/mutate transactions. Reviewed By: javache Differential Revision: D58530278 Pulled By: sammy-SC fbshipit-source-id: d6305963621000be11d51a50cffff64526cca934 --- .../renderer/mounting/MountingCoordinator.cpp | 53 ++++++++++--------- .../renderer/mounting/MountingCoordinator.h | 4 +- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/mounting/MountingCoordinator.cpp b/packages/react-native/ReactCommon/react/renderer/mounting/MountingCoordinator.cpp index 77cc62690b9f10..f24772694f51ad 100644 --- a/packages/react-native/ReactCommon/react/renderer/mounting/MountingCoordinator.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mounting/MountingCoordinator.cpp @@ -104,31 +104,33 @@ std::optional MountingCoordinator::pullTransaction() } // Override case - auto mountingOverrideDelegate = mountingOverrideDelegate_.lock(); - auto shouldOverridePullTransaction = mountingOverrideDelegate && - mountingOverrideDelegate->shouldOverridePullTransaction(); - - if (shouldOverridePullTransaction) { - SystraceSection section2("MountingCoordinator::overridePullTransaction"); - - auto mutations = ShadowViewMutation::List{}; - auto telemetry = TransactionTelemetry{}; - - if (transaction.has_value()) { - mutations = transaction->getMutations(); - telemetry = transaction->getTelemetry(); - } else { - number_++; - telemetry.willLayout(); - telemetry.didLayout(); - telemetry.willCommit(); - telemetry.didCommit(); - telemetry.willDiff(); - telemetry.didDiff(); - } + for (const auto& delegate : mountingOverrideDelegates_) { + auto mountingOverrideDelegate = delegate.lock(); + auto shouldOverridePullTransaction = mountingOverrideDelegate && + mountingOverrideDelegate->shouldOverridePullTransaction(); + + if (shouldOverridePullTransaction) { + SystraceSection section2("MountingCoordinator::overridePullTransaction"); + + auto mutations = ShadowViewMutation::List{}; + auto telemetry = TransactionTelemetry{}; + + if (transaction.has_value()) { + mutations = transaction->getMutations(); + telemetry = transaction->getTelemetry(); + } else { + number_++; + telemetry.willLayout(); + telemetry.didLayout(); + telemetry.willCommit(); + telemetry.didCommit(); + telemetry.willDiff(); + telemetry.didDiff(); + } - transaction = mountingOverrideDelegate->pullTransaction( - surfaceId_, number_, telemetry, std::move(mutations)); + transaction = mountingOverrideDelegate->pullTransaction( + surfaceId_, number_, telemetry, std::move(mutations)); + } } #ifdef RN_SHADOW_TREE_INTROSPECTION @@ -203,7 +205,8 @@ ShadowTreeRevision MountingCoordinator::getBaseRevision() const { void MountingCoordinator::setMountingOverrideDelegate( std::weak_ptr delegate) const { std::scoped_lock lock(mutex_); - mountingOverrideDelegate_ = std::move(delegate); + mountingOverrideDelegates_.insert( + mountingOverrideDelegates_.end(), std::move(delegate)); } } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/mounting/MountingCoordinator.h b/packages/react-native/ReactCommon/react/renderer/mounting/MountingCoordinator.h index 87078034076b6e..6b2bca898a8118 100644 --- a/packages/react-native/ReactCommon/react/renderer/mounting/MountingCoordinator.h +++ b/packages/react-native/ReactCommon/react/renderer/mounting/MountingCoordinator.h @@ -119,8 +119,8 @@ class MountingCoordinator final { mutable std::optional lastRevision_{}; mutable MountingTransaction::Number number_{0}; mutable std::condition_variable signal_; - mutable std::weak_ptr - mountingOverrideDelegate_; + mutable std::vector> + mountingOverrideDelegates_; TelemetryController telemetryController_;