react-native-reanimated 4.7.1 → 4.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/Common/cpp/reanimated/CSS/common/values/CSSNumber.cpp +5 -9
  2. package/Common/cpp/reanimated/CSS/common/values/CSSNumber.h +2 -6
  3. package/Common/cpp/reanimated/Fabric/updates/AnimatedPropsRegistry.cpp +135 -33
  4. package/Common/cpp/reanimated/Fabric/updates/AnimatedPropsRegistry.h +37 -3
  5. package/Common/cpp/reanimated/Fabric/updates/UpdatesRegistry.cpp +28 -0
  6. package/Common/cpp/reanimated/Fabric/updates/UpdatesRegistry.h +8 -0
  7. package/Common/cpp/reanimated/Fabric/updates/UpdatesRegistryManager.cpp +4 -1
  8. package/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy.cpp +228 -87
  9. package/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy.h +22 -5
  10. package/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxyCommon.cpp +9 -12
  11. package/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.cpp +6 -5
  12. package/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.h +5 -2
  13. package/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsUtils.h +22 -1
  14. package/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp +39 -9
  15. package/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h +1 -1
  16. package/Common/cpp/reanimated/Tools/PlatformDepMethodsHolder.h +13 -0
  17. package/Common/cpp/reanimated/Tools/TextMetrics.h +22 -0
  18. package/android/src/main/cpp/reanimated/android/NativeProxy.cpp +19 -0
  19. package/android/src/main/cpp/reanimated/android/NativeProxy.h +1 -0
  20. package/android/src/main/java/com/swmansion/reanimated/NativeProxy.kt +24 -0
  21. package/apple/reanimated/apple/REAReducedMotion.h +1 -3
  22. package/apple/reanimated/apple/native/PlatformDepMethodsHolderImpl.mm +28 -0
  23. package/jest/resolver.js +4 -1
  24. package/lib/module/PropsRegistryGarbageCollector.js +43 -25
  25. package/lib/module/PropsRegistryGarbageCollector.js.map +1 -1
  26. package/lib/module/ReanimatedModule/NativeReanimated.js +2 -2
  27. package/lib/module/ReanimatedModule/NativeReanimated.js.map +1 -1
  28. package/lib/module/commonTypes.js.map +1 -1
  29. package/lib/module/createAnimatedComponent/AnimatedComponent.js +1 -1
  30. package/lib/module/createAnimatedComponent/AnimatedComponent.js.map +1 -1
  31. package/lib/module/createAnimatedComponent/AnimatedComponent.native.js +10 -34
  32. package/lib/module/createAnimatedComponent/AnimatedComponent.native.js.map +1 -1
  33. package/lib/module/platform-specific/jsVersion.js +1 -1
  34. package/lib/module/platformFunctions/setNativeProps.native.js +2 -1
  35. package/lib/module/platformFunctions/setNativeProps.native.js.map +1 -1
  36. package/lib/module/updateProps/updateProps.native.js +5 -4
  37. package/lib/module/updateProps/updateProps.native.js.map +1 -1
  38. package/lib/typescript/PropsRegistryGarbageCollector.d.ts +2 -2
  39. package/lib/typescript/PropsRegistryGarbageCollector.d.ts.map +1 -1
  40. package/lib/typescript/ReanimatedModule/reanimatedModuleProxy.d.ts +1 -1
  41. package/lib/typescript/ReanimatedModule/reanimatedModuleProxy.d.ts.map +1 -1
  42. package/lib/typescript/commonTypes.d.ts +3 -1
  43. package/lib/typescript/commonTypes.d.ts.map +1 -1
  44. package/lib/typescript/createAnimatedComponent/AnimatedComponent.d.ts +1 -1
  45. package/lib/typescript/createAnimatedComponent/AnimatedComponent.d.ts.map +1 -1
  46. package/lib/typescript/createAnimatedComponent/AnimatedComponent.native.d.ts +1 -1
  47. package/lib/typescript/createAnimatedComponent/AnimatedComponent.native.d.ts.map +1 -1
  48. package/lib/typescript/createAnimatedComponent/commonTypes.d.ts +1 -1
  49. package/lib/typescript/createAnimatedComponent/commonTypes.d.ts.map +1 -1
  50. package/lib/typescript/platform-specific/jsVersion.d.ts +1 -1
  51. package/lib/typescript/updateProps/updateProps.native.d.ts.map +1 -1
  52. package/package.json +7 -7
  53. package/src/PropsRegistryGarbageCollector.ts +46 -25
  54. package/src/ReanimatedModule/NativeReanimated.ts +2 -2
  55. package/src/ReanimatedModule/reanimatedModuleProxy.ts +1 -1
  56. package/src/commonTypes.ts +3 -1
  57. package/src/createAnimatedComponent/AnimatedComponent.native.tsx +5 -30
  58. package/src/createAnimatedComponent/AnimatedComponent.tsx +1 -1
  59. package/src/createAnimatedComponent/commonTypes.ts +1 -1
  60. package/src/platform-specific/jsVersion.ts +1 -1
  61. package/src/platformFunctions/setNativeProps.native.ts +3 -1
  62. package/src/privateGlobals.d.ts +1 -0
  63. package/src/updateProps/updateProps.native.ts +10 -3
@@ -59,6 +59,39 @@ std::vector<AncestorOrigin> ancestorOrigins(
59
59
  }
60
60
  return chain;
61
61
  }
