@wardic/core 1.3.1 → 1.4.0

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 (245) hide show
  1. package/dist/Arc.d.ts +2 -2
  2. package/dist/Arc.js +1 -1
  3. package/dist/BaseValue.d.ts +11 -10
  4. package/dist/BaseValue.d.ts.map +1 -1
  5. package/dist/BaseValue.js +12 -15
  6. package/dist/BaseValue.js.map +1 -1
  7. package/dist/Batch.d.ts +12 -6
  8. package/dist/Batch.d.ts.map +1 -1
  9. package/dist/Batch.js +374 -30
  10. package/dist/Batch.js.map +1 -1
  11. package/dist/Clock.d.ts +16 -0
  12. package/dist/Clock.d.ts.map +1 -1
  13. package/dist/Clock.js +22 -8
  14. package/dist/Clock.js.map +1 -1
  15. package/dist/DefaultTransition.d.ts +3 -2
  16. package/dist/DefaultTransition.d.ts.map +1 -1
  17. package/dist/DefaultTransition.js +5 -3
  18. package/dist/DefaultTransition.js.map +1 -1
  19. package/dist/Drag.d.ts +24 -9
  20. package/dist/Drag.d.ts.map +1 -1
  21. package/dist/Drag.js +10 -10
  22. package/dist/Drag.js.map +1 -1
  23. package/dist/DragActivity.js +3 -3
  24. package/dist/DragRecognizer.d.ts +14 -8
  25. package/dist/DragRecognizer.d.ts.map +1 -1
  26. package/dist/DragRecognizer.js +93 -34
  27. package/dist/DragRecognizer.js.map +1 -1
  28. package/dist/Easing.d.ts +1 -1
  29. package/dist/Easing.js +1 -1
  30. package/dist/Frame.d.ts.map +1 -1
  31. package/dist/Frame.js +5 -3
  32. package/dist/Frame.js.map +1 -1
  33. package/dist/InputFeed.d.ts +11 -8
  34. package/dist/InputFeed.d.ts.map +1 -1
  35. package/dist/InputFeed.js +18 -37
  36. package/dist/InputFeed.js.map +1 -1
  37. package/dist/InputSource.d.ts +3 -3
  38. package/dist/InputSource.js +2 -2
  39. package/dist/Interpolator.d.ts +2 -2
  40. package/dist/Interpolator.js +2 -2
  41. package/dist/InterpolatorRegistry.d.ts +29 -15
  42. package/dist/InterpolatorRegistry.d.ts.map +1 -1
  43. package/dist/InterpolatorRegistry.js +17 -8
  44. package/dist/InterpolatorRegistry.js.map +1 -1
  45. package/dist/LiveValue.d.ts +16 -9
  46. package/dist/LiveValue.d.ts.map +1 -1
  47. package/dist/LiveValue.js +204 -27
  48. package/dist/LiveValue.js.map +1 -1
  49. package/dist/Machine.d.ts +21 -8
  50. package/dist/Machine.d.ts.map +1 -1
  51. package/dist/Machine.js +184 -60
  52. package/dist/Machine.js.map +1 -1
  53. package/dist/Playback.d.ts.map +1 -1
  54. package/dist/Playback.js +16 -9
  55. package/dist/Playback.js.map +1 -1
  56. package/dist/Projection.d.ts +9 -9
  57. package/dist/Projection.js +2 -2
  58. package/dist/Recognizer.d.ts +2 -2
  59. package/dist/Recognizer.d.ts.map +1 -1
  60. package/dist/Recognizer.js +15 -10
  61. package/dist/Recognizer.js.map +1 -1
  62. package/dist/ReducedMotion.d.ts +4 -4
  63. package/dist/ReducedMotion.js +6 -6
  64. package/dist/Renderer.d.ts +5 -5
  65. package/dist/Renderer.js +6 -6
  66. package/dist/Renderer.js.map +1 -1
  67. package/dist/Reorder.d.ts +1 -1
  68. package/dist/Reorder.d.ts.map +1 -1
  69. package/dist/Reorder.js +2 -1
  70. package/dist/Reorder.js.map +1 -1
  71. package/dist/ScrollSource.d.ts +1 -1
  72. package/dist/ScrollSource.d.ts.map +1 -1
  73. package/dist/Sequence.d.ts +1 -1
  74. package/dist/SharedSource.d.ts +3 -3
  75. package/dist/SharedSource.js +5 -5
  76. package/dist/SharedSource.js.map +1 -1
  77. package/dist/Spring.d.ts.map +1 -1
  78. package/dist/Spring.js +2 -1
  79. package/dist/Spring.js.map +1 -1
  80. package/dist/Values.d.ts +2 -2
  81. package/dist/Values.js +1 -1
  82. package/dist/index.d.ts +1 -1
  83. package/dist/index.js +1 -1
  84. package/dist/internal/carried.d.ts +13 -0
  85. package/dist/internal/carried.d.ts.map +1 -0
  86. package/dist/internal/carried.js +23 -0
  87. package/dist/internal/carried.js.map +1 -0
  88. package/dist/internal/firstSteps.d.ts +66 -0
  89. package/dist/internal/firstSteps.d.ts.map +1 -0
  90. package/dist/internal/firstSteps.js +107 -0
  91. package/dist/internal/firstSteps.js.map +1 -0
  92. package/dist/internal/frameLoop.d.ts +11 -3
  93. package/dist/internal/frameLoop.d.ts.map +1 -1
  94. package/dist/internal/frameLoop.js +190 -39
  95. package/dist/internal/frameLoop.js.map +1 -1
  96. package/dist/internal/guarded.d.ts +16 -0
  97. package/dist/internal/guarded.d.ts.map +1 -0
  98. package/dist/internal/guarded.js +68 -0
  99. package/dist/internal/guarded.js.map +1 -0
  100. package/dist/internal/lerpsNumbers.d.ts +11 -0
  101. package/dist/internal/lerpsNumbers.d.ts.map +1 -0
  102. package/dist/internal/lerpsNumbers.js +38 -0
  103. package/dist/internal/lerpsNumbers.js.map +1 -0
  104. package/dist/internal/options.d.ts +7 -7
  105. package/dist/internal/options.d.ts.map +1 -1
  106. package/dist/internal/options.js +10 -10
  107. package/dist/internal/options.js.map +1 -1
  108. package/dist/internal/stepper.d.ts +21 -4
  109. package/dist/internal/stepper.d.ts.map +1 -1
  110. package/dist/internal/stepper.js +21 -5
  111. package/dist/internal/stepper.js.map +1 -1
  112. package/dist/internal/tracing.d.ts +1 -1
  113. package/dist/internal/tracing.js +1 -1
  114. package/dist/unstable/AnimationControls.d.ts +45 -39
  115. package/dist/unstable/AnimationControls.d.ts.map +1 -1
  116. package/dist/unstable/AnimationControls.js +59 -35
  117. package/dist/unstable/AnimationControls.js.map +1 -1
  118. package/dist/unstable/AppearHandoff.d.ts +11 -5
  119. package/dist/unstable/AppearHandoff.d.ts.map +1 -1
  120. package/dist/unstable/AppearHandoff.js.map +1 -1
  121. package/dist/unstable/BatchCalls.d.ts +2 -2
  122. package/dist/unstable/BatchCalls.js +5 -5
  123. package/dist/unstable/Config.d.ts +21 -17
  124. package/dist/unstable/Config.d.ts.map +1 -1
  125. package/dist/unstable/Config.js.map +1 -1
  126. package/dist/unstable/FirstSteps.d.ts +25 -0
  127. package/dist/unstable/FirstSteps.d.ts.map +1 -0
  128. package/dist/unstable/FirstSteps.js +2 -0
  129. package/dist/unstable/FirstSteps.js.map +1 -0
  130. package/dist/unstable/FlushGate.d.ts +54 -50
  131. package/dist/unstable/FlushGate.d.ts.map +1 -1
  132. package/dist/unstable/FlushGate.js +201 -77
  133. package/dist/unstable/FlushGate.js.map +1 -1
  134. package/dist/unstable/FrameEffects.d.ts +6 -3
  135. package/dist/unstable/FrameEffects.d.ts.map +1 -1
  136. package/dist/unstable/FrameEffects.js +14 -3
  137. package/dist/unstable/FrameEffects.js.map +1 -1
  138. package/dist/unstable/FrameTime.d.ts +1 -1
  139. package/dist/unstable/FrameTime.js +1 -1
  140. package/dist/unstable/Imperative.d.ts +42 -13
  141. package/dist/unstable/Imperative.d.ts.map +1 -1
  142. package/dist/unstable/Imperative.js +279 -88
  143. package/dist/unstable/Imperative.js.map +1 -1
  144. package/dist/unstable/MeasuredPairs.d.ts +6 -2
  145. package/dist/unstable/MeasuredPairs.d.ts.map +1 -1
  146. package/dist/unstable/MeasuredPairs.js.map +1 -1
  147. package/dist/unstable/OwnValues.js +1 -1
  148. package/dist/unstable/PresenceRecord.d.ts +30 -26
  149. package/dist/unstable/PresenceRecord.d.ts.map +1 -1
  150. package/dist/unstable/PresenceRecord.js +8 -8
  151. package/dist/unstable/PresenceRecord.js.map +1 -1
  152. package/dist/unstable/Projector.d.ts +34 -0
  153. package/dist/unstable/Projector.d.ts.map +1 -0
  154. package/dist/unstable/Projector.js +9 -0
  155. package/dist/unstable/Projector.js.map +1 -0
  156. package/dist/unstable/Report.d.ts +8 -1
  157. package/dist/unstable/Report.d.ts.map +1 -1
  158. package/dist/unstable/Report.js +18 -2
  159. package/dist/unstable/Report.js.map +1 -1
  160. package/dist/unstable/RetargetLegs.d.ts +7 -1
  161. package/dist/unstable/RetargetLegs.d.ts.map +1 -1
  162. package/dist/unstable/RetargetLegs.js +24 -8
  163. package/dist/unstable/RetargetLegs.js.map +1 -1
  164. package/dist/unstable/Spine.d.ts +123 -68
  165. package/dist/unstable/Spine.d.ts.map +1 -1
  166. package/dist/unstable/Spine.js +319 -167
  167. package/dist/unstable/Spine.js.map +1 -1
  168. package/dist/unstable/Stops.d.ts +3 -3
  169. package/dist/unstable/Stops.js +4 -4
  170. package/dist/unstable/Subject.d.ts +2 -2
  171. package/dist/unstable/Subject.js +2 -2
  172. package/dist/unstable/Timelines.d.ts +11 -11
  173. package/dist/unstable/Timelines.d.ts.map +1 -1
  174. package/dist/unstable/Timelines.js +3 -3
  175. package/dist/unstable/VariantTarget.d.ts +32 -10
  176. package/dist/unstable/VariantTarget.d.ts.map +1 -1
  177. package/dist/unstable/VariantTarget.js +73 -20
  178. package/dist/unstable/VariantTarget.js.map +1 -1
  179. package/dist/unstable/VariantTree.d.ts +68 -34
  180. package/dist/unstable/VariantTree.d.ts.map +1 -1
  181. package/dist/unstable/VariantTree.js +83 -36
  182. package/dist/unstable/VariantTree.js.map +1 -1
  183. package/dist/unstable/WardicConfigValue.d.ts +13 -13
  184. package/dist/unstable/WardicConfigValue.d.ts.map +1 -1
  185. package/dist/unstable/WardicConfigValue.js +4 -4
  186. package/dist/unstable/WardicConfigValue.js.map +1 -1
  187. package/package.json +1 -1
  188. package/src/Arc.ts +3 -3
  189. package/src/BaseValue.ts +18 -21
  190. package/src/Batch.ts +391 -27
  191. package/src/Clock.ts +29 -8
  192. package/src/DefaultTransition.ts +5 -3
  193. package/src/Drag.ts +32 -14
  194. package/src/DragActivity.ts +3 -3
  195. package/src/DragRecognizer.ts +118 -41
  196. package/src/Easing.ts +1 -1
  197. package/src/Frame.ts +5 -3
  198. package/src/InputFeed.ts +39 -42
  199. package/src/InputSource.ts +5 -5
  200. package/src/Interpolator.ts +3 -3
  201. package/src/InterpolatorRegistry.ts +22 -13
  202. package/src/LiveValue.ts +186 -30
  203. package/src/Machine.ts +252 -81
  204. package/src/Playback.ts +15 -9
  205. package/src/Projection.ts +9 -9
  206. package/src/Recognizer.ts +16 -10
  207. package/src/ReducedMotion.ts +6 -6
  208. package/src/Renderer.ts +6 -6
  209. package/src/Reorder.ts +3 -2
  210. package/src/ScrollSource.ts +1 -1
  211. package/src/Sequence.ts +1 -1
  212. package/src/SharedSource.ts +5 -5
  213. package/src/Spring.ts +2 -1
  214. package/src/Values.ts +2 -2
  215. package/src/index.ts +1 -1
  216. package/src/internal/carried.ts +22 -0
  217. package/src/internal/firstSteps.ts +139 -0
  218. package/src/internal/frameLoop.ts +203 -41
  219. package/src/internal/guarded.ts +69 -0
  220. package/src/internal/lerpsNumbers.ts +53 -0
  221. package/src/internal/options.ts +10 -10
  222. package/src/internal/stepper.ts +33 -6
  223. package/src/internal/tracing.ts +1 -1
  224. package/src/unstable/AnimationControls.ts +88 -57
  225. package/src/unstable/AppearHandoff.ts +13 -6
  226. package/src/unstable/BatchCalls.ts +5 -5
  227. package/src/unstable/Config.ts +23 -17
  228. package/src/unstable/FirstSteps.ts +26 -0
  229. package/src/unstable/FlushGate.ts +250 -108
  230. package/src/unstable/FrameEffects.ts +17 -4
  231. package/src/unstable/FrameTime.ts +1 -1
  232. package/src/unstable/Imperative.ts +342 -101
  233. package/src/unstable/MeasuredPairs.ts +7 -1
  234. package/src/unstable/OwnValues.ts +1 -1
  235. package/src/unstable/PresenceRecord.ts +33 -29
  236. package/src/unstable/Projector.ts +45 -0
  237. package/src/unstable/Report.ts +20 -2
  238. package/src/unstable/RetargetLegs.ts +24 -9
  239. package/src/unstable/Spine.ts +399 -202
  240. package/src/unstable/Stops.ts +6 -6
  241. package/src/unstable/Subject.ts +2 -2
  242. package/src/unstable/Timelines.ts +12 -12
  243. package/src/unstable/VariantTarget.ts +102 -23
  244. package/src/unstable/VariantTree.ts +123 -42
  245. package/src/unstable/WardicConfigValue.ts +13 -13
