@wardic/core 1.3.1 → 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 (123) 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 +166 -20
  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/Config.d.ts +5 -1
  57. package/dist/unstable/Config.d.ts.map +1 -1
  58. package/dist/unstable/Config.js.map +1 -1
  59. package/dist/unstable/FlushGate.d.ts +4 -2
  60. package/dist/unstable/FlushGate.d.ts.map +1 -1
  61. package/dist/unstable/FlushGate.js +155 -38
  62. package/dist/unstable/FlushGate.js.map +1 -1
  63. package/dist/unstable/FrameEffects.d.ts +4 -1
  64. package/dist/unstable/FrameEffects.d.ts.map +1 -1
  65. package/dist/unstable/FrameEffects.js +12 -1
  66. package/dist/unstable/FrameEffects.js.map +1 -1
  67. package/dist/unstable/Imperative.d.ts +13 -4
  68. package/dist/unstable/Imperative.d.ts.map +1 -1
  69. package/dist/unstable/Imperative.js +202 -43
  70. package/dist/unstable/Imperative.js.map +1 -1
  71. package/dist/unstable/MeasuredPairs.d.ts +6 -2
  72. package/dist/unstable/MeasuredPairs.d.ts.map +1 -1
  73. package/dist/unstable/MeasuredPairs.js.map +1 -1
  74. package/dist/unstable/PresenceRecord.d.ts +4 -0
  75. package/dist/unstable/PresenceRecord.d.ts.map +1 -1
  76. package/dist/unstable/PresenceRecord.js.map +1 -1
  77. package/dist/unstable/Report.d.ts +7 -0
  78. package/dist/unstable/Report.d.ts.map +1 -1
  79. package/dist/unstable/Report.js +17 -1
  80. package/dist/unstable/Report.js.map +1 -1
  81. package/dist/unstable/RetargetLegs.d.ts +6 -0
  82. package/dist/unstable/RetargetLegs.d.ts.map +1 -1
  83. package/dist/unstable/RetargetLegs.js +15 -0
  84. package/dist/unstable/RetargetLegs.js.map +1 -1
  85. package/dist/unstable/Spine.d.ts +53 -7
  86. package/dist/unstable/Spine.d.ts.map +1 -1
  87. package/dist/unstable/Spine.js +190 -40
  88. package/dist/unstable/Spine.js.map +1 -1
  89. package/dist/unstable/VariantTarget.d.ts +25 -3
  90. package/dist/unstable/VariantTarget.d.ts.map +1 -1
  91. package/dist/unstable/VariantTarget.js +66 -13
  92. package/dist/unstable/VariantTarget.js.map +1 -1
  93. package/dist/unstable/VariantTree.d.ts +37 -3
  94. package/dist/unstable/VariantTree.d.ts.map +1 -1
  95. package/dist/unstable/VariantTree.js +54 -7
  96. package/dist/unstable/VariantTree.js.map +1 -1
  97. package/package.json +1 -1
  98. package/src/BaseValue.ts +2 -2
  99. package/src/Batch.ts +167 -17
  100. package/src/Clock.ts +20 -0
  101. package/src/DragRecognizer.ts +82 -30
  102. package/src/Frame.ts +1 -1
  103. package/src/InputFeed.ts +37 -40
  104. package/src/LiveValue.ts +140 -14
  105. package/src/Machine.ts +67 -34
  106. package/src/Playback.ts +10 -8
  107. package/src/Recognizer.ts +9 -3
  108. package/src/internal/frameLoop.ts +165 -19
  109. package/src/internal/guarded.ts +69 -0
  110. package/src/internal/stepper.ts +14 -5
  111. package/src/unstable/AnimationControls.ts +49 -21
  112. package/src/unstable/AppearHandoff.ts +7 -0
  113. package/src/unstable/Config.ts +7 -1
  114. package/src/unstable/FlushGate.ts +169 -37
  115. package/src/unstable/FrameEffects.ts +15 -2
  116. package/src/unstable/Imperative.ts +219 -48
  117. package/src/unstable/MeasuredPairs.ts +7 -1
  118. package/src/unstable/PresenceRecord.ts +4 -0
  119. package/src/unstable/Report.ts +19 -1
  120. package/src/unstable/RetargetLegs.ts +14 -0
  121. package/src/unstable/Spine.ts +235 -40
  122. package/src/unstable/VariantTarget.ts +93 -14
  123. package/src/unstable/VariantTree.ts +87 -6
@@ -54,7 +54,7 @@
54
54
  * run gets at its start when the check is on, so the tick is the same either way.
55
55
  *
56
56
  * Internal (ADR-0028, decision 11): the service's shape (`run`, `runLast`, `afterTick`,
57
- * `inLateWindow`) and `make`, behind `FrameLoop.layer`.
57
+ * `inLateWindow`, `tickCount`, `hint`) and `make`, behind `FrameLoop.layer`.
58
58
  */
59
59
  import * as Arr from "effect/Array"
60
60
  import * as Bool from "effect/Boolean"
