@rafaelaugusto/jolt 0.1.0-rc.13 → 0.1.0-rc.15

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 (238) hide show
  1. package/README.md +2 -2
  2. package/dist/animate.d.ts +2 -0
  3. package/dist/animate.d.ts.map +1 -1
  4. package/dist/animate.js +4 -4
  5. package/dist/animate.js.map +1 -1
  6. package/dist/core/drop.d.ts +34 -1
  7. package/dist/core/drop.d.ts.map +1 -1
  8. package/dist/core/drop.js +93 -8
  9. package/dist/core/drop.js.map +1 -1
  10. package/dist/core/layout-exit.d.ts +16 -0
  11. package/dist/core/layout-exit.d.ts.map +1 -0
  12. package/dist/core/layout-exit.js +29 -0
  13. package/dist/core/layout-exit.js.map +1 -0
  14. package/dist/core/list-changes.d.ts +52 -0
  15. package/dist/core/list-changes.d.ts.map +1 -0
  16. package/dist/core/list-changes.js +153 -0
  17. package/dist/core/list-changes.js.map +1 -0
  18. package/dist/core/loading.d.ts +125 -0
  19. package/dist/core/loading.d.ts.map +1 -0
  20. package/dist/core/loading.js +119 -0
  21. package/dist/core/loading.js.map +1 -0
  22. package/dist/core/numeric-map.d.ts +3 -0
  23. package/dist/core/numeric-map.d.ts.map +1 -1
  24. package/dist/core/numeric-map.js +1 -1
  25. package/dist/core/numeric-map.js.map +1 -1
  26. package/dist/core/pointer.d.ts +1 -1
  27. package/dist/core/preset-recipe.d.ts +2 -2
  28. package/dist/core/preset-recipe.d.ts.map +1 -1
  29. package/dist/core/preset-recipe.js +3 -1
  30. package/dist/core/preset-recipe.js.map +1 -1
  31. package/dist/core/scalar-program.d.ts +22 -0
  32. package/dist/core/scalar-program.d.ts.map +1 -0
  33. package/dist/core/scalar-program.js +47 -0
  34. package/dist/core/scalar-program.js.map +1 -0
  35. package/dist/core/scalar.d.ts +25 -0
  36. package/dist/core/scalar.d.ts.map +1 -0
  37. package/dist/core/scalar.js +84 -0
  38. package/dist/core/scalar.js.map +1 -0
  39. package/dist/core/text-pieces.d.ts +18 -0
  40. package/dist/core/text-pieces.d.ts.map +1 -0
  41. package/dist/core/text-pieces.js +43 -0
  42. package/dist/core/text-pieces.js.map +1 -0
  43. package/dist/drag.d.ts +7 -1
  44. package/dist/drag.d.ts.map +1 -1
  45. package/dist/drag.js +72 -37
  46. package/dist/drag.js.map +1 -1
  47. package/dist/drop-zones.d.ts +1 -1
  48. package/dist/drop-zones.d.ts.map +1 -1
  49. package/dist/drop-zones.js +2 -0
  50. package/dist/drop-zones.js.map +1 -1
  51. package/dist/engine.d.ts.map +1 -1
  52. package/dist/engine.js +7 -2
  53. package/dist/engine.js.map +1 -1
  54. package/dist/index.d.ts +4 -2
  55. package/dist/index.d.ts.map +1 -1
  56. package/dist/index.js +1 -0
  57. package/dist/index.js.map +1 -1
  58. package/dist/layout-exit.d.ts +19 -0
  59. package/dist/layout-exit.d.ts.map +1 -0
  60. package/dist/layout-exit.js +105 -0
  61. package/dist/layout-exit.js.map +1 -0
  62. package/dist/layout.d.ts +5 -0
  63. package/dist/layout.d.ts.map +1 -1
  64. package/dist/layout.js +50 -2
  65. package/dist/layout.js.map +1 -1
  66. package/dist/list-changes.d.ts +14 -0
  67. package/dist/list-changes.d.ts.map +1 -0
  68. package/dist/list-changes.js +251 -0
  69. package/dist/list-changes.js.map +1 -0
  70. package/dist/list-focus.d.ts +10 -0
  71. package/dist/list-focus.d.ts.map +1 -0
  72. package/dist/list-focus.js +103 -0
  73. package/dist/list-focus.js.map +1 -0
  74. package/dist/live-spring-driver.d.ts +2 -0
  75. package/dist/live-spring-driver.d.ts.map +1 -1
  76. package/dist/live-spring-driver.js +12 -0
  77. package/dist/live-spring-driver.js.map +1 -1
  78. package/dist/loader.d.ts +2 -1
  79. package/dist/loader.d.ts.map +1 -1
  80. package/dist/loader.js +17 -15
  81. package/dist/loader.js.map +1 -1
  82. package/dist/motion-value.d.ts +52 -19
  83. package/dist/motion-value.d.ts.map +1 -1
  84. package/dist/motion-value.js +615 -40
  85. package/dist/motion-value.js.map +1 -1
  86. package/dist/reveal.d.ts.map +1 -1
  87. package/dist/reveal.js +4 -3
  88. package/dist/reveal.js.map +1 -1
  89. package/dist/scroll-linked.d.ts +2 -1
  90. package/dist/scroll-linked.d.ts.map +1 -1
  91. package/dist/scroll-linked.js +101 -4
  92. package/dist/scroll-linked.js.map +1 -1
  93. package/dist/scroll-source.d.ts +18 -0
  94. package/dist/scroll-source.d.ts.map +1 -0
  95. package/dist/scroll-source.js +28 -0
  96. package/dist/scroll-source.js.map +1 -0
  97. package/dist/shared-layout/native.d.ts +1 -0
  98. package/dist/shared-layout/native.d.ts.map +1 -1
  99. package/dist/shared-layout/native.js +2 -0
  100. package/dist/shared-layout/native.js.map +1 -1
  101. package/dist/shimmer.d.ts.map +1 -1
  102. package/dist/shimmer.js +6 -4
  103. package/dist/shimmer.js.map +1 -1
  104. package/dist/value-track.d.ts +4 -0
  105. package/dist/value-track.d.ts.map +1 -1
  106. package/dist/value-track.js +3 -0
  107. package/dist/value-track.js.map +1 -1
  108. package/dist/vue/index.d.ts +45 -1
  109. package/dist/vue/index.d.ts.map +1 -1
  110. package/dist/vue/index.js +41 -2
  111. package/dist/vue/index.js.map +1 -1
  112. package/dist/web/drag.d.ts +8 -0
  113. package/dist/web/drag.d.ts.map +1 -1
  114. package/dist/web/drag.js +68 -14
  115. package/dist/web/drag.js.map +1 -1
  116. package/dist/web/drop-zones.d.ts +2 -2
  117. package/dist/web/drop-zones.d.ts.map +1 -1
  118. package/dist/web/drop-zones.js +21 -4
  119. package/dist/web/drop-zones.js.map +1 -1
  120. package/dist/web/layout-exit.d.ts +9 -0
  121. package/dist/web/layout-exit.d.ts.map +1 -0
  122. package/dist/web/layout-exit.js +148 -0
  123. package/dist/web/layout-exit.js.map +1 -0
  124. package/dist/web/layout.d.ts +25 -8
  125. package/dist/web/layout.d.ts.map +1 -1
  126. package/dist/web/layout.js +83 -2
  127. package/dist/web/layout.js.map +1 -1
  128. package/dist/web/list-changes.d.ts +41 -0
  129. package/dist/web/list-changes.d.ts.map +1 -0
  130. package/dist/web/list-changes.js +126 -0
  131. package/dist/web/list-changes.js.map +1 -0
  132. package/dist/web/loading.d.ts +2 -1
  133. package/dist/web/loading.d.ts.map +1 -1
  134. package/dist/web/loading.js +19 -15
  135. package/dist/web/loading.js.map +1 -1
  136. package/dist/web/pointer.d.ts +2 -0
  137. package/dist/web/pointer.d.ts.map +1 -1
  138. package/dist/web/pointer.js +13 -0
  139. package/dist/web/pointer.js.map +1 -1
  140. package/dist/web/presentation-copy.d.ts +9 -0
  141. package/dist/web/presentation-copy.d.ts.map +1 -0
  142. package/dist/web/presentation-copy.js +104 -0
  143. package/dist/web/presentation-copy.js.map +1 -0
  144. package/dist/web/public.d.ts +6 -1
  145. package/dist/web/public.d.ts.map +1 -1
  146. package/dist/web/public.js +1 -0
  147. package/dist/web/public.js.map +1 -1
  148. package/dist/web/reveal.d.ts.map +1 -1
  149. package/dist/web/reveal.js +4 -3
  150. package/dist/web/reveal.js.map +1 -1
  151. package/dist/web/runtime.d.ts +7 -7
  152. package/dist/web/runtime.d.ts.map +1 -1
  153. package/dist/web/runtime.js +85 -8
  154. package/dist/web/runtime.js.map +1 -1
  155. package/dist/web/scroll.d.ts +12 -1
  156. package/dist/web/scroll.d.ts.map +1 -1
  157. package/dist/web/scroll.js +25 -0
  158. package/dist/web/scroll.js.map +1 -1
  159. package/dist/web/shared-layout.d.ts.map +1 -1
  160. package/dist/web/shared-layout.js +2 -101
  161. package/dist/web/shared-layout.js.map +1 -1
  162. package/dist/web/spring-value.d.ts +3 -2
  163. package/dist/web/spring-value.d.ts.map +1 -1
  164. package/dist/web/spring-value.js +5 -5
  165. package/dist/web/spring-value.js.map +1 -1
  166. package/dist/web/text-effect.d.ts.map +1 -1
  167. package/dist/web/text-effect.js +10 -16
  168. package/dist/web/text-effect.js.map +1 -1
  169. package/dist/web/value.d.ts +43 -4
  170. package/dist/web/value.d.ts.map +1 -1
  171. package/dist/web/value.js +139 -6
  172. package/dist/web/value.js.map +1 -1
  173. package/dist/web/vue/index.d.ts +49 -2
  174. package/dist/web/vue/index.d.ts.map +1 -1
  175. package/dist/web/vue/index.js +45 -2
  176. package/dist/web/vue/index.js.map +1 -1
  177. package/package.json +13 -3
  178. package/platforms/android/java/org/rafaelaugusto/jolt/LiveSpring.java +52 -6
  179. package/platforms/android/java/org/rafaelaugusto/jolt/PointerInput.java +26 -4
  180. package/platforms/android/java/org/rafaelaugusto/jolt/ScalarProgram.java +76 -0
  181. package/platforms/android/java/org/rafaelaugusto/jolt/ScrollSource.java +86 -0
  182. package/platforms/android/java/org/rafaelaugusto/jolt/SharedLayoutFlight.java +8 -2
  183. package/platforms/ios/src/JoltLiveSpring.h +2 -0
  184. package/platforms/ios/src/JoltLiveSpring.m +52 -6
  185. package/platforms/ios/src/JoltScalarProgram.h +9 -0
  186. package/platforms/ios/src/JoltScalarProgram.m +80 -0
  187. package/platforms/ios/src/JoltScrollSource.h +11 -0
  188. package/platforms/ios/src/JoltScrollSource.m +57 -0
  189. package/platforms/ios/src/JoltSharedLayoutFlight.h +1 -0
  190. package/platforms/ios/src/JoltSharedLayoutFlight.m +7 -1
  191. package/platforms/ios/src/module.modulemap +1 -0
  192. package/src/android-plugin.d.ts +25 -0
  193. package/src/animate.ts +4 -4
  194. package/src/core/drop.ts +123 -5
  195. package/src/core/layout-exit.ts +43 -0
  196. package/src/core/list-changes.ts +206 -0
  197. package/src/core/loading.ts +172 -0
  198. package/src/core/numeric-map.ts +1 -1
  199. package/src/core/preset-recipe.ts +6 -2
  200. package/src/core/scalar-program.ts +69 -0
  201. package/src/core/scalar.ts +125 -0
  202. package/src/core/text-pieces.ts +52 -0
  203. package/src/drag.ts +81 -38
  204. package/src/drop-zones.ts +13 -3
  205. package/src/engine.ts +8 -3
  206. package/src/index.ts +8 -0
  207. package/src/ios-plugin.d.ts +17 -0
  208. package/src/layout-exit.ts +120 -0
  209. package/src/layout.ts +57 -1
  210. package/src/list-changes.ts +317 -0
  211. package/src/list-focus.ts +101 -0
  212. package/src/live-spring-driver.ts +18 -0
  213. package/src/loader.ts +23 -20
  214. package/src/motion-value.ts +689 -50
  215. package/src/reveal.ts +4 -3
  216. package/src/scroll-linked.ts +121 -5
  217. package/src/scroll-source.ts +60 -0
  218. package/src/shared-layout/native.ts +3 -0
  219. package/src/shimmer.ts +6 -4
  220. package/src/value-track.ts +7 -0
  221. package/src/vue/index.ts +51 -0
  222. package/src/web/drag.ts +70 -12
  223. package/src/web/drop-zones.ts +31 -6
  224. package/src/web/layout-exit.ts +179 -0
  225. package/src/web/layout.ts +103 -2
  226. package/src/web/list-changes.ts +194 -0
  227. package/src/web/loading.ts +38 -17
  228. package/src/web/pointer.ts +14 -0
  229. package/src/web/presentation-copy.ts +125 -0
  230. package/src/web/public.ts +17 -0
  231. package/src/web/reveal.ts +7 -8
  232. package/src/web/runtime.ts +116 -9
  233. package/src/web/scroll.ts +41 -0
  234. package/src/web/shared-layout.ts +3 -115
  235. package/src/web/spring-value.ts +7 -6
  236. package/src/web/text-effect.ts +20 -17
  237. package/src/web/value.ts +167 -12
  238. package/src/web/vue/index.ts +51 -0