package/src/LiveValue.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Live values: values derived from other values as they change, each an `effect/reactivity` Atom an
3
- * element's `style` can follow. `transform` maps one through ranges, `follow` and `spring` follow one on
3
+ * Subject's `style` can follow. `transform` maps one through ranges, `follow` and `spring` follow one on
4
4
  * a transition, `velocity` tracks how fast one moves, `scroll` gives a container's scroll, `time` the
5
5
  * time since it started, and `eachFrame` calls you every frame. A binding wraps them in its own
6
6
  * reactive values, as Solid's `createSpring` and `createTransform` do.
@@ -12,14 +12,15 @@
12
12
  * const x = yield* LiveValue.spring(pointerX, { stiffness: 300, damping: 30 })
13
13
  * const opacity = yield* LiveValue.transform(x, [0, 200], [1, 0])
14
14
  */
15
- // #130, ADR-0043: a Live value is one the user moves themselves, given in an element's `style`: the
16
- // key's `style` link, which the element follows while no entry of the Override stack sets it
15
+ // #130, ADR-0043: a Live value is one the user moves themselves, given in a Subject's `style`: the
16
+ // key's `style` link, which the Subject follows while no entry of the Override stack sets it
17
17
  // (CONTEXT.md). Its shared form is an Atom, as core's other live data is (`Probe.viewport`). Motion's
