react-native-smooth-clip-view 0.3.0 → 0.4.1

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 (199) hide show
  1. package/README.md +135 -386
  2. package/android/build.gradle +4 -7
  3. package/android/proguard-rules.pro +2 -2
  4. package/android/src/main/cpp/SmoothClipAndroid.h +9 -12
  5. package/android/src/main/cpp/SmoothClipBindings.cpp +300 -704
  6. package/android/src/main/cpp/SmoothClipRegistry.cpp +348 -594
  7. package/android/src/main/java/com/smoothclipview/ClipGeometryNormalizer.kt +36 -227
  8. package/android/src/main/java/com/smoothclipview/SmoothClipBindings.kt +16 -19
  9. package/android/src/main/java/com/smoothclipview/SmoothClipModule.kt +36 -294
  10. package/android/src/main/java/com/smoothclipview/SmoothClipView.kt +369 -486
  11. package/android/src/main/java/com/smoothclipview/SmoothClipViewManager.kt +75 -77
  12. package/cpp/SmoothClipAnimationCurve.h +237 -346
  13. package/cpp/SmoothClipRegistry.h +24 -52
  14. package/cpp/SmoothClipSharedGeometry.h +64 -94
  15. package/cpp/SmoothClipVelocityTracker.h +5 -5
  16. package/ios/SmoothClipGeometry.h +23 -34
  17. package/ios/SmoothClipRegistry.mm +206 -711
  18. package/ios/SmoothClipTurboModule.cpp +217 -715
  19. package/ios/SmoothClipTurboModule.h +54 -297
  20. package/ios/SmoothClipView.mm +599 -505
  21. package/ios/SmoothClipViewRegistry.h +2 -7
  22. package/lib/module/NativeSmoothClipModule.js.map +1 -1
  23. package/lib/module/SmoothClipView.ios.js +84 -17
  24. package/lib/module/SmoothClipView.ios.js.map +1 -1
  25. package/lib/module/SmoothClipView.js +1 -1
  26. package/lib/module/SmoothClipView.js.map +1 -1
  27. package/lib/module/SmoothClipViewNativeComponent.ts +20 -26
  28. package/lib/module/capabilities.native.js +1 -19
  29. package/lib/module/capabilities.native.js.map +1 -1
  30. package/lib/module/capabilityTypes.js +0 -9
  31. package/lib/module/capabilityTypes.js.map +1 -1
  32. package/lib/module/controllerInternals.js +28 -0
  33. package/lib/module/controllerInternals.js.map +1 -0
  34. package/lib/module/controllerLifecycle.js +9 -0
  35. package/lib/module/controllerLifecycle.js.map +1 -0
  36. package/lib/module/controllerTypes.js +4 -0
  37. package/lib/module/controllerTypes.js.map +1 -0
  38. package/lib/module/controllers.android.js +4 -0
  39. package/lib/module/controllers.android.js.map +1 -0
  40. package/lib/module/controllers.ios.js +4 -0
  41. package/lib/module/controllers.ios.js.map +1 -0
  42. package/lib/module/controllers.js +4 -0
  43. package/lib/module/controllers.js.map +1 -0
  44. package/lib/module/controllers.native.js +89 -0
  45. package/lib/module/controllers.native.js.map +1 -0
  46. package/lib/module/geometry.js +71 -51
  47. package/lib/module/geometry.js.map +1 -1
  48. package/lib/module/groupTypes.js +4 -0
  49. package/lib/module/{groupDriverTypes.js.map → groupTypes.js.map} +1 -1
  50. package/lib/module/groups.android.js +4 -0
  51. package/lib/module/groups.android.js.map +1 -0
  52. package/lib/module/groups.ios.js +4 -0
  53. package/lib/module/groups.ios.js.map +1 -0
  54. package/lib/module/groups.js +4 -0
  55. package/lib/module/groups.js.map +1 -0
  56. package/lib/module/groups.native.js +399 -0
  57. package/lib/module/groups.native.js.map +1 -0
  58. package/lib/module/ids.js +8 -0
  59. package/lib/module/ids.js.map +1 -0
  60. package/lib/module/index.js +3 -3
  61. package/lib/module/index.js.map +1 -1
  62. package/lib/module/nativeCompletion.js +37 -16
  63. package/lib/module/nativeCompletion.js.map +1 -1
  64. package/lib/module/presentationCodec.js +81 -0
  65. package/lib/module/presentationCodec.js.map +1 -0
  66. package/lib/typescript/src/NativeSmoothClipModule.d.ts +14 -23
  67. package/lib/typescript/src/NativeSmoothClipModule.d.ts.map +1 -1
  68. package/lib/typescript/src/SmoothClipView.d.ts +1 -1
  69. package/lib/typescript/src/SmoothClipView.d.ts.map +1 -1
  70. package/lib/typescript/src/SmoothClipView.ios.d.ts +18 -14
  71. package/lib/typescript/src/SmoothClipView.ios.d.ts.map +1 -1
  72. package/lib/typescript/src/SmoothClipViewNativeComponent.d.ts +10 -5
  73. package/lib/typescript/src/SmoothClipViewNativeComponent.d.ts.map +1 -1
  74. package/lib/typescript/src/capabilities.native.d.ts +1 -1
  75. package/lib/typescript/src/capabilities.native.d.ts.map +1 -1
  76. package/lib/typescript/src/capabilityTypes.d.ts +0 -6
  77. package/lib/typescript/src/capabilityTypes.d.ts.map +1 -1
  78. package/lib/typescript/src/controllerInternals.d.ts +14 -0
  79. package/lib/typescript/src/controllerInternals.d.ts.map +1 -0
  80. package/lib/typescript/src/controllerLifecycle.d.ts +3 -0
  81. package/lib/typescript/src/controllerLifecycle.d.ts.map +1 -0
  82. package/lib/typescript/src/controllerTypes.d.ts +62 -0
  83. package/lib/typescript/src/controllerTypes.d.ts.map +1 -0
  84. package/lib/typescript/src/controllers.android.d.ts +2 -0
  85. package/lib/typescript/src/controllers.android.d.ts.map +1 -0
  86. package/lib/typescript/src/controllers.d.ts +2 -0
  87. package/lib/typescript/src/controllers.d.ts.map +1 -0
  88. package/lib/typescript/src/controllers.ios.d.ts +2 -0
  89. package/lib/typescript/src/controllers.ios.d.ts.map +1 -0
  90. package/lib/typescript/src/controllers.native.d.ts +3 -0
  91. package/lib/typescript/src/controllers.native.d.ts.map +1 -0
  92. package/lib/typescript/src/geometry.d.ts +22 -12
  93. package/lib/typescript/src/geometry.d.ts.map +1 -1
  94. package/lib/typescript/src/groupTypes.d.ts +31 -0
  95. package/lib/typescript/src/groupTypes.d.ts.map +1 -0
  96. package/lib/typescript/src/groups.android.d.ts +2 -0
  97. package/lib/typescript/src/groups.android.d.ts.map +1 -0
  98. package/lib/typescript/src/groups.d.ts +2 -0
  99. package/lib/typescript/src/groups.d.ts.map +1 -0
  100. package/lib/typescript/src/groups.ios.d.ts +2 -0
  101. package/lib/typescript/src/groups.ios.d.ts.map +1 -0
  102. package/lib/typescript/src/groups.native.d.ts +3 -0
  103. package/lib/typescript/src/groups.native.d.ts.map +1 -0
  104. package/lib/typescript/src/ids.d.ts +2 -0
  105. package/lib/typescript/src/ids.d.ts.map +1 -0
  106. package/lib/typescript/src/index.d.ts +6 -6
  107. package/lib/typescript/src/index.d.ts.map +1 -1
  108. package/lib/typescript/src/nativeCompletion.d.ts +16 -6
  109. package/lib/typescript/src/nativeCompletion.d.ts.map +1 -1
  110. package/lib/typescript/src/presentationCodec.d.ts +10 -0
  111. package/lib/typescript/src/presentationCodec.d.ts.map +1 -0
  112. package/package.json +8 -3
  113. package/src/NativeSmoothClipModule.ts +41 -292
  114. package/src/SmoothClipView.ios.tsx +130 -23
  115. package/src/SmoothClipView.tsx +5 -1
  116. package/src/SmoothClipViewNativeComponent.ts +20 -26
  117. package/src/capabilities.native.ts +2 -52
  118. package/src/capabilityTypes.ts +0 -14
  119. package/src/controllerInternals.ts +52 -0
  120. package/src/controllerLifecycle.ts +6 -0
  121. package/src/controllerTypes.ts +87 -0
  122. package/src/controllers.android.ts +1 -0
  123. package/src/controllers.ios.ts +1 -0
  124. package/src/controllers.native.ts +104 -0
  125. package/src/controllers.ts +1 -0
  126. package/src/geometry.ts +118 -74
  127. package/src/groupTypes.ts +53 -0
  128. package/src/groups.android.ts +1 -0
  129. package/src/groups.ios.ts +1 -0
  130. package/src/groups.native.ts +604 -0
  131. package/src/groups.ts +1 -0
  132. package/src/ids.ts +6 -0
  133. package/src/index.ts +20 -28
  134. package/src/nativeCompletion.ts +67 -32
  135. package/src/presentationCodec.ts +134 -0
  136. package/lib/module/driverState.js +0 -82
  137. package/lib/module/driverState.js.map +0 -1
  138. package/lib/module/driverTypes.js +0 -4
  139. package/lib/module/driverTypes.js.map +0 -1
  140. package/lib/module/drivers.android.js +0 -6
  141. package/lib/module/drivers.android.js.map +0 -1
  142. package/lib/module/drivers.ios.js +0 -6
  143. package/lib/module/drivers.ios.js.map +0 -1
  144. package/lib/module/drivers.js +0 -4
  145. package/lib/module/drivers.js.map +0 -1
  146. package/lib/module/drivers.native.js +0 -756
  147. package/lib/module/drivers.native.js.map +0 -1
  148. package/lib/module/groupDriverTypes.js +0 -4
  149. package/lib/module/groupDrivers.android.js +0 -4
  150. package/lib/module/groupDrivers.android.js.map +0 -1
  151. package/lib/module/groupDrivers.ios.js +0 -4
  152. package/lib/module/groupDrivers.ios.js.map +0 -1
  153. package/lib/module/groupDrivers.js +0 -4
  154. package/lib/module/groupDrivers.js.map +0 -1
  155. package/lib/module/groupDrivers.native.js +0 -799
  156. package/lib/module/groupDrivers.native.js.map +0 -1
  157. package/lib/module/presentationProtocol.js +0 -15
  158. package/lib/module/presentationProtocol.js.map +0 -1
  159. package/lib/module/reactRequests.js +0 -56
  160. package/lib/module/reactRequests.js.map +0 -1
  161. package/lib/typescript/src/driverState.d.ts +0 -26
  162. package/lib/typescript/src/driverState.d.ts.map +0 -1
  163. package/lib/typescript/src/driverTypes.d.ts +0 -121
  164. package/lib/typescript/src/driverTypes.d.ts.map +0 -1
  165. package/lib/typescript/src/drivers.android.d.ts +0 -3
  166. package/lib/typescript/src/drivers.android.d.ts.map +0 -1
  167. package/lib/typescript/src/drivers.d.ts +0 -3
  168. package/lib/typescript/src/drivers.d.ts.map +0 -1
  169. package/lib/typescript/src/drivers.ios.d.ts +0 -3
  170. package/lib/typescript/src/drivers.ios.d.ts.map +0 -1
  171. package/lib/typescript/src/drivers.native.d.ts +0 -5
  172. package/lib/typescript/src/drivers.native.d.ts.map +0 -1
  173. package/lib/typescript/src/groupDriverTypes.d.ts +0 -66
  174. package/lib/typescript/src/groupDriverTypes.d.ts.map +0 -1
  175. package/lib/typescript/src/groupDrivers.android.d.ts +0 -2
  176. package/lib/typescript/src/groupDrivers.android.d.ts.map +0 -1
  177. package/lib/typescript/src/groupDrivers.d.ts +0 -2
  178. package/lib/typescript/src/groupDrivers.d.ts.map +0 -1
  179. package/lib/typescript/src/groupDrivers.ios.d.ts +0 -2
  180. package/lib/typescript/src/groupDrivers.ios.d.ts.map +0 -1
  181. package/lib/typescript/src/groupDrivers.native.d.ts +0 -4
  182. package/lib/typescript/src/groupDrivers.native.d.ts.map +0 -1
  183. package/lib/typescript/src/presentationProtocol.d.ts +0 -4
  184. package/lib/typescript/src/presentationProtocol.d.ts.map +0 -1
  185. package/lib/typescript/src/reactRequests.d.ts +0 -9
  186. package/lib/typescript/src/reactRequests.d.ts.map +0 -1
  187. package/src/driverState.ts +0 -118
  188. package/src/driverTypes.ts +0 -171
  189. package/src/drivers.android.ts +0 -8
  190. package/src/drivers.ios.ts +0 -8
  191. package/src/drivers.native.ts +0 -1599
  192. package/src/drivers.ts +0 -6
  193. package/src/groupDriverTypes.ts +0 -117
  194. package/src/groupDrivers.android.ts +0 -1
  195. package/src/groupDrivers.ios.ts +0 -1
  196. package/src/groupDrivers.native.ts +0 -1189
  197. package/src/groupDrivers.ts +0 -1
  198. package/src/presentationProtocol.ts +0 -28
  199. package/src/reactRequests.ts +0 -76
