react-native-reanimated 4.7.0 → 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 (85) 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/ReanimatedCommitHook.cpp +34 -2
  4. package/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.h +15 -1
  5. package/Common/cpp/reanimated/Fabric/ReanimatedMountHook.cpp +9 -0
  6. package/Common/cpp/reanimated/Fabric/ReanimatedMountHook.h +3 -0
  7. package/Common/cpp/reanimated/Fabric/updates/AnimatedPropsRegistry.cpp +135 -33
  8. package/Common/cpp/reanimated/Fabric/updates/AnimatedPropsRegistry.h +37 -3
  9. package/Common/cpp/reanimated/Fabric/updates/SynchronousWritesTracker.cpp +103 -0
  10. package/Common/cpp/reanimated/Fabric/updates/SynchronousWritesTracker.h +74 -0
  11. package/Common/cpp/reanimated/Fabric/updates/UpdatesRegistry.cpp +43 -0
  12. package/Common/cpp/reanimated/Fabric/updates/UpdatesRegistry.h +13 -0
  13. package/Common/cpp/reanimated/Fabric/updates/UpdatesRegistryManager.cpp +14 -1
  14. package/Common/cpp/reanimated/Fabric/updates/UpdatesRegistryManager.h +3 -0
  15. package/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy.cpp +318 -134
  16. package/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy.h +27 -6
  17. package/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxyCommon.cpp +32 -17
  18. package/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxyCommon.h +17 -1
  19. package/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.cpp +14 -13
  20. package/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.h +5 -2
  21. package/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsUtils.h +57 -9
  22. package/Common/cpp/reanimated/LayoutAnimations/PropsDiffer.h +2 -2
  23. package/Common/cpp/reanimated/LayoutAnimations/SharedTransitions.cpp +11 -11
  24. package/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp +114 -11
  25. package/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h +12 -2
  26. package/Common/cpp/reanimated/Tools/PlatformDepMethodsHolder.h +17 -1
  27. package/Common/cpp/reanimated/Tools/TextMetrics.h +22 -0
  28. package/android/src/main/cpp/reanimated/android/NativeProxy.cpp +32 -3
  29. package/android/src/main/cpp/reanimated/android/NativeProxy.h +3 -0
  30. package/android/src/main/java/com/swmansion/reanimated/NativeProxy.kt +108 -9
  31. package/android/src/main/java/com/swmansion/reanimated/NodesManager.kt +2 -4
  32. package/android/src/main/java/com/swmansion/reanimated/nativeProxy/EventHandler.kt +4 -3
  33. package/android/src/main/java/com/swmansion/reanimated/sensor/ReanimatedSensor.kt +1 -1
  34. package/android/src/main/java/com/swmansion/reanimated/sensor/ReanimatedSensorListener.kt +44 -33
  35. package/apple/reanimated/apple/REAReducedMotion.h +1 -3
  36. package/apple/reanimated/apple/REASynchronousPropsRewriter.h +18 -0
  37. package/apple/reanimated/apple/REASynchronousPropsRewriter.mm +37 -0
  38. package/apple/reanimated/apple/ReanimatedModule.mm +7 -0
  39. package/apple/reanimated/apple/keyboardObserver/REAKeyboardEventObserver.mm +20 -3
  40. package/apple/reanimated/apple/native/PlatformDepMethodsHolderImpl.mm +52 -1
  41. package/jest/resolver.js +4 -1
  42. package/lib/module/PropsRegistryGarbageCollector.js +43 -25
  43. package/lib/module/PropsRegistryGarbageCollector.js.map +1 -1
  44. package/lib/module/ReanimatedModule/NativeReanimated.js +2 -2
  45. package/lib/module/ReanimatedModule/NativeReanimated.js.map +1 -1
  46. package/lib/module/animation/timing.js +15 -2
  47. package/lib/module/animation/timing.js.map +1 -1
  48. package/lib/module/commonTypes.js.map +1 -1
  49. package/lib/module/createAnimatedComponent/AnimatedComponent.js +1 -1
  50. package/lib/module/createAnimatedComponent/AnimatedComponent.js.map +1 -1
  51. package/lib/module/createAnimatedComponent/AnimatedComponent.native.js +10 -34
  52. package/lib/module/createAnimatedComponent/AnimatedComponent.native.js.map +1 -1
  53. package/lib/module/platform-specific/jsVersion.js +1 -1
  54. package/lib/module/platformFunctions/setNativeProps.native.js +2 -1
  55. package/lib/module/platformFunctions/setNativeProps.native.js.map +1 -1
  56. package/lib/module/updateProps/updateProps.native.js +5 -4
  57. package/lib/module/updateProps/updateProps.native.js.map +1 -1
  58. package/lib/typescript/PropsRegistryGarbageCollector.d.ts +2 -2
  59. package/lib/typescript/PropsRegistryGarbageCollector.d.ts.map +1 -1
  60. package/lib/typescript/ReanimatedModule/reanimatedModuleProxy.d.ts +1 -1
  61. package/lib/typescript/ReanimatedModule/reanimatedModuleProxy.d.ts.map +1 -1
  62. package/lib/typescript/animation/timing.d.ts.map +1 -1
  63. package/lib/typescript/commonTypes.d.ts +3 -1
  64. package/lib/typescript/commonTypes.d.ts.map +1 -1
  65. package/lib/typescript/createAnimatedComponent/AnimatedComponent.d.ts +1 -1
  66. package/lib/typescript/createAnimatedComponent/AnimatedComponent.d.ts.map +1 -1
  67. package/lib/typescript/createAnimatedComponent/AnimatedComponent.native.d.ts +1 -1
  68. package/lib/typescript/createAnimatedComponent/AnimatedComponent.native.d.ts.map +1 -1
  69. package/lib/typescript/createAnimatedComponent/commonTypes.d.ts +1 -1
  70. package/lib/typescript/createAnimatedComponent/commonTypes.d.ts.map +1 -1
  71. package/lib/typescript/platform-specific/jsVersion.d.ts +1 -1
  72. package/lib/typescript/updateProps/updateProps.native.d.ts.map +1 -1
  73. package/package.json +9 -7
  74. package/src/PropsRegistryGarbageCollector.ts +46 -25
  75. package/src/ReanimatedModule/NativeReanimated.ts +2 -2
  76. package/src/ReanimatedModule/reanimatedModuleProxy.ts +1 -1
  77. package/src/animation/timing.ts +20 -2
  78. package/src/commonTypes.ts +3 -1
  79. package/src/createAnimatedComponent/AnimatedComponent.native.tsx +5 -30
  80. package/src/createAnimatedComponent/AnimatedComponent.tsx +1 -1
  81. package/src/createAnimatedComponent/commonTypes.ts +1 -1
  82. package/src/platform-specific/jsVersion.ts +1 -1
  83. package/src/platformFunctions/setNativeProps.native.ts +3 -1
  84. package/src/privateGlobals.d.ts +1 -0
  85. package/src/updateProps/updateProps.native.ts +10 -3