18
18
  // `useTransform` (with a `mixer`, #440), `interpolate`, `followValue` (#441), `useSpring`,
19
19
  // `MotionValue.jump` (#257 item 7), `useScroll` (#234), `useVelocity`, `useTime`, `useAnimationFrame`
20
20
  // (#443). Tier 2 (ADR-0008): Effect-typed, for bindings, which wrap them.
21
21
  import * as Arr from "effect/Array"
22
22
  import * as Bool from "effect/Boolean"
23
+ import * as Cause from "effect/Cause"
23
24
  import * as Context from "effect/Context"
24
25
  import * as Effect from "effect/Effect"
25
26
  import * as FiberHandle from "effect/FiberHandle"
@@ -32,6 +33,7 @@ import * as AtomRegistry from "effect/reactivity/AtomRegistry"
32
33
  import * as Rec from "effect/Record"
33
34
  import type * as Scope from "effect/Scope"
34
35
  import * as UndefinedOr from "effect/UndefinedOr"
36
+ import * as Clock from "./Clock.ts"
35
37
  import * as FrameLoop from "./FrameLoop.ts"
36
38
  import * as Easing from "./Easing.ts"
37
39
  import * as InterpolatorRegistry from "./InterpolatorRegistry.ts"
@@ -40,6 +42,7 @@ import * as Machine from "./Machine.ts"
40
42
  import * as ScrollSource from "./ScrollSource.ts"