@@ -1,756 +0,0 @@
1
- "use strict";
2
-
3
- // The shared native driver: iOS and Android both execute this file, via the
4
- // one-line re-exports in drivers.ios.ts / drivers.android.ts that Metro's
5
- // platform resolution picks up from index.ts's `./drivers` import. Only the
6
- // `./smoothClipNative` import below is platform-split.
7
- import { useEffect, useRef } from 'react';
8
- import { Platform } from 'react-native';
9
- import { useSharedValue } from 'react-native-reanimated';
10
- import { isRNRuntime, scheduleOnRN, scheduleOnUI } from 'react-native-worklets';
11
- import NativeSmoothClipModule from './smoothClipNative';
12
- import { getSmoothClipCapabilities } from './capabilities';
13
- import { allocateDriverId, attachDriverState, createDriverState, detachDriverState, getDriverState, setDriverState } from "./driverState.js";
14
- import { completeNativeAnimation, synchronizeNativeCompletion } from "./nativeCompletion.js";
15
- import { canonicalizeClipPresentation, clipPresentationEquals, createClipPresentation, isFiniteClipPresentation } from "./geometry.js";
16
- import { createReactRequest, rejectDriverRequests, resolveReactRequest } from "./reactRequests.js";
17
- const INTERACTIVE = 0;
18
- const NATIVE = 1;
19
-
20
- // Only Android consumes the Reanimated start stamp (its frame-clock anchor —
21
- // docs/android-frame-clock-anchor.md). iOS's TurboModule reads its declared
22
- // parameters positionally and drops the trailing argument, so capturing the
23
- // stamp there is a wasted native _getAnimationTimestamp() call per animateTo.
24
- // Resolved once at module scope; the worklet captures the primitive.
25
- const NEEDS_START_STAMP = Platform.OS === 'android';
26
- const PRESENTATION_PROTOCOL_VERSION = getSmoothClipCapabilities().presentationProtocolVersion;
27
- const NativeSmoothClipModuleV2 = NativeSmoothClipModule;
28
-
29
- // Widened like the animate* functions below: one trailing argument beyond the
30
- // TurboModule spec — whether this write should record an `'inherit'` velocity
31
- // sample. Android's JSI bindings read it (absent means record, the pre-flag
32
- // behavior); the iOS TurboModule reads its declared parameters positionally
33
- // and drops it, so iOS always records.
34
-
35
- const setPresentationHostFunction = NativeSmoothClipModule.setClipPresentation;
36
- const beginInteractionHostFunction = NativeSmoothClipModule.beginInteraction;
37
-
38
- // The animate* host functions take one argument beyond the TurboModule spec:
39
- // the Reanimated-rule start stamp in milliseconds (see animateOnUI). Android's
40
- // JSI bindings read it as an optional trailing argument; the iOS TurboModule
41
- // reads its declared parameters positionally and ignores extras, so the
42
- // codegen spec stays untouched and both platforms take the same call. A
43
- // narrower function is assignable to the widened type, so no cast is needed.
44
-
45
- const animateTimingHostFunction = NativeSmoothClipModule.animateTiming;
46
- const animateSpringHostFunction = NativeSmoothClipModule.animateSpring;
47
- const animateKeyframesHostFunction = NativeSmoothClipModule.animateKeyframes;
48
- const rejectAnimationHostFunction = NativeSmoothClipModule.rejectAnimation;
49
- const cancelAnimationHostFunction = NativeSmoothClipModule.cancelAnimation;
50
- const destroyDriverHostFunction = NativeSmoothClipModule.destroyDriver;
51
- const setPresentationV2HostFunction = NativeSmoothClipModuleV2.setClipPresentationV2;
52
- const beginInteractionV2HostFunction = NativeSmoothClipModuleV2.beginInteractionV2;
53
- const snapshotCurrentV2HostFunction = NativeSmoothClipModuleV2.snapshotCurrentV2;
54
- const animateTimingV2HostFunction = NativeSmoothClipModuleV2.animateTimingV2;
55
- const animateSpringV2HostFunction = NativeSmoothClipModuleV2.animateSpringV2;
56
- const animateKeyframesV2HostFunction = NativeSmoothClipModuleV2.animateKeyframesV2;
57
- const animateTimingFromV2HostFunction = NativeSmoothClipModuleV2.animateTimingFromV2;
58
- const animateSpringFromV2HostFunction = NativeSmoothClipModuleV2.animateSpringFromV2;
59
- const animateKeyframesFromV2HostFunction = NativeSmoothClipModuleV2.animateKeyframesFromV2;
60
- const cancelAnimationV2HostFunction = NativeSmoothClipModuleV2.cancelAnimationV2;
61
-
62
- // Workaround for react-native-worklets 0.10.0 (fixed upstream in >=0.10.1,
63
- // which drops the registry/proxy design; kept while the peer range still
64
- // allows 0.10.0 consumers): the RN-side serialization cache
65
- // hands out one remote-function registry id per function forever, but the
66
- // registry entry is deleted as soon as any transient UI-side proxy of that
67
- // function is garbage collected. A later scheduleOnRN for the same function
68
- // then reads `undefined` from the registry and aborts in Value::getObject.
69
- // Pinning one UI-side reference for the app lifetime keeps the proxy — and
70
- // with it the registry entry — alive. Remove once the peer range requires
71
- // worklets >=0.10.1.
72
- scheduleOnUI(resolver => {
73
- 'worklet';
74
-
75
- globalThis.__smoothClipPinnedResolver = resolver;
76
- }, resolveReactRequest);
77
- function reduceMotionCode(value) {
78
- switch (value) {
79
- case 'always':
80
- return 1;
81
- case 'never':
82
- return 2;
83
- default:
84
- return 0;
85
- }
86
- }
87
- function animationIsFinite(animation, strictV2) {
88
- 'worklet';
89
-
90
- if (animation.from !== undefined && !isFiniteClipPresentation(animation.from)) {
91
- // Silently dropping an invalid explicit start would re-open the exact
92
- // stale-start handoff `from` exists to close — reject the whole call.
93
- return false;
94
- }
95
- if (animation.type === 'timing') {
96
- const [x1,, x2] = animation.controlPoints;
97
- return Number.isFinite(animation.duration) && (!strictV2 || animation.duration >= 0) && animation.controlPoints.every(Number.isFinite) && (
98
- // A cubic-bezier easing is only defined for x within [0,1] (the CSS /
99
- // CoreAnimation / Reanimated Easing.bezier contract). Out-of-range x
100
- // makes the Android parameter solve meaningless while CoreAnimation
101
- // silently clamps — reject like any other invalid input instead of
102
- // diverging per platform.
103
- !strictV2 || x1 >= 0 && x1 <= 1 && x2 >= 0 && x2 <= 1);
104
- }
105
- if (animation.type === 'spring') {
106
- const mass = animation.mass ?? 1;
107
- const stiffness = animation.stiffness ?? 100;
108
- const damping = animation.damping ?? 10;
109
- const initialVelocity = animation.initialVelocity === 'inherit' ? 0 : animation.initialVelocity ?? 0;
110
- return [mass, stiffness, damping, initialVelocity].every(Number.isFinite) && (!strictV2 || mass > 0 && stiffness > 0 && damping >= 0);
111
- }
112
- // Keyframe frames validate inside flattenFiniteKeyframes — one traversal
113
- // both checks and flattens, instead of walking every frame twice. The type
114
- // check is statically always-true for the TS union but load-bearing for JS
115
- // callers: an unrecognized type must reject here, or it would skip the
116
- // keyframe gate entirely and hand native a null frames array.
117
- return animation.type === 'keyframes' && (!strictV2 || animation.duration >= 0);
118
- }
119
- function presentationFromNative(values, fallback, offset = 0) {
120
- 'worklet';
121
-
122
- if (values.length >= offset + 12) {
123
- const presentation = canonicalizeClipPresentation({
124
- clip: {
125
- x: values[offset],
126
- y: values[offset + 1],
127
- width: values[offset + 2],
128
- height: values[offset + 3],
129
- radius: 0,
130
- topLeftRadius: values[offset + 4],
131
- topRightRadius: values[offset + 5],
132
- bottomRightRadius: values[offset + 6],
133
- bottomLeftRadius: values[offset + 7],
134
- curve: values[offset + 8] === 1 ? 'continuous' : 'circular'
135
- },
136
- contentTranslateX: values[offset + 9],
137
- contentTranslateY: values[offset + 10],
138
- contentScale: values[offset + 11]
139
- });
140
- return presentation ?? fallback;
141
- }
142
- if (values.length < offset + 7) return fallback;
143
- const presentation = canonicalizeClipPresentation({
144
- clip: {
145
- x: values[offset],
146
- y: values[offset + 1],
147
- width: values[offset + 2],
148
- height: values[offset + 3],
149
- radius: values[offset + 4]
150
- },
151
- contentTranslateX: values[offset + 5],
152
- contentTranslateY: values[offset + 6]
153
- });
154
- return presentation ?? fallback;
155
- }
156
- function curveCode(presentation) {
157
- 'worklet';
158
-
159
- return presentation.clip.curve === 'continuous' ? 1 : 0;
160
- }
161
- function isV1Compatible(presentation) {
162
- 'worklet';
163
-
164
- const clip = presentation.clip;
165
- return clip.curve === 'circular' && clip.topLeftRadius === clip.radius && clip.topRightRadius === clip.radius && clip.bottomRightRadius === clip.radius && clip.bottomLeftRadius === clip.radius && presentation.contentScale === 1;
166
- }
167
- function springScaleIsProvablyPositive(start, target, animation) {
168
- 'worklet';
169
-
170
- if (start.contentScale === target.contentScale) return true;
171
-
172
- // A zero-velocity critically/over-damped step is monotonic, so its sampled
173
- // scale remains between two already-positive endpoints. Inherited or
174
- // under-damped configurations can overshoot; callers must compile those
175
- // plans to exact keyframes instead of risking a non-positive transform.
176
- const mass = animation.mass ?? 1;
177
- const stiffness = animation.stiffness ?? 100;
178
- const damping = animation.damping ?? 10;
179
- return animation.initialVelocity === 0 && damping * damping >= 4 * mass * stiffness;
180
- }
181
-
182
- // Validates and flattens the keyframe list in one traversal (the flat number
183
- // array is the wire format the native side reads back positionally). Returns
184
- // null when any frame is invalid — same rejection contract the two-pass
185
- // validate-then-flatten had.
186
- function flattenFiniteKeyframes(target, animation) {
187
- 'worklet';
188
-
189
- if (!Number.isFinite(animation.duration) || animation.frames.length < 2) {
190
- return null;
191
- }
192
- const values = [];
193
- const canonicalFrames = [];
194
- let previousOffset = -1;
195
- for (const frame of animation.frames) {
196
- const {
197
- offset
198
- } = frame;
199
- const presentation = canonicalizeClipPresentation(frame.presentation);
200
- if (!Number.isFinite(offset) || offset < 0 || offset > 1 || offset <= previousOffset || presentation === null || presentation.clip.curve !== target.clip.curve) {
201
- return null;
202
- }
203
- previousOffset = offset;
204
- canonicalFrames.push(presentation);
205
- const {
206
- clip,
207
- contentTranslateX,
208
- contentTranslateY,
209
- contentScale
210
- } = presentation;
211
- values.push(offset, clip.x, clip.y, clip.width, clip.height, clip.topLeftRadius, clip.topRightRadius, clip.bottomRightRadius, clip.bottomLeftRadius, curveCode(presentation), contentTranslateX, contentTranslateY, contentScale);
212
- }
213
- if (animation.frames[0]?.offset !== 0 || previousOffset !== 1 || !clipPresentationEquals(canonicalFrames[canonicalFrames.length - 1] ?? null, target)) {
214
- return null;
215
- }
216
- return {
217
- frames: values,
218
- canonicalFrames
219
- };
220
- }
221
- function flattenV1Keyframes(frames, offsets) {
222
- 'worklet';
223
-
224
- const values = [];
225
- for (let index = 0; index < frames.length; index += 1) {
226
- const presentation = frames[index];
227
- const offset = offsets[index];
228
- if (presentation === undefined || offset === undefined) continue;
229
- values.push(offset, presentation.clip.x, presentation.clip.y, presentation.clip.width, presentation.clip.height, presentation.clip.radius, presentation.contentTranslateX, presentation.contentTranslateY);
230
- }
231
- return values;
232
- }
233
- function uiOnly() {
234
- 'worklet';
235
-
236
- throw new Error('[SmoothClipView] driver.ui methods must run on the UI runtime. Use driver.react from React code.');
237
- }
238
- export function useSmoothClipDriver(initialValue, options = {}) {
239
- const requestedInitialPresentation = 'clip' in initialValue ? initialValue : createClipPresentation(initialValue);
240
- const initialPresentation = canonicalizeClipPresentation(requestedInitialPresentation);
241
- if (initialPresentation === null) {
242
- throw new Error('[SmoothClipView] Initial presentation must be finite.');
243
- }
244
- const presentation = useSharedValue(initialPresentation);
245
- const ownership = useSharedValue(INTERACTIVE);
246
- const suppressDeliveries = useSharedValue(0);
247
- const activeAnimationId = useSharedValue(0);
248
- // Non-zero after a setScalars hot write: driver.presentation is then stale,
249
- // so animateTo must start from the native registry's latest value instead
250
- // of snapping back to the stale SharedValue.
251
- const scalarsStale = useSharedValue(0);
252
- // Non-zero between the effect cleanup's native teardown and the next effect
253
- // run. Native cannot tell "this driver was never seeded" from "this driver
254
- // was destroyed and erased" — both are a missing registry entry — so the
255
- // animation entry points would happily create a fresh state for a call that
256
- // arrives after the hook is gone, leaving a latch that no view starts and no
257
- // destroy cancels. Only the UI side knows the difference, so it is decided
258
- // here.
259
- const disposed = useSharedValue(0);
260
- const callbackRef = useRef(options.onAnimationComplete);
261
- const driverRef = useRef(null);
262
- callbackRef.current = options.onAnimationComplete;
263
- if (driverRef.current === null) {
264
- const driverId = allocateDriverId();
265
- const reduceMotion = reduceMotionCode(options.reduceMotion ?? 'system');
266
- // Captured once, like reduceMotion. Only the per-frame setScalars stream
267
- // consults it; set(), the fused animateTo `from` seed, and the effect's
268
- // declarative deliveries always record, so every once-per-call channel
269
- // keeps its pre-flag 'inherit' behavior.
270
- const velocityTracking = options.velocityTracking === true;
271
- const seedPresentation = next => {
272
- 'worklet';
273
-
274
- if (clipPresentationEquals(presentation.value, next)) return;
275
- suppressDeliveries.value += 1;
276
- presentation.value = next;
277
- // The listener runs synchronously inside that assignment, so the credit
278
- // is either spent by now or was never spendable. deliver() returns before
279
- // the decrement whenever the value dedupes against `last` — which happens
280
- // exactly when native already holds it, e.g. freezing a latch back to the
281
- // value it started from. An unspent credit would then swallow the next
282
- // real declarative write and leave native a geometry behind for good.
283
- if (suppressDeliveries.value > 0) suppressDeliveries.value -= 1;
284
- };
285
- const beginOnUI = () => {
286
- 'worklet';
287
-
288
- const fallback = canonicalizeClipPresentation(presentation.value) ?? initialPresentation;
289
- if (disposed.value !== 0) return fallback;
290
- const current = presentationFromNative(PRESENTATION_PROTOCOL_VERSION === 2 && beginInteractionV2HostFunction ? beginInteractionV2HostFunction(driverId) : beginInteractionHostFunction(driverId), fallback);
291
- activeAnimationId.value = 0;
292
- ownership.value = INTERACTIVE;
293
- scalarsStale.value = 0;
294
- seedPresentation(current);
295
- return current;
296
- };
297
- const setOnUI = next => {
298
- 'worklet';
299
-
300
- const canonical = canonicalizeClipPresentation(next);
301
- if (disposed.value !== 0 || canonical === null) return;
302
- activeAnimationId.value = 0;
303
- ownership.value = INTERACTIVE;
304
- scalarsStale.value = 0;
305
- const {
306
- clip,
307
- contentTranslateX,
308
- contentTranslateY,
309
- contentScale
310
- } = canonical;
311
- // Always records: only the per-frame setScalars hot path is gated by
312
- // velocityTracking. set() already pays full validation and a SharedValue
313
- // seed per call, so the sample recording is noise here.
314
- if (PRESENTATION_PROTOCOL_VERSION === 2 && setPresentationV2HostFunction) {
315
- setPresentationV2HostFunction(driverId, clip.x, clip.y, clip.width, clip.height, clip.topLeftRadius, clip.topRightRadius, clip.bottomRightRadius, clip.bottomLeftRadius, curveCode(canonical), contentTranslateX, contentTranslateY, contentScale, true, false, true);
316
- } else if (isV1Compatible(canonical)) {
317
- setPresentationHostFunction(driverId, clip.x, clip.y, clip.width, clip.height, clip.radius, contentTranslateX, contentTranslateY, true, false);
318
- } else {
319
- return;
320
- }
321
- seedPresentation(canonical);
322
- };
323
- const setScalarsOnUI = (x, y, width, height, radius, contentTranslateX, contentTranslateY, overridePendingAnimation = false, forceRecordVelocity = false) => {
324
- 'worklet';
325
-
326
- // Resolved in the body, not as a default-parameter initializer: the
327
- // worklets Babel plugin only captures closure variables referenced
328
- // inside the function body, so `= velocityTracking` up in the parameter
329
- // list would throw "Property 'velocityTracking' doesn't exist" on the
330
- // UI runtime.
331
- const recordVelocity = forceRecordVelocity || velocityTracking;
332
- // Number.isFinite (not a fused arithmetic gate): it also rejects
333
- // non-number types, which arithmetic would coerce past the gate and
334
- // into a UI-runtime throw from the binding's asNumber. The gate must
335
- // stay on this side of the bridge: the native binding drops non-finite
336
- // writes silently, and flipping the ownership SharedValues below for a
337
- // write native never applied would let the deliver listener record
338
- // values native has not observed — a later identical declarative write
339
- // then dedupes against them and is swallowed for good.
340
- if (disposed.value !== 0 || !Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(width) || !Number.isFinite(height) || !Number.isFinite(radius) || !Number.isFinite(contentTranslateX) || !Number.isFinite(contentTranslateY)) {
341
- return;
342
- }
343
- if (activeAnimationId.value !== 0) activeAnimationId.value = 0;
344
- if (ownership.value !== INTERACTIVE) ownership.value = INTERACTIVE;
345
- if (scalarsStale.value === 0) scalarsStale.value = 1;
346
- setPresentationHostFunction(driverId, x, y, width, height, radius, contentTranslateX, contentTranslateY, true, overridePendingAnimation, recordVelocity);
347
- };
348
- const setPresentationScalarsOnUI = (x, y, width, height, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius, nextCurveCode, contentTranslateX, contentTranslateY, contentScale, overridePendingAnimation = false, forceRecordVelocity = false) => {
349
- 'worklet';
350
-
351
- const values = [x, y, width, height, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius, nextCurveCode, contentTranslateX, contentTranslateY, contentScale];
352
- if (disposed.value !== 0 || !values.every(Number.isFinite) || nextCurveCode !== 0 && nextCurveCode !== 1 || contentScale <= 0) {
353
- return;
354
- }
355
- const recordVelocity = forceRecordVelocity || velocityTracking;
356
- if (PRESENTATION_PROTOCOL_VERSION !== 2 || !setPresentationV2HostFunction) {
357
- if (nextCurveCode === 0 && contentScale === 1 && topLeftRadius === topRightRadius && topLeftRadius === bottomRightRadius && topLeftRadius === bottomLeftRadius) {
358
- setScalarsOnUI(x, y, width, height, topLeftRadius, contentTranslateX, contentTranslateY, overridePendingAnimation, forceRecordVelocity);
359
- }
360
- return;
361
- }
362
- if (activeAnimationId.value !== 0) activeAnimationId.value = 0;
363
- if (ownership.value !== INTERACTIVE) ownership.value = INTERACTIVE;
364
- if (scalarsStale.value === 0) scalarsStale.value = 1;
365
- setPresentationV2HostFunction(driverId, x, y, width, height, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius, nextCurveCode, contentTranslateX, contentTranslateY, contentScale, true, overridePendingAnimation, recordVelocity);
366
- };
367
- const animateOnUI = (target, animation) => {
368
- 'worklet';
369
-
370
- // Cleanup owns the terminal state. Check it before validation so even an
371
- // invalid stale call cannot mint a native rejection id/completion against
372
- // a driver whose listener and registry entry are already gone.
373
- if (disposed.value !== 0) return 0;
374
- const canonicalTarget = canonicalizeClipPresentation(target);
375
- const canonicalFrom = animation.from === undefined ? undefined : canonicalizeClipPresentation(animation.from);
376
- const keyframeResult = animation.type === 'keyframes' ? canonicalTarget === null ? null : flattenFiniteKeyframes(canonicalTarget, animation) : null;
377
- if (canonicalTarget === null || animation.from !== undefined && canonicalFrom === null || animation.type === 'keyframes' && keyframeResult === null) {
378
- return rejectAnimationHostFunction(driverId);
379
- }
380
- const resolvedFrom = canonicalFrom ?? undefined;
381
- const v1Compatible = isV1Compatible(canonicalTarget) && (resolvedFrom === undefined || isV1Compatible(resolvedFrom)) && (animation.type !== 'keyframes' || (keyframeResult?.canonicalFrames.every(isV1Compatible) ?? false));
382
- // V1-representable plans deliberately stay on the original seven-scalar
383
- // bridge even when V2 is installed. Besides preserving bridge shape,
384
- // this retains V1's finite-value/clamping and host-normalization rules.
385
- const useV2 = !v1Compatible;
386
- if (!animationIsFinite(animation, useV2)) {
387
- return rejectAnimationHostFunction(driverId);
388
- }
389
- const hasV2Protocol = PRESENTATION_PROTOCOL_VERSION === 2 && snapshotCurrentV2HostFunction !== undefined && animateTimingV2HostFunction !== undefined && animateSpringV2HostFunction !== undefined && animateKeyframesV2HostFunction !== undefined;
390
- const hasAtomicFromMethod = resolvedFrom === undefined || (animation.type === 'timing' ? animateTimingFromV2HostFunction !== undefined : animation.type === 'spring' ? animateSpringFromV2HostFunction !== undefined : animateKeyframesFromV2HostFunction !== undefined);
391
- // This is intentionally before sampling or any ownership/group/
392
- // presentation mutation. New JS against old native must fail closed,
393
- // and an explicit V2 start must never degrade to set()+animate().
394
- if (useV2 && (!hasV2Protocol || !hasAtomicFromMethod)) {
395
- return rejectAnimationHostFunction(driverId);
396
- }
397
- if (useV2 && resolvedFrom !== undefined && animation.type === 'keyframes' && !clipPresentationEquals(keyframeResult?.canonicalFrames[0] ?? null, resolvedFrom)) {
398
- return rejectAnimationHostFunction(driverId);
399
- }
400
- const currentPresentation = canonicalizeClipPresentation(presentation.value) ?? initialPresentation;
401
- const sampledStart = resolvedFrom ?? (useV2 ? presentationFromNative(snapshotCurrentV2HostFunction(driverId), currentPresentation) : currentPresentation);
402
- // setPresentationScalars intentionally leaves the public SharedValue
403
- // stale to keep the hot path allocation-free. Validate an implicit
404
- // retarget against the sampled native-visible start, not that stale JS
405
- // value; otherwise a streamed curve transition cannot settle natively on
406
- // its new (now stable) curve.
407
- if (canonicalTarget.clip.curve !== sampledStart.clip.curve) {
408
- return rejectAnimationHostFunction(driverId);
409
- }
410
- if (useV2 && animation.type === 'spring' && !springScaleIsProvablyPositive(sampledStart, canonicalTarget, animation)) {
411
- return rejectAnimationHostFunction(driverId);
412
- }
413
- // Past the hook's cleanup the native entry is gone for good, and an
414
- // interactive start here would recreate it as a latch nothing can ever
415
- // start or cancel. Reject before any side effect — not via
416
- // rejectAnimation, which would mint an id and a completion for a driver
417
- // whose JS state is already detached. This is the "unsupported dispatch"
418
- // arm of the documented 0 contract.
419
- const from = resolvedFrom;
420
- if (from !== undefined && !useV2) {
421
- // Fused hot write: exactly setScalars(from…) issued immediately
422
- // before the handoff, so native's latest value — the animation
423
- // start below — is the caller's explicit presentation. Also
424
- // re-grabs from a running animation, which the implicit
425
- // interactive-start path would silently skip. Always records a
426
- // velocity sample regardless of velocityTracking: this is one write
427
- // per animateTo, not the per-frame stream the flag exists to keep
428
- // cheap, and an unrecorded seed would invalidate the history a
429
- // set()-driven drag just recorded (the tracker's coalesce and
430
- // identical-re-record rules were designed for exactly this write).
431
- setScalarsOnUI(from.clip.x, from.clip.y, from.clip.width, from.clip.height, from.clip.radius, from.contentTranslateX, from.contentTranslateY, true, true);
432
- }
433
-
434
- // After a setScalars hot write the SharedValue no longer matches what
435
- // is on screen; passing it as the start would snap the animation back.
436
- // Without an interactive start, native resolves the start from its own
437
- // latest value instead.
438
- const scalarsWereStale = scalarsStale.value !== 0;
439
- const hasInteractiveStart = ownership.value === INTERACTIVE && !scalarsWereStale;
440
- const start = canonicalizeClipPresentation(presentation.value) ?? initialPresentation;
441
- // V1 preserves the old optimistic SharedValue ownership transition.
442
- // V2 commits it only after native has atomically accepted the plan.
443
- if (!useV2) {
444
- ownership.value = NATIVE;
445
- presentation.value = canonicalTarget;
446
- scalarsStale.value = 0;
447
- }
448
- const dispatchStart = useV2 && from !== undefined ? from : start;
449
- const startClip = dispatchStart.clip;
450
- const targetClip = canonicalTarget.clip;
451
- // Reanimated stamps a parallel animation's t0 on this same runtime as
452
- // `__frameTimestamp || _getAnimationTimestamp()` (valueSetter.ts).
453
- // Capturing the identical value here and handing it to the integrator
454
- // keeps clip and content phase-locked in every branch — including a
455
- // start issued from CALLBACK_INPUT (batched gesture moves), where the
456
- // dispatching frame's stamp is EARLIER than the call: the native min()
457
- // anchor alone would adopt that frame stamp while Reanimated, outside
458
- // its rAF flush where __frameTimestamp is cleared, keeps the wall
459
- // clock — a lead lasting the whole animation. NaN when the globals are
460
- // absent (tests, non-worklets runtimes): native then falls back to its
461
- // own clock plus the min() anchor, the pre-stamp behavior. On iOS the
462
- // stamp is always NaN: Core Animation anchors at commit time and the
463
- // TurboModule discards the argument, so the capture would only cost.
464
- const workletGlobal = globalThis;
465
- const startedAtMs = !NEEDS_START_STAMP ? Number.NaN : workletGlobal.__frameTimestamp || (typeof workletGlobal._getAnimationTimestamp === 'function' ? workletGlobal._getAnimationTimestamp() : Number.NaN);
466
- let animationId;
467
- if (animation.type === 'timing' && useV2) {
468
- const [x1, y1, x2, y2] = animation.controlPoints;
469
- animationId = from !== undefined ? animateTimingFromV2HostFunction(driverId, startClip.x, startClip.y, startClip.width, startClip.height, startClip.topLeftRadius, startClip.topRightRadius, startClip.bottomRightRadius, startClip.bottomLeftRadius, curveCode(dispatchStart), dispatchStart.contentTranslateX, dispatchStart.contentTranslateY, dispatchStart.contentScale, targetClip.x, targetClip.y, targetClip.width, targetClip.height, targetClip.topLeftRadius, targetClip.topRightRadius, targetClip.bottomRightRadius, targetClip.bottomLeftRadius, curveCode(canonicalTarget), canonicalTarget.contentTranslateX, canonicalTarget.contentTranslateY, canonicalTarget.contentScale, animation.duration, x1, y1, x2, y2, reduceMotion, startedAtMs) : animateTimingV2HostFunction(driverId, hasInteractiveStart, startClip.x, startClip.y, startClip.width, startClip.height, startClip.topLeftRadius, startClip.topRightRadius, startClip.bottomRightRadius, startClip.bottomLeftRadius, curveCode(dispatchStart), dispatchStart.contentTranslateX, dispatchStart.contentTranslateY, dispatchStart.contentScale, targetClip.x, targetClip.y, targetClip.width, targetClip.height, targetClip.topLeftRadius, targetClip.topRightRadius, targetClip.bottomRightRadius, targetClip.bottomLeftRadius, curveCode(canonicalTarget), canonicalTarget.contentTranslateX, canonicalTarget.contentTranslateY, canonicalTarget.contentScale, animation.duration, x1, y1, x2, y2, reduceMotion, startedAtMs);
470
- } else if (animation.type === 'timing') {
471
- const [x1, y1, x2, y2] = animation.controlPoints;
472
- animationId = animateTimingHostFunction(driverId, hasInteractiveStart, startClip.x, startClip.y, startClip.width, startClip.height, startClip.radius, start.contentTranslateX, start.contentTranslateY, targetClip.x, targetClip.y, targetClip.width, targetClip.height, targetClip.radius, canonicalTarget.contentTranslateX, canonicalTarget.contentTranslateY, Math.max(0, animation.duration), x1, y1, x2, y2, reduceMotion, startedAtMs);
473
- } else if (animation.type === 'spring' && useV2) {
474
- const inheritVelocity = animation.initialVelocity === undefined || animation.initialVelocity === 'inherit';
475
- const mass = animation.mass ?? 1;
476
- const stiffness = animation.stiffness ?? 100;
477
- const damping = animation.damping ?? 10;
478
- const initialVelocity = inheritVelocity ? 0 : animation.initialVelocity;
479
- animationId = from !== undefined ? animateSpringFromV2HostFunction(driverId, startClip.x, startClip.y, startClip.width, startClip.height, startClip.topLeftRadius, startClip.topRightRadius, startClip.bottomRightRadius, startClip.bottomLeftRadius, curveCode(dispatchStart), dispatchStart.contentTranslateX, dispatchStart.contentTranslateY, dispatchStart.contentScale, targetClip.x, targetClip.y, targetClip.width, targetClip.height, targetClip.topLeftRadius, targetClip.topRightRadius, targetClip.bottomRightRadius, targetClip.bottomLeftRadius, curveCode(canonicalTarget), canonicalTarget.contentTranslateX, canonicalTarget.contentTranslateY, canonicalTarget.contentScale, mass, stiffness, damping, initialVelocity, inheritVelocity, reduceMotion, startedAtMs) : animateSpringV2HostFunction(driverId, hasInteractiveStart, startClip.x, startClip.y, startClip.width, startClip.height, startClip.topLeftRadius, startClip.topRightRadius, startClip.bottomRightRadius, startClip.bottomLeftRadius, curveCode(dispatchStart), dispatchStart.contentTranslateX, dispatchStart.contentTranslateY, dispatchStart.contentScale, targetClip.x, targetClip.y, targetClip.width, targetClip.height, targetClip.topLeftRadius, targetClip.topRightRadius, targetClip.bottomRightRadius, targetClip.bottomLeftRadius, curveCode(canonicalTarget), canonicalTarget.contentTranslateX, canonicalTarget.contentTranslateY, canonicalTarget.contentScale, mass, stiffness, damping, initialVelocity, inheritVelocity, reduceMotion, startedAtMs);
480
- } else if (animation.type === 'spring') {
481
- const inheritVelocity = animation.initialVelocity === undefined || animation.initialVelocity === 'inherit';
482
- animationId = animateSpringHostFunction(driverId, hasInteractiveStart, startClip.x, startClip.y, startClip.width, startClip.height, startClip.radius, start.contentTranslateX, start.contentTranslateY, targetClip.x, targetClip.y, targetClip.width, targetClip.height, targetClip.radius, canonicalTarget.contentTranslateX, canonicalTarget.contentTranslateY, Math.max(0.0001, animation.mass ?? 1), Math.max(0.0001, animation.stiffness ?? 100), Math.max(0, animation.damping ?? 10), inheritVelocity ? 0 : animation.initialVelocity, inheritVelocity, reduceMotion, startedAtMs);
483
- } else if (useV2) {
484
- const frames = keyframeResult.frames;
485
- animationId = from !== undefined ? animateKeyframesFromV2HostFunction(driverId, startClip.x, startClip.y, startClip.width, startClip.height, startClip.topLeftRadius, startClip.topRightRadius, startClip.bottomRightRadius, startClip.bottomLeftRadius, curveCode(dispatchStart), dispatchStart.contentTranslateX, dispatchStart.contentTranslateY, dispatchStart.contentScale, targetClip.x, targetClip.y, targetClip.width, targetClip.height, targetClip.topLeftRadius, targetClip.topRightRadius, targetClip.bottomRightRadius, targetClip.bottomLeftRadius, curveCode(canonicalTarget), canonicalTarget.contentTranslateX, canonicalTarget.contentTranslateY, canonicalTarget.contentScale, animation.duration, frames, reduceMotion, startedAtMs) : animateKeyframesV2HostFunction(driverId, hasInteractiveStart, startClip.x, startClip.y, startClip.width, startClip.height, startClip.topLeftRadius, startClip.topRightRadius, startClip.bottomRightRadius, startClip.bottomLeftRadius, curveCode(dispatchStart), dispatchStart.contentTranslateX, dispatchStart.contentTranslateY, dispatchStart.contentScale, targetClip.x, targetClip.y, targetClip.width, targetClip.height, targetClip.topLeftRadius, targetClip.topRightRadius, targetClip.bottomRightRadius, targetClip.bottomLeftRadius, curveCode(canonicalTarget), canonicalTarget.contentTranslateX, canonicalTarget.contentTranslateY, canonicalTarget.contentScale, animation.duration, frames, reduceMotion, startedAtMs);
486
- } else {
487
- animationId = animateKeyframesHostFunction(driverId, hasInteractiveStart, startClip.x, startClip.y, startClip.width, startClip.height, startClip.radius, start.contentTranslateX, start.contentTranslateY, targetClip.x, targetClip.y, targetClip.width, targetClip.height, targetClip.radius, canonicalTarget.contentTranslateX, canonicalTarget.contentTranslateY, Math.max(0, animation.duration),
488
- // Non-null here: the validation gate above rejected null before any
489
- // side effect.
490
- flattenV1Keyframes(keyframeResult.canonicalFrames, animation.frames.map(frame => frame.offset)), reduceMotion, startedAtMs);
491
- }
492
- if (animationId === 0) {
493
- // The 0 sentinel: off-main, invalid-id, or otherwise unsupported
494
- // dispatch. A missing driver is accepted when this call carries the
495
- // authoritative interactive start above; a start-less missing-state
496
- // request remains unsupported rather than inventing zero geometry.
497
- // Validation failures never get here: they reject before any side
498
- // effect, with a fresh id and one finished:false completion once the
499
- // native entry exists — in the pre-seed window rejectAnimation has no
500
- // entry and returns the bare 0 sentinel with no completion instead.
501
- if (!useV2) {
502
- ownership.value = INTERACTIVE;
503
- presentation.value = start;
504
- if (scalarsWereStale) scalarsStale.value = 1;
505
- activeAnimationId.value = 0;
506
- }
507
- return rejectAnimationHostFunction(driverId);
508
- }
509
- if (useV2) {
510
- ownership.value = NATIVE;
511
- presentation.value = canonicalTarget;
512
- scalarsStale.value = 0;
513
- }
514
- activeAnimationId.value = animationId;
515
- return animationId;
516
- };
517
- const cancelOnUI = (animationId = 0, behavior = 'current') => {
518
- 'worklet';
519
-
520
- // Past the hook's cleanup the native entry is gone; the call would fail
521
- // defined (handled=false) but it is the last driver.ui path that still
522
- // crosses into native after the tombstone. Guard it like the rest.
523
- const fallback = canonicalizeClipPresentation(presentation.value) ?? initialPresentation;
524
- if (disposed.value !== 0) return fallback;
525
- const values = PRESENTATION_PROTOCOL_VERSION === 2 && cancelAnimationV2HostFunction ? cancelAnimationV2HostFunction(driverId, animationId, behavior === 'target' ? 1 : 0) : cancelAnimationHostFunction(driverId, animationId, behavior === 'target' ? 1 : 0);
526
- if (values.length < 8 || values[0] !== 1) return fallback;
527
- const current = presentationFromNative(values, fallback, 1);
528
- activeAnimationId.value = 0;
529
- ownership.value = INTERACTIVE;
530
- scalarsStale.value = 0;
531
- seedPresentation(current);
532
- return current;
533
- };
534
- const driver = {
535
- kind: 'hybrid',
536
- presentation,
537
- ui: {
538
- beginInteraction() {
539
- 'worklet';
540
-
541
- if (isRNRuntime()) return uiOnly();
542
- return beginOnUI();
543
- },
544
- set(next) {
545
- 'worklet';
546
-
547
- if (isRNRuntime()) return uiOnly();
548
- setOnUI(next);
549
- },
550
- setScalars(x, y, width, height, radius, contentTranslateX, contentTranslateY) {
551
- 'worklet';
552
-
553
- if (isRNRuntime()) return uiOnly();
554
- setScalarsOnUI(x, y, width, height, radius, contentTranslateX, contentTranslateY);
555
- },
556
- setPresentationScalars(x, y, width, height, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius, nextCurveCode, contentTranslateX, contentTranslateY, contentScale) {
557
- 'worklet';
558
-
559
- if (isRNRuntime()) return uiOnly();
560
- setPresentationScalarsOnUI(x, y, width, height, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius, nextCurveCode, contentTranslateX, contentTranslateY, contentScale);
561
- },
562
- animateTo(target, animation) {
563
- 'worklet';
564
-
565
- if (isRNRuntime()) return uiOnly();
566
- return animateOnUI(target, animation);
567
- },
568
- cancel(animationId = 0, behavior = 'current') {
569
- 'worklet';
570
-
571
- if (isRNRuntime()) return uiOnly();
572
- return cancelOnUI(animationId, behavior);
573
- }
574
- },
575
- react: {
576
- beginInteraction() {
577
- const {
578
- requestId,
579
- promise
580
- } = createReactRequest(driverId);
581
- scheduleOnUI(() => {
582
- 'worklet';
583
-
584
- const result = beginOnUI();
585
- scheduleOnRN(resolveReactRequest, driverId, requestId, result);
586
- });
587
- return promise;
588
- },
589
- set(next) {
590
- // Teardown resolves (undefined) rather than rejects: `set` is a
591
- // documented fire-and-forget call, and a promise discarded with
592
- // `void` must not become an unhandled rejection at unmount.
593
- const {
594
- requestId,
595
- promise
596
- } = createReactRequest(driverId, false, {
597
- value: undefined
598
- });
599
- scheduleOnUI(() => {
600
- 'worklet';
601
-
602
- setOnUI(next);
603
- scheduleOnRN(resolveReactRequest, driverId, requestId, undefined);
604
- });
605
- return promise;
606
- },
607
- animateTo(target, animation) {
608
- // Teardown resolves with the documented 0 rejection sentinel (see
609
- // `set` above): the README's own example voids this promise.
610
- const {
611
- requestId,
612
- promise
613
- } = createReactRequest(driverId, true, {
614
- value: 0
615
- });
616
- scheduleOnUI(() => {
617
- 'worklet';
618
-
619
- const result = animateOnUI(target, animation);
620
- scheduleOnRN(resolveReactRequest, driverId, requestId, result, true);
621
- });
622
- return promise;
623
- },
624
- cancel(animationId = 0, behavior = 'current') {
625
- const {
626
- requestId,
627
- promise
628
- } = createReactRequest(driverId, true);
629
- scheduleOnUI(() => {
630
- 'worklet';
631
-
632
- const result = cancelOnUI(animationId, behavior);
633
- scheduleOnRN(resolveReactRequest, driverId, requestId, result, true);
634
- });
635
- return promise;
636
- }
637
- },
638
- __smoothClipHandle: {
639
- driverId,
640
- presentation,
641
- ownership,
642
- activeAnimationId,
643
- disposed
644
- }
645
- };
646
- setDriverState(driver, createDriverState(driverId, initialPresentation, presentation, callbackRef, activeAnimationId, ownership));
647
- driverRef.current = driver;
648
- }
649
- const driver = driverRef.current;
650
- useEffect(() => {
651
- const currentDriver = driverRef.current;
652
- if (!currentDriver) return undefined;
653
- const state = getDriverState(currentDriver);
654
- const {
655
- driverId
656
- } = state;
657
- // Re-attach on every effect run: StrictMode/<Activity> replay the effect
658
- // after its cleanup detached this state and destroyed the native driver.
659
- // The authoritative seed below recreates the native entry in that case.
660
- attachDriverState(state);
661
- const subscription = NativeSmoothClipModule.onClipAnimationComplete(result => {
662
- if (result.driverId !== driverId) return;
663
- synchronizeNativeCompletion(activeAnimationId, ownership, result.animationId, result.finished);
664
- completeNativeAnimation(driverId, result.animationId, result.finished);
665
- });
666
- scheduleOnUI((source, listenerId, nativeDriverId, owner, suppressed, active, stale, gone, setter, setterV2, protocolVersion) => {
667
- 'worklet';
668
-
669
- // First, before the listener or the seed: this effect run owns the
670
- // driver again, so a StrictMode/<Activity> replay must clear the
671
- // previous cleanup's tombstone or every later write would be rejected.
672
- gone.value = 0;
673
- let last = null;
674
- const deliver = next => {
675
- const canonical = canonicalizeClipPresentation(next);
676
- if (canonical === null || clipPresentationEquals(last, canonical)) {
677
- return;
678
- }
679
- // Record `last` only for values native has actually observed:
680
- // deliveries dropped during native ownership must stay eligible
681
- // for re-delivery once interactive ownership returns.
682
- if (owner.value !== INTERACTIVE) return;
683
- if (suppressed.value > 0) {
684
- suppressed.value -= 1;
685
- last = canonical;
686
- return;
687
- }
688
- if (protocolVersion !== 2 && !isV1Compatible(canonical)) {
689
- return;
690
- }
691
- last = canonical;
692
- if (stale.value !== 0) stale.value = 0;
693
- const {
694
- clip,
695
- contentTranslateX,
696
- contentTranslateY,
697
- contentScale
698
- } = canonical;
699
- // Declarative deliveries always record velocity samples — only the
700
- // setScalars hot path is gated by the velocityTracking option.
701
- if (protocolVersion === 2 && setterV2) {
702
- setterV2(nativeDriverId, clip.x, clip.y, clip.width, clip.height, clip.topLeftRadius, clip.topRightRadius, clip.bottomRightRadius, clip.bottomLeftRadius, curveCode(canonical), contentTranslateX, contentTranslateY, contentScale, false, false, true);
703
- } else {
704
- setter(nativeDriverId, clip.x, clip.y, clip.width, clip.height, clip.radius, contentTranslateX, contentTranslateY, false, false);
705
- }
706
- };
707
- source.addListener(listenerId, deliver);
708
- // Authoritative take-ownership seed. Creates the native entry before
709
- // any view mounts on the ordinary path, and revives a driver destroyed
710
- // by an effect-replay cleanup (StrictMode/<Activity>) on later runs.
711
- // An animation worklet can win the scheduling race and create a latch
712
- // first; Native ownership then marks that intent as newer, so this
713
- // passive seed must not clear its active id or replay the target value.
714
- const current = canonicalizeClipPresentation(source.value);
715
- if (owner.value === INTERACTIVE && current !== null) {
716
- active.value = 0;
717
- owner.value = INTERACTIVE;
718
- stale.value = 0;
719
- last = current;
720
- if (protocolVersion === 2 && setterV2) {
721
- setterV2(nativeDriverId, current.clip.x, current.clip.y, current.clip.width, current.clip.height, current.clip.topLeftRadius, current.clip.topRightRadius, current.clip.bottomRightRadius, current.clip.bottomLeftRadius, curveCode(current), current.contentTranslateX, current.contentTranslateY, current.contentScale, true, false, true);
722
- } else if (isV1Compatible(current)) {
723
- setter(nativeDriverId, current.clip.x, current.clip.y, current.clip.width, current.clip.height, current.clip.radius, current.contentTranslateX, current.contentTranslateY, true, false);
724
- }
725
- }
726
- }, presentation, driverId, driverId, ownership, suppressDeliveries, activeAnimationId, scalarsStale, disposed, setPresentationHostFunction, setPresentationV2HostFunction, PRESENTATION_PROTOCOL_VERSION);
727
- return () => {
728
- subscription.remove();
729
- rejectDriverRequests(driverId);
730
- detachDriverState(state);
731
- scheduleOnUI((source, listenerId, nativeDriverId, owner, active, stale, suppressed, gone, destroy) => {
732
- 'worklet';
733
-
734
- source.removeListener(listenerId);
735
- destroy(nativeDriverId);
736
- // StrictMode replays the effect with these same SharedValues. Reset
737
- // the UI-side tombstone only after native teardown so the next setup
738
- // can seed or animate without inheriting stale Native ownership.
739
- active.value = 0;
740
- owner.value = INTERACTIVE;
741
- stale.value = 0;
742
- // Defensive: seedPresentation now reclaims its own unspent credit at
743
- // the point of issue, so this should already be 0. Kept because the
744
- // failure mode — one declarative write silently dropped for the rest
745
- // of the next hook's life — is invisible until someone reports a
746
- // stale clip rect.
747
- suppressed.value = 0;
748
- // Last: the native entry is now erased, so until the next effect run
749
- // clears this, any call that would recreate it is rejected instead.
750
- gone.value = 1;
751
- }, presentation, driverId, driverId, ownership, activeAnimationId, scalarsStale, suppressDeliveries, disposed, destroyDriverHostFunction);
752
- };
753
- }, [activeAnimationId, disposed, ownership, presentation, scalarsStale, suppressDeliveries]);
754
- return driver;
755
- }
756
- //# sourceMappingURL=drivers.native.js.map