@wardic/core 1.3.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/dist/BaseValue.d.ts +1 -1
  2. package/dist/BaseValue.js +2 -2
  3. package/dist/BaseValue.js.map +1 -1
  4. package/dist/Batch.d.ts +5 -9
  5. package/dist/Batch.d.ts.map +1 -1
  6. package/dist/Batch.js +195 -24
  7. package/dist/Batch.js.map +1 -1
  8. package/dist/Clock.d.ts +16 -0
  9. package/dist/Clock.d.ts.map +1 -1
  10. package/dist/Clock.js +13 -0
  11. package/dist/Clock.js.map +1 -1
  12. package/dist/DragRecognizer.d.ts +10 -5
  13. package/dist/DragRecognizer.d.ts.map +1 -1
  14. package/dist/DragRecognizer.js +60 -24
  15. package/dist/DragRecognizer.js.map +1 -1
  16. package/dist/Frame.js +1 -1
  17. package/dist/Frame.js.map +1 -1
  18. package/dist/InputFeed.d.ts +10 -7
  19. package/dist/InputFeed.d.ts.map +1 -1
  20. package/dist/InputFeed.js +16 -35
  21. package/dist/InputFeed.js.map +1 -1
  22. package/dist/LiveValue.d.ts +9 -3
  23. package/dist/LiveValue.d.ts.map +1 -1
  24. package/dist/LiveValue.js +160 -14
  25. package/dist/LiveValue.js.map +1 -1
  26. package/dist/Machine.d.ts +16 -6
  27. package/dist/Machine.d.ts.map +1 -1
  28. package/dist/Machine.js +50 -27
  29. package/dist/Machine.js.map +1 -1
  30. package/dist/Playback.d.ts.map +1 -1
  31. package/dist/Playback.js +10 -8
  32. package/dist/Playback.js.map +1 -1
  33. package/dist/Recognizer.d.ts +1 -1
  34. package/dist/Recognizer.d.ts.map +1 -1
  35. package/dist/Recognizer.js +9 -4
  36. package/dist/Recognizer.js.map +1 -1
  37. package/dist/internal/frameLoop.d.ts +10 -2
  38. package/dist/internal/frameLoop.d.ts.map +1 -1
  39. package/dist/internal/frameLoop.js +148 -18
  40. package/dist/internal/frameLoop.js.map +1 -1
  41. package/dist/internal/guarded.d.ts +16 -0
  42. package/dist/internal/guarded.d.ts.map +1 -0
  43. package/dist/internal/guarded.js +68 -0
  44. package/dist/internal/guarded.js.map +1 -0
  45. package/dist/internal/stepper.d.ts +9 -3
  46. package/dist/internal/stepper.d.ts.map +1 -1
  47. package/dist/internal/stepper.js +5 -4
  48. package/dist/internal/stepper.js.map +1 -1
  49. package/dist/unstable/AnimationControls.d.ts +15 -8
  50. package/dist/unstable/AnimationControls.d.ts.map +1 -1
  51. package/dist/unstable/AnimationControls.js +36 -15
  52. package/dist/unstable/AnimationControls.js.map +1 -1
  53. package/dist/unstable/AppearHandoff.d.ts +6 -0
  54. package/dist/unstable/AppearHandoff.d.ts.map +1 -1
  55. package/dist/unstable/AppearHandoff.js.map +1 -1
  56. package/dist/unstable/BatchCalls.d.ts +16 -1
  57. package/dist/unstable/BatchCalls.d.ts.map +1 -1
  58. package/dist/unstable/BatchCalls.js +8 -0
  59. package/dist/unstable/BatchCalls.js.map +1 -1
  60. package/dist/unstable/Config.d.ts +16 -1
  61. package/dist/unstable/Config.d.ts.map +1 -1
  62. package/dist/unstable/Config.js.map +1 -1
  63. package/dist/unstable/FlushGate.d.ts +4 -2
  64. package/dist/unstable/FlushGate.d.ts.map +1 -1
  65. package/dist/unstable/FlushGate.js +155 -38
  66. package/dist/unstable/FlushGate.js.map +1 -1
  67. package/dist/unstable/FrameEffects.d.ts +4 -1
  68. package/dist/unstable/FrameEffects.d.ts.map +1 -1
  69. package/dist/unstable/FrameEffects.js +12 -1
  70. package/dist/unstable/FrameEffects.js.map +1 -1
  71. package/dist/unstable/Imperative.d.ts +42 -11
  72. package/dist/unstable/Imperative.d.ts.map +1 -1
  73. package/dist/unstable/Imperative.js +413 -77
  74. package/dist/unstable/Imperative.js.map +1 -1
  75. package/dist/unstable/MeasuredPairs.d.ts +6 -2
  76. package/dist/unstable/MeasuredPairs.d.ts.map +1 -1
  77. package/dist/unstable/MeasuredPairs.js.map +1 -1
  78. package/dist/unstable/PresenceRecord.d.ts +4 -0
  79. package/dist/unstable/PresenceRecord.d.ts.map +1 -1
  80. package/dist/unstable/PresenceRecord.js.map +1 -1
  81. package/dist/unstable/Report.d.ts +7 -0
  82. package/dist/unstable/Report.d.ts.map +1 -1
  83. package/dist/unstable/Report.js +17 -1
  84. package/dist/unstable/Report.js.map +1 -1
  85. package/dist/unstable/RetargetLegs.d.ts +6 -0
  86. package/dist/unstable/RetargetLegs.d.ts.map +1 -1
  87. package/dist/unstable/RetargetLegs.js +15 -0
  88. package/dist/unstable/RetargetLegs.js.map +1 -1
  89. package/dist/unstable/Spine.d.ts +62 -10
  90. package/dist/unstable/Spine.d.ts.map +1 -1
  91. package/dist/unstable/Spine.js +190 -40
  92. package/dist/unstable/Spine.js.map +1 -1
  93. package/dist/unstable/VariantTarget.d.ts +25 -3
  94. package/dist/unstable/VariantTarget.d.ts.map +1 -1
  95. package/dist/unstable/VariantTarget.js +66 -13
  96. package/dist/unstable/VariantTarget.js.map +1 -1
  97. package/dist/unstable/VariantTree.d.ts +37 -3
  98. package/dist/unstable/VariantTree.d.ts.map +1 -1
  99. package/dist/unstable/VariantTree.js +54 -7
  100. package/dist/unstable/VariantTree.js.map +1 -1
  101. package/package.json +1 -1
  102. package/src/BaseValue.ts +2 -2
  103. package/src/Batch.ts +210 -20
  104. package/src/Clock.ts +20 -0
  105. package/src/DragRecognizer.ts +82 -30
  106. package/src/Frame.ts +1 -1
  107. package/src/InputFeed.ts +37 -40
  108. package/src/LiveValue.ts +140 -14
  109. package/src/Machine.ts +67 -34
  110. package/src/Playback.ts +10 -8
  111. package/src/Recognizer.ts +9 -3
  112. package/src/internal/frameLoop.ts +165 -19
  113. package/src/internal/guarded.ts +69 -0
  114. package/src/internal/stepper.ts +14 -5
  115. package/src/unstable/AnimationControls.ts +49 -21
  116. package/src/unstable/AppearHandoff.ts +7 -0
  117. package/src/unstable/BatchCalls.ts +20 -2
  118. package/src/unstable/Config.ts +24 -1
  119. package/src/unstable/FlushGate.ts +169 -37
  120. package/src/unstable/FrameEffects.ts +15 -2
  121. package/src/unstable/Imperative.ts +447 -89
  122. package/src/unstable/MeasuredPairs.ts +7 -1
  123. package/src/unstable/PresenceRecord.ts +4 -0
  124. package/src/unstable/Report.ts +19 -1
  125. package/src/unstable/RetargetLegs.ts +14 -0
  126. package/src/unstable/Spine.ts +244 -43
  127. package/src/unstable/VariantTarget.ts +93 -14
  128. package/src/unstable/VariantTree.ts +87 -6