41
43
  import * as Supersede from "./Supersede.ts"
42
44
  import { dual } from "./internal/dual.ts"
45
+ import * as Report from "./unstable/Report.ts"
43
46
 
44
47
  /** A blend between two outputs, as Motion's `MixerFactory`: made once per stretch, then read at each progress through it, 0 to 1. */
45
48
  // #440.
@@ -164,7 +167,8 @@ const interpolateWith: <O>(
164
167
 
165
168
  /**
166
169
  * `source` mapped through ranges, as `interpolate` maps a number: an Atom that follows `source`, mapped
167
- * afresh at each change.
170
+ * afresh at each change. A `mixer` that throws keeps the value it gave last, and is logged once until it
171
+ * returns; one that throws for its first value throws to whatever reads it then.
168
172
  *
169
173
  * @example
170
174
  * const scale = yield* LiveValue.transform(scrollY, [0, 300], [1, 0.8])
@@ -173,7 +177,28 @@ export const transform: {
173
177
  <O extends number | string>(source: Atom.Atom<number>, input: ReadonlyArray<number>, output: ReadonlyArray<O>, options?: TransformOptions<O>): Effect.Effect<Atom.Atom<O>, never, InterpolatorRegistry.InterpolatorRegistry>
174
178
  <O>(source: Atom.Atom<number>, input: ReadonlyArray<number>, output: ReadonlyArray<O>, options: MixedTransformOptions<O>): Effect.Effect<Atom.Atom<O>, never, InterpolatorRegistry.InterpolatorRegistry>
175
179
  } = <O>(source: Atom.Atom<number>, input: ReadonlyArray<number>, output: ReadonlyArray<O>, options: TransformOptions<O> = {}) =>
176
- Effect.map(interpolateWith(input, output, options), (map) => transformedFrom(Atom.make((get) => map(get(source))), source, input, output, options))
180
+ Effect.zipWith(interpolateWith(input, output, options), Effect.context<never>(), (map, context) => {
181
+ // An app's `mixer` that throws: its last value kept, reported at the first throw of a run of them, again
182
+ // after a clean one; with none yet to keep, its first value's throw is its reader's (#979). The try is in
183
+ // the read itself, around a direct call: a helper called from it measured a few points a change on Bun.
184
+ let last: O | undefined
185
+ let kept = false
186
+ let failing = false
187
+ const read = (get: Atom.AtomContext): O => {
188
+ const v = get(source)
189
+ try {
190
+ last = map(v)
191
+ kept = true
192
+ if (failing) failing = false
193
+ } catch (defect) {
194
+ if (!kept) throw defect
195
+ if (!failing) Report.failureOn(context, MIXER_FAILED, Cause.die(defect))
196
+ failing = true
197
+ }
198
+ return last as O
199
+ }
200
+ return transformedFrom(Atom.make(read), source, input, output, options)
201
+ })
177
202
 
178
203
  /** `v`'s stretch, as Motion's: the first that ends above it, else the last. Read on each change of the source. */
179
204
  const stretchAt = <O>(stretches: Arr.NonEmptyReadonlyArray<Stretch<O>>, v: number): Stretch<O> =>
@@ -302,7 +327,8 @@ export const jump: {
302
327
  }),