@@ -44,15 +44,7 @@ std::string CSSNumberBase<TDerived, TValue>::toString() const {
44
44
 
45
45
  template <typename TDerived, typename TValue>
46
46
  TDerived CSSNumberBase<TDerived, TValue>::interpolate(double progress, const TDerived &other) const {
47
- const auto interpolated = value + progress * (other.value - value);
48
-
49
- if constexpr (requires { TDerived::epsilon; }) {
50
- if (std::abs(other.value - interpolated) < TDerived::epsilon) {
51
- return other;
52
- }
53
- }
54
-
55
- return TDerived(interpolated);
47
+ return TDerived(value + progress * (other.value - value));
56
48
  }
57
49
 
58
50
  template <typename TDerived, typename TValue>
@@ -60,6 +52,10 @@ bool CSSNumberBase<TDerived, TValue>::operator==(const CSSNumberBase<TDerived, T
60
52
  return value == other.value;
61
53
  }
62
54
 
55
+ CSSTextDouble CSSTextDouble::interpolate(double progress, const CSSTextDouble &other) const {
56
+ return CSSTextDouble(roundTextMetric(value + progress * (other.value - value)));
57
+ }
58
+
63
59
  CSSInteger CSSInteger::interpolate(double progress, const CSSInteger &other) const {
64
60
  return CSSInteger(static_cast<int>(std::round(value + progress * (other.value - value))));
65
61
  }
@@ -2,7 +2,7 @@
2
2
 
3
3
  #include <reanimated/CSS/common/values/CSSValue.h>
4
4
 
5
- #include <react/utils/FloatComparison.h>
5
+ #include <reanimated/Tools/TextMetrics.h>
6
6
 
7
7
  #include <string>
8
8
 
@@ -44,11 +44,7 @@ struct CSSDouble : public CSSNumberBase<CSSDouble, double> {
44
44
  struct CSSTextDouble : public CSSNumberBase<CSSTextDouble, double> {
45
45
  using CSSNumberBase::CSSNumberBase;
46
46
 
47
- /// React Native compares text attributes with this epsilon but lays them out
48
- /// exactly, so a smaller step moves the frame while the text keeps its old
49
- /// metrics. Interpolation lands on the endpoint once it gets that close.
50
- /// https://github.com/facebook/react-native/blob/v0.87.0/packages/react-native/ReactCommon/react/renderer/attributedstring/TextAttributes.cpp#L174-L177
51
- static constexpr double epsilon = facebook::react::kDefaultEpsilon;
47
+ CSSTextDouble interpolate(double progress, const CSSTextDouble &other) const override;
52
48
  };
53
49
 
54
50
  struct CSSInteger : public CSSNumberBase<CSSInteger, int> {
@@ -16,11 +16,13 @@ ReanimatedCommitHook::ReanimatedCommitHook(
16
16
  const std::shared_ptr<UIManager> &uiManager,
17
17
  const std::shared_ptr<UpdatesRegistryManager> &updatesRegistryManager,
18
18
  const std::shared_ptr<css::ViewStylesRepository> &viewStylesRepository,
19
- const std::shared_ptr<LayoutAnimationsProxyRegistry> &layoutAnimationsProxyRegistry)
19
+ const std::shared_ptr<LayoutAnimationsProxyRegistry> &layoutAnimationsProxyRegistry,
20
+ const std::shared_ptr<SynchronousWritesTracker> &synchronousWritesTracker)
20
21
  : uiManager_(uiManager),
21
22
  updatesRegistryManager_(updatesRegistryManager),
22
23
  viewStylesRepository_(viewStylesRepository),
23
- layoutAnimationsProxyRegistry_(layoutAnimationsProxyRegistry) {
24
+ layoutAnimationsProxyRegistry_(layoutAnimationsProxyRegistry),
25
+ synchronousWritesTracker_(synchronousWritesTracker) {
24
26
  uiManager_->registerCommitHook(*this);
25
27
  uiManager_->getShadowTreeRegistry().enumerate(
26
28
  [this](const ShadowTree &shadowTree, bool & /*stop*/) { registerLayoutAnimations(shadowTree); });
@@ -57,6 +59,9 @@ RootShadowNode::Unshared ReanimatedCommitHook::shadowTreeWillCommit(
57
59
  // A stopping surface commits an empty root; its mount is not reported on a paused Android host.
58
60
  auto lock = updatesRegistryManager_->lock();
59
61
  viewStylesRepository_->removeSurface(shadowTree.getSurfaceId());
62
+ if (synchronousWritesTracker_) {
63
+ synchronousWritesTracker_->onSurfaceStop(shadowTree.getSurfaceId());
64
+ }
60
65
  }
61
66
 
62
67
  auto reaShadowNode = std::reinterpret_pointer_cast<ReanimatedCommitShadowNode>(newRootShadowNode);
@@ -66,6 +71,7 @@ RootShadowNode::Unshared ReanimatedCommitHook::shadowTreeWillCommit(
66
71
  // the updates registry manager
67
72
  reaShadowNode->unsetReanimatedCommitTrait();
68
73
  reaShadowNode->setReanimatedMountTrait();
74
+ trackCommit(newRootShadowNode, false);
69
75
  return newRootShadowNode;
70
76
  }
71
77
 
@@ -74,6 +80,7 @@ RootShadowNode::Unshared ReanimatedCommitHook::shadowTreeWillCommit(
74
80
  // which means that all animation changes are already included.
75
81
  // Therefore, there's no need to reapply styles from the props map.
76
82
  if (commitOptions.source != ShadowTreeCommitSource::React) {
83
+ trackCommit(newRootShadowNode, false);
77
84
  return newRootShadowNode;
78
85
  }
79
86
  }
@@ -90,6 +97,8 @@ RootShadowNode::Unshared ReanimatedCommitHook::shadowTreeWillCommit(
90
97
  updatesRegistryManager_->cancelCommitAfterPause();
91
98
 
92
99
  rootNode = cloneShadowTreeWithNewProps(*rootNode, propsMap);
100
+ // Must share the registry lock with `collectProps`, or a write can get the epoch of a snapshot that lacks it.
101
+ trackCommit(rootNode, true);
93
102
  // If the commit comes from React Native then pause commits from
94
103
  // Reanimated since the ShadowTree to be committed by Reanimated may not
95
104
  // include the new changes from React Native yet and all changes of animated
@@ -104,4 +113,27 @@ RootShadowNode::Unshared ReanimatedCommitHook::shadowTreeWillCommit(
104
113
  return rootNode;
105
114
  }
106
115
 
116
+ #if REACT_NATIVE_VERSION_MINOR >= 88
117
+ void ReanimatedCommitHook::shadowTreeDidCommit(
118
+ const ShadowTree &,
119
+ const RootShadowNode::Shared &rootShadowNode,
120
+ const std::vector<const LayoutableShadowNode *> &) noexcept {
121
+ if (synchronousWritesTracker_) {
122
+ synchronousWritesTracker_->onDidCommit(rootShadowNode);
123
+ }
124
+ }
125
+ #endif
126
+
127
+ void ReanimatedCommitHook::trackCommit(const RootShadowNode::Shared &rootShadowNode, const bool carriesRegistryValues)
128
+ const {
129
+ if (!synchronousWritesTracker_) {
130
+ return;
131
+ }
132
+ synchronousWritesTracker_->onWillCommit(rootShadowNode, carriesRegistryValues);
133
+ #if REACT_NATIVE_VERSION_MINOR < 88
134
+ // Before 0.88 there is no `shadowTreeDidCommit`. The root that this hook returns is the only one available.
135
+ synchronousWritesTracker_->onDidCommit(rootShadowNode);
136
+ #endif
137
+ }
138
+
107
139
  } // namespace reanimated
@@ -1,12 +1,15 @@
1
1
  #pragma once
2
2
 
3
3
  #include <reanimated/CSS/misc/ViewStylesRepository.h>
4
+ #include <reanimated/Compat/ReactNativeVersionCompat.h>
5
+ #include <reanimated/Fabric/updates/SynchronousWritesTracker.h>
4
6
  #include <reanimated/Fabric/updates/UpdatesRegistryManager.h>
5
7
  #include <reanimated/LayoutAnimations/LayoutAnimationsProxyRegistry.h>
6
8
 
7
9
  #include <react/renderer/uimanager/UIManagerCommitHook.h>
8
10
 
9
11
  #include <memory>
12
+ #include <vector>
10
13
 
11
14
  using namespace facebook::react;
12
15
 
@@ -18,7 +21,8 @@ class ReanimatedCommitHook : public UIManagerCommitHook {
18
21
  const std::shared_ptr<UIManager> &uiManager,
19
22
  const std::shared_ptr<UpdatesRegistryManager> &updatesRegistryManager,
20
23
  const std::shared_ptr<css::ViewStylesRepository> &viewStylesRepository,
21
- const std::shared_ptr<LayoutAnimationsProxyRegistry> &layoutAnimationsProxyRegistry);
24
+ const std::shared_ptr<LayoutAnimationsProxyRegistry> &layoutAnimationsProxyRegistry,
25
+ const std::shared_ptr<SynchronousWritesTracker> &synchronousWritesTracker);
22
26
 
23
27
  ~ReanimatedCommitHook() noexcept override;
24
28
 
@@ -34,11 +38,21 @@ class ReanimatedCommitHook : public UIManagerCommitHook {
34
38
  RootShadowNode::Unshared const &newRootShadowNode,
35
39
  const ShadowTreeCommitOptions &commitOptions) noexcept override;
36
40
 
41
+ #if REACT_NATIVE_VERSION_MINOR >= 88
42
+ void shadowTreeDidCommit(
43
+ const ShadowTree &shadowTree,
44
+ const RootShadowNode::Shared &rootShadowNode,
45
+ const std::vector<const LayoutableShadowNode *> &affectedLayoutableNodes) noexcept override;
46
+ #endif
47
+
37
48
  private:
49
+ void trackCommit(const RootShadowNode::Shared &rootShadowNode, bool carriesRegistryValues) const;
50
+
38
51
  std::shared_ptr<UIManager> uiManager_;
39
52
  std::shared_ptr<UpdatesRegistryManager> updatesRegistryManager_;
40
53
  std::shared_ptr<css::ViewStylesRepository> viewStylesRepository_;
41
54
  std::shared_ptr<LayoutAnimationsProxyRegistry> layoutAnimationsProxyRegistry_;
55
+ std::shared_ptr<SynchronousWritesTracker> synchronousWritesTracker_;
42
56
  };
43
57
 
44
58
  } // namespace reanimated
@@ -12,11 +12,13 @@ ReanimatedMountHook::ReanimatedMountHook(
12
12
  const std::shared_ptr<UpdatesRegistryManager> &updatesRegistryManager,
13
13
  const std::shared_ptr<css::ViewStylesRepository> &viewStylesRepository,
14
14
  const std::shared_ptr<LayoutAnimationsProxyRegistry> &layoutAnimationsProxyRegistry,
15
+ const std::shared_ptr<SynchronousWritesTracker> &synchronousWritesTracker,
15
16
  const std::function<void()> &requestFlush)
16
17
  : uiManager_(uiManager),
17
18
  updatesRegistryManager_(updatesRegistryManager),
18
19
  viewStylesRepository_(viewStylesRepository),
19
20
  layoutAnimationsProxyRegistry_(layoutAnimationsProxyRegistry),
21
+ synchronousWritesTracker_(synchronousWritesTracker),
20
22
  requestFlush_(requestFlush) {
21
23
  uiManager_->registerMountHook(*this);
22
24
  }
@@ -35,6 +37,10 @@ void ReanimatedMountHook::shadowTreeDidMount(
35
37
  return;
36
38
  }
37
39
 
40
+ if (synchronousWritesTracker_) {
41
+ synchronousWritesTracker_->onMountReport(rootShadowNode);
42
+ }
43
+
38
44
  auto reaShadowNode = std::reinterpret_pointer_cast<ReanimatedCommitShadowNode>(
39
45
  std::const_pointer_cast<RootShadowNode>(rootShadowNode));
40
46
 
@@ -73,6 +79,9 @@ void ReanimatedMountHook::shadowTreeDidUnmount(SurfaceId surfaceId, HighResTimeS
73
79
  if (layoutAnimationsProxyRegistry_) {
74
80
  layoutAnimationsProxyRegistry_->remove(surfaceId);
75
81
  }
82
+ if (synchronousWritesTracker_) {
83
+ synchronousWritesTracker_->onSurfaceStop(surfaceId);
84
+ }
76
85
 
77
86
  auto lock = updatesRegistryManager_->lock();
78
87
  viewStylesRepository_->removeSurface(surfaceId);
@@ -2,6 +2,7 @@
2
2
 
3
3
  #include <reanimated/CSS/misc/ViewStylesRepository.h>
4
4
  #include <reanimated/Fabric/ShadowTreeCloner.h>
5
+ #include <reanimated/Fabric/updates/SynchronousWritesTracker.h>
5
6
  #include <reanimated/Fabric/updates/UpdatesRegistryManager.h>
6
7
  #include <reanimated/LayoutAnimations/LayoutAnimationsProxyRegistry.h>
7
8
 
@@ -20,6 +21,7 @@ class ReanimatedMountHook : public UIManagerMountHook {
20
21
  const std::shared_ptr<UpdatesRegistryManager> &updatesRegistryManager,
21
22
  const std::shared_ptr<css::ViewStylesRepository> &viewStylesRepository,
22
23
  const std::shared_ptr<LayoutAnimationsProxyRegistry> &layoutAnimationsProxyRegistry,
24
+ const std::shared_ptr<SynchronousWritesTracker> &synchronousWritesTracker,
23
25
  const std::function<void()> &requestFlush);
24
26
  ~ReanimatedMountHook() noexcept override;
25
27
 
@@ -32,6 +34,7 @@ class ReanimatedMountHook : public UIManagerMountHook {
32
34
  const std::shared_ptr<UpdatesRegistryManager> updatesRegistryManager_;
33
35
  const std::shared_ptr<css::ViewStylesRepository> viewStylesRepository_;
34
36
  const std::shared_ptr<LayoutAnimationsProxyRegistry> layoutAnimationsProxyRegistry_;
37
+ const std::shared_ptr<SynchronousWritesTracker> synchronousWritesTracker_;
35
38
  const std::function<void()> requestFlush_;
36
39
  };
37
40
 
@@ -1,11 +1,13 @@
1
1
  #include <reanimated/Fabric/updates/AnimatedPropsRegistry.h>
2
2
  #include <reanimated/Fabric/updates/UpdatesRegistryManager.h>
3
3
  #include <reanimated/Tools/FeatureFlags.h>
4
+ #include <reanimated/Tools/TextMetrics.h>
4
5
 
5
6
  #include <react/debug/react_native_assert.h>
6
7
 
7
- #include <functional>
8
+ #include <algorithm>
8
9
  #include <memory>
10
+ #include <string>
9
11
  #include <utility>
10
12
  #include <vector>
11
13
 
@@ -17,6 +19,16 @@ static inline std::shared_ptr<const ShadowNode> shadowNodeFromValue(
17
19
  return Bridging<std::shared_ptr<const ShadowNode>>::fromJs(rt, shadowNodeWrapper);
18
20
  }
19
21
 
22
+ static void roundTextMetrics(jsi::Runtime &rt, const jsi::Value &updates) {
23
+ const auto object = updates.asObject(rt);
24
+ for (const auto *propName : TEXT_METRIC_PROP_NAMES) {
25
+ const auto value = object.getProperty(rt, propName);
26
+ if (value.isNumber()) {
27
+ object.setProperty(rt, propName, roundTextMetric(value.asNumber()));
28
+ }
29
+ }
30
+ }
31
+
20
32
  void AnimatedPropsRegistry::update(jsi::Runtime &rt, const jsi::Value &operations, const double timestamp) {
21
33
  react_native_assert(UpdatesRegistryManager::isLockedByCurrentThread());
22
34
  auto operationsArray = operations.asObject(rt).asArray(rt);
@@ -25,91 +37,181 @@ void AnimatedPropsRegistry::update(jsi::Runtime &rt, const jsi::Value &operation
25
37
  auto item = operationsArray.getValueAtIndex(rt, i).asObject(rt);
26
38
  auto shadowNodeWrapper = item.getProperty(rt, "shadowNodeWrapper");
27
39
  auto shadowNode = shadowNodeFromValue(rt, shadowNodeWrapper);
40
+ if (isUnmounted(shadowNode->getFamily())) {
41
+ continue;
42
+ }
28
43
 
29
44
  jsi::Value updates = item.getProperty(rt, "updates");
45
+ roundTextMetrics(rt, updates);
30
46
 
31
47
  if constexpr (StaticFeatureFlags::getFlag("USE_ANIMATION_BACKEND")) {
32
48
  addJSIPropsToAnimatedPropsBatch(shadowNode->getFamilyShared(), rt, updates);
33
49
  } else {
34
- addUpdatesToBatch(shadowNode->getFamilyShared(), jsi::dynamicFromValue(rt, updates));
50
+ const auto dynamicUpdates = jsi::dynamicFromValue(rt, updates);
51
+ // With USE_ANIMATION_BACKEND, no update reaches `updatesRegistry_`, so
52
+ // `collectSettledUpdates` never reads `writeHistories_`.
53
+ if constexpr (StaticFeatureFlags::getFlag("FORCE_REACT_RENDER_FOR_SETTLED_ANIMATIONS")) {
54
+ const bool isAnimatedProps = item.getProperty(rt, "isAnimatedProps").asBool();
55
+ trackUpdate(shadowNode->getTag(), dynamicUpdates, isAnimatedProps, timestamp);
56
+ }
57
+ addUpdatesToBatch(shadowNode->getFamilyShared(), dynamicUpdates);
35
58
  }
59
+ }
60
+ }
36
61
 
37
- // When USE_ANIMATION_BACKEND is enabled, updates bypass `updatesRegistry_`,
38
- // so entries added to `timestampMap_` would never be synced and thus never
39
- // evicted, leaking until view unmount.
40
- if constexpr (
41
- StaticFeatureFlags::getFlag("FORCE_REACT_RENDER_FOR_SETTLED_ANIMATIONS") &&
42
- !StaticFeatureFlags::getFlag("USE_ANIMATION_BACKEND")) {
43
- const auto tag = shadowNode->getTag();
44
- timestampMap_[tag] = timestamp;
45
- // If JS already has a `settledProps` snapshot for this tag, it is now
46
- // stale — schedule a refresh on the next `collectSettledUpdates`.
47
- if (syncedTags_.erase(tag) > 0) {
48
- invalidatedTags_.insert(tag);
49
- }
62
+ void AnimatedPropsRegistry::trackUpdate(
63
+ const Tag tag,
64
+ const folly::dynamic &updates,
65
+ const bool isAnimatedProps,
66
+ const double timestamp) {
67
+ auto &writeHistory = writeHistories_[tag];
68
+ writeHistory.lastWriteTimestamp = timestamp;
69
+
70
+ for (const auto &key : updates.keys()) {
71
+ auto &keyOrigins = writeHistory.keyOrigins[key.getString()];
72
+ if (isAnimatedProps) {
73
+ keyOrigins.writtenByAnimatedProps = true;
74
+ } else {
75
+ keyOrigins.writtenByAnimatedStyle = true;
50
76
  }
77
+ keyOrigins.lastWrittenByAnimatedStyle = !isAnimatedProps;
51
78
  }
79
+
80
+ invalidateSyncedTag(tag);
52
81
  }
53
82
 
54
- jsi::Value AnimatedPropsRegistry::collectSettledUpdates(jsi::Runtime &rt, const double settledTimestamp) {
83
+ void AnimatedPropsRegistry::invalidateSyncedTag(const Tag tag) {
84
+ // If JS already has a `settledProps` snapshot for this tag, it is now
85
+ // stale — schedule a refresh on the next `collectSettledUpdates`.
86
+ if (syncedTags_.erase(tag) > 0) {
87
+ invalidatedTags_.insert(tag);
88
+ }
89
+ }
90
+
91
+ jsi::Object AnimatedPropsRegistry::createSettledUpdate(
92
+ jsi::Runtime &rt,
93
+ const Tag viewTag,
94
+ const folly::dynamic &props,
95
+ const WriteHistory &writeHistory) {
96
+ jsi::Object settledProps(rt);
97
+ jsi::Object settledStyle(rt);
98
+ std::vector<jsi::Value> keysLastWrittenByAnimatedStyle;
99
+
100
+ for (const auto &[key, value] : props.items()) {
101
+ const auto &keyString = key.getString();
102
+ const auto keyOriginsIt = writeHistory.keyOrigins.find(keyString);
103
+ if (keyOriginsIt == writeHistory.keyOrigins.end()) {
104
+ continue;
105
+ }
106
+ const auto &keyOrigins = keyOriginsIt->second;
107
+ if (keyOrigins.writtenByAnimatedProps) {
108
+ settledProps.setProperty(rt, keyString.c_str(), jsi::valueFromDynamic(rt, value));
109
+ }
110
+ if (keyOrigins.writtenByAnimatedStyle) {
111
+ settledStyle.setProperty(rt, keyString.c_str(), jsi::valueFromDynamic(rt, value));
112
+ }
113
+ if (keyOrigins.lastWrittenByAnimatedStyle) {
114
+ keysLastWrittenByAnimatedStyle.emplace_back(jsi::String::createFromUtf8(rt, keyString));
115
+ }
116
+ }
117
+
118
+ jsi::Array keysLastWrittenByAnimatedStyleArray(rt, keysLastWrittenByAnimatedStyle.size());
119
+ for (size_t i = 0; i < keysLastWrittenByAnimatedStyle.size(); ++i) {
120
+ keysLastWrittenByAnimatedStyleArray.setValueAtIndex(rt, i, std::move(keysLastWrittenByAnimatedStyle[i]));
121
+ }
122
+
123
+ jsi::Object settledUpdate(rt);
124
+ settledUpdate.setProperty(rt, "viewTag", viewTag);
125
+ settledUpdate.setProperty(rt, "props", std::move(settledProps));
126
+ settledUpdate.setProperty(rt, "style", std::move(settledStyle));
127
+ settledUpdate.setProperty(rt, "keysLastWrittenByAnimatedStyle", std::move(keysLastWrittenByAnimatedStyleArray));
128
+ return settledUpdate;
129
+ }
130
+
131
+ jsi::Value AnimatedPropsRegistry::collectSettledUpdates(
132
+ jsi::Runtime &rt,
133
+ const double settledTimestamp,
134
+ const std::unordered_set<Tag> &registeredViewTags) {
55
135
  react_native_assert(UpdatesRegistryManager::isLockedByCurrentThread());
56
136
 
57
- std::vector<std::pair<Tag, std::reference_wrapper<const folly::dynamic>>> updates;
137
+ std::vector<jsi::Object> settledUpdates;
138
+ const auto pendingTags = getPendingTags();
58
139
 
59
140
  for (auto it = updatesRegistry_.begin(); it != updatesRegistry_.end();) {
60
141
  const auto viewTag = it->first;
61
142
 
143
+ if (!registeredViewTags.contains(viewTag) || pendingTags.contains(viewTag)) {
144
+ ++it;
145
+ continue;
146
+ }
147
+
62
148
  if (syncedTags_.contains(viewTag)) {
63
149
  // React already has the latest value for this tag (synced on a previous
64
150
  // call, so the `settledProps` state is committed by now) — the registry
65
151
  // entry is redundant. `syncedTags_` is intentionally retained to detect
66
152
  // re-animation staleness. Note that `syncedTags_` and `invalidatedTags_`
67
153
  // are disjoint — `update()` moves tags from the former to the latter.
68
- timestampMap_.erase(viewTag);
69
154
  it = updatesRegistry_.erase(it);
70
155
  continue;
71
156
  }
72
157
 
73
- const auto timestampIt = timestampMap_.find(viewTag);
74
- if (timestampIt == timestampMap_.end()) {
158
+ const auto writeHistoryIt = writeHistories_.find(viewTag);
159
+ if (writeHistoryIt == writeHistories_.end()) {
75
160
  ++it;
76
161
  continue;
77
162
  }
78
- const bool isSettled = timestampIt->second < settledTimestamp;
163
+ const auto &writeHistory = writeHistoryIt->second;
164
+ const bool isSettled = writeHistory.lastWriteTimestamp < settledTimestamp;
79
165
  const auto invalidatedIt = invalidatedTags_.find(viewTag);
80
166
  const bool isInvalidated = invalidatedIt != invalidatedTags_.end();
81
167
  if (isSettled || isInvalidated) {
82
- updates.emplace_back(viewTag, std::cref(it->second.second));
168
+ settledUpdates.push_back(createSettledUpdate(rt, viewTag, it->second.second, writeHistory));
83
169
  if (isSettled) {
84
170
  // Only settled-path tags are tracked as "synced" so that an ongoing
85
171
  // animation doesn't re-trigger an invalidation/sync on every GC tick.
86
172
  syncedTags_.insert(viewTag);
87
173
  }
88
174
  if (isInvalidated) {
89
- // Only erase serviced invalidations; if a tag was invalidated but the
90
- // matching update batch hasn't been flushed into updatesRegistry_ yet,
91
- // we leave the entry so the next sync picks it up.
92
175
  invalidatedTags_.erase(invalidatedIt);
93
176
  }
94
177
  }
95
178
  ++it;
96
179
  }
97
180
 
98
- const jsi::Array array(rt, updates.size());
99
- size_t i = 0;
100
- for (const auto &[viewTag, styleProps] : updates) {
101
- const jsi::Object item(rt);
102
- item.setProperty(rt, "viewTag", viewTag);
103
- item.setProperty(rt, "styleProps", jsi::valueFromDynamic(rt, styleProps.get()));
104
- array.setValueAtIndex(rt, i++, item);
181
+ const jsi::Array array(rt, settledUpdates.size());
182
+ for (size_t i = 0; i < settledUpdates.size(); ++i) {
183
+ array.setValueAtIndex(rt, i, std::move(settledUpdates[i]));
105
184
  }
106
185
 
107
186
  return jsi::Value(rt, array);
108
187
  }
109
188
 
189
+ void AnimatedPropsRegistry::removeUnmounted(const ShadowNodeFamily::Shared &shadowNodeFamily) {
190
+ UpdatesRegistry::removeUnmounted(shadowNodeFamily);
191
+ const auto tag = shadowNodeFamily->getTag();
192
+ unmountedFamilies_[tag] = shadowNodeFamily;
193
+ if (unmountedFamilies_.size() >= unmountedFamiliesPruneThreshold_) {
194
+ pruneExpiredUnmountedFamilies();
195
+ }
196
+ }
197
+
198
+ void AnimatedPropsRegistry::handleRemount(const Tag tag) {
199
+ unmountedFamilies_.erase(tag);
200
+ }
201
+
202
+ bool AnimatedPropsRegistry::isUnmounted(const ShadowNodeFamily &shadowNodeFamily) const {
203
+ const auto it = unmountedFamilies_.find(shadowNodeFamily.getTag());
204
+ return it != unmountedFamilies_.end() && it->second.lock().get() == &shadowNodeFamily;
205
+ }
206
+
207
+ void AnimatedPropsRegistry::pruneExpiredUnmountedFamilies() {
208
+ std::erase_if(unmountedFamilies_, [](const auto &entry) { return entry.second.expired(); });
209
+ unmountedFamiliesPruneThreshold_ = std::max(MIN_UNMOUNTED_FAMILIES_PRUNE_THRESHOLD, 2 * unmountedFamilies_.size());
210
+ }
211
+
110
212
  void AnimatedPropsRegistry::removeTag(const Tag tag) {
111
213
  updatesRegistry_.erase(tag);
112
- timestampMap_.erase(tag);
214
+ writeHistories_.erase(tag);
113
215
  syncedTags_.erase(tag);
114
216
  invalidatedTags_.erase(tag);
115
217
  }
@@ -4,6 +4,8 @@
4
4
 
5
5
  #include <react/renderer/uimanager/UIManager.h>
6
6
 
7
+ #include <memory>
8
+ #include <string>
7
9
  #include <unordered_map>
8
10
  #include <unordered_set>
9
11
 
@@ -15,20 +17,52 @@ class AnimatedPropsRegistry : public UpdatesRegistry {
15
17
 
16
18
  /// Returns updates that settled (received no update since `settledTimestamp`)
17
19
  /// or whose synced `settledProps` snapshot was invalidated by a fresh update.
20
+ /// Skips tags with updates not yet flushed into the registry.
18
21
  /// Also evicts entries that have already been synced to React — by the time
19
22
  /// of the next call, the corresponding `settledProps` state is guaranteed to
20
- /// be committed, so the registry entries are redundant.
21
- jsi::Value collectSettledUpdates(jsi::Runtime &rt, double settledTimestamp);
23
+ /// be committed, so the registry entries are redundant. Skips views that JS
24
+ /// cannot deliver to, e.g. ones hidden by a Suspense boundary.
25
+ jsi::Value
26
+ collectSettledUpdates(jsi::Runtime &rt, double settledTimestamp, const std::unordered_set<Tag> &registeredViewTags);
27
+
28
+ void removeUnmounted(const ShadowNodeFamily::Shared &shadowNodeFamily) override;
29
+ void handleRemount(Tag tag) override;
22
30
 
23
31
  private:
24
- std::unordered_map<Tag, double> timestampMap_;
32
+ static constexpr size_t MIN_UNMOUNTED_FAMILIES_PRUNE_THRESHOLD = 256;
33
+
34
+ struct KeyOrigins {
35
+ bool writtenByAnimatedProps = false;
36
+ bool writtenByAnimatedStyle = false;
37
+ bool lastWrittenByAnimatedStyle = false;
38
+ };
39
+
40
+ struct WriteHistory {
41
+ double lastWriteTimestamp = 0;
42
+ std::unordered_map<std::string, KeyOrigins> keyOrigins;
43
+ };
44
+
45
+ // Kept after eviction of the registry entry, until `removeTag`. Each sync
46
+ // must send a key to the same React fields as the syncs before it.
47
+ std::unordered_map<Tag, WriteHistory> writeHistories_;
25
48
  // Tags whose latest values have already been pushed to React `settledProps`.
26
49
  // Intentionally retained after eviction to detect re-animation staleness.
27
50
  std::unordered_set<Tag> syncedTags_;
28
51
  // Tags that were synced to React but received a fresh worklet update since;
29
52
  // their `settledProps` are stale and need to be refreshed on the next sync.
30
53
  std::unordered_set<Tag> invalidatedTags_;
54
+ // A mapper can still update a view for a few frames after the view left the
55
+ // shadow tree. Such updates must not add the view to the registry again.
56
+ std::unordered_map<Tag, std::weak_ptr<const ShadowNodeFamily>> unmountedFamilies_;
57
+ size_t unmountedFamiliesPruneThreshold_ = MIN_UNMOUNTED_FAMILIES_PRUNE_THRESHOLD;
58
+
59
+ bool isUnmounted(const ShadowNodeFamily &shadowNodeFamily) const;
60
+ void pruneExpiredUnmountedFamilies();
31
61
 
62
+ void trackUpdate(Tag tag, const folly::dynamic &updates, bool isAnimatedProps, double timestamp);
63
+ void invalidateSyncedTag(Tag tag);
64
+ static jsi::Object
65
+ createSettledUpdate(jsi::Runtime &rt, Tag viewTag, const folly::dynamic &props, const WriteHistory &writeHistory);
32
66
  void removeTag(Tag tag) override;
33
67
  };
34
68
 
@@ -0,0 +1,103 @@
1
+ #include <reanimated/Fabric/updates/SynchronousWritesTracker.h>
2
+
3
+ #include <algorithm>
4
+
5
+ namespace reanimated {
6
+
7
+ void SynchronousWritesTracker::onWillCommit(
8
+ const RootShadowNode::Shared &rootShadowNode,
9
+ const bool carriesRegistryValues) {
10
+ auto lock = std::scoped_lock{mutex_};
11
+ const auto threadId = std::this_thread::get_id();
12
+ pendingCommits_.erase(threadId);
13
+ if (rootShadowNode->getChildren().empty()) {
14
+ return;
15
+ }
16
+ const auto surfaceId = rootShadowNode->getSurfaceId();
17
+ auto &surface = surfaces_[surfaceId];
18
+ if (carriesRegistryValues) {
19
+ surface.epoch++;
20
+ }
21
+ pendingCommits_.emplace(threadId, PendingCommit{surfaceId, surface.epoch});
22
+ }
23
+
24
+ void SynchronousWritesTracker::onDidCommit(const RootShadowNode::Shared &rootShadowNode) {
25
+ auto lock = std::scoped_lock{mutex_};
26
+ const auto pendingCommit = pendingCommits_.extract(std::this_thread::get_id());
27
+ if (pendingCommit.empty() || pendingCommit.mapped().surfaceId != rootShadowNode->getSurfaceId()) {
28
+ return;
29
+ }
30
+ const auto surfaceIt = surfaces_.find(pendingCommit.mapped().surfaceId);
31
+ if (surfaceIt == surfaces_.end()) {
32
+ return;
33
+ }
34
+ rememberRoot(surfaceIt->second, rootShadowNode, pendingCommit.mapped().epoch);
35
+ }
36
+
37
+ void SynchronousWritesTracker::rememberRoot(
38
+ SurfaceState &surface,
39
+ const RootShadowNode::Shared &rootShadowNode,
40
+ const Epoch epoch) {
41
+ auto &committedRoots = surface.committedRoots;
42
+ while (!committedRoots.empty() && committedRoots.front().rootShadowNode.expired()) {
43
+ committedRoots.pop_front();
44
+ }
45
+ committedRoots.push_back({rootShadowNode, epoch});
46
+ }
47
+
48
+ void SynchronousWritesTracker::onSynchronousWrite(const UpdatesBatch &synchronousUpdatesBatch) {
49
+ auto lock = std::scoped_lock{mutex_};
50
+ for (const auto &[shadowNodeFamily, props] : synchronousUpdatesBatch) {
51
+ const auto surfaceIt = surfaces_.find(shadowNodeFamily->getSurfaceId());
52
+ if (surfaceIt == surfaces_.end() || surfaceIt->second.epoch == surfaceIt->second.mountedEpoch) {
53
+ continue;
54
+ }
55
+ auto &surface = surfaceIt->second;
56
+ surface.writes.insert_or_assign(shadowNodeFamily->getTag(), Write{shadowNodeFamily, surface.epoch});
57
+ }
58
+ }
59
+
60
+ void SynchronousWritesTracker::onMountReport(const RootShadowNode::Shared &mountedRootShadowNode) {
61
+ auto lock = std::scoped_lock{mutex_};
62
+ const auto surfaceIt = surfaces_.find(mountedRootShadowNode->getSurfaceId());
63
+ if (surfaceIt == surfaces_.end()) {
64
+ return;
65
+ }
66
+ auto &surface = surfaceIt->second;
67
+ auto &committedRoots = surface.committedRoots;
68
+ const auto it = std::find_if(committedRoots.begin(), committedRoots.end(), [&](const CommittedRoot &root) {
69
+ return root.rootShadowNode.lock() == mountedRootShadowNode;
70
+ });
71
+ if (it == committedRoots.end()) {
72
+ return;
73
+ }
74
+ surface.reportedEpoch = it->epoch;
75
+ committedRoots.erase(committedRoots.begin(), it + 1);
76
+ }
77
+
78
+ void SynchronousWritesTracker::onSurfaceStop(const SurfaceId surfaceId) {
79
+ auto lock = std::scoped_lock{mutex_};
80
+ surfaces_.erase(surfaceId);
81
+ }
82
+
83
+ std::vector<ShadowNodeFamily::Shared> SynchronousWritesTracker::getFamiliesToRewrite() const {
84
+ auto lock = std::scoped_lock{mutex_};
85
+ std::vector<ShadowNodeFamily::Shared> families;
86
+ for (const auto &[_, surface] : surfaces_) {
87
+ for (const auto &[tag, write] : surface.writes) {
88
+ families.push_back(write.shadowNodeFamily);
89
+ }
90
+ }
91
+ return families;
92
+ }
93
+
94
+ void SynchronousWritesTracker::onRewrite() {
95
+ auto lock = std::scoped_lock{mutex_};
96
+ for (auto &[_, surface] : surfaces_) {
97
+ const auto mountedEpoch = surface.reportedEpoch;
98
+ surface.mountedEpoch = mountedEpoch;
99
+ std::erase_if(surface.writes, [mountedEpoch](const auto &entry) { return entry.second.epoch <= mountedEpoch; });
100
+ }
101
+ }
102
+
103
+ } // namespace reanimated