package/src/InputFeed.ts CHANGED
@@ -31,7 +31,8 @@
31
31
  // authors; nothing here names a DOM type.
32
32
  import * as Arr from "effect/Array"
33
33
  import * as Bool from "effect/Boolean"
34
- import * as Cause from "effect/Cause"
34
+ import type * as Cause from "effect/Cause"
35
+ import type * as Context from "effect/Context"
35
36
  import * as Effect from "effect/Effect"
36
37
  import { constVoid } from "effect/Function"
37
38
  import * as MutableRef from "effect/MutableRef"
@@ -41,6 +42,7 @@ import * as Queue from "effect/Queue"
41
42
  import type * as Scope from "effect/Scope"
42
43
  import * as Stream from "effect/Stream"
43
44
  import * as UndefinedOr from "effect/UndefinedOr"
45
+ import { guarded, switchedOn } from "./internal/guarded.ts"
44
46
 
45
47
  /**
46
48
  * A source of events: called with `emit` and `end`, it calls `emit` with each event from then on, and
@@ -140,10 +142,13 @@ export const byTarget = <A>(lookup: (target: unknown) => InputFeed<A>): ((target
140
142
  }
141
143
 
142
144
  /**
143
- * Where a watch hands a throw from its handler, so a binding can report it its own way; the watch goes
144
- * on with the next event. With none, the throw goes up the platform's dispatch.
145
+ * Where a watch hands its handler's run of throws, at its first, so a binding can report it its own way:
146
+ * a run lasts from a throw to the next call that returns, and the watch goes on with the next event.
147
+ * With none, the run is reported then, on the context the watch started in, and the watch goes on.
145
148
  */
146
- // #318, ADR-0055. Core reports nothing (ADR-0030, decision 4).
149
+ // #318, ADR-0055. With none, core reports the throw itself (#979, row 27; ADR-0030, *Amendment (#979,
150
+ // groups D and G)*): where no binding's fiber is there to see it. Once a run either way (#979 group I,
151
+ // row 26; ADR-0074 decision 3).
147
152
  export type OnDefect = (cause: Cause.Cause<never>) => void
148
153
 
149
154
  /** A watch's handlers. */