303
328
  )
304
329
 
305
- /** Motion's spring, for the physics a follow's spring leaves out (`motion-dom`'s `springDefaults`). */
330
+ /** Motion's spring, for the physics a follow's spring leaves out (Motion's `springDefaults`). */
331
+ // `motion-dom`'s `springDefaults`.
306
332
  const MOTION_PHYSICS = { stiffness: 100, damping: 10, mass: 1 } as const
307
333
 
308
334
  /** Motion's follower's rest thresholds, for those a spring or an inertia leaves out (`follow()`'s). */
@@ -377,6 +403,18 @@ const velocityOfFrame = (frame: Frame.Frame<number>): number => {
377
403
  return velocity
378
404
  }
379
405
 
406
+ // #979, group D (ADR-0074): a run goes on past an Atom over it, or under it, that throws, its failure
407
+ // reported on what it was built with (`Report.failureOn`), off the per-frame path.
408
+
409
+ /** A run's dependent that threw at its write: an app's Atom over its value, say. */
410
+ const DEPENDENT_FAILED = "wardic.liveValue: its dependent failed"
411
+
412
+ /** A run's source that threw as it was read: an app's Atom under it, say. */
413
+ const SOURCE_FAILED = "wardic.liveValue: its source failed"
414
+
415
+ /** A transform's ranges that threw: an app's `mixer`. */
416
+ const MIXER_FAILED = "wardic.transform: its mixer failed"
417
+
380
418
  const FOLLOW_SPAN: Machine.Span = { name: "wardic.follow", attributes: {} }
381
419
 
382
420
  /**
@@ -395,6 +433,9 @@ const FOLLOW_SPAN: Machine.Span = { name: "wardic.follow", attributes: {} }
395
433
  * is a `jump`; with `skipAnimations`, or while `skipped()` says so, every change is. Its run and its
396
434
  * subscription end with the Scope.
397
435
  *
436
+ * A source that throws as it's read is followed to where it was last read, and an Atom over it that throws
437
+ * doesn't stop it: each is logged once until it reads, or is written, cleanly.
438
+ *
398
439
  * @example
399
440
  * const eased = yield* LiveValue.follow(target, { type: "tween", duration: 0.4, ease: "easeOut" })
400
441
  */