62
+
63
+ void collectSubtreeTags(const std::shared_ptr<LightNode> &node, std::unordered_set<Tag> &tags) {
64
+ tags.insert(node->current.tag);
65
+ for (const auto &child : node->children) {
66
+ collectSubtreeTags(child, tags);
67
+ }
68
+ }
69
+
70
+ // Every exiting ancestor that the removals leave without children.
71
+ void collectDroppedAncestors(const std::shared_ptr<LightNode> &node, std::unordered_set<Tag> &removals) {
72
+ for (auto ancestor = node->parent.lock(); ancestor && ancestor->isExiting() && ancestor->state != ANIMATING;
73
+ ancestor = ancestor->parent.lock()) {
74
+ const auto tag = ancestor->current.tag;
75
+ const auto keepsAChild = std::ranges::any_of(
76
+ ancestor->children, [&removals](const auto &child) { return !removals.contains(child->current.tag); });
77
+ if (removals.contains(tag) || keepsAChild) {
78
+ return;
79
+ }
80
+ removals.insert(tag);
81
+ }
82
+ }
83
+
84
+ // The subtrees of the roots and the ancestors that they drop.
85
+ std::unordered_set<Tag> collectRemovals(const std::vector<std::shared_ptr<LightNode>> &roots) {
86
+ std::unordered_set<Tag> removals;
87
+ for (const auto &root : roots) {
88
+ collectSubtreeTags(root, removals);
89
+ }
90
+ for (const auto &root : roots) {
91
+ collectDroppedAncestors(root, removals);
92
+ }
93
+ return removals;
94
+ }
62
95
  } // namespace
63
96
 
64
97
  std::shared_ptr<LayoutAnimationsProxyRegistry> createLayoutAnimationsProxyDefaultRegistry(
@@ -102,6 +135,20 @@ void LayoutAnimationsProxy::warnIfSnapshotIsStale(const ShadowView &snapshot, co
102
135
  }
103
136
  }
104
137
 