@@ -80,6 +80,7 @@ import type * as Frame from "../Frame.ts"
80
80
  import * as Supersede from "../Supersede.ts"
81
81
  import type * as FrameLoop from "../FrameLoop.ts"
82
82
  import type * as Machine from "../Machine.ts"
83
+ import * as Report from "../unstable/Report.ts"
83
84
  import { none, SpansGiven, traced } from "./tracing.ts"
84
85
 
85
86
  // @wardic/core compiles against ES2022 alone, which does not declare `performance` (see
@@ -88,11 +89,13 @@ declare const performance: { readonly now: () => number }
88
89
  // The same for `queueMicrotask` (see Microtask.ts): the Late window's end (#680).
89
90
  declare const queueMicrotask: (callback: () => void) => void
90
91
 
91
- /** A run's options: the token it joins, so its caller can end it with others. */
92
- // #200.
92
+ /** A run's options: the token it joins, so its caller can end it with others, and its timeline. */
93
+ // #200; #1026.
93
94
  export interface RunOptions {
94
95
  /** The token: `Supersede.end` on it ends every run that joined it. */
95
- readonly supersede: Supersede.Supersede
96
+ readonly supersede?: Supersede.Supersede | undefined
97
+ /** Its start read from the frame timeline: it steps at the ticks' dt from its first frame ({@link dtOf}). */
98
+ readonly onFrameTimeline?: boolean | undefined
96
99
  }
97
100
 
98
101
  /** `run`'s and `runLast`'s shape: a run given a token can also end `Superseded`. */