@@ -1,5 +1,19 @@
1
+ import {
2
+ createNativeScrollSource,
3
+ nativeScrollAvailable,
4
+ type NativeScrollSource,
5
+ } from './scroll-source.js'
6
+ import {
7
+ scalarOperations,
8
+ type ScalarOperation,
9
+ type ScalarOperations,
10
+ type ScalarSample,
11
+ } from './core/scalar.js'
12
+ import { scalarProgram, type ScalarInstruction } from './core/scalar-program.js'
13
+ import { numericMap } from './core/numeric-map.js'
1
14
  import {
2
15
  Application,
16
+ isIOS,
3
17
  Color,
4
18
  ScrollView,
5
19
  type EventData,
@@ -62,7 +76,12 @@ import {
62
76
  import type { KeyframeProperty, Target } from './types.js'
63
77
  import { playbackSource, type PlaybackAnimation, type PlaybackClock } from './playback.js'
64
78
  import { TimedAnimation } from './timed-animation.js'
65
- import { compositionSpan, valueTrack } from './value-track.js'
79
+ import {
80
+ compositionSpan,
81
+ valueTrack,
82
+ sampledValueTrack,
83
+ type ValuePart,
84
+ } from './value-track.js'
66
85
  import {
67
86
  ValueObservers,
68
87
  type SubscribeOptions,
@@ -80,7 +99,10 @@ export type ValueAnimationOptions = Transition & { layer?: string }
80
99
  declare const motionValueBrand: unique symbol
81
100
 
82
101
  /** A number derived from a motion value, ready to bind to a view. */
83
- export interface DerivedValue {
102
+ export interface DerivedValue extends ScalarOperations<DerivedValue> {
103
+ getVelocity(): number
104
+ readonly disposed: boolean
105
+ dispose(): void
84
106
  /** The value now, while it animates too. */
85
107
  get(): number
86
108
  /** Observe changes in JavaScript on the native frame clock. Unsubscribe when finished. */
@@ -137,31 +159,114 @@ type Presentation = Pick<Run, 'track' | 'base' | 'span' | 'startedAt' | 'clock'>
137
159
 
138
160
  const bound = new WeakMap<View, Map<Property, Binding>>()
139
161
 
162
+ interface Derived extends ScalarOperations<DerivedValue> {}
140
163
  class Derived {
164
+ private disposedMapping = false
165
+ private frozenMapping: number | undefined
166
+ private releases = new Set<() => void>()
167
+ private readonly endings = new Set<() => void>()
168
+ get disposed(): boolean {
169
+ return this.disposedMapping || (this.source?.disposed ?? false)
170
+ }
171
+ onDispose(callback: () => void) {
172
+ this.endings.add(callback)
173
+ return () => {
174
+ this.endings.delete(callback)
175
+ }
176
+ }
177
+ dispose() {
178
+ this.finishDerived()
179
+ }
180
+ protected finishDerived() {
181
+ if (this.disposedMapping) return
182
+ if (this.source) this.frozenMapping = this.read(this.root.raw())
183
+ this.disposedMapping = true
184
+ for (const stop of this.releases) stop()
185
+ this.releases.clear()
186
+ for (const binding of [...this.root.bindings])
187
+ if (binding.source === this) release(binding)
188
+ for (const callback of [...this.endings]) callback()
189
+ this.endings.clear()
190
+ }
191
+ getVelocity(): number {
192
+ if (this.color || this.disposed) return 0
193
+ let value = this.root.raw(),
194
+ velocity = this.root.rawVelocity()
195
+ for (const map of this.mappings) {
196
+ const last = map.input.length - 1
197
+ if (map.clamp && value <= map.input[0]!) {
198
+ value = map.output[0]!
199
+ velocity = 0
200
+ continue
201
+ }
202
+ if (map.clamp && value >= map.input[last]!) {
203
+ value = map.output[last]!
204
+ velocity = 0
205
+ continue
206
+ }
207
+ let i = 0
208
+ while (i < last - 1 && value > map.input[i + 1]!) i++
209
+ const span = map.input[i + 1]! - map.input[i]!,
210
+ slope = span > 0 ? (map.output[i + 1]! - map.output[i]!) / span : 0
211
+ const fraction =
212
+ span > 0 ? (value - map.input[i]!) / span : value < map.input[i]! ? 0 : 1
213
+ value = map.output[i]! + (map.output[i + 1]! - map.output[i]!) * fraction
214
+ velocity *= slope
215
+ }
216
+ return velocity
217
+ }
141
218
  constructor(
142
219
  private readonly source: Root | undefined,
143
220
  readonly read: (value: number) => number,
144
221
  readonly color: boolean,
145
222
  readonly mappings: readonly LiveMapping[] = [],
146
- ) {}
223
+ ) {
224
+ Object.assign(
225
+ this,
226
+ scalarOperations<DerivedValue>((op, operand, maximum) => {
227
+ if (this.color || this.disposed)
228
+ throw new TypeError('Scalar operations require a live numeric source.')
229
+ if (
230
+ operand !== undefined &&
231
+ !(operand instanceof Derived) &&
232
+ (typeof operand !== 'number' || !Number.isFinite(operand))
233
+ )
234
+ throw new TypeError('Expected a finite number or a Jolt numeric value.')
235
+ return new GraphRoot(
236
+ this,
237
+ op,
238
+ operand as Derived | number | undefined,
239
+ maximum,
240
+ ) as unknown as DerivedValue
241
+ }),
242
+ )
243
+ }
244
+
245
+ get depth(): number {
246
+ return this.source ? this.source.depth + this.mappings.length : 0
247
+ }
147
248
 
148
249
  get root(): Root {
149
250
  return this.source ?? (this as unknown as Root)
150
251
  }
151
252
 
152
253
  get(): number | Color {
153
- const value = this.read(this.root.raw())
254
+ const value = this.frozenMapping ?? this.read(this.root.raw())
154
255
  return this.color ? new Color(value) : value
155
256
  }
156
257
 
157
258
  subscribe(listener: ValueListener<number | Color>, options?: SubscribeOptions) {
158
- const release = this.root.retainConsumer()
259
+ if (this.disposed) throw new Error('The value is disposed.')
260
+ const release = this.root.retainObservation()
159
261
  try {
160
262
  const stop = this.root.observers.subscribe(this.read, this.color, listener, options)
161
- return () => {
263
+ const cleanup = () => {
162
264
  stop()
163
265
  release()
266
+ this.releases.delete(cleanup)
164
267
  }
268
+ this.releases.add(cleanup)
269
+ return cleanup
165
270
  } catch (error) {
166
271
  release()
167
272
  throw error
@@ -174,6 +279,7 @@ class Derived {
174
279
  options?: MapOptions,
175
280
  ): Derived {
176
281
  if (this.color) throw new TypeError('A color cannot be mapped again.')
282
+ if (this.disposed) throw new Error('The value is disposed.')
177
283
  const map = mapping(input, output, options)
178
284
  const read = this.read
179
285
  return new Derived(this.root, (value) => map.read(read(value)), map.color, [
@@ -188,7 +294,49 @@ class Derived {
188
294
  }
189
295
  }
190
296
 
297
+ function retainAll<T>(
298
+ values: readonly T[],
299
+ retain: (value: T) => () => void,
300
+ ): (() => void)[] {
301
+ const releases: (() => void)[] = []
302
+ try {
303
+ for (const value of values) releases.push(retain(value))
304
+ return releases
305
+ } catch (error) {
306
+ for (const release of releases.reverse()) release()
307
+ throw error
308
+ }
309
+ }
191
310
  class Root extends Derived {
311
+ private observations = 0
312
+ readonly observationListeners = new Set<() => void>()
313
+ get observed() {
314
+ return this.observations > 0
315
+ }
316
+ protected observationDemandChanged() {
317
+ for (const callback of [...this.observationListeners]) callback()
318
+ }
319
+ retainObservation() {
320
+ if (this.disposed) throw new Error('The value is disposed.')
321
+ this.observations++
322
+ let release: () => void
323
+ try {
324
+ this.observationDemandChanged()
325
+ release = this.retainConsumer()
326
+ } catch (error) {
327
+ this.observations--
328
+ this.observationDemandChanged()
329
+ throw error
330
+ }
331
+ let ended = false
332
+ return () => {
333
+ if (ended) return
334
+ ended = true
335
+ release()
336
+ this.observations--
337
+ this.observationDemandChanged()
338
+ }
339
+ }
192
340
  private consumers = 0
193
341
  readonly demandListeners = new Set<(active: boolean) => void>()
194
342
  get demanded() {
@@ -242,6 +390,42 @@ class Root extends Derived {
242
390
  return result
243
391
  }
244
392
 
393
+ rawVelocity(): number {
394
+ return this.disposed
395
+ ? 0
396
+ : [...this.runs.keys()].reduce((sum, key) => sum + this.channelVelocity(key), 0)
397
+ }
398
+ snapshot(): { rest: number; parts: ValuePart[] } {
399
+ const now = getDriver().now()
400
+ let rest = 0
401
+ const parts: ValuePart[] = []
402
+ for (const [key, value] of this.rests) if (!this.runs.has(key)) rest += value
403
+ for (const [key, run] of this.runs) {
404
+ if (run.animation.paused) rest += this.part(key, now)
405
+ else
406
+ parts.push({
407
+ track: run.track,
408
+ base: run.base,
409
+ span: run.span,
410
+ elapsed: this.runTime(run, now),
411
+ })
412
+ }
413
+ return { rest, parts }
414
+ }
415
+ override dispose() {
416
+ if (this.disposed) return
417
+ this.stop()
418
+ for (const binding of [...this.bindings]) release(binding)
419
+ this.cancelPresentation?.()
420
+ this.composer?.dispose()
421
+ this.composer = undefined
422
+ this.observers.clear()
423
+ super.dispose()
424
+ for (const listener of [...this.listeners]) listener()
425
+ this.listeners.clear()
426
+ this.demandListeners.clear()
427
+ }
428
+
245
429
  /** Drag input owns only the base; bounds and snap points stay in base units. */
246
430
  base(): number {
247
431
  return this.part('')
@@ -261,13 +445,16 @@ class Root extends Derived {
261
445
  /** Internal development snapshot; reading it installs no observers. */
262
446
  inspect() {
263
447
  return {
264
- velocity: [...this.runs.keys()].reduce((sum, key) => sum + this.velocity(key), 0),
448
+ velocity: [...this.runs.keys()].reduce(
449
+ (sum, key) => sum + this.channelVelocity(key),
450
+ 0,
451
+ ),
265
452
  resources:
266
453
  this.bindings.size + this.listeners.size + this.observers.size + this.runs.size,
267
454
  }
268
455
  }
269
456
 
270
- private velocity(key: string): number {
457
+ private channelVelocity(key: string): number {
271
458
  const run = this.runs.get(key)
272
459
  if (!run || run.animation.paused) return 0
273
460
  return run.span * run.track.velocityAt(this.runTime(run))
@@ -279,6 +466,7 @@ class Root extends Derived {
279
466
  ): PlaybackAnimation {
280
467
  const options: ValueAnimationOptions =
281
468
  typeof transition === 'string' ? springOf(transition) : transition
469
+ if (this.disposed) throw new Error('The value is disposed.')
282
470
  const key = options.layer === undefined ? '' : layerName(options.layer)
283
471
  const from = this.part(key)
284
472
  const frames = (typeof target === 'number' ? [null, target] : [...target]).map(
@@ -290,7 +478,7 @@ class Root extends Derived {
290
478
  const resolved = resolveTransition(options, 'x', frames.length)
291
479
  const base = Math.min(...frames)
292
480
  const span = Math.max(...frames) - base
293
- const velocity = this.velocity(key)
481
+ const velocity = this.channelVelocity(key)
294
482
  if (resolved.curve.type === 'gravity') {
295
483
  // Acceleration and velocity belong to the value's units, not normalized progress.
296
484
  const track = createTrack('x', frames, resolved, velocity)
@@ -375,6 +563,7 @@ class Root extends Derived {
375
563
  }
376
564
 
377
565
  set(value: number) {
566
+ if (this.disposed) throw new Error('The value is disposed.')
378
567
  if (!Number.isFinite(value))
379
568
  throw new RangeError(`A motion value takes a finite number, got ${value}.`)
380
569
  for (const key of [...this.runs.keys()]) this.halt(key)
@@ -438,7 +627,7 @@ class Root extends Derived {
438
627
  }
439
628
 
440
629
  /** Every run as a native input on its own clock; rests and paused runs stand still. */
441
- private inputs(now: number) {
630
+ inputs(now: number) {
442
631
  let rest = 0
443
632
  for (const [key, value] of this.rests) if (!this.runs.has(key)) rest += value
444
633
  const inputs: LiveInput[] = []
@@ -631,6 +820,16 @@ export interface SpringValue extends DerivedValue {
631
820
  }
632
821
 
633
822
  class SpringRoot extends Root {
823
+ private observerSource?: () => void
824
+ protected override observationDemandChanged() {
825
+ if (this.observed && !this.observerSource)
826
+ this.observerSource = this.input.root.retainObservation()
827
+ else if (!this.observed) {
828
+ this.observerSource?.()
829
+ this.observerSource = undefined
830
+ }
831
+ super.observationDemandChanged()
832
+ }
634
833
  private releaseDemand: (() => void) | undefined
635
834
  externalActive = false
636
835
  protected override demandChanged() {
@@ -699,13 +898,15 @@ class SpringRoot extends Root {
699
898
  if (options.bounce !== undefined && !Number.isFinite(options.bounce))
700
899
  throw new RangeError('springValue bounce must be finite.')
701
900
  super(initial)
702
- this.node = liveSpringDriver().create(initial, physics, instant || reducedMotion())
703
901
  const root = source.root
902
+ const graphLease =
903
+ root instanceof GraphRoot && root.live ? root.nativeLease() : undefined
904
+ this.node = liveSpringDriver().create(initial, physics, instant || reducedMotion())
704
905
  try {
705
906
  let update: () => void
706
- if (root instanceof SpringRoot) {
907
+ if (root instanceof SpringRoot || graphLease) {
707
908
  if (root.disposed) throw new Error('Cannot follow a disposed spring value.')
708
- this.node.follow(root.node, source.mappings)
909
+ this.node.follow(graphLease?.node ?? (root as SpringRoot).node, source.mappings)
709
910
  update = () => this.notify()
710
911
  } else {
711
912
  const playback = valuePlayback(source as unknown as DerivedValue)
@@ -745,6 +946,7 @@ class SpringRoot extends Root {
745
946
  root.listeners.add(update)
746
947
  this.releaseSource = () => {
747
948
  root.listeners.delete(update)
949
+ graphLease?.release()
748
950
  }
749
951
  this.releaseReduced = onReducedMotion((reduced) => {
750
952
  this.node.reduced(instant || reduced)
@@ -772,7 +974,7 @@ class SpringRoot extends Root {
772
974
  get moving(): boolean {
773
975
  return this.running
774
976
  }
775
- get disposed(): boolean {
977
+ override get disposed(): boolean {
776
978
  return this.ended
777
979
  }
778
980
  override get paused(): boolean {
@@ -795,8 +997,11 @@ class SpringRoot extends Root {
795
997
  this.demandChanged()
796
998
  this.notify()
797
999
  }
798
- getVelocity(): number {
799
- return this.ended ? 0 : this.node.velocity()
1000
+ override getVelocity(): number {
1001
+ return this.rawVelocity()
1002
+ }
1003
+ override rawVelocity(): number {
1004
+ return this.ended || this.held || this.background ? 0 : this.node.velocity()
800
1005
  }
801
1006
  override plan(): Plan {
802
1007
  throw new TypeError(
@@ -807,17 +1012,20 @@ class SpringRoot extends Root {
807
1012
  this.observers.changed(this.running)
808
1013
  for (const listener of [...this.listeners]) listener()
809
1014
  }
810
- dispose() {
1015
+ override dispose() {
811
1016
  if (this.ended) return
812
1017
  this.ended = true
813
1018
  this.demandChanged()
814
1019
  this.releaseSource()
1020
+ this.observerSource?.()
1021
+ this.observerSource = undefined
815
1022
  this.releaseReduced()
816
1023
  Application.off(Application.suspendEvent, this.suspend)
817
1024
  Application.off(Application.resumeEvent, this.resume)
818
1025
  for (const binding of [...this.bindings]) release(binding)
819
1026
  this.node.dispose()
820
1027
  this.observers.clear()
1028
+ this.finishDerived()
821
1029
  for (const listener of [...this.listeners]) listener()
822
1030
  this.listeners.clear()
823
1031
  }
@@ -858,6 +1066,299 @@ export function liveValueInput(value: DerivedValue) {
858
1066
  }
859
1067
  }
860
1068
 
1069
+ /** Compile finite sources once; live sources stay in the native evaluator's ordered frame batch. */
1070
+ class GraphRoot extends Root {
1071
+ readonly level: number
1072
+ readonly sources: Root[]
1073
+ readonly program: ScalarInstruction[]
1074
+ readonly live: boolean
1075
+ private readonly evaluate: ReturnType<typeof scalarProgram>
1076
+ private stops: (() => void)[] = []
1077
+ private demandStops: (() => void)[] = []
1078
+ private observerStops: (() => void)[] = []
1079
+ private graphNode?: LiveNode
1080
+ private bridges: { root: Root; node: LiveNode }[] = []
1081
+ private leases = 0
1082
+ private cached?: { track: Track; started: number }
1083
+ private last: ScalarSample
1084
+ private readonly suspendGraph = () => this.graphNode?.paused(true)
1085
+ private readonly resumeGraph = () => this.graphNode?.paused(false)
1086
+ constructor(
1087
+ readonly left: Derived,
1088
+ readonly operation: ScalarOperation,
1089
+ readonly right?: Derived | number,
1090
+ readonly maximum?: number,
1091
+ ) {
1092
+ const sources = [...new Set([left, ...(right instanceof Derived ? [right] : [])])]
1093
+ if (sources.some((source) => source.color || source.disposed || source.depth >= 32))
1094
+ throw new TypeError('Scalar operations need live numeric sources within 32 levels.')
1095
+ const graph = compileGraph(left, operation, right, maximum)
1096
+ const evaluate = scalarProgram(graph.program, graph.sources.length)
1097
+ const initial = evaluate(
1098
+ graph.sources.map((root) => ({ value: root.raw(), velocity: root.rawVelocity() })),
1099
+ )
1100
+ super(initial.value)
1101
+ this.last = initial
1102
+ this.sources = graph.sources
1103
+ this.program = graph.program
1104
+ this.evaluate = evaluate
1105
+ this.level = Math.max(...sources.map((source) => source.depth)) + 1
1106
+ this.live = this.sources.some((root) => root instanceof SpringRoot)
1107
+ const update = () => {
1108
+ if (this.disposed) return
1109
+ if (
1110
+ sources.some((source) => source.disposed) ||
1111
+ this.sources.some((root) => root.disposed)
1112
+ ) {
1113
+ this.dispose()
1114
+ return
1115
+ }
1116
+ this.cached = undefined
1117
+ for (const { root, node } of this.bridges) {
1118
+ const snapshot = root.inputs(getDriver().now())
1119
+ node.inputs(snapshot.rest, snapshot.inputs)
1120
+ }
1121
+ this.changed()
1122
+ }
1123
+ for (const root of this.sources) {
1124
+ root.listeners.add(update)
1125
+ this.stops.push(() => root.listeners.delete(update))
1126
+ }
1127
+ for (const source of sources) this.stops.push(source.onDispose(() => this.dispose()))
1128
+ Application.on(Application.suspendEvent, this.suspendGraph)
1129
+ Application.on(Application.resumeEvent, this.resumeGraph)
1130
+ }
1131
+ override get depth() {
1132
+ return this.level ?? 0
1133
+ }
1134
+ override raw() {
1135
+ if (this.disposed) return this.last.value
1136
+ return this.evaluate(
1137
+ this.sources.map((root) => ({ value: root.raw(), velocity: root.rawVelocity() })),
1138
+ ).value
1139
+ }
1140
+ override rawVelocity() {
1141
+ return this.disposed
1142
+ ? 0
1143
+ : this.evaluate(
1144
+ this.sources.map((root) => ({
1145
+ value: root.raw(),
1146
+ velocity: root.rawVelocity(),
1147
+ })),
1148
+ ).velocity
1149
+ }
1150
+ override get running() {
1151
+ return !this.disposed && this.sources.some((root) => root.running)
1152
+ }
1153
+ protected override demandChanged() {
1154
+ const active = !this.disposed && (this.demanded || this.leases > 0)
1155
+ if (active && !this.demandStops.length)
1156
+ this.demandStops = retainAll(this.sources, (root) => root.retainConsumer())
1157
+ else if (!active) {
1158
+ for (const stop of this.demandStops) stop()
1159
+ this.demandStops = []
1160
+ }
1161
+ super.demandChanged()
1162
+ }
1163
+ protected override observationDemandChanged() {
1164
+ if (this.observed && !this.observerStops.length)
1165
+ this.observerStops = retainAll(this.sources, (root) => root.retainObservation())
1166
+ else if (!this.observed) {
1167
+ for (const stop of this.observerStops) stop()
1168
+ this.observerStops = []
1169
+ }
1170
+ super.observationDemandChanged()
1171
+ }
1172
+ private timeline() {
1173
+ return this.sources.map((root) => root.snapshot())
1174
+ }
1175
+ override composerNode(): LiveNode | undefined {
1176
+ const snapshots = this.timeline()
1177
+ if (
1178
+ !this.live &&
1179
+ !compositionSpan(snapshots.flatMap((snapshot) => snapshot.parts))?.windowed
1180
+ )
1181
+ return undefined
1182
+ return this.ensureNode()
1183
+ }
1184
+ nativeLease() {
1185
+ const node = this.ensureNode()
1186
+ this.leases++
1187
+ try {
1188
+ this.demandChanged()
1189
+ } catch (error) {
1190
+ this.leases--
1191
+ this.demandChanged()
1192
+ this.settle()
1193
+ throw error
1194
+ }
1195
+ let released = false
1196
+ return {
1197
+ node,
1198
+ release: () => {
1199
+ if (released) return
1200
+ released = true
1201
+ this.leases--
1202
+ this.demandChanged()
1203
+ this.settle()
1204
+ },
1205
+ }
1206
+ }
1207
+ private ensureNode(): LiveNode {
1208
+ if (this.graphNode) return this.graphNode
1209
+ const driver = liveSpringDriver()
1210
+ try {
1211
+ const inputs = this.sources.map((root) => {
1212
+ if (root instanceof SpringRoot) return root.node
1213
+ const node = driver.create(root.raw(), stillPhysics, true)
1214
+ this.bridges.push({ root, node })
1215
+ const snapshot = root.inputs(getDriver().now())
1216
+ node.inputs(snapshot.rest, snapshot.inputs)
1217
+ return node
1218
+ })
1219
+ const node = driver.create(this.raw(), stillPhysics, true)
1220
+ this.graphNode = node
1221
+ if (!node.compute)
1222
+ throw new Error('Combined live values need rebuilt Jolt native classes.')
1223
+ node.compute(inputs, this.program)
1224
+ return node
1225
+ } catch (error) {
1226
+ this.clearNode()
1227
+ throw error
1228
+ }
1229
+ }
1230
+ private clearNode() {
1231
+ this.graphNode?.dispose()
1232
+ this.graphNode = undefined
1233
+ for (const { node } of this.bridges) node.dispose()
1234
+ this.bridges = []
1235
+ }
1236
+ override settle() {
1237
+ if (!this.demanded && !this.leases) this.clearNode()
1238
+ }
1239
+ private presentationTrack() {
1240
+ if (this.cached) return this.cached
1241
+ const snapshots = this.timeline()
1242
+ const at = (time: number) =>
1243
+ this.evaluate(
1244
+ snapshots.map((snapshot) => ({
1245
+ value: snapshot.parts.reduce(
1246
+ (sum, part) =>
1247
+ sum + part.base + part.span * part.track.valueAt(part.elapsed + time),
1248
+ snapshot.rest,
1249
+ ),
1250
+ velocity: snapshot.parts.reduce(
1251
+ (sum, part) => sum + part.span * part.track.velocityAt(part.elapsed + time),
1252
+ 0,
1253
+ ),
1254
+ })),
1255
+ ).value
1256
+ const { track } = sampledValueTrack(
1257
+ snapshots.flatMap((snapshot) => snapshot.parts),
1258
+ at,
1259
+ )
1260
+ return (this.cached = { track, started: getDriver().now() })
1261
+ }
1262
+ override plan(binding: Binding): Plan {
1263
+ const source = this.presentationTrack().track
1264
+ const at = (value: number) =>
1265
+ isDecorationProperty(binding.property)
1266
+ ? clampDecoration(binding.property, binding.read(value))
1267
+ : binding.read(value)
1268
+ return {
1269
+ property: binding.property,
1270
+ keyframes: [],
1271
+ transition: still,
1272
+ track: () => {
1273
+ if (
1274
+ reducedMotion() &&
1275
+ binding.property !== 'opacity' &&
1276
+ !isColorProperty(binding.property)
1277
+ )
1278
+ return instantTrack(binding.property, at(source.to))
1279
+ return {
1280
+ ...source,
1281
+ property: binding.property,
1282
+ values: source.values.map(at),
1283
+ to: at(source.to),
1284
+ valueAt: (time: number) => at(source.valueAt(time)),
1285
+ velocityAt: (time: number) =>
1286
+ (at(source.valueAt(time + 0.5)) - at(source.valueAt(time - 0.5))) * 1000,
1287
+ }
1288
+ },
1289
+ }
1290
+ }
1291
+ override elapsed() {
1292
+ return getDriver().now() - this.presentationTrack().started
1293
+ }
1294
+ override captureTime() {
1295
+ const start = this.presentationTrack().started
1296
+ return () => getDriver().now() - start
1297
+ }
1298
+ override dispose() {
1299
+ if (this.disposed) return
1300
+ this.last = { value: this.raw(), velocity: 0 }
1301
+ for (const stop of this.stops) stop()
1302
+ this.stops = []
1303
+ for (const stop of this.observerStops) stop()
1304
+ this.observerStops = []
1305
+ for (const stop of this.demandStops) stop()
1306
+ this.demandStops = []
1307
+ Application.off(Application.suspendEvent, this.suspendGraph)
1308
+ Application.off(Application.resumeEvent, this.resumeGraph)
1309
+ super.dispose()
1310
+ this.clearNode()
1311
+ }
1312
+ }
1313
+
1314
+ function compileGraph(
1315
+ left: Derived,
1316
+ operation: ScalarOperation,
1317
+ right?: Derived | number,
1318
+ maximum?: number,
1319
+ ) {
1320
+ const sources: Root[] = [],
1321
+ program: ScalarInstruction[] = []
1322
+ const known = new Map<Derived, number>()
1323
+ const append = (node: ScalarInstruction) => {
1324
+ program.push(node)
1325
+ return program.length - 1
1326
+ }
1327
+ const walk = (value: Derived | number): number => {
1328
+ if (typeof value === 'number') return append({ op: 'constant', value })
1329
+ const cached = known.get(value)
1330
+ if (cached !== undefined) return cached
1331
+ const root = value.root
1332
+ let index: number
1333
+ if (root instanceof GraphRoot) {
1334
+ const a = walk(root.left),
1335
+ b = root.right === undefined ? undefined : walk(root.right)
1336
+ index = append({ op: root.operation, a, b, maximum: root.maximum })
1337
+ } else {
1338
+ let input = sources.indexOf(root)
1339
+ if (input < 0) {
1340
+ input = sources.length
1341
+ sources.push(root)
1342
+ }
1343
+ index = append({ op: 'input', a: input })
1344
+ }
1345
+ for (const map of value.mappings)
1346
+ index = append({
1347
+ op: 'map',
1348
+ a: index,
1349
+ input: map.input,
1350
+ output: map.output,
1351
+ clamp: map.clamp,
1352
+ })
1353
+ known.set(value, index)
1354
+ return index
1355
+ }
1356
+ const a = walk(left),
1357
+ b = right === undefined ? undefined : walk(right)
1358
+ append({ op: operation, a, b, maximum })
1359
+ return { sources, program }
1360
+ }
1361
+
861
1362
  function layerName(name: string): string {
862
1363
  if (typeof name !== 'string' || !name.length)
863
1364
  throw new TypeError('A layer is named by a nonempty string.')
@@ -885,7 +1386,11 @@ export function motionValue(initial = 0): MotionValue {
885
1386
  }
886
1387
 
887
1388
  export function isMotionValue(target: unknown): target is MotionValue {
888
- return target instanceof Root && !(target instanceof SpringRoot)
1389
+ return (
1390
+ target instanceof Root &&
1391
+ !(target instanceof SpringRoot) &&
1392
+ !(target instanceof GraphRoot)
1393
+ )
889
1394
  }
890
1395
 
891
1396
  export function asRoot(value: MotionValue): Root {
@@ -907,6 +1412,8 @@ export function bind(target: Target | readonly Target[], bindings: Bindings): ()
907
1412
  const entries: [Property, Derived][] = []
908
1413
  for (const [key, source] of Object.entries(bindings)) {
909
1414
  if (source === undefined) continue
1415
+ if (source instanceof Derived && source.disposed)
1416
+ throw new Error('The value is disposed.')
910
1417
  if (!(source instanceof Derived))
911
1418
  throw new TypeError(`"${key}" needs a motion value, got ${String(source)}.`)
912
1419
  const properties: Property[] =
@@ -970,12 +1477,16 @@ function attach(view: View, property: Property, source: Derived) {
970
1477
  return attachSpring(view, property as LiveProperty, source, root)
971
1478
  let native: LiveBinding | undefined, composer: LiveNode | undefined
972
1479
  let releaseMix: (() => void) | undefined
1480
+ let releaseDemand: (() => void) | undefined
973
1481
  const detach = () => {
974
1482
  if (!native) return
975
1483
  native.cancel()
976
1484
  releaseMix?.()
977
1485
  releaseMix = undefined
978
1486
  native = composer = undefined
1487
+ releaseDemand?.()
1488
+ releaseDemand = undefined
1489
+ root.settle()
979
1490
  syncLiveSprings(view)
980
1491
  }
981
1492
  const binding: Binding = {
@@ -986,16 +1497,20 @@ function attach(view: View, property: Property, source: Derived) {
986
1497
  read: source.read,
987
1498
  sync() {
988
1499
  if (!view.isLoaded || !view.nativeViewProtected) return
989
- const node = layerable(property) && !source.color ? root.composerNode() : undefined
1500
+ const node =
1501
+ root instanceof GraphRoot || (layerable(property) && !source.color)
1502
+ ? root.composerNode()
1503
+ : undefined
990
1504
  if (node) {
991
1505
  if (composer === node) return
992
1506
  detach()
993
1507
  binding.animation?.stop()
994
1508
  binding.animation = undefined
995
1509
  stopBase(view, [property])
996
- const handle = node.bind(view, property as NumericProperty, source.mappings)
1510
+ const handle = node.bind(view, property as LiveProperty, source.mappings)
997
1511
  native = handle
998
1512
  composer = node
1513
+ releaseDemand = root.retainConsumer()
999
1514
  releaseMix = mixFor(view, property).attachLive({
1000
1515
  read: () => handle.value(),
1001
1516
  layers: (layers) => handle.layers(layers),
@@ -1437,56 +1952,180 @@ function springOf(name: SpringName): Transition {
1437
1952
  return springs[name]
1438
1953
  }
1439
1954
 
1440
- /** A number that follows a scroll view's offset. Bind it like a motion value. */
1955
+ /** Scroll offsets in DIPs/points. iOS includes the adjusted leading inset; bounce stays negative. */
1441
1956
  export interface ScrollValue extends DerivedValue {
1442
- /** Follows this scroll view instead, or none with `null`. Stable, so it works as a template ref. */
1443
1957
  readonly ref: (scrollView: Target | null) => void
1444
- /** Stops following; bound views stay where they are. */
1445
- dispose(): void
1958
+ readonly execution: 'native' | 'javascript'
1959
+ refresh(): void
1960
+ }
1961
+ export interface ScrollValueOptions {
1962
+ axis?: 'x' | 'y'
1963
+ /** auto uses rebuilt native classes; javascript preserves the observable ScrollView fallback. */
1964
+ execution?: 'auto' | 'native' | 'javascript'
1965
+ /** Required on Android virtualized lists: fixed row height; horizontal CollectionView uses item stride. */
1966
+ rowHeight?: number
1967
+ }
1968
+ const scrollDiagnostics = new WeakMap<
1969
+ object,
1970
+ () => { offset: number; updates: number; callbacks: number }
1971
+ >()
1972
+ /** Development audit; counts Jolt callbacks, not events emitted independently by NativeScript Core. */
1973
+ export function inspectScrollValue(value: ScrollValue) {
1974
+ return scrollDiagnostics.get(value)?.()
1446
1975
  }
1447
1976
 
1448
- /**
1449
- * The scroll offset of a ScrollView, in DIPs, as a value that bindings follow.
1450
- *
1451
- * ```ts
1452
- * const offset = scrollValue(scroll)
1453
- * bind(header, { y: offset.to([0, 120], [0, -60]), opacity: offset.to([0, 80], [1, 0]) })
1454
- * ```
1455
- */
1456
1977
  export function scrollValue(
1457
1978
  scrollView?: Target | null,
1458
- options: { axis?: 'x' | 'y' } = {},
1979
+ options: ScrollValueOptions = {},
1459
1980
  ): ScrollValue {
1460
- const axis = options.axis ?? 'y'
1981
+ if (
1982
+ !options ||
1983
+ typeof options !== 'object' ||
1984
+ Object.keys(options).some((key) => !['axis', 'execution', 'rowHeight'].includes(key))
1985
+ )
1986
+ throw new TypeError('Expected scroll value options.')
1987
+ const axis = options.axis ?? 'y',
1988
+ execution = options.execution ?? 'auto',
1989
+ rowHeight = options.rowHeight ?? 0
1461
1990
  if (axis !== 'x' && axis !== 'y') throw new TypeError('Scroll axis must be "x" or "y".')
1462
- const root = new Root(0)
1463
- let source: ScrollView | undefined
1991
+ if (
1992
+ !['auto', 'native', 'javascript'].includes(execution) ||
1993
+ !Number.isFinite(rowHeight) ||
1994
+ rowHeight < 0
1995
+ )
1996
+ throw new TypeError(
1997
+ 'Expected a valid execution path and a nonnegative fixed rowHeight.',
1998
+ )
1999
+ const native = execution !== 'javascript' && nativeScrollAvailable()
2000
+ if (!native && execution === 'native')
2001
+ throw new Error('Native scroll values need rebuilt Jolt classes.')
2002
+ const initial = new Root(0),
2003
+ root: Root = native ? new SpringRoot(initial, { duration: 0 }) : initial
2004
+ const disposeRoot = root.dispose.bind(root)
2005
+ let source: View | undefined,
2006
+ handle: NativeScrollSource | undefined,
2007
+ ended = false,
2008
+ background = false
2009
+ let updates = 0,
2010
+ callbacks = 0
2011
+ const adjusted = (offset: number) => {
2012
+ const inset = isIOS ? source?.nativeViewProtected?.adjustedContentInset : undefined
2013
+ return offset + (inset ? (axis === 'x' ? inset.left : inset.top) : 0)
2014
+ }
1464
2015
  const read = () => {
1465
- if (!source) return
1466
- const offset = axis === 'x' ? source.horizontalOffset : source.verticalOffset
1467
- if (Number.isFinite(offset) && offset !== root.raw()) root.set(offset)
2016
+ if (handle) {
2017
+ handle.refresh()
2018
+ return
2019
+ }
2020
+ if (!(source instanceof ScrollView) || native) return
2021
+ const offset = adjusted(
2022
+ axis === 'x' ? source.horizontalOffset : source.verticalOffset,
2023
+ )
2024
+ if (Number.isFinite(offset) && offset !== root.raw()) {
2025
+ updates++
2026
+ root.set(offset)
2027
+ }
1468
2028
  }
1469
2029
  const onScroll = (event: EventData) => {
1470
- const { scrollX, scrollY } = event as ScrollEventData
1471
- const offset = axis === 'x' ? scrollX : scrollY
1472
- if (Number.isFinite(offset) && offset !== root.raw()) root.set(offset)
2030
+ if (ended || background) return
2031
+ callbacks++
2032
+ const scroll = event as ScrollEventData,
2033
+ offset = adjusted(axis === 'x' ? scroll.scrollX : scroll.scrollY)
2034
+ if (Number.isFinite(offset) && offset !== root.raw()) {
2035
+ updates++
2036
+ root.set(offset)
2037
+ }
2038
+ }
2039
+ const observe = () => handle?.observe(root.observed)
2040
+ const detachNative = () => {
2041
+ handle?.dispose()
2042
+ handle = undefined
2043
+ }
2044
+ const attach = () => {
2045
+ if (ended || background) return
2046
+ if (!native) {
2047
+ read()
2048
+ return
2049
+ }
2050
+ if (handle || !source?.isLoaded || !source.nativeViewProtected) return
2051
+ handle = createNativeScrollSource(
2052
+ source,
2053
+ (root as SpringRoot).node,
2054
+ axis,
2055
+ rowHeight,
2056
+ () => (root as SpringRoot).notify(),
2057
+ )
2058
+ observe()
2059
+ ;(root as SpringRoot).notify()
2060
+ }
2061
+ const suspend = () => {
2062
+ background = true
2063
+ handle?.enabled(false)
2064
+ }
2065
+ const resume = () => {
2066
+ background = false
2067
+ attach()
2068
+ handle?.enabled(true)
2069
+ read()
1473
2070
  }
1474
2071
  const detach = () => {
2072
+ detachNative()
1475
2073
  source?.off(ScrollView.scrollEvent, onScroll)
1476
- source?.off('loaded', read)
2074
+ source?.off('loaded', attach)
2075
+ source?.off('unloaded', detachNative)
1477
2076
  source = undefined
1478
2077
  }
1479
2078
  const ref = (target: Target | null) => {
2079
+ if (ended) {
2080
+ if (target === null) return
2081
+ throw new Error('The scroll value is disposed.')
2082
+ }
1480
2083
  const next = target === null ? undefined : toViews(target)[0]
1481
2084
  if (next === source) return
1482
- if (next !== undefined && !(next instanceof ScrollView))
1483
- throw new TypeError('scrollValue() follows a NativeScript ScrollView.')
2085
+ if (next && !native && !(next instanceof ScrollView))
2086
+ throw new TypeError(
2087
+ 'JavaScript scroll values require a NativeScript ScrollView; native list input needs rebuilt classes.',
2088
+ )
1484
2089
  detach()
1485
2090
  source = next
1486
- source?.on(ScrollView.scrollEvent, onScroll)
1487
- source?.on('loaded', read)
1488
- read()
2091
+ if (!native) source?.on(ScrollView.scrollEvent, onScroll)
2092
+ source?.on('loaded', attach)
2093
+ source?.on('unloaded', detachNative)
2094
+ try {
2095
+ attach()
2096
+ } catch (error) {
2097
+ detach()
2098
+ throw error
2099
+ }
2100
+ }
2101
+ const dispose = () => {
2102
+ if (ended) return
2103
+ ended = true
2104
+ detach()
2105
+ root.observationListeners.delete(observe)
2106
+ Application.off(Application.suspendEvent, suspend)
2107
+ Application.off(Application.resumeEvent, resume)
2108
+ disposeRoot()
2109
+ if (native) initial.dispose()
2110
+ }
2111
+ root.observationListeners.add(observe)
2112
+ Application.on(Application.suspendEvent, suspend)
2113
+ Application.on(Application.resumeEvent, resume)
2114
+ const result = Object.assign(root as unknown as DerivedValue, {
2115
+ ref,
2116
+ dispose,
2117
+ refresh: read,
2118
+ execution: native ? ('native' as const) : ('javascript' as const),
2119
+ })
2120
+ scrollDiagnostics.set(
2121
+ result,
2122
+ () => handle?.snapshot() ?? { offset: root.raw(), updates, callbacks },
2123
+ )
2124
+ try {
2125
+ if (scrollView) ref(scrollView)
2126
+ } catch (error) {
2127
+ dispose()
2128
+ throw error
1489
2129
  }
1490
- if (scrollView) ref(scrollView)
1491
- return Object.assign(root as unknown as DerivedValue, { ref, dispose: detach })
2130
+ return result
1492
2131
  }