@@ -151,7 +156,7 @@ export type OnDefect = (cause: Cause.Cause<never>) => void
151
156
  export interface Handlers<A> {
152
157
  /** Each event or value, inside the event's dispatch. */
153
158
  readonly on: (value: A) => void
154
- /** A throw from a handler. Without it, the throw goes up the platform's dispatch. */
159
+ /** A handler's run of throws, at its first. Without it, the run is reported then, and the watch goes on. */
155
160
  readonly onDefect?: OnDefect | undefined
156
161
  /** The feed ran out, as a test's script does. */
157
162
  readonly onEnd?: (() => void) | undefined
@@ -162,19 +167,6 @@ export interface Handlers<A> {
162
167
  readonly onStop?: (() => void) | undefined
163
168
  }
164
169
 
165
- /** `handle`, a throw from it handed to `onDefect` when there is one. */
166
- const guarded = <A>(handle: (event: A) => void, onDefect: OnDefect | undefined): ((event: A) => void) =>
167
- UndefinedOr.match(onDefect, {
168
- onUndefined: () => handle,
169
- onDefined: (defect) => (event: A): void => {
170
- try {
171
- handle(event)
172
- } catch (error) {
173
- defect(Cause.die(error))
174
- }
175
- },
176
- })
177
-
178
170
  /** A subscription its owner opens and closes: `switched`'s. */
179
171
  // #460.
180
172
  export interface Switch {
@@ -186,38 +178,39 @@ export interface Switch {
186
178
 
187
179
  /**
188
180
  * A subscription to `feed` its owner opens and closes, each event handed to `handle` inside its emit, a
189
- * throw to `onDefect`, as `watchWith`'s are. For a feed a watch needs only some of the time: a keyboard
181
+ * run of throws to `onDefect` at its first. With none, the run is reported then on Effect's default runtime, as
182
+ * a subscription has no context of its own, and it goes on. For a feed a watch needs only some of the time: a keyboard
190
183
  * press holds its target's keys only while focus is within it, so an element at rest holds none. The feed
191
184
  * running out ends nothing; its owner closes it when it's done.
192
185
  */
193
186
  // #460: a **Keyboard press**.
194
- export const switched = <A>(feed: InputFeed<A>, handle: (event: A) => void, onDefect?: OnDefect): Switch => {
195
- const on = guarded(handle, onDefect)
196
- let stop: (() => void) | undefined
197
- return {
198
- open: () => {
199
- if (Predicate.isUndefined(stop)) stop = feed(on, constVoid)
200
- },
201
- close: () => {
202
- if (Predicate.isUndefined(stop)) return
203
- const running = stop
204
- stop = undefined
205
- running()
206
- },
207
- }
208
- }
187
+ export const switched = <A>(feed: InputFeed<A>, handle: (event: A) => void, onDefect?: OnDefect): Switch => switchedOn(feed, handle, onDefect, undefined)
209
188
 
210
189
  /**
211
190
  * Watches `feed` while the Scope is open, with a handler that can stop: `make` makes this subscription's
212
191
  * handler, which hears each event inside its emit and returns whether to go on. `false` stops it, even
213
- * from inside the feed's start. A throw from it goes to `handlers.onDefect`, `handlers.onEnd` is told if
214
- * the feed runs out, and closing the Scope stops the feed.
192
+ * from inside the feed's start. A run of throws from it goes to `handlers.onDefect` at its first, else it's
193
+ * reported then on the context the watch started in, and the watch goes on. `handlers.onEnd` is told if the
194
+ * feed runs out, and closing the Scope stops the feed.
215
195
  */
216
196
  // #318, #379: no fiber and no Stream. A recognizer's `once` returns `false`.
217
197
  export const watchWith = <A>(
218
198
  feed: InputFeed<A>,
219
199
  make: () => (event: A) => boolean,
220
200
  handlers: Pick<Handlers<never>, "onDefect" | "onEnd" | "onStop">,
201
+ ): Effect.Effect<void, never, Scope.Scope> =>
202
+ // The context a throw with no `onDefect` is reported on (#979, row 27), read only for such a watch.
203
+ UndefinedOr.match(handlers.onDefect, {
204
+ onDefined: () => watching(feed, make, handlers, undefined),
205
+ onUndefined: () => Effect.flatMap(Effect.context<never>(), (context) => watching(feed, make, handlers, context)),
206
+ })
207
+
208
+ /** `watchWith`'s watch, a throw with no `onDefect` reported on `context`. */
209
+ const watching = <A>(
210
+ feed: InputFeed<A>,
211
+ make: () => (event: A) => boolean,
212
+ handlers: Pick<Handlers<never>, "onDefect" | "onEnd" | "onStop">,
213
+ context: Context.Context<never> | undefined,
221
214
  ): Effect.Effect<void, never, Scope.Scope> =>
222
215
  Effect.acquireRelease(
223
216
  Effect.sync(() => {
@@ -231,10 +224,14 @@ export const watchWith = <A>(
231
224
  if (Predicate.isNotUndefined(handlers.onStop)) handlers.onStop()
232
225
  }
233
226
  const stop = feed(
234
- guarded((event: A) => {
235
- if (MutableRef.get(done)) return
236
- if (Bool.not(handle(event))) finish()
237
- }, handlers.onDefect),
227
+ guarded(
228
+ (event: A) => {
229
+ if (MutableRef.get(done)) return
230
+ if (Bool.not(handle(event))) finish()
231
+ },
232
+ handlers.onDefect,
233
+ context,
234
+ ),
238
235
  () => {
239
236
  if (MutableRef.get(done)) return
240
237
  UndefinedOr.map(handlers.onEnd, (onEnd) => onEnd())
package/src/LiveValue.ts CHANGED
@@ -20,6 +20,7 @@
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"
@@ -40,6 +41,7 @@ import * as Machine from "./Machine.ts"
40
41
  import * as ScrollSource from "./ScrollSource.ts"
41
42
  import * as Supersede from "./Supersede.ts"
42
43
  import { dual } from "./internal/dual.ts"
44
+ import * as Report from "./unstable/Report.ts"
43
45
 
44
46
  /** A blend between two outputs, as Motion's `MixerFactory`: made once per stretch, then read at each progress through it, 0 to 1. */
45
47
  // #440.
@@ -164,7 +166,8 @@ const interpolateWith: <O>(
164
166
 
165
167
  /**
166
168
  * `source` mapped through ranges, as `interpolate` maps a number: an Atom that follows `source`, mapped
167
- * afresh at each change.
169
+ * afresh at each change. A `mixer` that throws keeps the value it gave last, and is logged once until it
170
+ * returns; one that throws for its first value throws to whatever reads it then.
168
171
  *
169
172
  * @example
170
173
  * const scale = yield* LiveValue.transform(scrollY, [0, 300], [1, 0.8])
@@ -173,7 +176,28 @@ export const transform: {
173
176
  <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
177
  <O>(source: Atom.Atom<number>, input: ReadonlyArray<number>, output: ReadonlyArray<O>, options: MixedTransformOptions<O>): Effect.Effect<Atom.Atom<O>, never, InterpolatorRegistry.InterpolatorRegistry>
175
178
  } = <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))
179
+ Effect.zipWith(interpolateWith(input, output, options), Effect.context<never>(), (map, context) => {
180
+ // An app's `mixer` that throws: its last value kept, reported at the first throw of a run of them, again
181
+ // after a clean one; with none yet to keep, its first value's throw is its reader's (#979). The try is in
182
+ // the read itself, around a direct call: a helper called from it measured a few points a change on Bun.
183
+ let last: O | undefined
184
+ let kept = false
185
+ let failing = false
186
+ const read = (get: Atom.AtomContext): O => {
187
+ const v = get(source)
188
+ try {
189
+ last = map(v)
190
+ kept = true
191
+ if (failing) failing = false
192
+ } catch (defect) {
193
+ if (!kept) throw defect
194
+ if (!failing) Report.failureOn(context, MIXER_FAILED, Cause.die(defect))
195
+ failing = true
196
+ }
197
+ return last as O
198
+ }
199
+ return transformedFrom(Atom.make(read), source, input, output, options)
200
+ })
177
201
 
178
202
  /** `v`'s stretch, as Motion's: the first that ends above it, else the last. Read on each change of the source. */
179
203
  const stretchAt = <O>(stretches: Arr.NonEmptyReadonlyArray<Stretch<O>>, v: number): Stretch<O> =>
@@ -377,6 +401,18 @@ const velocityOfFrame = (frame: Frame.Frame<number>): number => {
377
401
  return velocity
378
402
  }
379
403
 
404
+ // #979, group D (ADR-0074): a run goes on past an Atom over it, or under it, that throws, its failure
405
+ // reported on what it was built with (`Report.failureOn`), off the per-frame path.
406
+
407
+ /** A run's dependent that threw at its write: an app's Atom over its value, say. */
408
+ const DEPENDENT_FAILED = "wardic.liveValue: its dependent failed"
409
+
410
+ /** A run's source that threw as it was read: an app's Atom under it, say. */
411
+ const SOURCE_FAILED = "wardic.liveValue: its source failed"
412
+
413
+ /** A transform's ranges that threw: an app's `mixer`. */
414
+ const MIXER_FAILED = "wardic.transform: its mixer failed"
415
+
380
416
  const FOLLOW_SPAN: Machine.Span = { name: "wardic.follow", attributes: {} }
381
417
 
382
418
  /**
@@ -395,6 +431,9 @@ const FOLLOW_SPAN: Machine.Span = { name: "wardic.follow", attributes: {} }
395
431
  * is a `jump`; with `skipAnimations`, or while `skipped()` says so, every change is. Its run and its
396
432
  * subscription end with the Scope.
397
433
  *
434
+ * A source that throws as it's read is followed to where it was last read, and an Atom over it that throws
435
+ * doesn't stop it: each is logged once until it reads, or is written, cleanly.
436
+ *
398
437
  * @example
399
438
  * const eased = yield* LiveValue.follow(target, { type: "tween", duration: 0.4, ease: "easeOut" })
400
439
  */
@@ -408,6 +447,7 @@ export const follow: (
408
447
  ) => Effect.Effect<Followed, never, FrameLoop.FrameLoop | AtomRegistry.AtomRegistry | Scope.Scope> = Effect.fnUntraced(
409
448
  function* (source: Atom.Atom<number>, options: FollowOptions = {}, skipped: () => boolean = constFalse) {
410
449
  const registry = yield* AtomRegistry.AtomRegistry
450
+ const context = yield* Effect.context<never>()
411
451
  const { skipInitialAnimation, skipAnimations, ...transition } = options
412
452
  const plan = planOf(transition as FollowTransition)
413
453
  const skips = Bool.match(skipAnimations === true, { onTrue: () => constTrue, onFalse: () => skipped })
@@ -423,7 +463,22 @@ export const follow: (
423
463
  let changed = false
424
464
  // Whether the source's next change is a jump: its first, with `skipInitialAnimation`.
425
465
  let skipping = skipInitialAnimation === true
426
- const target = (): number => registry.get(source)
466
+ // A source that throws (an app's Atom under it): a run steering toward it keeps its last read,
467
+ // reported at the first throw of a run of them, again after a clean read (#979).
468
+ let lastTarget = registry.get(out)
469
+ let sourceFailing = false
470
+ const target = (): number => {
471
+ try {
472
+ lastTarget = registry.get(source)
473
+ if (sourceFailing) sourceFailing = false
474
+ } catch (defect) {
475
+ if (!sourceFailing) Report.failureOn(context, SOURCE_FAILED, Cause.die(defect))
476
+ sourceFailing = true
477
+ }
478
+ return lastTarget
479
+ }
480
+ // Whether its writes are throwing back a dependent's failure, reported as the source's are.
481
+ let failing = false
427
482
  /** One run: the plan's, from where `out` is, started again from where it's reached at the step after a change. */
428
483
  const machine = Machine.make({
429
484
  settles: false,
@@ -464,7 +519,15 @@ export const follow: (
464
519
  machine.pipe(
465
520
  Machine.run(
466
521
  (frame) => {
467
- registry.set(out, frame.value)
522
+ // Per frame, a try around the direct call: the registry has set it and told its other
523
+ // listeners before a dependent's throw comes back (an app's Atom over it), so the run goes on.
524
+ try {
525
+ registry.set(out, frame.value)
526
+ if (failing) failing = false
527
+ } catch (defect) {
528
+ if (!failing) Report.failureOn(context, DEPENDENT_FAILED, Cause.die(defect))
529
+ failing = true
530
+ }
468
531
  velocity = velocityOfFrame(frame)
469
532
  // At rest on the source: end the run; the next change starts another.
470
533
  if (frame.done) {
@@ -681,14 +744,18 @@ const numberMachine = (name: string, settles: boolean, start: () => (dt: number)
681
744
  * How fast `source` is changing, in its units a second, as Motion's `useVelocity`: 0 to begin with, and
682
745
  * from each change on, each frame's change over the frame's time, until a frame finds no change, sets 0
683
746
  * 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.
747
+ * acceleration. Its run ends with the Scope. A source that throws as it's read reads as still, and an Atom
748
+ * over it that throws doesn't stop it: each is logged once until it reads, or is written, cleanly.
685
749
  *
686
750
  * @example
687
751
  * const speed = yield* LiveValue.velocity(scrollY)
688
752
  */
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`.
753
+ // Each tick's `dt` is clamped at `Machine.MAX_DT`, as every run's is. A step reads the change "since the end
754
+ // of the tick before", read by an `afterTick` hook, not the run's own last read: a run started inside a
755
+ // tick whose Late step doesn't come (back-to-back frames, or past the Late rounds) takes its first step at
756
+ // the next tick, and would otherwise count two frames' change over one `dt`. Its Late step, at the loop's
757
+ // tick count it started at, reads from the source before the change that started it, since the tick's hook
758
+ // has read past that change by then (#991, ADR-0043 *Amendment (#991)*).
692
759
  export const velocity: (source: Atom.Atom<number>) => Effect.Effect<Atom.Atom<number>, never, FrameLoop.FrameLoop | AtomRegistry.AtomRegistry | Scope.Scope> = Effect.fnUntraced(
693
760
  function* (source: Atom.Atom<number>) {
694
761
  const registry = yield* AtomRegistry.AtomRegistry
@@ -696,30 +763,76 @@ export const velocity: (source: Atom.Atom<number>) => Effect.Effect<Atom.Atom<nu
696
763
  const out = Atom.make(0)
697
764
  const unmount = registry.mount(out)
698
765
  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.
766
+ const context = yield* Effect.context<never>()
767
+ // A source that throws (an app's Atom under it) reads as still, its last read kept, reported at the
768
+ // first throw of a run of them, again after a clean read; its writes as follow's are (#979).
769
+ // Each read's try is written where it reads, around a direct call: a reader called from both measured
770
+ // +16% on Bun's velocity frame. A flag is cleared only when it's set: a check on the clean path.
771
+ let sourceFailing = false
772
+ let failing = false
773
+ // The source at the end of the tick before: where each tick's change is read from. A read that throws
774
+ // keeps it.
700
775
  let previous = registry.get(source)
776
+ // A run's start (#991): the source from before the change that started it, the loop's tick count then,
777
+ // and whether its first step is still due (only that step can be its Late step).
778
+ let seed = previous
779
+ let startedAt = 0
780
+ let firstDue = false
701
781
  const loop = yield* FrameLoop.FrameLoop
702
782
  yield* loop.afterTick(() => {
703
- previous = registry.get(source)
783
+ try {
784
+ previous = registry.get(source)
785
+ if (sourceFailing) sourceFailing = false
786
+ } catch (defect) {
787
+ if (!sourceFailing) Report.failureOn(context, SOURCE_FAILED, Cause.die(defect))
788
+ sourceFailing = true
789
+ }
704
790
  return undefined
705
791
  })
706
792
  // A jump of the source isn't motion: the change is read from where it jumped to.
707
793
  const unhear = onJump(registry, source, (value) => {
708
794
  previous = value
795
+ seed = value
709
796
  })
710
797
  // One token for its lifetime, and one run program, started afresh on the change after a rest:
711
798
  // a change while a run goes costs a check, nothing built.
712
799
  const token = Supersede.make()
713
800
  let running = false
801
+ // Where a run's first step reads its change from, clearing its due flag: its Late step, at the tick
802
+ // count it started at, from the source before the change that started it (the tick's hook has moved
803
+ // `previous` past it); one at a later tick, from the end of the tick before, as every later step does.
804
+ const takeFirstFrom = (): number => {
805
+ firstDue = false
806
+ if (loop.tickCount() === startedAt) return seed
807
+ return previous
808
+ }
714
809
  const program = numberMachine("wardic.velocity", true, () => (dt) => {
715
810
  const step = Math.min(dt, Machine.MAX_DT)
716
811
  // A tick in no time (never from the Clock) reads as still.
717
812
  if (step === 0) return { value: 0, done: true }
718
- const v = (registry.get(source) - previous) / step
813
+ // A check on every later step: the first's alone reads the tick count (#991).
814
+ let before = previous
815
+ if (firstDue) before = takeFirstFrom()
816
+ // A read that throws reads as still.
817
+ let now = before
818
+ try {
819
+ now = registry.get(source)
820
+ if (sourceFailing) sourceFailing = false
821
+ } catch (defect) {
822
+ if (!sourceFailing) Report.failureOn(context, SOURCE_FAILED, Cause.die(defect))
823
+ sourceFailing = true
824
+ }
825
+ const v = (now - before) / step
719
826
  return { value: v, done: v === 0 }
720
827
  }).pipe(
721
828
  Machine.run((frame) => {
722
- registry.set(out, frame.value)
829
+ try {
830
+ registry.set(out, frame.value)
831
+ if (failing) failing = false
832
+ } catch (defect) {
833
+ if (!failing) Report.failureOn(context, DEPENDENT_FAILED, Cause.die(defect))
834
+ failing = true
835
+ }
723
836
  if (frame.done) running = false
724
837
  return undefined
725
838
  }, { supersede: token }),
@@ -727,6 +840,9 @@ export const velocity: (source: Atom.Atom<number>) => Effect.Effect<Atom.Atom<nu
727
840
  const follow = (): void => {
728
841
  if (running) return
729
842
  running = true
843
+ seed = previous
844
+ startedAt = loop.tickCount()
845
+ firstDue = true
730
846
  run(program, { onlyIfMissing: true })
731
847
  }
732
848
  const unsubscribe = registry.subscribe(source, follow)
@@ -742,7 +858,8 @@ export const velocity: (source: Atom.Atom<number>) => Effect.Effect<Atom.Atom<nu
742
858
 
743
859
  /**
744
860
  * 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.
861
+ * keeps the Clock ticking every frame while it runs; its run ends with the Scope. An Atom over it that
862
+ * throws doesn't stop it: that's logged once until it's written cleanly.
746
863
  *
747
864
  * @example
748
865
  * const rotate = yield* LiveValue.transform(yield* LiveValue.time(), [0, 4000], [0, 360], { clamp: false })
@@ -754,6 +871,9 @@ export const time: () => Effect.Effect<Atom.Atom<number>, never, FrameLoop.Frame
754
871
  const out = Atom.make(0)
755
872
  const unmount = registry.mount(out)
756
873
  const run = yield* FiberHandle.makeRuntime<FrameLoop.FrameLoop, never, unknown>()
874
+ // Past a dependent that throws, it goes on counting, reported as follow's writes are (#979).
875
+ const context = yield* Effect.context<never>()
876
+ let failing = false
757
877
  const token = Supersede.make()
758
878
  const elapsed = numberMachine("wardic.time", false, () => {
759
879
  let ms = 0
@@ -765,7 +885,13 @@ export const time: () => Effect.Effect<Atom.Atom<number>, never, FrameLoop.Frame
765
885
  run(
766
886
  elapsed.pipe(
767
887
  Machine.run((frame) => {
768
- registry.set(out, frame.value)
888
+ try {
889
+ registry.set(out, frame.value)
890
+ if (failing) failing = false
891
+ } catch (defect) {
892
+ if (!failing) Report.failureOn(context, DEPENDENT_FAILED, Cause.die(defect))
893
+ failing = true
894
+ }
769
895
  return undefined
770
896
  }, { supersede: token }),
771
897
  ),
package/src/Machine.ts CHANGED
@@ -70,6 +70,7 @@ import {
70
70
  stopTimesOf,
71
71
  timeIn,
72
72
  stepTween,
73
+ type WritableFrame,
73
74
  TweenFields,
74
75
  } from "./internal/stepper.ts"
75
76
  import * as Inertia from "./Inertia.ts"
@@ -95,7 +96,8 @@ export interface Machine<out F extends Frame.Frame<unknown>> extends Pipeable, I
95
96
  readonly [TypeId]: { readonly _F: Covariant<F> }
96
97
  /**
97
98
  * One run: a step from each tick's `dt` (seconds, as the Clock reports it) to that tick's
98
- * Frame. Each run holds its own state, so a machine can be started any number of times.
99
+ * Frame. Each run holds its own state, so a machine can be started any number of times. A Frame
100
+ * is the run's until its next step, which may rewrite it in place: copy one to keep it.
99
101
  */
100
102
  readonly start: () => (dt: number) => F
101
103
  /**
@@ -729,13 +731,12 @@ const isMultiPhysics = (frame: Frame.Frame<unknown>): frame is Frame.MultiPhysic
729
731
  Predicate.isObject((frame as Frame.MultiPhysics).velocity)
730
732
 
731
733
  /**
732
- * A Frame its Stream consumer owns: a record `value`, and a record `velocity` beside it, copied,
733
- * since a step may rewrite them in place (`springValues`, ADR-0030 decision 11). Any other Frame
734
- * passes as it is: a leaf's is fresh each tick, and a held one is never rewritten. On the
735
- * per-frame path (ADR-0025): statement `if`s and plain calls.
734
+ * A Frame its Stream consumer owns, copied, since a step may rewrite it in place: a record `value`, and
735
+ * a record `velocity` beside it (`springValues`, ADR-0030 decision 11), and any other Frame whole (a
736
+ * tween's, ADR-0030 *Amendment (#895)*). On the per-frame path (ADR-0025): statement `if`s and plain calls.
736
737
  */
737
738
  const owned = <F extends Frame.Frame<unknown>>(frame: F): F => {
738
- if (!Predicate.isObject(frame.value)) return frame
739
+ if (!Predicate.isObject(frame.value)) return { ...frame }
739
740
  if (isMultiPhysics(frame)) {
740
741
  // A `springValues` Frame's arrays are its run's, rewritten each tick: its copy is its records alone (#663).
741
742
  if (Frame.isSpringValues(frame)) return { value: { ...frame.value }, velocity: { ...frame.velocity }, done: frame.done } as unknown as F
@@ -759,8 +760,8 @@ export type Streamed<F> = F extends Frame.SpringValues ? Frame.MultiPhysics : F
759
760
  * yield* Machine.toStream(fadeIn).pipe(Stream.runForEach((frame) => Effect.sync(() => draw(frame.value))))
760
761
  */
761
762
  // Each subscription's run under the machine's one span, with a `<name>: subscribe` debug log annotated
762
- // with its attributes; per-frame work is unlogged. A record Frame is copied (`owned`) before it's
763
- // emitted, so a consumer that collects Frames never sees one rewritten (ADR-0030, decision 11). A
763
+ // with its attributes; per-frame work is unlogged. Every Frame is copied (`owned`) before it's
764
+ // emitted, so a consumer that collects Frames never sees one rewritten (ADR-0030, decision 11; #895). A
764
765
  // Stream, not a Channel: measured 2026-11-02, Stream's own layers were ~5% of a pull of the Clock's
765
766
  // ticks, and elements never use `toStream`, so a Channel would gain nothing measurable (#618, M5).
766
767
  export const toStream = <F extends Frame.Frame<unknown>>(self: Machine<F>): Stream.Stream<Streamed<F>, never, Clock.Clock> =>
@@ -793,8 +794,8 @@ export const toStream = <F extends Frame.Frame<unknown>>(self: Machine<F>): Stre
793
794
  // A plain call, typed `(frame) => undefined`, so a sink that returns an Effect doesn't compile
794
795
  // (ADR-0027, decision 8). Dies if the step or the sink throws, or with the loop's own failure. One span
795
796
  // per run, with a `<name>: start` debug log. The token (#200, ADR-0027 *Amendment (#200)*): it leaves the
796
- // loop at once, as an interrupted run does, about a sixth of the cost. `springValues` rewrites one Frame
797
- // per run (ADR-0030, decision 11). A machine built with a service: `Effect.flatMap(Machine.run(sink))`.
797
+ // loop at once, as an interrupted run does, about a sixth of the cost. `springValues`, a tween and the
798
+ // blends rewrite one Frame per run (ADR-0030, decision 11 and *Amendment (#895)*). A machine built with a service: `Effect.flatMap(Machine.run(sink))`.
798
799
  export const run: {
799
800
  <F extends Frame.Frame<unknown>>(sink: FrameLoop.Sink<F>): (self: Machine<F>) => Effect.Effect<F, never, FrameLoop.FrameLoop>
800
801
  <F extends Frame.Frame<unknown>>(
@@ -814,26 +815,34 @@ export const run: {
814
815
  FrameLoop.FrameLoop.use((loop) => loop.run(self, sink, options as RunOptions)),
815
816
  )
816
817
 
817
- /** `run`'s options: the token the run joins, so `Supersede.end` ends it with the others. */
818
- // #200.
818
+ /**
819
+ * `run`'s options: the token the run joins, so `Supersede.end` ends it with the others; and whether the
820
+ * run keeps the frame timeline's time, its start read from it (an Appear animation's `currentTime`), so
821
+ * the Frame loop never leaves a late frame's time out of its first steps (`Clock.LateFrames`).
822
+ */
823
+ // #200; #1026: a run on wall time (a video's `currentTime`, `performance.now()`) leaves `onFrameTimeline`
824
+ // out, and is right that way.
819
825
  export interface RunOptions {
820
- readonly supersede: Supersede.Supersede
826
+ readonly supersede?: Supersede.Supersede | undefined
827
+ readonly onFrameTimeline?: boolean | undefined
821
828
  }
822
829
 
823
830
  // --- The leaves --------------------------------------------------------------
824
831
 
825
832
  /** A tween's step from 0 elapsed: `tween`'s, and each of `animate`'s and `keyframes`' Intervals. */
833
+ // In place (#895, ADR-0030 *Amendment (#895)*): its time and its Frame are its run's, made at `start()` and
834
+ // rewritten each tick, as `springValues`' are (decision 11); `toStream` copies what it emits.
826
835
  const tweenLeaf = (from: number, to: number, tween: ResolvedTween, span: Span): Machine<Frame.Frame<number>> =>
827
836
  make({
828
837
  settles: true,
829
838
  span,
830
839
  start: () => {
831
- let state = initTween()
840
+ const state = initTween()
841
+ const frame: WritableFrame<number> = { value: from, done: false }
832
842
  // Per frame (ADR-0025): the tween resolved when the machine is built, `dt` clamped (#78).
833
843
  return (dt) => {
834
- const result = stepTween(state, Math.min(dt, MAX_DT), from, to, tween)
835
- state = result.state
836
- return { value: result.value, done: result.done }
844
+ stepTween(state, frame, Math.min(dt, MAX_DT), from, to, tween)
845
+ return frame
837
846
  }
838
847
  },
839
848
  })
@@ -845,7 +854,8 @@ const tweenSpan = (from: number, to: number, duration: number): Span => ({
845
854
  })
846
855
 
847
856
  /**
848
- * A tween from `from` to `to` over `options.duration`, eased by its `ease`, after its `delay`.
857
+ * A tween from `from` to `to` over `options.duration`, eased by its `ease`, after its `delay`. Its Frame is
858
+ * the run's, rewritten every tick: copy one to keep it.
849
859
  *
850
860
  * @example
851
861
  * const slide = Machine.tween(0, 240, { duration: 0.4, ease: "easeInOut" })
@@ -1369,6 +1379,7 @@ const resolveAnimate: (options: AnimateOptions) => ResolvedAnimate = cachedResol
1369
1379
  * `"mirror"`), and `reverse` swaps its ends. `delay` holds once, before the first play, as Motion's
1370
1380
  * does, and `repeatDelay` after each play but the last, where it ended. Each play starts the moment the
1371
1381
  * one before ends; a finite `repeat` settles once the last ends, and `repeat: Infinity` never settles.
1382
+ * Its Frame may be the run's, rewritten every tick: copy one to keep it.
1372
1383
  *
1373
1384
  * @example
1374
1385
  * const breathe = Machine.animate(1, 1.08, { duration: 1.2, ease: "easeInOut", repeat: Infinity, repeatType: "mirror" })
@@ -1509,26 +1520,48 @@ const blenderFor = (registry: InterpolatorRegistry.InterpolatorRegistry["Service
1509
1520
  Option.getOrElse(registry.keyBlendOf(key, a, b), () => Effect.map(registry.resolve(a, b), (blend): Blender => (t) => blend(t)))
1510
1521
 
1511
1522
  /**
1512
- * Every key's blend at one progress Frame. On the per-frame path (ADR-0025): a loop over the
1513
- * `[key, blender]` pairs built with the machine, not `Rec.map`, which spreads the record and then
1514
- * reassigns every key (#63 measured `Rec.map` 1.6× this loop in Chromium, 2.3× in JSC).
1523
+ * `progress`'s run with every key blended at the progress `at` reads off its Frame. On the per-frame path
1524
+ * (ADR-0025): a loop over the `[key, blender]` pairs built with the machine, not `Rec.map`, which spreads the
1525
+ * record and then reassigns every key (#63 measured `Rec.map` 1.6× this loop in Chromium, 2.3× in JSC).
1515
1526
  */
1516
- const blendAt = (
1527
+ // In place (#895, ADR-0030 *Amendment (#895)*): its record and its Frame are its run's, made at `start()` and
1528
+ // rewritten each tick, as `springValues`' are (decision 11); `toStream` copies what it emits.
1529
+ const blendedBy = <F extends Frame.Frame<unknown>>(
1530
+ progress: Machine<F>,
1517
1531
  blenders: ReadonlyArray<readonly [key: string, blend: Blender]>,
1518
- t: number,
1519
- done: boolean,
1520
- ): Frame.Frame<Values.Values> => {
1521
- const value: Record<string, number | string> = {}
1522
- for (const pair of blenders) value[pair[0]] = pair[1](t, done)
1523
- return { value, done }
1524
- }
1532
+ at: (frame: F) => number,
1533
+ ): Machine<Frame.Frame<Values.Values>> =>
1534
+ make({
1535
+ settles: progress.settles,
1536
+ span: progress.span,
1537
+ start: () => {
1538
+ const step = progress.start()
1539
+ const frame: WritableFrame<Record<string, number | string>> = { value: {}, done: false }
1540
+ return (dt) => {
1541
+ const stepped = step(dt)
1542
+ const t = at(stepped)
1543
+ const done = stepped.done
1544
+ const value = frame.value
1545
+ for (const pair of blenders) value[pair[0]] = pair[1](t, done)
1546
+ frame.done = done
1547
+ return frame
1548
+ }
1549
+ },
1550
+ })
1551
+
1552
+ /** A tween's progress, 0 to 1. */
1553
+ const tweenProgressOf = (frame: Frame.Frame<number>): number => frame.value
1554
+
1555
+ /** A progress spring's progress, 0 to 1 from its 0 to 100 (#663: its one key by index). */
1556
+ const springProgressOf = (frame: Frame.SpringValues): number => frame.positions[0] / 100
1525
1557
 
1526
1558
  /**
1527
1559
  * A bag of values tweened from `from` to `to`, each key blended as its Environment blends it: numbers,
1528
1560
  * unit strings, complex values, and its own kinds, such as `@wardic/dom`'s colours. One tween drives every
1529
1561
  * key, so the repeats and `delay` apply to the whole bag. A pair that can't blend switches at the first
1530
1562
  * frame; the Environment can hold a key until the last, as `@wardic/dom` does a key that hides an element.
1531
- * Building it needs the `InterpolatorRegistry`, once.
1563
+ * Building it needs the `InterpolatorRegistry`, once. Its Frame and its record are the run's, rewritten
1564
+ * every tick: copy one to keep it.
1532
1565
  *
1533
1566
  * @example
1534
1567
  * const fade = yield* Machine.animateValues({ opacity: 0, color: "#bf616a" }, { opacity: 1, color: "#a3be8c" }, { duration: 0.3 })
@@ -1544,7 +1577,7 @@ export const animateValues = (
1544
1577
  // Per leg (#460): the combinators called data-first.
1545
1578
  Effect.map(blendersFor(from, to), (blenders) =>
1546
1579
  withSpan(
1547
- map(animate(0, 1, options), (progress) => blendAt(blenders, progress.value, progress.done)),
1580
+ blendedBy(animate(0, 1, options), blenders, tweenProgressOf),
1548
1581
  "wardic.animateValues",
1549
1582
  { attributes: { keys: blenders.length } },
1550
1583
  ))
@@ -1552,7 +1585,8 @@ export const animateValues = (
1552
1585
  /**
1553
1586
  * A bag of values blended from `from` to `to` by one spring's progress: the spring runs from rest on a
1554
1587
  * 0 to 100 progress, and each key is blended at progress / 100. It's how a spring moves values that
1555
- * aren't numbers, a colour or a unit string, as Motion's spring does.
1588
+ * aren't numbers, a colour or a unit string, as Motion's spring does. Its Frame and its record are the
1589
+ * run's, rewritten every tick: copy one to keep it.
1556
1590
  *
1557
1591
  * - **The progress spans 0 to 100**, as a number going 100 would, so `restDelta` and `restSpeed` mean
1558
1592
  * the same for both, and a string key beside an `x` going 0 to 100 lands with it.
@@ -1572,8 +1606,7 @@ export const springBlend = (
1572
1606
  Effect.map(blendersFor(from, to), (blenders) =>
1573
1607
  withSpan(
1574
1608
  // Its one key read by index (#663).
1575
- map(springValues({ progress: 100 }, options, { from: { progress: 0 }, velocity: { progress: 0 }, records: "value" }), (frame) =>
1576
- blendAt(blenders, frame.positions[0] / 100, frame.done)),
1609
+ blendedBy(springValues({ progress: 100 }, options, { from: { progress: 0 }, velocity: { progress: 0 }, records: "value" }), blenders, springProgressOf),
1577
1610
  "wardic.springBlend",
1578
1611
  { attributes: { keys: blenders.length } },
1579
1612
  ))