@@ -140,6 +143,13 @@ export interface Shape {
140
143
  // ADR-0027 *Amendment (#680)* (was `ticking()`, #673): the projection's Measure pass reads it at a
141
144
  // trigger, to know it will run after its frame's tick.
142
145
  readonly inLateWindow: () => boolean
146
+ /**
147
+ * How many ticks the loop has run: 0 before any. Neither a Late round nor a binding's hint window is a
148
+ * tick, so a run's Late step reads the count it was started at, and any step at a later tick a greater one.
149
+ */
150
+ // #991 (ADR-0043, *Amendment (#991)*): `velocity` tells its run's Late step by it. Not the Clock's
151
+ // `frameAt`, which repeats on Firefox's frame after an input (#18) and is `NaN` before any frame.
152
+ readonly tickCount: () => number
143
153
  /**
144
154
  * A binding's hint: its code runs inside the Refresh frame whose time is `frameTime`, after the
145
155
  * frame's tick, as a binding that renders in a frame callback of its own does (ADR-0071 decision 32).
@@ -156,6 +166,8 @@ export interface Shape {
156
166
  /** One hook on the loop's ticks, one object per `afterTick`, so removing it removes only that one. */
157
167
  interface Hook {
158
168
  readonly hook: (dt: number) => undefined
169
+ /** Whether its calls are throwing: reported at the first, again once one has returned. */
170
+ failing: boolean
159
171
  }
160
172
 
161
173
  /** One run on the loop. */
@@ -175,11 +187,36 @@ interface Entry {
175
187
  late: boolean
176
188
  /** Its span, when traced: marked `late` at its Late step (#680). */
177
189
  readonly span: Tracer.Span | undefined
190
+ /**
191
+ * What a long task held of its first frames, in seconds, for it to leave out (#1026, `firstDt`): `NaN`
192
+ * until its first step, then what of that frame a long task held, which its second step leaves out, then
193
+ * 0. 0 from the start for a run on the frame timeline, and on an Environment that tells no late frame.
194
+ */
195
+ owed: number
196
+ /** When it was made, on the Clock's `now()` (ms), where `owed` starts `NaN`. */
197
+ readonly madeAt: number
198
+ /** Made in the Late window of a frame a long task held: what it held, in seconds, which a first step at a later tick leaves out. */
199
+ readonly madeLate: number
178
200
  }
179
201
 
180
202
  /** How many Late rounds a window runs before the rest wait for the next tick: the Measure pass's cap (#680). */
181
203
  const MAX_LATE_ROUNDS = 5
182
204
 
205
+ /** The line past which a Refresh frame ran late, in ms (`Clock.LateFrames`): `NaN` where none is told. */
206
+ const lateAfterOf = (timing: Clock.LateFrameTiming): number => {
207
+ if (timing === "timestamp") return Number.NaN
208
+ return timing.ranAfter
209
+ }
210
+
211
+ /** Whether a run keeps the frame timeline's time (`RunOptions.onFrameTimeline`). */
212
+ const onFrameTimeline = (options: RunOptions | undefined): boolean => {
213
+ if (Predicate.isUndefined(options)) return false
214
+ return options.onFrameTimeline === true
215
+ }
216
+
217
+ /** `dt`, less `left` seconds left out of it: a nominal frame at least, and never more than `dt`. */
218
+ const leavingOut = (dt: number, left: number): number => Math.min(dt, Math.max(dt - left, Clock.NOMINAL_DT))
219
+
183
220
  /**
184
221
  * The Frame loop's metrics: the running animations, the runs that settled and failed, and each tick's
185
222
  * ms. None unless an app gives them (`FrameLoop.layerMetrics`, #518), so an app that reads none ships and
@@ -304,6 +341,10 @@ export const make = Effect.fnUntraced(
304
341
  const clock = yield* Clock.Clock
305
342
  // The burst runs here, under the Layer's build context, never in a caller's.
306
343
  const buildContext = yield* Effect.context<Clock.Clock | Scope.Scope>()
344
+ /** A hook's throw, reported on what the loop was built with: its failure path. */
345
+ const hookFailed = (defect: unknown): void => {
346
+ Effect.runForkWith(buildContext)(Report.failure("wardic.frameLoop: an afterTick hook failed", Cause.die(defect)))
347
+ }
307
348
  // The burst's fiber (#746: what a `FiberHandle` held, in a field, so an app ships no FiberHandle): a
308
349
  // new burst's replaces it, interrupting it, and the loop's Scope interrupts it and lets it go. Once
309
350
  // that Scope has closed, the loop takes no run (`entered`), as the handle's closed state had it.
@@ -346,6 +387,16 @@ export const make = Effect.fnUntraced(
346
387
  // The dt of the tick that opened the window: a Late step's. A nominal frame before any tick, for a
347
388
  // window a hint opens while idle (#791).
348
389
  let windowDt = Clock.NOMINAL_DT
390
+ // The ticks run, for `tickCount` (#991): one add a tick.
391
+ let ticked = 0
392
+ // A late frame, as the Environment tells one (#1026, `Clock.LateFrames`): the line, in ms, `NaN` for
393
+ // none. Telling one, each tick reads its frame's timestamp and when it ran, on the Clock's `now()`, and
394
+ // what a long task held of it, in seconds; a window a hint opens tells nothing.
395
+ const lateAfter = lateAfterOf(yield* Clock.LateFrames)
396
+ const tellsLate = !Number.isNaN(lateAfter)
397
+ let windowRanAt = Number.NaN
398
+ let windowLate = 0
399
+ let lastStamp = Number.NaN
349
400
  // The `run` runs added in the window, in add order: their Late step is due. The `runLast` runs added
350
401
  // in it take none, and are marked only so the dt-0 step leaves them out: they step at the next tick.
351
402
  const lateFirst: Array<Entry> = []
@@ -379,11 +430,60 @@ export const make = Effect.fnUntraced(
379
430
  }
380
431
  // The loop's own hook, the first: the counts reach the counters at the end of every tick, and
381
432
  // `tick`, the per-frame path, is as it was (#542).
382
- hooks = [{ hook: countEnds }]
433
+ hooks = [{ hook: countEnds, failing: false }]
383
434
 
384
435
  // Per frame (ADR-0025): plain calls only, in the one `Stream.map` call per tick (ADR-0027,
385
436
  // decision 8); counted loops, statement `if`s, a `try` per entry (it costs nothing measurable).
386
437
 
438
+ /**
439
+ * The tick's frame, told late or on time (#1026): late once its callback ran past the line after its
440
+ * timestamp, a long task holding it. A frame that repeats the one before's timestamp (Firefox's after
441
+ * an input), which the Clock times by `now()`, is on time. Once a tick, where late frames are told.
442
+ */
443
+ const tellLate = (): void => {
444
+ const stamp = clock.frameAt()
445
+ const ranAt = clock.now()
446
+ let late = 0
447
+ if (stamp !== lastStamp && ranAt - stamp > lateAfter) late = (ranAt - stamp) / 1000
448
+ lastStamp = stamp
449
+ windowRanAt = ranAt
450
+ windowLate = late
451
+ }
452
+
453
+ /**
454
+ * The dt `entry` steps at (#1026): the tick's, but a run takes none of a long task's time from before it
455
+ * was on the page. A long task delays a frame, whose timestamp is the time it was due, from before the
456
+ * task, so the next frame's dt holds the task's time. A run made in that task leaves out what that
457
+ * frame was held of its second step; one made in that frame's Late window and first stepped at the next
458
+ * tick, of its first; and one that waited past the line for its first frame (Motion's
459
+ * `MAX_START_DELAY`, however its frame is stamped) begins there, a 60 Hz frame at most. What was
460
+ * running catches up, as before; a run on the frame timeline keeps the ticks' dt. Per frame: a field
461
+ * read, and nothing made.
462
+ */
463
+ const dtOf = (entry: Entry, dt: number): number => {
464
+ const owed = entry.owed
465
+ if (owed === 0) return dt
466
+ if (owed > 0) {
467
+ entry.owed = 0
468
+ return leavingOut(dt, owed)
469
+ }
470
+ return firstDt(entry, dt, entry.madeLate)
471
+ }
472
+
473
+ /** A run's first step (`owed` `NaN`): what of its frame a long task held, owed to its second; `before` left out. */
474
+ const firstDt = (entry: Entry, dt: number, before: number): number => {
475
+ entry.owed = windowLate
476
+ if (windowRanAt - entry.madeAt > lateAfter) return Math.min(dt, Clock.NOMINAL_DT)
477
+ if (before > 0) return leavingOut(dt, before)
478
+ return dt
479
+ }
480
+
481
+ /** A Late step's dt: its first step, in the window it was made in, with nothing of the window's left out. */
482
+ const lateDtOf = (entry: Entry, dt: number): number => {
483
+ if (entry.owed === 0) return dt
484
+ return firstDt(entry, dt, 0)
485
+ }
486
+
387
487
  /**
388
488
  * Step the last lane's first `count` entries, in add order, as `tick` steps the others. Per frame,
389
489
  * and only while it holds a run.
@@ -393,8 +493,9 @@ export const make = Effect.fnUntraced(
393
493
  const entry = lastEntries[i]
394
494
  if (entry.removed) continue
395
495
  try {
396
- const frame = entry.step(dt)
397
- entry.sink(frame, dt)
496
+ const at = dtOf(entry, dt)
497
+ const frame = entry.step(at)
498
+ entry.sink(frame, at)
398
499
  if (entry.settles && frame.done) end(entry, Exit.succeed(frame))
399
500
  } catch (defect) {
400
501
  end(entry, dieNamed(entry.name, defect))
@@ -418,11 +519,13 @@ export const make = Effect.fnUntraced(
418
519
  // The first lane's loops stay inline in `tick`: moved into a function shared with the last lane,
419
520
  // Node's `chase/loop` ran 55% slower (`bench:core`, ADR-0027 *Amendment (#137)*).
420
521
  const tick = (dt: number): boolean => {
522
+ ticked++
421
523
  // A window this tick finds open is the last frame's, its checkpoint not yet run (back-to-back
422
524
  // frames in a test): its runs are on the lanes, so this tick steps them.
423
525
  if (lateFirst.length + lateLast.length > 0) dropLate()
424
526
  windowOpen = true
425
527
  windowDt = dt
528
+ if (tellsLate) tellLate()
426
529
  // The tick's start, read only for the metrics' histogram (#518).
427
530
  let t0 = 0
428
531
  if (Predicate.isNotUndefined(metrics)) t0 = performance.now()
@@ -434,8 +537,9 @@ export const make = Effect.fnUntraced(
434
537
  const entry = entries[i]
435
538
  if (entry.removed) continue
436
539
  try {
437
- const frame = entry.step(dt)
438
- entry.sink(frame, dt)
540
+ const at = dtOf(entry, dt)
541
+ const frame = entry.step(at)
542
+ entry.sink(frame, at)
439
543
  if (entry.settles && frame.done) end(entry, Exit.succeed(frame))
440
544
  } catch (defect) {
441
545
  end(entry, dieNamed(entry.name, defect))
@@ -459,8 +563,19 @@ export const make = Effect.fnUntraced(
459
563
  }
460
564
  // The end of the tick: everything for this Refresh frame is written (ADR-0033). After
461
565
  // `tickMs`, which stays the cost of the steps alone.
566
+ // Each hook's own (#979, ADR-0074): one that throws is reported once until a call returns, and the
567
+ // others run. A try per hook, around its direct call, its flag cleared only when it's set.
462
568
  const ended = hooks
463
- for (let i = 0; i < ended.length; i++) ended[i].hook(dt)
569
+ for (let i = 0; i < ended.length; i++) {
570
+ const added = ended[i]
571
+ try {
572
+ added.hook(dt)
573
+ if (added.failing) added.failing = false
574
+ } catch (defect) {
575
+ if (!added.failing) hookFailed(defect)
576
+ added.failing = true
577
+ }
578
+ }
464
579
  // The window stays open to the end of this frame's microtasks, which take the Late steps (#680).
465
580
  if (!windowQueued) {
466
581
  windowQueued = true
@@ -486,11 +601,14 @@ export const make = Effect.fnUntraced(
486
601
  list.length = left
487
602
  }
488
603
 
489
- /** One step of `entry` at `dt`: its Frame to its sink, its settle, or its defect, as a tick's. */
490
- const stepOne = (entry: Entry, dt: number): void => {
604
+ /**
605
+ * One step of `entry` at `at`: its Frame to its sink, its settle, or its defect, as a tick's. A Late
606
+ * step's dt is its first's (`lateDtOf`); a dt-0 step leaves what it owes as it was.
607
+ */
608
+ const stepOne = (entry: Entry, at: number): void => {
491
609
  try {
492
- const frame = entry.step(dt)
493
- entry.sink(frame, dt)
610
+ const frame = entry.step(at)
611
+ entry.sink(frame, at)
494
612
  if (entry.settles && frame.done) end(entry, Exit.succeed(frame))
495
613
  } catch (defect) {
496
614
  end(entry, dieNamed(entry.name, defect))
@@ -502,7 +620,7 @@ export const make = Effect.fnUntraced(
502
620
  entry.late = false
503
621
  if (entry.removed) return false
504
622
  if (Predicate.isNotUndefined(entry.span)) entry.span.attribute("late", true)
505
- stepOne(entry, windowDt)
623
+ stepOne(entry, lateDtOf(entry, windowDt))
506
624
  return true
507
625
  }
508
626
 
@@ -525,7 +643,17 @@ export const make = Effect.fnUntraced(
525
643
  }
526
644
  dropFirst(lateFirst, firstCount)
527
645
  const again = hooks
528
- for (let i = 0; i < again.length; i++) again[i].hook(windowDt)
646
+ // As the tick's (#979).
647
+ for (let i = 0; i < again.length; i++) {
648
+ const added = again[i]
649
+ try {
650
+ added.hook(windowDt)
651
+ if (added.failing) added.failing = false
652
+ } catch (defect) {
653
+ if (!added.failing) hookFailed(defect)
654
+ added.failing = true
655
+ }
656
+ }
529
657
  }
530
658
 
531
659
  /**
@@ -572,14 +700,19 @@ export const make = Effect.fnUntraced(
572
700
  if (windowOpen) return
573
701
  if (bursting && clock.frameAt() !== frameTime) return
574
702
  windowOpen = true
703
+ // No tick told its frame (#1026): a run made in it is told nothing late.
704
+ windowRanAt = Number.NaN
705
+ windowLate = 0
575
706
  if (!windowQueued) {
576
707
  windowQueued = true
577
708
  queueMicrotask(endLateWindow)
578
709
  }
579
710
  }
580
711
 
712
+ const tickCount = (): number => ticked
713
+
581
714
  const afterTick = (hook: (dt: number) => undefined): Effect.Effect<void, never, Scope.Scope> => {
582
- const added: Hook = { hook }
715
+ const added: Hook = { hook, failing: false }
583
716
  return Effect.acquireRelease(
584
717
  Effect.sync(() => {
585
718
  hooks = Arr.append(hooks, added)
@@ -709,6 +842,16 @@ export const make = Effect.fnUntraced(
709
842
  if (closed) return Effect.interrupt
710
843
  let checkedSink: FrameLoop.Sink<F> = sink
711
844
  if (check) checkedSink = checked(sink)
845
+ // What a long task held of its first frames (#1026), where late frames are told and it isn't on the
846
+ // frame timeline: when it was made, and, made in a window, what its frame was held.
847
+ let owed = 0
848
+ let madeAt = Number.NaN
849
+ let madeLate = 0
850
+ if (tellsLate && !onFrameTimeline(options)) {
851
+ owed = Number.NaN
852
+ madeAt = clock.now()
853
+ if (windowOpen) madeLate = windowLate
854
+ }
712
855
  const entry: Entry = {
713
856
  name: machine.span.name,
714
857
  step: machine.start(),
@@ -718,16 +861,19 @@ export const make = Effect.fnUntraced(
718
861
  removed: false,
719
862
  late: windowOpen,
720
863
  span,
864
+ owed,
865
+ madeAt,
866
+ madeLate,
721
867
  }
722
868
  lane.push(entry)
723
869
  if (entry.late) late.push(entry)
724
- if (Predicate.isNotUndefined(options)) joinSupersede(options.supersede, entry)
870
+ if (Predicate.isNotUndefined(options) && Predicate.isNotUndefined(options.supersede)) joinSupersede(options.supersede, entry)
725
871
  return Effect.as(startBurst, entry)
726
872
  }
727
873
 
728
874
  const inLateWindow = (): boolean => windowOpen
729
875
 
730
- return { run: runIn(entries, lateFirst), runLast: runIn(lastEntries, lateLast), afterTick, inLateWindow, hint } satisfies Shape
876
+ return { run: runIn(entries, lateFirst), runLast: runIn(lastEntries, lateLast), afterTick, inLateWindow, tickCount, hint } satisfies Shape
731
877
  },
732
878
  (self) => traced(self, "wardic.frameloop.make"),
733
879
  )
@@ -0,0 +1,69 @@
1
+ /**
2
+ * A watch's handler, guarded (#318, ADR-0055): a run of throws from it, from its first to the next call
3
+ * that returns, goes to the watch's `onDefect` at its first, else, with none, is reported then on the
4
+ * context the watch started in, and the watch goes on. `InputFeed`'s watches and its `switched` are built
5
+ * on it, and Recognizer's keys, which hand `switched` the context of the watch that opens them.
6
+ */
7
+ // #979, row 27 (ADR-0074): with no `onDefect`, a throw went up the platform's dispatch, so a shared feed's
8
+ // later subscribers (a ward's own hover on the same element), a later node on the event's path and a later
9
+ // member missed the event. Every watch of Wardic's own passes an `onDefect`, each a binding's report.
10
+ // #979 group I, row 26 (decision 3): `onDefect` heard every throw, so a handler throwing at each move was
11
+ // logged at each; it hears a run's first now, as the report with none does. A watch of many members
12
+ // (`watchDragAmong`) is one handler, so its members share a run.
13
+ import * as Cause from "effect/Cause"
14
+ import type * as Context from "effect/Context"
15
+ import * as Effect from "effect/Effect"
16
+ import { constVoid } from "effect/Function"
17
+ import * as Predicate from "effect/Predicate"
18
+ import * as UndefinedOr from "effect/UndefinedOr"
19
+ import type { InputFeed, OnDefect, Switch } from "../InputFeed.ts"
20
+ import * as Report from "../unstable/Report.ts"
21
+
22
+ /** A watch's handler that threw, with no `onDefect` to take it. */
23
+ const HANDLER_FAILED = "wardic.input: a watch's handler failed"
24
+
25
+ /**
26
+ * The context a watch with no `onDefect` reports on: the one it starts in, read only for such a watch.
27
+ * `undefined` with an `onDefect`, which takes the throws.
28
+ */
29
+ export const contextFor = (onDefect: OnDefect | undefined): Effect.Effect<Context.Context<never> | undefined> =>
30
+ UndefinedOr.match(onDefect, { onDefined: () => Effect.succeed(undefined), onUndefined: () => Effect.context<never>() })
31
+
32
+ /**
33
+ * `handle`, a run of throws from it (from its first throw to the next call that returns) handed to
34
+ * `onDefect` at its first; with none, reported then on `context`. The next event is heard either way.
35
+ */
36
+ export const guarded = <A>(handle: (event: A) => void, onDefect: OnDefect | undefined, context: Context.Context<never> | undefined): ((event: A) => void) => {
37
+ const tell = UndefinedOr.match(onDefect, {
38
+ onDefined: (defect) => (error: unknown) => defect(Cause.die(error)),
39
+ onUndefined: () => (error: unknown) => Report.failureOn(context, HANDLER_FAILED, Cause.die(error)),
40
+ })
41
+ let failing = false
42
+ return (event: A): void => {
43
+ try {
44
+ handle(event)
45
+ if (failing) failing = false
46
+ } catch (error) {
47
+ if (failing) return
48
+ failing = true
49
+ tell(error)
50
+ }
51
+ }
52
+ }
53
+
54
+ /** `InputFeed.switched`, its handler guarded with `context` for a throw with no `onDefect`. */
55
+ export const switchedOn = <A>(feed: InputFeed<A>, handle: (event: A) => void, onDefect: OnDefect | undefined, context: Context.Context<never> | undefined): Switch => {
56
+ const on = guarded(handle, onDefect, context)
57
+ let stop: (() => void) | undefined
58
+ return {
59
+ open: () => {
60
+ if (Predicate.isUndefined(stop)) stop = feed(on, constVoid)
61
+ },
62
+ close: () => {
63
+ if (Predicate.isUndefined(stop)) return
64
+ const running = stop
65
+ stop = undefined
66
+ running()
67
+ },
68
+ }
69
+ }
@@ -42,8 +42,15 @@ export const durationOf = (duration: number): number => {
42
42
 
43
43
  // --- Tween -------------------------------------------------------------------
44
44
 
45
+ /** A tween run's time, rewritten in place each tick (#895): a field, which V8 rewrites where it is. */
45
46
  export interface TweenState {
46
- readonly elapsed: number
47
+ elapsed: number
48
+ }
49
+
50
+ /** A run's own Frame, rewritten in place each tick (#895): a tween's, a blend's. */
51
+ export interface WritableFrame<A> {
52
+ value: A
53
+ done: boolean
47
54
  }
48
55
 
49
56
  /** A tween's options with every default filled in. */
@@ -102,19 +109,21 @@ export const resolveTween: (options: TweenOptions) => ResolvedTween = /*#__PURE_
102
109
 
103
110
  export const initTween = (): TweenState => ({ elapsed: 0 })
104
111
 
105
- /** One frame of a resolved tween. */
112
+ /** One frame of a resolved tween, into its run's `state` and `frame`: nothing made (#895). */
106
113
  export const stepTween = (
107
114
  state: TweenState,
115
+ frame: WritableFrame<number>,
108
116
  dt: number,
109
117
  from: number,
110
118
  to: number,
111
119
  tween: ResolvedTween,
112
- ): StepResult<TweenState, number> => {
120
+ ): void => {
113
121
  const elapsed = state.elapsed + dt
122
+ state.elapsed = elapsed
114
123
  const active = Math.max(0, elapsed - tween.delay)
115
124
  const t = Scalar.clamp(active / tween.duration, 0, 1)
116
- const eased = tween.ease(t)
117
- return { state: { elapsed }, value: Scalar.lerp(from, to, eased), done: elapsed >= tween.delay + tween.duration }
125
+ frame.value = Scalar.lerp(from, to, tween.ease(t))
126
+ frame.done = elapsed >= tween.delay + tween.duration
118
127
  }
119
128
 
120
129
  // --- A run of repeats, and keyframes (#226) ----------------------------------
@@ -15,7 +15,8 @@
15
15
  * - **`stop()`** ends each element's running legs where they are.
16
16
  *
17
17
  * A function definition is called once per element, with its `custom`, values and velocities, as
18
- * Motion's `controls.start((i) => …)`. An element hears the controls from its mount to its unmount.
18
+ * Motion's `controls.start((i) => …)`, every element's before any is set: one that throws for an element
19
+ * refuses the call, taking nothing. An element hears the controls from its mount to its unmount.
19
20
  *
20
21
  * Unstable, for Wardic's own packages: public, with no semver promise.
21
22
  *
@@ -58,9 +59,11 @@ export interface ControlsChange {
58
59
  readonly landed: () => void
59
60
  }
60
61
 
61
- /** What a controlled element does at a change, as its binding wires it: its entry set, and the change's landing promised. */
62
+ /** What a controlled element does at a change, as its binding wires it: its entry worked out, then set, and the change's landing promised. */
62
63
  export interface Controlled {
63
- readonly change: (definition: Config.WardicControlsDefinition, override: Config.WardicTransition | undefined) => Promise<void>
64
+ /** Its entry for `definition`: a function definition called with its `custom`, values and velocities; anything else as it is. */
65
+ readonly entryOf: (definition: Config.WardicControlsDefinition) => Config.WardicVariant | Config.WardicVariantLabels
66
+ readonly change: (entry: Config.WardicVariant | Config.WardicVariantLabels, override: Config.WardicTransition | undefined) => Promise<void>
64
67
  readonly stop: () => void
65
68
  }
66
69
 
@@ -71,6 +74,15 @@ const subscribers = new WeakMap<Config.WardicAnimationControls, Set<Controlled>>
71
74
  /** `set`'s transition (#433, Motion's `setValues`): none to speak of, and no orchestration. */
72
75
  export const INSTANT: Config.WardicTransition = { type: "tween", duration: 0 }
73
76
 
77
+ /**
78
+ * Each element's entry for `definition`, worked out before any is set: a function definition that throws
79
+ * for one refuses the call, taking nothing, as an `animate()` call refuses one.
80
+ */
81
+ // #979 (ADR-0074, row 35): `start` and `set` set each element's entry as they called its function, so one
82
+ // that threw for a later element left the earlier ones changed and the rest unreached, as #986's row 20.
83
+ const entriesOf = (elements: ReadonlyArray<Controlled>, definition: Config.WardicControlsDefinition): ReadonlyArray<Config.WardicVariant | Config.WardicVariantLabels> =>
84
+ Arr.map(elements, (element) => element.entryOf(definition))
85
+
74
86
  /**
75
87
  * Makes animation controls, which animate elements from code by their variants or targets: give them
76
88
  * to each element as `animate={controls}`, on a `w.*` element, in a ward's config or on a
@@ -87,12 +99,18 @@ export const createAnimationControls = (): Config.WardicAnimationControls => {
87
99
  const reached = new Set<Controlled>()
88
100
  const controls: Config.WardicAnimationControls = {
89
101
  [Config.CONTROLS]: true,
90
- start: (definition, transitionOverride) =>
91
- Promise.all(Arr.map(Arr.fromIterable(reached), (element) => element.change(definition, transitionOverride))).then(() => undefined),
92
- set: (definition) =>
93
- reached.forEach((element) => {
94
- void element.change(definition, INSTANT)
95
- }),
102
+ start: (definition, transitionOverride) => {
103
+ const elements = Arr.fromIterable(reached)
104
+ const entries = entriesOf(elements, definition)
105
+ return Promise.all(Arr.zipWith(elements, entries, (element, entry) => element.change(entry, transitionOverride))).then(() => undefined)
106
+ },
107
+ set: (definition) => {
108
+ const elements = Arr.fromIterable(reached)
109
+ const entries = entriesOf(elements, definition)
110
+ Arr.forEach(Arr.zip(elements, entries), ([element, entry]) => {
111
+ void element.change(entry, INSTANT)
112
+ })
113
+ },
96
114
  stop: () => reached.forEach((element) => element.stop()),
97
115
  }
98
116
  subscribers.set(controls, reached)
@@ -121,26 +139,35 @@ export const subscribe = (controls: Config.WardicAnimationControls, element: Con
121
139
  let serials = 0
122
140
 
123
141
  /**
124
- * A change of one element (#433): its entry, `definition` called with the element's `custom`, values
125
- * and velocities when it's a resolver (once, as Motion's), on `override`; and its landing, promised. It
126
- * lands once every element it reaches has joined, at the end of the binding's flush (`armed`), and
127
- * left. One that reaches no element lands then.
142
+ * An element's entry for `definition`: a resolver called with its `custom`, values and velocities (once,
143
+ * as Motion's), anything else as it is.
128
144
  */
129
- export const changeOf = (
145
+ // #433; #979 (row 35): worked out for every element before any is set.
146
+ export const entryOf = (
130
147
  definition: Config.WardicControlsDefinition,
131
- override: Config.WardicTransition | undefined,
132
148
  custom: unknown,
133
149
  values: Readonly<Record<string, number | string>>,
134
150
  velocities: Readonly<Record<string, number>>,
151
+ ): Config.WardicVariant | Config.WardicVariantLabels =>
152
+ Bool.match(Predicate.isFunction(definition), {
153
+ onTrue: () => (definition as Config.WardicVariantResolver)(custom, { ...values }, { ...velocities }),
154
+ onFalse: () => definition as Config.WardicVariant | Config.WardicVariantLabels,
155
+ })
156
+
157
+ /**
158
+ * A change of one element: its entry, on `override`, and its landing, promised. It lands once every
159
+ * element it reaches has joined, at the end of the binding's flush (`armed`), and left. One that reaches
160
+ * no element lands then.
161
+ */
162
+ // #433.
163
+ export const changeOf = (
164
+ entry: Config.WardicVariant | Config.WardicVariantLabels,
165
+ override: Config.WardicTransition | undefined,
135
166
  ): { readonly change: ControlsChange; readonly landing: Promise<void> } => {
136
167
  let resolve!: () => void
137
168
  const landing = new Promise<void>((done) => {
138
169
  resolve = done
139
170
  })
140
- const entry = Bool.match(Predicate.isFunction(definition), {
141
- onTrue: () => (definition as Config.WardicVariantResolver)(custom, { ...values }, { ...velocities }),
142
- onFalse: () => definition as Config.WardicVariant | Config.WardicVariantLabels,
143
- })
144
171
  const change: ControlsChange = { serial: ++serials, definition: entry, override, pending: 0, armed: false, landed: resolve }
145
172
  return { change, landing }
146
173
  }
@@ -194,8 +221,9 @@ export interface ControlledSnapshot {
194
221
  * every element it reaches joined by then; `stop` ends its legs.
195
222
  */
196
223
  export const controlledElement = (setChange: (change: ControlsChange) => void, state: ControlledState, stop: () => void): Controlled => ({
197
- change: (definition, override) => {
198
- const made = changeOf(definition, override, state.custom(), state.snapshot.values, state.snapshot.velocitiesNow(state.snapshot))
224
+ entryOf: (definition) => entryOf(definition, state.custom(), state.snapshot.values, state.snapshot.velocitiesNow(state.snapshot)),
225
+ change: (entry, override) => {
226
+ const made = changeOf(entry, override)
199
227
  setChange(made.change)
200
228
  queueMicrotask(() => arm(made.change))
201
229
  return made.landing
@@ -42,6 +42,13 @@ export interface MountAnimation {
42
42
  readonly skipped?: boolean | undefined
43
43
  /** Its **Transform template** (#443): its stops drawn through it, as the client draws them. Left out with none. */
44
44
  readonly template?: Config.SpineConfig["transformTemplate"]
45
+ /**
46
+ * Each key already at its target, which it leaves where it is, at its value, as the client writes it
47
+ * beside the moving keys: its Environment may draw some of them together with those in each stop.
48
+ * Part of its key when it draws any; none, or an empty one, holds nothing.
49
+ */
50
+ // #1004 (ADR-0044, *Amendment (#1004)*).
51
+ readonly held?: Values.Values | undefined
45
52
  }
46
53
 
47
54
  /** Whether a transition repeats for ever: a tween's `repeat: Infinity`, a tween as it runs (ADR-0051). */
@@ -263,7 +263,12 @@ export type WardicVariantLabels = string | ReadonlyArray<string>
263
263
  * A dynamic variant (Motion's `TargetResolver`): called each time the variant is
264
264
  * resolved with the element's `custom` (or, for an exit, its `<AnimatePresence custom>`), its values
265
265
  * now and their velocities, and returns its target, or another variant's name.
266
+ *
267
+ * One that throws is reported, and only its element is affected: it keeps its last target until the
268
+ * variant resolves, leaves without that exit, or starts without that variant's values.
266
269
  */
270
+ // #979 (group F; ADR-0074, ADR-0007's Amendment): core's one call site catches it, and the failure
271
+ // travels as the target's value, so no binding's update sees the throw.
267
272
  export type WardicVariantResolver = (
268
273
  // Motion's `custom` is `any`: a resolver declares its own (`(direction: number) => …`).
269
274
  custom: any,
@@ -291,9 +296,10 @@ export interface WardicAnimationControls {
291
296
  * Animates every element given these controls to `definition`, as a change of its `animate` would,
292
297
  * its variant children following. `transitionOverride`, when given, is the transition of this change,
293
298
  * over the definition's own, and its stagger. Resolves once each element and its children have landed.
299
+ * A function definition that throws for one element refuses the call, throwing, with no element changed.
294
300
  */
295
301
  readonly start: (definition: WardicControlsDefinition, transitionOverride?: WardicTransition) => Promise<void>
296
- /** Sets every element given these controls, and its variant children, at `definition` at once. */
302
+ /** Sets every element given these controls, and its variant children, at `definition` at once; refused, with none set, as `start` is. */
297
303
  readonly set: (definition: WardicControlsDefinition) => void
298
304
  /** Ends every element's running animation where it is. */
299
305
  readonly stop: () => void