@@ -408,6 +449,7 @@ export const follow: (
408
449
  ) => Effect.Effect<Followed, never, FrameLoop.FrameLoop | AtomRegistry.AtomRegistry | Scope.Scope> = Effect.fnUntraced(
409
450
  function* (source: Atom.Atom<number>, options: FollowOptions = {}, skipped: () => boolean = constFalse) {
410
451
  const registry = yield* AtomRegistry.AtomRegistry
452
+ const context = yield* Effect.context<never>()
411
453
  const { skipInitialAnimation, skipAnimations, ...transition } = options
412
454
  const plan = planOf(transition as FollowTransition)
413
455
  const skips = Bool.match(skipAnimations === true, { onTrue: () => constTrue, onFalse: () => skipped })
@@ -423,7 +465,22 @@ export const follow: (
423
465
  let changed = false
424
466
  // Whether the source's next change is a jump: its first, with `skipInitialAnimation`.
425
467
  let skipping = skipInitialAnimation === true
426
- const target = (): number => registry.get(source)
468
+ // A source that throws (an app's Atom under it): a run steering toward it keeps its last read,
469
+ // reported at the first throw of a run of them, again after a clean read (#979).
470
+ let lastTarget = registry.get(out)
471
+ let sourceFailing = false
472
+ const target = (): number => {
473
+ try {
474
+ lastTarget = registry.get(source)
475
+ if (sourceFailing) sourceFailing = false
476
+ } catch (defect) {
477
+ if (!sourceFailing) Report.failureOn(context, SOURCE_FAILED, Cause.die(defect))
478
+ sourceFailing = true
479
+ }
480
+ return lastTarget
481
+ }
482
+ // Whether its writes are throwing back a dependent's failure, reported as the source's are.
483
+ let failing = false
427
484
  /** One run: the plan's, from where `out` is, started again from where it's reached at the step after a change. */
428
485
  const machine = Machine.make({
429
486
  settles: false,
@@ -464,7 +521,15 @@ export const follow: (
464
521
  machine.pipe(
465
522
  Machine.run(
466
523
  (frame) => {
467
- registry.set(out, frame.value)
524
+ // Per frame, a try around the direct call: the registry has set it and told its other
525
+ // listeners before a dependent's throw comes back (an app's Atom over it), so the run goes on.
526
+ try {
527
+ registry.set(out, frame.value)
528
+ if (failing) failing = false
529
+ } catch (defect) {
530
+ if (!failing) Report.failureOn(context, DEPENDENT_FAILED, Cause.die(defect))
531
+ failing = true
532
+ }
468
533
  velocity = velocityOfFrame(frame)
469
534
  // At rest on the source: end the run; the next change starts another.
470
535
  if (frame.done) {
@@ -567,8 +632,8 @@ export interface ScrollTransform {
567
632
  * How a Live value derives from a container's scroll, in no Environment's terms: the scroll it
568
633
  * reads, as `scroll` was given it; whether it reads how far the container is scrolled (`current`) or
569
634
  * its progress; on which axis; and each `transform` it went through since, in order. An Environment
570
- * that can run a scroll-linked value with the scroll itself, as the DOM's native scroll timelines do (a
571
- * **Timeline value**), reads it to decide whether it can, and how.
635
+ * that can run a scroll-linked value with the scroll itself, as `@wardic/dom` can (a **Timeline value**),
636
+ * reads it to decide whether it can, and how.
572
637
  */
573
638
  export interface ScrollDerivation {
574
639
  /** The scroll's options, as `scroll` was given them. */
@@ -637,7 +702,7 @@ const derivationNow = (atom: Atom.Atom<unknown>): ScrollDerivation | undefined =
637
702
  /**
638
703
  * Describe `atom` as deriving from a container's scroll as `source` does: for an Atom a binding
639
704
  * keeps for a value's life and sets from `source`, made after it, as a `createScroll` does once its
640
- * elements are there. A `transform` made over `atom` before then derives through it too. Called again,
705
+ * Subjects are there. A `transform` made over `atom` before then derives through it too. Called again,
641
706
  * it derives as the new `source` does. `source` mustn't derive from `atom`.
642
707
  *
643
708
  * @example
@@ -660,8 +725,8 @@ export const derivesAs: {
660
725
  * How `atom` derives from a container's scroll, now: one of `scroll`'s values, an Atom
661
726
  * `derivesAs` one, or a `transform` over either, a `transform` over that, and so on; `None` for any
662
727
  * other, a `spring` or a `velocity` of one among them. Read again while nothing it derives from
663
- * changed, it's the same object. An Environment reads it to run the value on a native scroll timeline
664
- * where it can (a **Timeline value**).
728
+ * changed, it's the same object. An Environment reads it to run the value with the scroll itself where it
729
+ * can, as `@wardic/dom` can (a **Timeline value**).
665
730
  *
666
731
  * @example
667
732
  * const { scrollYProgress } = yield* LiveValue.scroll({ container })
@@ -681,52 +746,130 @@ const numberMachine = (name: string, settles: boolean, start: () => (dt: number)
681
746
  * How fast `source` is changing, in its units a second, as Motion's `useVelocity`: 0 to begin with, and
682
747
  * from each change on, each frame's change over the frame's time, until a frame finds no change, sets 0
683
748
  * and stops. Nothing runs while the source rests. A velocity is a value too: `velocity(velocity(x))` is
684
- * acceleration. Its run ends with the Scope.
749
+ * acceleration. Its run ends with the Scope. A source that throws as it's read reads as still, and an Atom
750
+ * over it that throws doesn't stop it: each is logged once until it reads, or is written, cleanly.
685
751
  *
686
752
  * @example
687
753
  * const speed = yield* LiveValue.velocity(scrollY)
688
754
  */
689
- // Each tick's `dt` is clamped at `Machine.MAX_DT`, as every run's is. "Since the end of the tick before",
690
- // read by an `afterTick` hook, not the run's own last read: a run started inside a tick takes its first
691
- // step at the next one, and would otherwise count two frames' change over one `dt`.
692
- export const velocity: (source: Atom.Atom<number>) => Effect.Effect<Atom.Atom<number>, never, FrameLoop.FrameLoop | AtomRegistry.AtomRegistry | Scope.Scope> = Effect.fnUntraced(
755
+ // Each tick's `dt` is clamped at `Machine.MAX_DT`, as every run's is. A step reads the change "since the end
756
+ // of the tick before", read by an `afterTick` hook, not the run's own last read: a run started inside a
757
+ // tick whose Late step doesn't come (back-to-back frames, or past the Late rounds) takes its first step at
758
+ // the next tick, and would otherwise count two frames' change over one `dt`. Its Late step, at the loop's
759
+ // tick count it started at, reads from the source before the change that started it, since the tick's hook
760
+ // has read past that change by then (#991, ADR-0043 *Amendment (#991)*). A change is over the time it took
761
+ // (#1046): the tick's dt, on the frame timeline, unless the time since the read at the run's own last step,
762
+ // a tick before, differs from it by more than half, a late frame's stamps misstating it either way (below).
763
+ export const velocity: (source: Atom.Atom<number>) => Effect.Effect<Atom.Atom<number>, never, Clock.Clock | FrameLoop.FrameLoop | AtomRegistry.AtomRegistry | Scope.Scope> = Effect.fnUntraced(
693
764
  function* (source: Atom.Atom<number>) {
694
765
  const registry = yield* AtomRegistry.AtomRegistry
695
766
  // Mounted for the Scope, not kept alive: a kept-alive Atom's node never leaves the registry.
696
767
  const out = Atom.make(0)
697
768
  const unmount = registry.mount(out)
698
769
  const run = yield* FiberHandle.makeRuntime<FrameLoop.FrameLoop, never, unknown>()
699
- // The source at the end of the tick before: where each tick's change is read from.
770
+ const context = yield* Effect.context<never>()
771
+ // A source that throws (an app's Atom under it) reads as still, its last read kept, reported at the
772
+ // first throw of a run of them, again after a clean read; its writes as follow's are (#979).
773
+ // Each read's try is written where it reads, around a direct call: a reader called from both measured
774
+ // +16% on Bun's velocity frame. A flag is cleared only when it's set: a check on the clean path.
775
+ let sourceFailing = false
776
+ let failing = false
777
+ // The source at the end of the tick before: where each tick's change is read from. A read that throws
778
+ // keeps it.
700
779
  let previous = registry.get(source)
780
+ // A run's start (#991): the source from before the change that started it, the loop's tick count then,
781
+ // and whether its first step is still due (only that step can be its Late step).
782
+ let seed = previous
783
+ let startedAt = 0
784
+ let firstDue = false
701
785
  const loop = yield* FrameLoop.FrameLoop
786
+ // When `previous` was read, on the Clock's `now()`, and at which tick; and the tick of the run's last
787
+ // step, `-1` before its first. A step right after the run's own last one divides its change by the time
788
+ // since that tick's read, which a late frame's stamps misstate (#1046): a running run keeps the loop
789
+ // ticking every frame, where the loop idles between runs and its tick count skips the frames it missed.
790
+ const clock = yield* Clock.Clock
791
+ let previousAt = 0
792
+ let previousTick = -1
793
+ let stepTick = -1
702
794
  yield* loop.afterTick(() => {
703
- previous = registry.get(source)
795
+ try {
796
+ previous = registry.get(source)
797
+ previousAt = clock.now()
798
+ previousTick = loop.tickCount()
799
+ if (sourceFailing) sourceFailing = false
800
+ } catch (defect) {
801
+ if (!sourceFailing) Report.failureOn(context, SOURCE_FAILED, Cause.die(defect))
802
+ sourceFailing = true
803
+ }
704
804
  return undefined
705
805
  })
706
806
  // A jump of the source isn't motion: the change is read from where it jumped to.
707
807
  const unhear = onJump(registry, source, (value) => {
708
808
  previous = value
809
+ seed = value
810
+ stepTick = -1
709
811
  })
710
812
  // One token for its lifetime, and one run program, started afresh on the change after a rest:
711
813
  // a change while a run goes costs a check, nothing built.
712
814
  const token = Supersede.make()
713
815
  let running = false
816
+ // Where a run's first step reads its change from, clearing its due flag: its Late step, at the tick
817
+ // count it started at, from the source before the change that started it (the tick's hook has moved
818
+ // `previous` past it); one at a later tick, from the end of the tick before, as every later step does.
819
+ const takeFirstFrom = (): number => {
820
+ firstDue = false
821
+ if (loop.tickCount() === startedAt) return seed
822
+ return previous
823
+ }
714
824
  const program = numberMachine("wardic.velocity", true, () => (dt) => {
715
- const step = Math.min(dt, Machine.MAX_DT)
825
+ // Over the tick's dt, unless, right after the run's own last step, the time since that tick's read
826
+ // differs from it by more than half: a late frame, whose stamps misstate it (#1046). An on-time frame's
827
+ // reads differ from its dt by their jitter, which the stamps don't have. A run's first step, its Late
828
+ // step among them, has no read of its own a tick before.
829
+ const tick = loop.tickCount()
830
+ let seconds = dt
831
+ if (stepTick === tick - 1 && previousTick === stepTick) {
832
+ const since = (clock.now() - previousAt) / 1000
833
+ if (Math.abs(since - dt) > dt / 2) seconds = since
834
+ }
835
+ stepTick = tick
836
+ const step = Math.min(seconds, Machine.MAX_DT)
716
837
  // A tick in no time (never from the Clock) reads as still.
717
838
  if (step === 0) return { value: 0, done: true }
718
- const v = (registry.get(source) - previous) / step
839
+ // A check on every later step: the first's alone reads the tick count (#991).
840
+ let before = previous
841
+ if (firstDue) before = takeFirstFrom()
842
+ // A read that throws reads as still.
843
+ let now = before
844
+ try {
845
+ now = registry.get(source)
846
+ if (sourceFailing) sourceFailing = false
847
+ } catch (defect) {
848
+ if (!sourceFailing) Report.failureOn(context, SOURCE_FAILED, Cause.die(defect))
849
+ sourceFailing = true
850
+ }
851
+ const v = (now - before) / step
719
852
  return { value: v, done: v === 0 }
720
853
  }).pipe(
721
854
  Machine.run((frame) => {
722
- registry.set(out, frame.value)
855
+ try {
856
+ registry.set(out, frame.value)
857
+ if (failing) failing = false
858
+ } catch (defect) {
859
+ if (!failing) Report.failureOn(context, DEPENDENT_FAILED, Cause.die(defect))
860
+ failing = true
861
+ }
723
862
  if (frame.done) running = false
724
863
  return undefined
725
- }, { supersede: token }),
864
+ }, { supersede: token, onFrameTimeline: true }),
726
865
  )
727
866
  const follow = (): void => {
728
867
  if (running) return
729
868
  running = true
869
+ seed = previous
870
+ startedAt = loop.tickCount()
871
+ firstDue = true
872
+ stepTick = -1
730
873
  run(program, { onlyIfMissing: true })
731
874
  }
732
875
  const unsubscribe = registry.subscribe(source, follow)
@@ -742,18 +885,24 @@ export const velocity: (source: Atom.Atom<number>) => Effect.Effect<Atom.Atom<nu
742
885
 
743
886
  /**
744
887
  * Milliseconds since it started, as Motion's `useTime`: wall time, minutes the app spent hidden included. It
745
- * keeps the Clock ticking every frame while it runs; its run ends with the Scope.
888
+ * keeps the Clock ticking every frame while it runs; its run ends with the Scope. An Atom over it that
889
+ * throws doesn't stop it: that's logged once until it's written cleanly.
746
890
  *
747
891
  * @example
748
892
  * const rotate = yield* LiveValue.transform(yield* LiveValue.time(), [0, 4000], [0, 360], { clamp: false })
749
893
  */
750
- // A run on the Frame loop that never settles, adding each tick's `dt` as the Clock gives it, unclamped.
894
+ // A run on the Frame loop that never settles, adding each tick's `dt` as the Clock gives it, unclamped. On
895
+ // the frame timeline (#1046, ADR-0027 *Amendment (#1046)*): it reports the frames' time, so a late first frame's
896
+ // counts whole, where an animation made in a long task leaves it out.
751
897
  export const time: () => Effect.Effect<Atom.Atom<number>, never, FrameLoop.FrameLoop | AtomRegistry.AtomRegistry | Scope.Scope> = Effect.fnUntraced(
752
898
  function* () {
753
899
  const registry = yield* AtomRegistry.AtomRegistry
754
900
  const out = Atom.make(0)
755
901
  const unmount = registry.mount(out)
756
902
  const run = yield* FiberHandle.makeRuntime<FrameLoop.FrameLoop, never, unknown>()
903
+ // Past a dependent that throws, it goes on counting, reported as follow's writes are (#979).
904
+ const context = yield* Effect.context<never>()
905
+ let failing = false
757
906
  const token = Supersede.make()
758
907
  const elapsed = numberMachine("wardic.time", false, () => {
759
908
  let ms = 0
@@ -765,9 +914,15 @@ export const time: () => Effect.Effect<Atom.Atom<number>, never, FrameLoop.Frame
765
914
  run(
766
915
  elapsed.pipe(
767
916
  Machine.run((frame) => {
768
- registry.set(out, frame.value)
917
+ try {
918
+ registry.set(out, frame.value)
919
+ if (failing) failing = false
920
+ } catch (defect) {
921
+ if (!failing) Report.failureOn(context, DEPENDENT_FAILED, Cause.die(defect))
922
+ failing = true
923
+ }
769
924
  return undefined
770
- }, { supersede: token }),
925
+ }, { supersede: token, onFrameTimeline: true }),
771
926
  ),
772
927
  )
773
928
  yield* Effect.addFinalizer(() =>
@@ -796,7 +951,8 @@ const FRAME_DELTA = { minimum: 1, maximum: 40 } as const
796
951
  * yield* LiveValue.eachFrame((time) => draw(context, time))
797
952
  */
798
953
  // #443. A run's first frame after the Clock wakes is the Clock's nominal frame, 16.7 ms, as Motion's is.
799
- // A throw is a run's defect (ADR-0027, decision 7).
954
+ // A throw is a run's defect (ADR-0027, decision 7). On the frame timeline (#1046): its time and delta are the
955
+ // frames' own, as Motion's `timestamp - initialTimestamp` is, a late frame's included.
800
956
  export const eachFrame: (callback: (time: number, delta: number) => void) => Effect.Effect<void, never, FrameLoop.FrameLoop | Scope.Scope> = Effect.fnUntraced(
801
957
  function* (callback: (time: number, delta: number) => void) {
802
958
  const run = yield* FiberHandle.makeRuntime<FrameLoop.FrameLoop, never, unknown>()
@@ -813,7 +969,7 @@ export const eachFrame: (callback: (time: number, delta: number) => void) => Eff
813
969
  return { value: ms, done: false }
814
970
  }
815
971
  })
816
- run(frames.pipe(Machine.run(constUndefined, { supersede: token })))
972
+ run(frames.pipe(Machine.run(constUndefined, { supersede: token, onFrameTimeline: true })))
817
973
  yield* Effect.addFinalizer(() => Effect.sync(() => Supersede.end(token)))
818
974
  })
819
975