138
+ // A layout animation sends its frames to the platform as Updates from its current view.
139
+ const ShadowView &LayoutAnimationsProxy::mountedView(const std::shared_ptr<LightNode> &node) const {
140
+ const auto tag = node->current.tag;
141
+ if (const auto animationIt = layoutAnimations_.find(tag); animationIt != layoutAnimations_.end()) {
142
+ return animationIt->second.currentView;
143
+ }
144
+ if (const auto completedAnimationIt = completedAnimations_.find(tag);
145
+ completedAnimationIt != completedAnimations_.end()) {
146
+ return completedAnimationIt->second.animation.currentView;
147
+ }
148
+ resolveLightNodeProps(node);
149
+ return node->current;
150
+ }
151
+
105
152
  std::optional<ShadowView> LayoutAnimationsProxy::reparentLayoutAnimation(
106
153
  const Tag tag,
107
154
  const Tag parentTag,
@@ -157,7 +204,7 @@ std::optional<MountingTransaction> LayoutAnimationsProxy::pullTransaction(
157
204
  auto rootChildCount = static_cast<int>(lightNodes_[surfaceId_]->children.size());
158
205
  const bool flushStructuralMutations = shouldFlushStructuralMutations();
159
206
 
160
- reconcileContradictedRemovals(mutations, filteredMutations);
207
+ reconcileContradictedRemovals(mutations, transaction, propsParserContext);
161
208
 
162
209
  if constexpr (!StaticFeatureFlags::getFlag("ENABLE_SHARED_ELEMENT_TRANSITIONS")) {
163
210
  if (!mutations.empty()) {
@@ -217,14 +264,15 @@ std::optional<MountingTransaction> LayoutAnimationsProxy::pullTransaction(
217
264
  filteredMutations.insert(
218
265
  filteredMutations.end(), transaction.teardownMutations.begin(), transaction.teardownMutations.end());
219
266
 
267
+ collectDueRemovals(transaction);
220
268
  if (flushStructuralMutations) {
221
- flushCompletedRemovals(filteredMutations);
269
+ tearDown(transaction.dueRemovals, filteredMutations);
222
270
  }
223
271
 
224
272
  configLock.unlock();
225
273
  flushLayoutAnimationOperations(lock);
226
274
 
227
- addOngoingAnimations(filteredMutations);
275
+ addOngoingAnimations(transaction);
228
276
 
229
277
  cleanupAnimations(transaction, propsParserContext, flushStructuralMutations);
230
278
 
@@ -240,6 +288,10 @@ std::optional<MountingTransaction> LayoutAnimationsProxy::pullTransaction(
240
288
  keepTransitioningViewsHidden(filteredMutations, propsParserContext);
241
289
  }
242
290
 
291
+ react_native_assert(!deletesCreatedTag(filteredMutations) && "Transaction deletes a view that it creates");
292
+ #ifdef ANDROID
293
+ holdFramesUntilBatchIsQueued(filteredMutations);
294
+ #endif
243
295
  return MountingTransaction{surfaceId, transactionNumber, std::move(filteredMutations), telemetry};
244
296
  }
245
297
 
@@ -271,47 +323,79 @@ void LayoutAnimationsProxy::unmapLightNode(const std::shared_ptr<LightNode> &nod
271
323
  }
272
324
  }
273
325
 
274
- // If React re-creates or re-inserts a tag whose exiting removal we are still
275
- // withholding, it has contradicted that withheld removal. Flush it now instead
276
- // of letting the stale node linger: updateLightTree would overwrite its
277
- // lightNodes_ entry (the "LightNode already exists" assert is compiled out in
278
- // release), orphaning the still-mounted exiting view, and the eventual
279
- // removal flush would then remove the wrong, live view and crash the
280
- // mounting layer.
281
- //
282
- // This must run before updateLightTree (so the tag is re-registered cleanly)
283
- // and before addOngoingAnimations (which would otherwise emit an Update for a
284
- // tag we are about to Delete this frame).
326
+ // React re-creating a tag that the light tree still holds keeps its mounted view: the Create becomes an Update of it
327
+ // and the Delete is dropped, because Android mounts the Deletes of a transaction after its Creates. A live node
328
+ // is deleted in the same transaction, for example when React Native's LayoutAnimation flushes a Remove and Delete
329
+ // that it deferred.
285
330
  void LayoutAnimationsProxy::reconcileContradictedRemovals(
286
- const ShadowViewMutationList &mutations,
287
- ShadowViewMutationList &filteredMutations) const {
331
+ ShadowViewMutationList &mutations,
332
+ TransactionMeta &transaction,
333
+ [[maybe_unused]] const PropsParserContext &propsParserContext) const {
334
+ auto &filteredMutations = transaction.filteredMutations;
335
+ auto &recreatedTags = transaction.recreatedTags;
336
+ std::unordered_set<Tag> deletedTags;
288
337
  for (const auto &mutation : mutations) {
289
- if (mutation.type != ShadowViewMutation::Type::Create && mutation.type != ShadowViewMutation::Type::Insert) {
338
+ if (mutation.type == ShadowViewMutation::Type::Delete) {
339
+ deletedTags.insert(mutation.oldChildShadowView.tag);
340
+ }
341
+ }
342
+ std::vector<std::shared_ptr<LightNode>> withheldNodes;
343
+ for (auto &mutation : mutations) {
344
+ if (mutation.type != ShadowViewMutation::Type::Create) {
290
345
  continue;
291
346
  }
292
347
  const auto tag = mutation.newChildShadowView.tag;
293
348
  const auto it = lightNodes_.find(tag);
294
- if (it == lightNodes_.end() || it->second->state == LIVE) {
349
+ if (it == lightNodes_.end() || (it->second->state == LIVE && !deletedTags.contains(tag))) {
295
350
  continue;
296
351
  }
297
- const auto node = it->second;
298
- completedAnimations_.erase(tag);
299
- updateMap_.erase(tag);
300
- unmapLightNode(node);
352
+ const auto &node = it->second;
301
353
  const auto parent = node->parent.lock();
302
354
  react_native_assert(parent && "Parent node is nullptr");
303
- if (!parent) {
304
- continue;
355
+ recreatedTags.insert(tag);
356
+ const auto &mounted = mountedView(node);
357
+ auto recreated = mutation.newChildShadowView;
358
+ #ifdef ANDROID
359
+ recreated = resetPropsMissingFrom(recreated, mounted, propsParserContext);
360
+ #endif
361
+ auto update = ShadowViewMutation::UpdateMutation(mounted, recreated, parent->current.tag);
362
+ if (node->state == LIVE) {
363
+ mutation = std::move(update);
364
+ } else {
365
+ filteredMutations.push_back(std::move(update));
366
+ withheldNodes.push_back(node);
305
367
  }
306
- const auto index = parent->removeChild(node);
307
- react_native_assert(index != -1 && "Exiting node not found");
308
- if (index == -1) {
309
- continue;
368
+ }
369
+ tearDown(collectRemovals(withheldNodes), filteredMutations);
370
+ const auto deletesRecreatedTag = [&recreatedTags](const ShadowViewMutation &mutation) {
371
+ return mutation.type == ShadowViewMutation::Delete && recreatedTags.contains(mutation.oldChildShadowView.tag);
372
+ };
373
+ std::erase_if(mutations, deletesRecreatedTag);
374
+ std::erase_if(filteredMutations, deletesRecreatedTag);
375
+ }
376
+
377
+ #ifdef ANDROID
378
+ // Android applies the raw props of an Update as they are, so a prop that the mounted view has and the view leaves
379
+ // out is set to null, which resets it.
380
+ ShadowView LayoutAnimationsProxy::resetPropsMissingFrom(
381
+ const ShadowView &view,
382
+ const ShadowView &mounted,
383
+ const PropsParserContext &propsParserContext) const {
384
+ auto rawProps = view.props->rawProps;
385
+ for (const auto &[name, _] : mounted.props->rawProps.items()) {
386
+ if (rawProps.count(name) == 0) {
387
+ rawProps[name] = nullptr;
310
388
  }
311
- endAnimationsRecursively(node, index, filteredMutations);
312
- maybeDropAncestors(parent, filteredMutations);
313
389
  }
390
+ if (rawProps.size() == view.props->rawProps.size()) {
391
+ return view;
392
+ }
393
+ auto newView = view;
394
+ newView.props = componentDescriptorRegistry_->at(view.componentHandle)
395
+ .cloneProps(propsParserContext, view.props, RawProps(std::move(rawProps)));
396
+ return newView;
314
397
  }
398
+ #endif
315
399
 
316
400
  bool LayoutAnimationsProxy::shouldOverridePullTransaction() const {
317
401
  // we need to listen to every possible mutation to keep the light tree updated
@@ -327,7 +411,6 @@ void LayoutAnimationsProxy::updateLightTree(
327
411
  ReanimatedSystraceSection s("updateLightTree");
328
412
  auto &filteredMutations = transaction.filteredMutations;
329
413
  std::unordered_set<Tag> inserted, moved, deleted;
330
- std::unordered_map<Tag, IndexCursors> indexCursors;
331
414
  std::unordered_map<Tag, ShadowView> updatedViews;
332
415
  std::unordered_map<Tag, std::vector<AncestorOrigin>> oldChains;
333
416
  std::unordered_set<Tag> removedSubtreeRoots;
@@ -403,7 +486,9 @@ void LayoutAnimationsProxy::updateLightTree(
403
486
  }
404
487
  lightNodes_[mutation.newChildShadowView.tag] = node;
405
488
  staleSynchronousProps_.forget(mutation.newChildShadowView.tag);
406
- filteredMutations.push_back(mutation);
489
+ if (!transaction.recreatedTags.contains(mutation.newChildShadowView.tag)) {
490
+ filteredMutations.push_back(mutation);
491
+ }
407
492
  break;
408
493
  }
409
494
  case ShadowViewMutation::Delete: {
@@ -427,9 +512,12 @@ void LayoutAnimationsProxy::updateLightTree(
427
512
  case ShadowViewMutation::Insert: {
428
513
  transferConfigFromNativeID(mutation.newChildShadowView.props->nativeId, mutation.newChildShadowView.tag);
429
514
  auto &node = lightNodes_[mutation.newChildShadowView.tag];
515
+ react_native_assert(node->state == LIVE && "React inserts a view whose removal is withheld");
430
516
  auto &parent = lightNodes_[mutation.parentTag];
431
- const auto hostIndex = parent->toHostIndexForInsert(mutation.index, indexCursors[mutation.parentTag]);
517
+ auto &cursors = transaction.indexCursors[mutation.parentTag];
518
+ const auto hostIndex = parent->toHostIndexForInsert(mutation.index, cursors);
432
519
  parent->children.insert(parent->children.begin() + hostIndex, node);
520
+ cursors.remove = {};
433
521
  node->parent = parent;
434
522
  const auto tag = mutation.newChildShadowView.tag;
435
523
  bool hasSharedTransition = false;
@@ -490,7 +578,9 @@ void LayoutAnimationsProxy::updateLightTree(
490
578
  const auto parentTag = mutation.parentTag;
491
579
  const auto &parent = lightNodes_[parentTag];
492
580
  react_native_assert(parent && "Remove mutation from an unknown parent");
493
- const auto hostIndex = parent->toHostIndexForRemove(mutation.index, indexCursors[parentTag]);
581
+ auto &cursors = transaction.indexCursors[parentTag];
582
+ const auto hostIndex = parent->toHostIndexForRemove(mutation.index, cursors);
583
+ cursors.insert = {};
494
584
  react_native_assert(
495
585
  hostIndex < static_cast<int>(parent->children.size()) &&
496
586
  parent->children[hostIndex]->current.tag == mutation.oldChildShadowView.tag &&
@@ -750,6 +840,7 @@ void LayoutAnimationsProxy::handleSubtreeRemoval(
750
840
  react_native_assert(node->state == DECISION_PENDING && "A subtree that does not animate stays DECISION_PENDING");
751
841
  const auto hostIndex = parent->removeChild(node);
752
842
  react_native_assert(hostIndex != -1 && "Removed node not found in its parent");
843
+ transaction.indexCursors.erase(parent->current.tag);
753
844
  node->setExitingState(TORN_DOWN);
754
845
  unmapLightNode(node);
755
846
  cancelLayoutAnimation(node->current.tag);
@@ -776,41 +867,47 @@ bool LayoutAnimationsProxy::holdsSnapshottedScreen([[maybe_unused]] const std::s
776
867
  return false;
777
868
  }
778
869
 
779
- void LayoutAnimationsProxy::flushCompletedRemovals(ShadowViewMutationList &filteredMutations) const {
780
- ReanimatedSystraceSection s("flushCompletedRemovals");
781
-
782
- std::vector<Tag> completedRemovalTags;
783
- completedRemovalTags.reserve(completedAnimations_.size());
870
+ // A UI-thread pull on Android must not update the views that the next flushing pull removes, because
871
+ // Android can mount that JS-thread pull first.
872
+ void LayoutAnimationsProxy::collectDueRemovals(TransactionMeta &transaction) const {
873
+ std::vector<std::shared_ptr<LightNode>> finishedExits;
784
874
  for (const auto &[tag, completedAnimation] : completedAnimations_) {
785
- if (hasPendingLayoutAnimation(tag) || !completedAnimation.shouldRemove) {
875
+ const auto nodeIt = lightNodes_.find(tag);
876
+ if (!completedAnimation.shouldRemove || hasPendingLayoutAnimation(tag) || nodeIt == lightNodes_.end() ||
877
+ nodeIt->second->state != COMPLETED) {
786
878
  continue;
787
879
  }
788
- completedRemovalTags.push_back(tag);
880
+ finishedExits.push_back(nodeIt->second);
789
881
  }
882
+ transaction.dueRemovals = collectRemovals(finishedExits);
883
+ }
790
884
 
791
- for (const auto tag : completedRemovalTags) {
792
- const auto completedAnimationIt = completedAnimations_.find(tag);
793
- if (completedAnimationIt == completedAnimations_.end() || hasPendingLayoutAnimation(tag) ||
794
- !completedAnimationIt->second.shouldRemove) {
795
- continue;
796
- }
797
- const auto nodeIt = lightNodes_.find(tag);
798
- if (nodeIt == lightNodes_.end() || nodeIt->second->state != COMPLETED) {
799
- continue;
800
- }
801
- const auto node = nodeIt->second;
802
- auto parent = node->parent.lock();
885
+ void LayoutAnimationsProxy::tearDown(const std::unordered_set<Tag> &removals, ShadowViewMutationList &mutations) const {
886
+ ReanimatedSystraceSection s("tearDown");
887
+ std::vector<std::shared_ptr<LightNode>> roots;
888
+ for (const auto tag : removals) {
889
+ const auto &node = lightNodes_.at(tag);
890
+ const auto parent = node->parent.lock();
803
891
  react_native_assert(parent && "Parent node is nullptr");
804
- auto index = parent->removeChild(node);
805
- react_native_assert(index != -1 && "Completed node not found");
806
-
807
- endAnimationsRecursively(node, index, filteredMutations);
808
- maybeDropAncestors(parent, filteredMutations);
892
+ if (!removals.contains(parent->current.tag)) {
893
+ roots.push_back(node);
894
+ }
895
+ }
896
+ [[maybe_unused]] const auto mutationCount = mutations.size();
897
+ for (const auto &root : roots) {
898
+ const auto index = root->parent.lock()->removeChild(root);
899
+ react_native_assert(index != -1 && "Removed node not found in its parent");
900
+ endAnimationsRecursively(root, index, mutations);
809
901
  }
902
+ react_native_assert(
903
+ mutations.size() - mutationCount == 2 * removals.size() &&
904
+ std::ranges::none_of(removals, [this](const Tag tag) { return lightNodes_.contains(tag); }) &&
905
+ "The teardown must remove exactly the removals");
810
906
  }
811
907
 
812
- void LayoutAnimationsProxy::addOngoingAnimations(ShadowViewMutationList &mutations) const {
908
+ void LayoutAnimationsProxy::addOngoingAnimations(TransactionMeta &transaction) const {
813
909
  ReanimatedSystraceSection s1("addOngoingAnimations");
910
+ auto &mutations = transaction.filteredMutations;
814
911
  #ifdef ANDROID
815
912
  std::optional<std::unique_ptr<int[]>> maybeCorrectedTags;
816
913
 
@@ -844,6 +941,9 @@ void LayoutAnimationsProxy::addOngoingAnimations(ShadowViewMutationList &mutatio
844
941
  // https://github.com/software-mansion/react-native-reanimated/issues/7493
845
942
  continue;
846
943
  }
944
+ if (transaction.dueRemovals.contains(tag) || unqueuedBatchTags_.contains(tag)) {
945
+ continue;
946
+ }
847
947
  #endif
848
948
 
849
949
  auto layoutAnimationIt = layoutAnimations_.find(tag);
@@ -864,11 +964,16 @@ void LayoutAnimationsProxy::addOngoingAnimations(ShadowViewMutationList &mutatio
864
964
  mutations.push_back(
865
965
  ShadowViewMutation::UpdateMutation(layoutAnimation.currentView, newView, layoutAnimation.parentTag));
866
966
  layoutAnimation.currentView = newView;
867
- if (layoutAnimation.opacity && getViewProps(newView).opacity == *layoutAnimation.opacity) {
967
+ if (layoutAnimation.opacity &&
968
+ (updateValues.animatesOpacity || getViewProps(newView).opacity == *layoutAnimation.opacity)) {
868
969
  layoutAnimation.opacity.reset();
869
970
  }
870
971
  }
972
+ #ifdef ANDROID
973
+ std::erase_if(updateMap_, [this](const auto &update) { return !unqueuedBatchTags_.contains(update.first); });
974
+ #else
871
975
  updateMap_.clear();
976
+ #endif
872
977
  }
873
978
 
874
979
  void LayoutAnimationsProxy::endAnimationsRecursively(
@@ -900,34 +1005,12 @@ void LayoutAnimationsProxy::endAnimationsRecursively(
900
1005
  mutations.push_back(ShadowViewMutation::DeleteMutation(node->current));
901
1006
  }
902
1007
 
903
- void LayoutAnimationsProxy::maybeDropAncestors(
904
- const std::shared_ptr<LightNode> &node,
905
- ShadowViewMutationList &cleanupMutations) const {
906
- if (node->children.size() != 0 || node->state == ANIMATING || node->state == LIVE) {
907
- return;
908
- }
909
-
910
- auto parent = node->parent.lock();
911
- react_native_assert(parent && "Parent node is nullptr");
912
- auto index = parent->removeChild(node);
913
- react_native_assert(index != -1 && "Child node not found");
914
-
915
- node->setExitingState(TORN_DOWN);
916
- unmapLightNode(node);
917
- cancelLayoutAnimation(node->current.tag);
918
- if constexpr (StaticFeatureFlags::getFlag("ENABLE_SHARED_ELEMENT_TRANSITIONS")) {
919
- hiddenViewTags_.erase(node->current.tag);
920
- }
921
- cleanupMutations.push_back(ShadowViewMutation::RemoveMutation(parent->current.tag, node->current, index));
922
- cleanupMutations.push_back(ShadowViewMutation::DeleteMutation(node->current));
923
- maybeDropAncestors(parent, cleanupMutations);
924
- }
925
-
926
1008
  bool LayoutAnimationsProxy::startAnimationsRecursively(
927
1009
  const std::shared_ptr<LightNode> &node,
928
1010
  TransactionMeta &transaction,
929
1011
  StartAnimationsRecursivelyConfig config) const {
930
1012
  react_native_assert(node->state == DECISION_PENDING && "Subtree removal of a view that React does not delete");
1013
+ transaction.indexCursors.erase(node->current.tag);
931
1014
  auto &[shouldRemoveSubviewsWithoutAnimations, shouldAnimate, isScreenPop, defersTeardown] = config;
932
1015
  auto &mutations = defersTeardown ? transaction.teardownMutations : transaction.filteredMutations;
933
1016
  if (isRNSScreenOrStack(node)) {
@@ -1011,6 +1094,7 @@ void LayoutAnimationsProxy::clearSurfaceState() const {
1011
1094
  }
1012
1095
  #ifdef ANDROID
1013
1096
  cleanupPullScheduled_ = false;
1097
+ unqueuedBatchTags_.clear();
1014
1098
  #endif
1015
1099
  }
1016
1100
 
@@ -1056,7 +1140,15 @@ void LayoutAnimationsProxy::cleanupAnimations(
1056
1140
  const PropsParserContext &propsParserContext,
1057
1141
  const bool flushStructuralMutations) const {
1058
1142
  ReanimatedSystraceSection s("cleanupAnimations");
1059
- std::unordered_set<Tag> preservedContainerTags;
1143
+ std::unordered_set<Tag> preservedTags;
1144
+ #ifdef ANDROID
1145
+ if (!flushStructuralMutations) {
1146
+ preservedTags = transaction.dueRemovals;
1147
+ }
1148
+ for (const auto &[tag, _] : unqueuedBatchTags_) {
1149
+ preservedTags.insert(tag);
1150
+ }
1151
+ #endif
1060
1152
  if constexpr (StaticFeatureFlags::getFlag("ENABLE_SHARED_ELEMENT_TRANSITIONS")) {
1061
1153
  for (const auto &[tag, _] : completedAnimations_) {
1062
1154
  if (hasPendingLayoutAnimation(tag)) {
@@ -1067,7 +1159,7 @@ void LayoutAnimationsProxy::cleanupAnimations(
1067
1159
  continue;
1068
1160
  }
1069
1161
  if (!flushStructuralMutations) {
1070
- preservedContainerTags.insert(tag);
1162
+ preservedTags.insert(tag);
1071
1163
  continue;
1072
1164
  }
1073
1165
  if (containerIt->second.restoreAfterNode) {
@@ -1078,7 +1170,7 @@ void LayoutAnimationsProxy::cleanupAnimations(
1078
1170
 
1079
1171
  cleanupSharedTransitions(transaction, propsParserContext);
1080
1172
  }
1081
- cleanupCompletedAnimations(transaction.filteredMutations, propsParserContext, true, preservedContainerTags);
1173
+ cleanupCompletedAnimations(transaction.filteredMutations, propsParserContext, true, preservedTags);
1082
1174
  }
1083
1175
 
1084
1176
  #ifdef ANDROID
@@ -1097,6 +1189,54 @@ void LayoutAnimationsProxy::maybeScheduleCleanupPull(const bool flushedStructura
1097
1189
  scheduleCleanupPull();
1098
1190
  }
1099
1191
  }
1192
+
1193
+ // The push model queues the batch after the pull returns, in the same JS task. A frame that the UI thread pulls
1194
+ // in between mounts before the batch, so the frames of its views wait for the next JS task.
1195
+ // https://github.com/facebook/react-native/blob/v0.88.0-rc.3/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.cpp#L315-L346
1196
+ void LayoutAnimationsProxy::holdFramesUntilBatchIsQueued(const ShadowViewMutationList &mutations) const {
1197
+ if (mutations.empty() || isMountingCoordinatorPullModelEnabled() || worklets::isOnUIThread(uiScheduler_)) {
1198
+ return;
1199
+ }
1200
+ std::vector<Tag> tags;
1201
+ for (const auto &mutation : mutations) {
1202
+ const auto isRemoval = mutation.type == ShadowViewMutation::Remove || mutation.type == ShadowViewMutation::Delete;
1203
+ const auto tag = (isRemoval ? mutation.oldChildShadowView : mutation.newChildShadowView).tag;
1204
+ const auto hasRecord =
1205
+ layoutAnimations_.contains(tag) || completedAnimations_.contains(tag) || hasPendingLayoutAnimation(tag);
1206
+ if (hasRecord) {
1207
+ tags.push_back(tag);
1208
+ unqueuedBatchTags_[tag]++;
1209
+ }
1210
+ }
1211
+ if (tags.empty()) {
1212
+ return;
1213
+ }
1214
+ jsInvoker_->invokeAsync([this, weakThis = weak_from_this(), tags = std::move(tags)](jsi::Runtime &) {
1215
+ if (const auto strongThis = weakThis.lock()) {
1216
+ releaseFramesOfQueuedBatch(tags);
1217
+ }
1218
+ });
1219
+ }
1220
+
1221
+ void LayoutAnimationsProxy::releaseFramesOfQueuedBatch(const std::vector<Tag> &tags) const {
1222
+ const auto lock = std::unique_lock<std::recursive_mutex>(mutex);
1223
+ bool hasHeldFrame = false;
1224
+ for (const auto tag : tags) {
1225
+ const auto it = unqueuedBatchTags_.find(tag);
1226
+ if (it == unqueuedBatchTags_.end()) {
1227
+ continue;
1228
+ }
1229
+ if (--it->second == 0) {
1230
+ unqueuedBatchTags_.erase(it);
1231
+ }
1232
+ const auto completedIt = completedAnimations_.find(tag);
1233
+ const auto waitsForCleanup = completedIt != completedAnimations_.end() && !completedIt->second.shouldRemove;
1234
+ hasHeldFrame = hasHeldFrame || updateMap_.contains(tag) || waitsForCleanup;
1235
+ }
1236
+ if (hasHeldFrame) {
1237
+ schedulePullOnNextFrame();
1238
+ }
1239
+ }
1100
1240
  #endif
1101
1241
 
1102
1242
  // MARK: Start Animation
@@ -1109,10 +1249,11 @@ void LayoutAnimationsProxy::startEnteringAnimation(
1109
1249
  const auto opacity = getViewProps(newChildShadowView).opacity;
1110
1250
  const auto &parent = node->parent.lock();
1111
1251
  react_native_assert(parent && "Parent node is nullptr");
1252
+ const PropsParserContext propsParserContext{surfaceId_, *contextContainer_};
1112
1253
  enqueueLayoutAnimation(ManagedLayoutAnimationStart{
1113
1254
  .tag = newChildShadowView.tag,
1114
1255
  .type = LayoutAnimationType::ENTERING,
1115
- .before = newChildShadowView,
1256
+ .before = cloneViewWithoutOpacity(newChildShadowView, propsParserContext),
1116
1257
  .after = newChildShadowView,
1117
1258
  .parentTag = parent->current.tag,
1118
1259
  .opacity = opacity,
@@ -88,6 +88,10 @@ struct TransactionMeta {
88
88
  std::vector<std::shared_ptr<LightNode>> nodesToRestore;
89
89
  std::vector<std::shared_ptr<LightNode>> containersToRemove;
90
90
  std::unordered_map<Tag, Tag> staleSnapshots;
91
+ std::unordered_set<Tag> dueRemovals;
92
+ std::unordered_set<Tag> recreatedTags;
93
+ // Whatever changes a node's children or their exiting states during updateLightTree must reset its cursors.
94
+ std::unordered_map<Tag, IndexCursors> indexCursors;
91
95
  };
92
96
 
93
97
  struct LayoutAnimationsProxy : public LayoutAnimationsProxyCommon {
@@ -105,6 +109,8 @@ struct LayoutAnimationsProxy : public LayoutAnimationsProxyCommon {
105
109
  mutable bool surfaceToRemove_ = false;
106
110
  #ifdef ANDROID
107
111
  mutable bool cleanupPullScheduled_ = false;
112
+ // tags of JS-thread batches that are not in the mount queue yet
113
+ mutable std::unordered_map<Tag, int> unqueuedBatchTags_;
108
114
  #endif
109
115
 
110
116
  #ifdef __APPLE__
@@ -147,6 +153,7 @@ struct LayoutAnimationsProxy : public LayoutAnimationsProxyCommon {
147
153
 
148
154
  std::optional<ShadowView>
149
155
  reparentLayoutAnimation(Tag tag, Tag parentTag, const ShadowView &newView, react::Point offset) const;
156
+ const ShadowView &mountedView(const std::shared_ptr<LightNode> &node) const;
150
157
 
151
158
  void applyInitialMutationsToLightTree(const ShadowViewMutationList &mutations) const;
152
159
  void updateLightNodeProps(
@@ -161,8 +168,16 @@ struct LayoutAnimationsProxy : public LayoutAnimationsProxyCommon {
161
168
 
162
169
  void applySynchronousProps(const UpdatesBatch &updatesBatch, bool trackInLightTree) const override;
163
170
 
164
- void reconcileContradictedRemovals(const ShadowViewMutationList &mutations, ShadowViewMutationList &filteredMutations)
165
- const;
171
+ void reconcileContradictedRemovals(
172
+ ShadowViewMutationList &mutations,
173
+ TransactionMeta &transaction,
174
+ const PropsParserContext &propsParserContext) const;
175
+ #ifdef ANDROID
176
+ ShadowView resetPropsMissingFrom(
177
+ const ShadowView &view,
178
+ const ShadowView &mounted,
179
+ const PropsParserContext &propsParserContext) const;
180
+ #endif
166
181
 
167
182
  void handleSharedTransitionsStart(
168
183
  const std::shared_ptr<LightNode> &afterTopScreen,
@@ -180,6 +195,8 @@ struct LayoutAnimationsProxy : public LayoutAnimationsProxyCommon {
180
195
  #ifdef ANDROID
181
196
  bool hasPendingStructuralCleanup() const;
182
197
  void maybeScheduleCleanupPull(bool flushedStructuralMutations) const;
198
+ void holdFramesUntilBatchIsQueued(const ShadowViewMutationList &mutations) const;
199
+ void releaseFramesOfQueuedBatch(const std::vector<Tag> &tags) const;
183
200
  #endif
184
201
 
185
202
  void hideTransitioningViews(
@@ -250,9 +267,10 @@ struct LayoutAnimationsProxy : public LayoutAnimationsProxyCommon {
250
267
  const std::shared_ptr<LightNode> &parent,
251
268
  TransactionMeta &transaction) const;
252
269
  bool holdsSnapshottedScreen(const std::shared_ptr<LightNode> &node) const;
253
- void flushCompletedRemovals(ShadowViewMutationList &filteredMutations) const;
270
+ void collectDueRemovals(TransactionMeta &transaction) const;
271
+ void tearDown(const std::unordered_set<Tag> &removals, ShadowViewMutationList &mutations) const;
254
272
 
255
- void addOngoingAnimations(ShadowViewMutationList &mutations) const;
273
+ void addOngoingAnimations(TransactionMeta &transaction) const;
256
274
  ShadowView cloneViewWithoutOpacity(const ShadowView &shadowView, const PropsParserContext &propsParserContext) const;
257
275
 
258
276
  bool startAnimationsRecursively(
@@ -261,7 +279,6 @@ struct LayoutAnimationsProxy : public LayoutAnimationsProxyCommon {
261
279
  StartAnimationsRecursivelyConfig config) const;
262
280
  void endAnimationsRecursively(const std::shared_ptr<LightNode> &node, int index, ShadowViewMutationList &mutations)
263
281
  const;
264
- void maybeDropAncestors(const std::shared_ptr<LightNode> &node, ShadowViewMutationList &cleanupMutations) const;
265
282
 
266
283
  // MountingOverrideDelegate
267
284
 
@@ -80,12 +80,10 @@ std::optional<SurfaceId> LayoutAnimationsProxyCommon::progressLayoutAnimation(
80
80
  }
81
81
 
82
82
  auto &layoutAnimation = layoutAnimationIt->second;
83
- if (newStyle.hasProperty(uiRuntime_, "opacity")) {
84
- layoutAnimation.opacity.reset();
85
- }
83
+ const bool animatesOpacity = newStyle.hasProperty(uiRuntime_, "opacity");
86
84
 
87
85
  auto rawProps = std::make_shared<RawProps>(uiRuntime_, jsi::Value(uiRuntime_, newStyle));
88
- if (layoutAnimation.opacity) {
86
+ if (layoutAnimation.opacity && !animatesOpacity) {
89
87
  auto props = (folly::dynamic)*rawProps;
90
88
  props["opacity"] = *layoutAnimation.opacity;
91
89
  rawProps = std::make_shared<RawProps>(std::move(props));
@@ -97,7 +95,7 @@ std::optional<SurfaceId> LayoutAnimationsProxyCommon::progressLayoutAnimation(
97
95
  #endif
98
96
  auto newProps = componentDescriptorRegistry_->at(layoutAnimation.finalView.componentHandle)
99
97
  .cloneProps(propsParserContext, layoutAnimation.finalView.props, std::move(*rawProps));
100
- updateMap_.insert_or_assign(tag, UpdateValues{newProps, Frame(uiRuntime_, newStyle)});
98
+ updateMap_.insert_or_assign(tag, UpdateValues{newProps, Frame(uiRuntime_, newStyle), animatesOpacity});
101
99
 
102
100
  return surfaceId_;
103
101
  }
@@ -264,13 +262,12 @@ ShadowView LayoutAnimationsProxyCommon::materializeLayoutAnimation(
264
262
  const std::shared_ptr<Serializable> &config) const {
265
263
  auto currentView = before;
266
264
  const auto activeAnimationIt = layoutAnimations_.find(tag);
267
- if (type == LayoutAnimationType::ENTERING) {
268
- currentView = after;
269
- } else if (activeAnimationIt != layoutAnimations_.end()) {
270
- currentView = activeAnimationIt->second.currentView;
271
- } else if (const auto completedAnimationIt = completedAnimations_.find(tag);
272
- completedAnimationIt != completedAnimations_.end()) {
273
- if (!completedAnimationIt->second.shouldRemove) {
265
+ // An entering view starts from the view that the Insert mounted.
266
+ if (type != LayoutAnimationType::ENTERING) {
267
+ if (activeAnimationIt != layoutAnimations_.end()) {
268
+ currentView = activeAnimationIt->second.currentView;
269
+ } else if (const auto completedAnimationIt = completedAnimations_.find(tag);
270
+ completedAnimationIt != completedAnimations_.end() && !completedAnimationIt->second.shouldRemove) {
274
271
  currentView = completedAnimationIt->second.animation.currentView;
275
272
  }
276
273
  }
@@ -364,11 +364,10 @@ void LayoutAnimationsProxy_Legacy::handleUpdatesAndEnterings(
364
364
  continue;
365
365
  }
366
366
 
367
- startEnteringAnimation(tag, mutation, enteringConfig);
368
- filteredMutations.push_back(mutation);
369
-
370
367
  // temporarily set opacity to 0 to prevent flickering on android
371
368
  std::shared_ptr<ShadowView> newView = cloneViewWithoutOpacity(mutation, propsParserContext);
369
+ startEnteringAnimation(tag, mutation, *newView, enteringConfig);
370
+ filteredMutations.push_back(mutation);
372
371
 
373
372
  filteredMutations.push_back(
374
373
  ShadowViewMutation::UpdateMutation(mutation.newChildShadowView, *newView, parentTag));
@@ -471,7 +470,8 @@ void LayoutAnimationsProxy_Legacy::addOngoingAnimations(ShadowViewMutationList &
471
470
  mutations.push_back(
472
471
  ShadowViewMutation::UpdateMutation(layoutAnimation.currentView, newView, layoutAnimation.parentTag));
473
472
  layoutAnimation.currentView = newView;
474
- if (layoutAnimation.opacity && getViewProps(newView).opacity == *layoutAnimation.opacity) {
473
+ if (layoutAnimation.opacity &&
474
+ (updateValues.animatesOpacity || getViewProps(newView).opacity == *layoutAnimation.opacity)) {
475
475
  layoutAnimation.opacity.reset();
476
476
  }
477
477
  }
@@ -650,6 +650,7 @@ bool LayoutAnimationsProxy_Legacy::shouldOverridePullTransaction() const {
650
650
  void LayoutAnimationsProxy_Legacy::startEnteringAnimation(
651
651
  const int tag,
652
652
  ShadowViewMutation &mutation,
653
+ const ShadowView &hiddenView,
653
654
  const std::shared_ptr<Serializable> &config) const {
654
655
  #ifdef LAYOUT_ANIMATIONS_LOGS
655
656
  LOG(INFO) << "start entering animation for tag " << tag << std::endl;
@@ -657,7 +658,7 @@ void LayoutAnimationsProxy_Legacy::startEnteringAnimation(
657
658
  enqueueLayoutAnimation(ManagedLayoutAnimationStart{
658
659
  .tag = tag,
659
660
  .type = LayoutAnimationType::ENTERING,
660
- .before = mutation.newChildShadowView,
661
+ .before = hiddenView,
661
662
  .after = mutation.newChildShadowView,
662
663
  .parentTag = mutation.parentTag,
663
664
  .opacity = getViewProps(mutation.newChildShadowView).opacity,