Skip to content

Commit

Permalink
fix: make it possible to add multiple mountingOverrideDelegates (#44927)
Browse files Browse the repository at this point in the history
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 software-mansion/react-native-screens#2134 and `react-native-reanimated` in software-mansion/react-native-reanimated#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:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[GENERAL] [FIXED] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[GENERAL] [FIXED] - Add option for multiple `mountingOverrideDelegates`

Pull Request resolved: #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
  • Loading branch information
WoLewicki authored and facebook-github-bot committed Jun 14, 2024
1 parent 9093f6f commit 358fe46
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,31 +104,33 @@ std::optional<MountingTransaction> 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
Expand Down Expand Up @@ -203,7 +205,8 @@ ShadowTreeRevision MountingCoordinator::getBaseRevision() const {
void MountingCoordinator::setMountingOverrideDelegate(
std::weak_ptr<const MountingOverrideDelegate> delegate) const {
std::scoped_lock lock(mutex_);
mountingOverrideDelegate_ = std::move(delegate);
mountingOverrideDelegates_.insert(
mountingOverrideDelegates_.end(), std::move(delegate));
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ class MountingCoordinator final {
mutable std::optional<ShadowTreeRevision> lastRevision_{};
mutable MountingTransaction::Number number_{0};
mutable std::condition_variable signal_;
mutable std::weak_ptr<const MountingOverrideDelegate>
mountingOverrideDelegate_;
mutable std::vector<std::weak_ptr<const MountingOverrideDelegate>>
mountingOverrideDelegates_;

TelemetryController telemetryController_;

Expand Down

0 comments on commit 358fe46

Please sign in to comment.