@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/Batch.ts CHANGED
@@ -18,10 +18,11 @@
18
18
  // a member's keys at `slot * stride + k` (#663's shape); stepped by `Spring.matrixFor`'s exact step
19
19
  // (#370), the probe's loop (`.tmp/564-canvas/perf/group.ts` on `probe/564-canvas`).
20
20
  import * as Arr from "effect/Array"
21
+ import * as Cause from "effect/Cause"
22
+ import * as Context from "effect/Context"
21
23
  import * as Effect from "effect/Effect"
22
24
  import { identity } from "effect/Function"
23
25
  import * as Inspectable from "effect/Inspectable"
24
- import * as Iter from "effect/Iterable"
25
26
  import * as Match from "effect/Match"
26
27
  import { type Pipeable, pipeArguments } from "effect/Pipeable"
27
28
  import * as Predicate from "effect/Predicate"
@@ -35,6 +36,7 @@ import type * as Playback from "./Playback.ts"
35
36
  import * as Scalar from "./Scalar.ts"
36
37
  import * as Spring from "./Spring.ts"
37
38
  import * as UnitScale from "./UnitScale.ts"
39
+ import * as Report from "./unstable/Report.ts"
38
40
  import { dual } from "./internal/dual.ts"
39
41
  import { tickOf, writableOf } from "./internal/playhead.ts"
40
42
  import { resolveKeyframes } from "./internal/keyframes.ts"
@@ -271,6 +273,20 @@ interface State<S> {
271
273
  running: number
272
274
  /** How many runs it has had: the next one's number. */
273
275
  runs: number
276
+ /** What its failures are reported on: its last run's services, an empty Context before its first. */
277
+ services: Context.Context<never>
278
+ /** Whether a member's write threw in this step. */
279
+ writeThrewInStep: boolean
280
+ /** Whether a member's write throw is reported, with no step since that wrote every member clean. */
281
+ writeReported: boolean
282
+ /** Whether a member's start partway threw in this step. */
283
+ startThrewInStep: boolean
284
+ /** Whether a member's start throw is reported, with no step since that started every member clean. */
285
+ startReported: boolean
286
+ /** Whether its writer's `flush` throw is reported, with no `flush` since that returned. */
287
+ flushReported: boolean
288
+ /** Whether its `onFrame` throw is reported, with no `onFrame` since that returned. */
289
+ onFrameReported: boolean
274
290
  }
275
291
 
276
292
  const Proto = {
@@ -474,6 +490,13 @@ export const make = <S>(options: Options<S>): Batch<S> => {
474
490
  frame: 0,
475
491
  running: 0,
476
492
  runs: 0,
493
+ services: Context.empty(),
494
+ writeThrewInStep: false,
495
+ writeReported: false,
496
+ startThrewInStep: false,
497
+ startReported: false,
498
+ flushReported: false,
499
+ onFrameReported: false,
477
500
  }
478
501
  // The Batch's own transitions, pinned: the first entries, rule 0 the first key's.
479
502
  state.ownRules = rulesOf(state, options.transition, unnamedKey(state))
@@ -798,6 +821,48 @@ export const retarget: {
798
821
  return setOff
799
822
  })
800
823
 
824
+ /**
825
+ * `defect`, a failure on the Batch's path named by `message`, logged and handed to the ErrorReporters of
826
+ * what it runs on, annotated with its member's slot where it's one member's.
827
+ */
828
+ // #977 (ADR-0074): one member's failure is its own. Its failure path, off the per-frame one.
829
+ const report = <S>(state: State<S>, message: string, defect: unknown, slot?: number): void => {
830
+ const failure = Report.failure(message, Cause.die(defect))
831
+ if (Predicate.isUndefined(slot)) {
832
+ Effect.runForkWith(state.services)(failure)
833
+ return
834
+ }
835
+ Effect.runForkWith(state.services)(Effect.annotateLogs(failure, { member: slot }))
836
+ }
837
+
838
+ /** A member's write that threw: reported once, until a step writes every member clean. */
839
+ const writeFailed = <S>(state: State<S>, slot: number, defect: unknown): void => {
840
+ state.writeThrewInStep = true
841
+ if (state.writeReported) return
842
+ state.writeReported = true
843
+ report(state, "wardic.batch: a member's write failed", defect, slot)
844
+ }
845
+
846
+ /** A member's start partway that threw: reported once, until a step starts every member clean. */
847
+ const startFailed = <S>(state: State<S>, slot: number, defect: unknown): void => {
848
+ state.startThrewInStep = true
849
+ if (state.startReported) return
850
+ state.startReported = true
851
+ report(state, "wardic.batch: a member's start failed", defect, slot)
852
+ }
853
+
854
+ /** The writer's `flush`, a step's or a draw's: a throw reported once, until one returns. */
855
+ const flushed = <S>(state: State<S>): void => {
856
+ try {
857
+ state.flush()
858
+ if (state.flushReported) state.flushReported = false
859
+ } catch (defect) {
860
+ if (state.flushReported) return
861
+ state.flushReported = true
862
+ report(state, "wardic.batch: its writer's flush failed", defect)
863
+ }
864
+ }
865
+
801
866
  /** Whether any member moves: one `step` would move. */
802
867
  export const isMoving = <S>(self: Batch<S>): boolean => {
803
868
  const state = stateOf(self)
@@ -809,6 +874,11 @@ export const isMoving = <S>(self: Batch<S>): boolean => {
809
874
  * outside a step set (an `enter`, a `replace`, a start from a given value, a play, seek, stop or cancel):
810
875
  * whether nothing moves now. One a delay or a paused Playhead holds still isn't written. The run on the
811
876
  * Frame loop calls it each tick.
877
+ *
878
+ * One member's failure is its own: a write or a start partway that throws is reported once, until a step
879
+ * runs every member's clean, and its member steps on with the others. A landing's callback that throws is
880
+ * reported each time, the other members told and the other changes landed; the writer's `flush` and
881
+ * `onFrame` once until they return. Each is reported on what the Batch last ran on.
812
882
  */
813
883
  // Per frame (ADR-0025, ADR-0030 decisions 10 and 11): `dt` clamped once (#78), then the two moving
814
884
  // lists. Each steps a key by `springValues`' exact step, or `Machine.tween`'s, its spring's matrix
@@ -817,12 +887,35 @@ export const step = <S>(self: Batch<S>, dt: number): boolean => {
817
887
  const state = stateOf(self)
818
888
  const clamped = Math.min(dt, Machine.MAX_DT)
819
889
  state.frame = state.frame + 1
890
+ // Whether a member's write or start throws in this step: a `draw`'s between steps isn't one (#977).
891
+ state.writeThrewInStep = false
892
+ state.startThrewInStep = false
820
893
  if (state.startList.length > 0) startPass(state, clamped)
821
894
  if (state.plainCount > 0) stepPlain(state, clamped)
822
895
  if (state.ruledCount > 0) stepRuled(state, clamped)
823
- if (state.animations.size > 0) Iter.forEach(state.animations, checkLanding)
824
- state.flush()
825
- state.onFrame()
896
+ // Each change's landing its own: a callback that throws leaves the others to land (#977).
897
+ if (state.animations.size > 0) {
898
+ for (const animation of state.animations) {
899
+ try {
900
+ checkLanding(animation)
901
+ } catch (defect) {
902
+ report(state, "wardic.batch: a change's landing failed", defect)
903
+ }
904
+ }
905
+ }
906
+ flushed(state)
907
+ try {
908
+ state.onFrame()
909
+ if (state.onFrameReported) state.onFrameReported = false
910
+ } catch (defect) {
911
+ if (!state.onFrameReported) {
912
+ state.onFrameReported = true
913
+ report(state, "wardic.batch: an onFrame failed", defect)
914
+ }
915
+ }
916
+ // A path that stepped clean is reported again at its next throw (#977).
917
+ if (!state.writeThrewInStep) state.writeReported = false
918
+ if (!state.startThrewInStep) state.startReported = false
826
919
  // Counted after `onFrame`: a change it makes keeps the run going.
827
920
  if (state.plainCount + state.ruledCount > 0) return false
828
921
  // A driven change playing, paused at rest, still holds its entries.
@@ -843,9 +936,13 @@ export const draw = <S>(self: Batch<S>): void => {
843
936
  for (let slot = 0; slot < state.used; slot++) {
844
937
  if ((flags[slot] & LIVE) === 0) continue
845
938
  flags[slot] = flags[slot] & ~DUE
846
- write(subjects[slot] as S, positions, slot * stride)
939
+ try {
940
+ write(subjects[slot] as S, positions, slot * stride)
941
+ } catch (defect) {
942
+ writeFailed(state, slot, defect)
943
+ }
847
944
  }
848
- state.flush()
945
+ flushed(state)
849
946
  }
850
947
 
851
948
  /**
@@ -936,7 +1033,13 @@ const stepPlain = <S>(state: State<S>, clamped: number): void => {
936
1033
  elapsed[at] = time
937
1034
  }
938
1035
  // One a delay holds still isn't written: an unchanged write is a style write all the same.
939
- if (moved) write(subjects[slot] as S, positions, base)
1036
+ if (moved) {
1037
+ try {
1038
+ write(subjects[slot] as S, positions, base)
1039
+ } catch (defect) {
1040
+ writeFailed(state, slot, defect)
1041
+ }
1042
+ }
940
1043
  // At rest: still its member's, in neither list.
941
1044
  if (still) {
942
1045
  flags[slot] = LIVE
@@ -1066,7 +1169,13 @@ const stepRuled = <S>(state: State<S>, clamped: number): void => {
1066
1169
  positions[at] = position
1067
1170
  velocities[at] = speed
1068
1171
  }
1069
- if (moved) write(subjects[slot] as S, positions, base)
1172
+ if (moved) {
1173
+ try {
1174
+ write(subjects[slot] as S, positions, base)
1175
+ } catch (defect) {
1176
+ writeFailed(state, slot, defect)
1177
+ }
1178
+ }
1070
1179
  // At rest: still its member's, in neither list (a member is in one at most).
1071
1180
  if (still) {
1072
1181
  flags[slot] = LIVE
@@ -1167,7 +1276,13 @@ const stepRuledDriven = <S>(state: State<S>): void => {
1167
1276
  positions[at] = position
1168
1277
  velocities[at] = speed
1169
1278
  }
1170
- if (moved) write(subjects[slot] as S, positions, base)
1279
+ if (moved) {
1280
+ try {
1281
+ write(subjects[slot] as S, positions, base)
1282
+ } catch (defect) {
1283
+ writeFailed(state, slot, defect)
1284
+ }
1285
+ }
1171
1286
  // At rest: still its member's, in neither list (a member is in one at most).
1172
1287
  if (still) {
1173
1288
  flags[slot] = LIVE
@@ -1276,6 +1391,12 @@ interface AnimationState<S> {
1276
1391
  /** Each member it moves, by rank, its first key's index in `ats`, and one past the last's; whether each has been told it landed. */
1277
1392
  firsts: Int32Array
1278
1393
  told: Uint8Array
1394
+ /**
1395
+ * Each of its keys, 1 once it has rested at its target while it moved it, or from its play when it
1396
+ * starts there (one that snaps): a key taken after it rested doesn't keep it from landing.
1397
+ */
1398
+ // #951, Motion's: a value's animation that finished before a later one took the value had resolved.
1399
+ rested: Uint8Array
1279
1400
  /** The members it's given that it moves nothing of, told they landed at its first play. */
1280
1401
  idle: ReadonlyArray<number>
1281
1402
  idleTold: boolean
@@ -1412,7 +1533,13 @@ const startedPartway = <S>(state: State<S>, slot: number, dt: number): boolean =
1412
1533
  const { stride, elapsed, rules, positions, velocities, targets, originVelocities, startAts } = state
1413
1534
  const startAt = UndefinedOr.getOrThrow(startAts[slot])
1414
1535
  startAts[slot] = undefined
1415
- const time = startAt(slot)
1536
+ // A start that throws is its member's: it starts from its start, as `NaN` gives (#977).
1537
+ let time = Number.NaN
1538
+ try {
1539
+ time = startAt(slot)
1540
+ } catch (defect) {
1541
+ startFailed(state, slot, defect)
1542
+ }
1416
1543
  const base = slot * stride
1417
1544
  const started = !Number.isNaN(time)
1418
1545
  for (let k = 0; k < stride; k++) {
@@ -1433,7 +1560,11 @@ const startedPartway = <S>(state: State<S>, slot: number, dt: number): boolean =
1433
1560
  }
1434
1561
  if (started) {
1435
1562
  state.flags[slot] = state.flags[slot] & ~DUE
1436
- state.write(state.subjects[slot] as S, positions, base)
1563
+ try {
1564
+ state.write(state.subjects[slot] as S, positions, base)
1565
+ } catch (defect) {
1566
+ writeFailed(state, slot, defect)
1567
+ }
1437
1568
  }
1438
1569
  return started
1439
1570
  }
@@ -1521,7 +1652,14 @@ const tellIdle = <S>(animation: AnimationState<S>): void => {
1521
1652
  animation.idleTold = true
1522
1653
  const tell = animation.options.onMemberLanded
1523
1654
  if (Predicate.isUndefined(tell)) return
1524
- Arr.forEach(animation.idle, (slot) => tell(slot))
1655
+ // Each member's own: a landing that throws leaves the others told (#977).
1656
+ Arr.forEach(animation.idle, (slot) => {
1657
+ try {
1658
+ tell(slot)
1659
+ } catch (defect) {
1660
+ report(animation.state, "wardic.batch: a member's landing failed", defect, slot)
1661
+ }
1662
+ })
1525
1663
  }
1526
1664
 
1527
1665
  /**
@@ -1546,14 +1684,20 @@ const tellMembers = <S>(animation: AnimationState<S>, tell: (slot: number) => vo
1546
1684
  }
1547
1685
  if (!kept || !atRest) continue
1548
1686
  told[r] = 1
1549
- tell(slotAt(state, ats[firsts[r]]))
1687
+ const slot = slotAt(state, ats[firsts[r]])
1688
+ try {
1689
+ tell(slot)
1690
+ } catch (defect) {
1691
+ report(state, "wardic.batch: a member's landing failed", defect, slot)
1692
+ }
1550
1693
  }
1551
1694
  }
1552
1695
 
1553
1696
  /**
1554
1697
  * Whether `animation` has landed, once a step while it plays: every key it still moves at its target,
1555
1698
  * past its delay; or, played backwards, back at its start. It ends then. A key a later change took keeps
1556
- * it from landing (#433's latest-wins, as a ward's): it ends unheard, once the keys it kept are at rest.
1699
+ * it from landing (#433's latest-wins, as a ward's): it ends once the keys it kept are at rest, telling
1700
+ * `onTaken` (#951). One taken after it rested there, or that started there, doesn't.
1557
1701
  */
1558
1702
  const checkLanding = <S>(animation: AnimationState<S>): void => {
1559
1703
  const { state, options } = animation
@@ -1561,6 +1705,7 @@ const checkLanding = <S>(animation: AnimationState<S>): void => {
1561
1705
  const tell = options.onMemberLanded
1562
1706
  if (Predicate.isNotUndefined(tell) && !backwards) tellMembers(animation, tell)
1563
1707
  const count = animation.ats.length
1708
+ const rested = animation.rested
1564
1709
  let atRest = true
1565
1710
  let held = 0
1566
1711
  // Once a step, per key of a playing change: a counted loop with plain reads.
@@ -1568,7 +1713,9 @@ const checkLanding = <S>(animation: AnimationState<S>): void => {
1568
1713
  if (!holds(animation, i)) continue
1569
1714
  held++
1570
1715
  if (backwards) continue
1571
- if (!keyAtRest(animation, i)) atRest = false
1716
+ const resting = keyAtRest(animation, i)
1717
+ if (resting) rested[i] = 1
1718
+ if (!resting) atRest = false
1572
1719
  }
1573
1720
  if (held > 0 && backwards && animation.time > 0) return
1574
1721
  if (!atRest) return
@@ -1582,10 +1729,36 @@ const checkLanding = <S>(animation: AnimationState<S>): void => {
1582
1729
  }
1583
1730
  }
1584
1731
  finish(animation)
1585
- if (held < count) return
1732
+ if (held < count) {
1733
+ if (takenInFlight(animation)) {
1734
+ UndefinedOr.map((options as TakenHook).onTaken, (told) => told())
1735
+ return
1736
+ }
1737
+ }
1586
1738
  UndefinedOr.map(options.onLanded, (told) => told())
1587
1739
  }
1588
1740
 
1741
+ /**
1742
+ * A change's `onTaken`, which a binding's options carry beside `animate`'s or `keyframes`': called once
1743
+ * the change ends with a key a later change took while it moved it, which it then never lands.
1744
+ */
1745
+ // #951: Motion's never resolves then; the scope it was started in hears it end. Not on AnimateOptions or
1746
+ // KeyframesOptions, so outside core's semver: a binding gives it as `@wardic/core/unstable/BatchCalls`'
1747
+ // `Taken<O>`, the one field this names.
1748
+ interface TakenHook {
1749
+ readonly onTaken?: (() => void) | undefined
1750
+ }
1751
+
1752
+ /** Whether a later change took a key of `animation`'s before it rested there (#951): at its end, once. */
1753
+ const takenInFlight = <S>(animation: AnimationState<S>): boolean => {
1754
+ const count = animation.ats.length
1755
+ for (let i = 0; i < count; i++) {
1756
+ if (animation.rested[i] !== 0) continue
1757
+ if (!holds(animation, i)) return true
1758
+ }
1759
+ return false
1760
+ }
1761
+
1589
1762
  /** A driven change's transition for a member's key, before its copy: its function's, or its own or `default`, else the key's own. */
1590
1763
  const animationBagsOf = <S>(state: State<S>, given: RetargetOptions["transition"]): ((slot: number, k: number) => Transition) => {
1591
1764
  if (Predicate.isFunction(given)) return given
@@ -1661,6 +1834,7 @@ const animationFor = <S>(
1661
1834
  indexAt: new Int32Array(0),
1662
1835
  firsts: new Int32Array(0),
1663
1836
  told: new Uint8Array(0),
1837
+ rested: new Uint8Array(0),
1664
1838
  idle: [],
1665
1839
  idleTold: false,
1666
1840
  }
@@ -1965,12 +2139,15 @@ export const play = (self: Animation): number => {
1965
2139
  state.drivers[rule] = animation
1966
2140
  })
1967
2141
  let setOff = 0
2142
+ // Played again, each key rests anew; one that starts at its target, as one that snaps does, has.
2143
+ if (animation.rested.length !== count) animation.rested = new Uint8Array(count)
1968
2144
  for (let i = 0; i < count; i++) {
1969
2145
  const at = animation.ats[i]
1970
2146
  const rule = animation.rules[i]
1971
2147
  turn(state, at, rule, animation.goals[i], animation.starts[i], animation.speeds[i], animation.waits[i])
1972
2148
  state.elapsed[at] = time
1973
2149
  valueAt(state, at, rule, time, true)
2150
+ animation.rested[i] = Number(keyAtRest(animation, i))
1974
2151
  setOff += moving(state, slotAt(state, at))
1975
2152
  }
1976
2153
  return setOff
@@ -2142,23 +2319,36 @@ const machineOf = <S>(self: Batch<S>, id: number): Machine.Machine<Frame.Frame<n
2142
2319
  /**
2143
2320
  * Runs `self` on the Frame loop while any member moves, from the next tick: each tick steps every
2144
2321
  * moving member and writes it. It succeeds once nothing moves. While one runs, another does nothing,
2145
- * so a binding calls it after each change.
2322
+ * so a binding calls it after each change. A member's failure is reported on the services it runs with,
2323
+ * the Environment's Logger and ErrorReporters, and the run goes on.
2146
2324
  *
2147
2325
  * @example
2148
2326
  * Batch.retarget(grid, slots, targetOf)
2149
2327
  * runtime.runFork(Batch.run(grid))
2150
2328
  */
2329
+ /** A Batch run's options (#1026): on the frame timeline while members take their Appear animations over, or not. */
2330
+ const TAKING_OVER: Machine.RunOptions = { onFrameTimeline: true }
2331
+ const ON_ITS_OWN: Machine.RunOptions = {}
2332
+
2151
2333
  // One run, not a fiber per member (ADR-0069, decision 10). Its span is `wardic.batch`.
2152
- export const run = <S>(self: Batch<S>): Effect.Effect<void, never, FrameLoop.FrameLoop> =>
2153
- Effect.suspend(() => {
2334
+ export const run =<S>(self: Batch<S>): Effect.Effect<void, never, FrameLoop.FrameLoop> =>
2335
+ Effect.contextWith((services: Context.Context<never>) => {
2154
2336
  const state = stateOf(self)
2155
2337
  if (state.running !== 0) return Effect.void
2338
+ // Its failures are reported on what it runs on, the Environment's Logger and ErrorReporters, kept
2339
+ // for a draw or a play between runs; a second run while one runs swaps nothing (#977).
2340
+ state.services = services
2156
2341
  if (state.plainCount + state.ruledCount === 0) return Effect.void
2157
2342
  const id = state.runs + 1
2158
2343
  state.runs = id
2159
2344
  state.running = id
2345
+ // A run that starts with members taking their Appear animations over keeps the frame timeline's time,
2346
+ // where those animations' `currentTime` is (#1026); one that doesn't takes no long task's time from
2347
+ // before it was on the page.
2348
+ let options = ON_ITS_OWN
2349
+ if (state.startList.length > 0) options = TAKING_OVER
2160
2350
  return Effect.ensuring(
2161
- Effect.asVoid(Machine.run(machineOf(self, id), ignore)),
2351
+ Effect.asVoid(Machine.run(machineOf(self, id), ignore, options)),
2162
2352
  Effect.sync(() => {
2163
2353
  if (state.running === id) state.running = 0
2164
2354
  }),
package/src/Clock.ts CHANGED
@@ -95,6 +95,26 @@ export class Clock extends Context.Service<Clock, {
95
95
  // @16c6122).
96
96
  export const NOMINAL_DT = 1 / 60
97
97
 
98
+ /**
99
+ * How an Environment's Refresh frames run late ({@link LateFrames}): by their timestamps alone, or, once a
100
+ * frame's callback ran more than `ranAfter` ms after its timestamp, late, a long task holding it.
101
+ */
102
+ export type LateFrameTiming = "timestamp" | { readonly ranAfter: number }
103
+
104
+ /**
105
+ * Whether the Frame loop tells a late Refresh frame (#1026): not by default; for an Environment whose
106
+ * frame timestamps are on its Clock's `now()`'s timebase, as the DOM's are, a frame that ran more than
107
+ * `ranAfter` ms after its timestamp. A long task delays a frame, whose timestamp is the time it was due,
108
+ * from before the task, so the next frame's dt holds the task's time. A run made in that task wasn't on
109
+ * the page before its first frame ran, and the loop leaves that time out of its first steps. The Clock's
110
+ * time stays the frames' timestamps, what every other run and reading counts.
111
+ */
112
+ // The DOM provides `{ ranAfter: 40 }` to its Frame loop (`DomLive`); `@wardic/object`'s pumped frames,
113
+ // whose times are its host's, keep the default.
114
+ export const LateFrames: Context.Reference<LateFrameTiming> = /*#__PURE__*/ Context.Reference<LateFrameTiming>("@wardic/core/Clock/LateFrames", {
115
+ defaultValue: () => "timestamp",
116
+ })
117
+
98
118
  // @wardic/core compiles against ES2022 alone (no DOM lib, no Node types), which does
99
119
  // not declare `performance` or `queueMicrotask`. They are host globals in every environment
100
120
  // Wardic targets (browsers, Node, React Native), so they are declared here, in module scope only.
@@ -409,12 +409,17 @@ export interface DragOptions {
409
409
  readonly threshold?: ((down: InputSource.PointerLikeEvent) => Option.Option<number>) | undefined
410
410
  /**
411
411
  * Where each pointer point is for the gesture, as Motion's `transformPagePoint`: read at each down, and
412
- * applied to all that gesture's points, its down's and each move's. None, or no function given, leaves
413
- * them as they are. It's handed the point on the screen and gives one; the scroll a hold counts in is
414
- * added back after. A caller gives back the same `Some` while its mapping is the same, so a down
415
- * allocates nothing.
412
+ * applied to the points the gesture is made of, its down's, each pressed move's and its release's. A
413
+ * cancel, a move with no button pressed, an enter and a leave aren't mapped. None, or no function given,
414
+ * leaves the points as they are. It's handed the point on the screen and gives one; the scroll a hold
415
+ * counts in is added back after. A caller gives back the same `Some` while its mapping is the same, so
416
+ * a down allocates nothing.
417
+ *
418
+ * A mapping that throws is reported through `onDefect`, once a run of throws, and the gesture goes on
419
+ * without that point: a move is left out; a release ends the gesture at its last point, with its moves'
420
+ * velocity, as a cancel does; a down starts nothing, and ends a gesture its pointer had left open.
416
421
  */
417
- // #443; an `Option` (#618, A1).
422
+ // #443; an `Option` (#618, A1). #1000 (ADR-0074): the points mapped, and a throw's.
418
423
  readonly mapPoint?: LazyArg<Option.Option<PointMap>> | undefined
419
424
  }
420
425
 
@@ -425,20 +430,30 @@ export type PointMap = (point: Point.Point) => Point.Point
425
430
  /** `watchPan`'s options. */
426
431
  // #443.
427
432
  export interface PanOptions {
428
- /** Where each pointer point is for the gesture, as `DragOptions`' `mapPoint`. */
433
+ /** Where each pointer point is for the gesture, as `DragOptions`' `mapPoint`: the same points, and a throw the same way. */
429
434
  readonly mapPoint?: LazyArg<Option.Option<PointMap>> | undefined
430
435
  }
431
436
 
432
437
  /**
433
- * A gesture's point mapping (#443): read at each down, `event` mapped while it's set. The point
434
- * mapped is the one on the screen (`x - scroll.x`), the scroll the hold counts in added back after.
435
- * Allocates only for a mapped gesture.
438
+ * Whether a drag's fold reads `event`'s point: a down's, a pressed move's and a release's. A cancel ends at
439
+ * the last move's point and an unpressed move (a lost release) at the last pressed one, and an enter or
440
+ * a leave moves nothing.
441
+ */
442
+ // #1000 (ADR-0074): mapped, a cancel at (0, 0), where Chromium's lands, or any hover move could throw.
443
+ const readsPoint = (event: InputSource.PointerLikeEvent): boolean =>
444
+ Bool.or(Bool.or(event.type === "down", event.type === "up"), Bool.and(event.type === "move", event.pressed))
445
+
446
+ /**
447
+ * A gesture's point mapping (#443): read at each down, `event` mapped while it's set, when its fold reads
448
+ * its point ({@link readsPoint}). The point mapped is the one on the screen (`x - scroll.x`), the scroll
449
+ * the hold counts in added back after. Allocates only for a mapped gesture.
436
450
  */
437
451
  const mappedEvents = (mapPoint: DragOptions["mapPoint"]): ((event: InputSource.PointerLikeEvent) => InputSource.PointerLikeEvent) => {
438
452
  if (Predicate.isUndefined(mapPoint)) return identity
439
453
  const current = MutableRef.make<Option.Option<PointMap>>(Option.none())
440
454
  return (event) => {
441
455
  if (event.type === "down") MutableRef.set(current, mapPoint())
456
+ if (Bool.not(readsPoint(event))) return event
442
457
  const mapping = MutableRef.get(current)
443
458
  if (Option.isNone(mapping)) return event
444
459
  const { scroll } = event
@@ -447,6 +462,54 @@ const mappedEvents = (mapPoint: DragOptions["mapPoint"]): ((event: InputSource.P
447
462
  }
448
463
  }
449
464
 
465
+ /**
466
+ * The event folded in place of one whose point its mapping can't give, an event whose point isn't read:
467
+ * a release's a cancel, which ends the gesture at its last point with its moves' velocity; a down's an
468
+ * unpressed move, a lost release, which ends a gesture of its pointer's still open, at its last point
469
+ * with no velocity, and starts none. None for a move, left out.
470
+ */
471
+ // #1000 (ADR-0074): a release left out kept its gesture going, active and claimed, until the next press;
472
+ // a down left out kept one a lost release had left open, for good while the mapping threw at each down.
473
+ const standInFor: (event: InputSource.PointerLikeEvent) => Option.Option<InputSource.PointerLikeEvent> = /*#__PURE__*/ Match.type<InputSource.PointerLikeEvent>().pipe(
474
+ Match.when({ type: "up" }, (up) => Option.some<InputSource.PointerLikeEvent>({ ...up, type: "cancel" })),
475
+ Match.when({ type: "down" }, (down) => Option.some<InputSource.PointerLikeEvent>({ ...down, type: "move", pressed: false })),
476
+ Match.orElse(() => Option.none()),
477
+ )
478
+
479
+ /**
480
+ * A watch's handler over `step`: each event's point mapped first, its state kept in `state` and its
481
+ * samples told to `tell`. An event whose mapping throws is folded as its stand-in ({@link standInFor}),
482
+ * and the throw goes on to the watch's guard, which reports it, once its samples are told.
483
+ */
484
+ // #443; #1000: the throw is rethrown so the guard stays the one place a run of throws is reported, once.
485
+ // A throw from `tell` as the stand-in's samples are told reaches the guard in the mapping's place: still
486
+ // one report for the run.
487
+ const mappedHandler = <S>(
488
+ mapPoint: DragOptions["mapPoint"],
489
+ step: (state: DragState, event: InputSource.PointerLikeEvent) => readonly [DragState, ReadonlyArray<S>],
490
+ state: MutableRef.MutableRef<DragState>,
491
+ tell: (sample: S) => void,
492
+ ): ((event: InputSource.PointerLikeEvent) => boolean) => {
493
+ const mapped = mappedEvents(mapPoint)
494
+ const fold = (event: InputSource.PointerLikeEvent): void => {
495
+ const [next, samples] = step(MutableRef.get(state), event)
496
+ MutableRef.set(state, next)
497
+ Arr.forEach(samples, tell)
498
+ }
499
+ return (event) => {
500
+ let mappedEvent: InputSource.PointerLikeEvent
501
+ try {
502
+ mappedEvent = mapped(event)
503
+ } catch (error) {
504
+ const standIn = standInFor(event)
505
+ if (Option.isSome(standIn)) fold(standIn.value)
506
+ throw error
507
+ }
508
+ fold(mappedEvent)
509
+ return true
510
+ }
511
+ }
512
+
450
513
  /** A drag's watch, as one with no drag controls gives it back: its Scope ends it, and nothing else does. */
451
514
  // #618 (F3): a plain watch's type has no `stop` or `cancel`, which did nothing on it.
452
515
  export interface WatchedDrag {}
@@ -591,10 +654,13 @@ export const watchDragAmong: (
591
654
  }
592
655
  const own: PressedMember = { scope: Scope.forkUnsafe(scope, "sequential"), pointerId: told.pointerId, started: false }
593
656
  pressed.set(member, own)
657
+ // #979 group I, row 28 (ADR-0074, group A's *Order first*): the watch ended before the app hears the
658
+ // end, so a handler that throws at it can't leave the member pressed, its next press starting nothing.
659
+ // The end closes the watch after this dispatch, so the handler still hears this sample.
594
660
  const on = (sample: Drag.Sample): void => {
595
661
  if (sample.phase === "start") own.started = true
596
- handlers.on(member, sample)
597
662
  if (sample.phase === "end") end(member, own)
663
+ handlers.on(member, sample)
598
664
  }
599
665
  run(watchDrag(member, { ...options, listen: false, adoptable: true }, { on, onDefect: handlers.onDefect }).pipe(Scope.provide(own.scope)))
600
666
  hand(member, own, told)
@@ -670,7 +736,6 @@ export const watchDrag: {
670
736
  handlers: InputFeed.Handlers<Drag.Sample>,
671
737
  ): Effect.Effect<AdoptableDrag, never, Scope.Scope | InputSource.InputSource> => {
672
738
  const attributes = { threshold: THRESHOLD, velocityWindowMs: VELOCITY_WINDOW_MS }
673
- const mapped = mappedEvents(mapPoint)
674
739
  const axisAt = axisAtOf(axis)
675
740
  const fold = dragFoldOf(lock, listen, threshold)
676
741
  const on = handlers.on
@@ -738,12 +803,7 @@ export const watchDrag: {
738
803
  "wardic.drag",
739
804
  attributes,
740
805
  feed,
741
- () => (event) => {
742
- const [next, samples] = fold(MutableRef.get(state), mapped(event))
743
- MutableRef.set(state, next)
744
- Arr.forEach(samples, admit)
745
- return true
746
- },
806
+ () => mappedHandler(mapPoint, fold, state, admit),
747
807
  handlers,
748
808
  )
749
809
  // Its ends from outside its pointer (#431) only for a target given Drag controls (#539): no other
@@ -847,19 +907,11 @@ export const watchPan: (target: unknown, handlers: InputFeed.Handlers<PanSample>
847
907
  "wardic.pan",
848
908
  PAN_ATTRIBUTES,
849
909
  input.pointer(target),
850
- () => {
851
- const state = MutableRef.make(initState)
852
- const mapped = mappedEvents(options.mapPoint)
853
- return (event) => {
854
- const [next, samples] = panStep(MutableRef.get(state), mapped(event))
855
- MutableRef.set(state, next)
856
- Arr.forEach(samples, (sample) => {
857
- counted(sample)
858
- handlers.on(sample)
859
- })
860
- return true
861
- }
862
- },
910
+ () =>
911
+ mappedHandler(options.mapPoint, panStep, MutableRef.make(initState), (sample) => {
912
+ counted(sample)
913
+ handlers.on(sample)
914
+ }),
863
915
  handlers,
864
916
  )
865
917
  })
package/src/Frame.ts CHANGED
@@ -14,7 +14,7 @@
14
14
  */
15
15
  // CONTEXT.md's Frame; docs/adr/0001-core-stream-contract.md. A plain shape, so it has no prototype
16
16
  // (ADR-0028, decision 1): a step allocates one every tick, or rewrites one per run in place, as
17
- // `springValues`' does (ADR-0030, decision 11). Only the physics Primitives (`spring`, `decay`, `chase`)
17
+ // `springValues`', a tween's and the blends' do (ADR-0030, decision 11 and *Amendment (#895)*). Only the physics Primitives (`spring`, `decay`, `chase`)
18
18
  // emit `velocity`, the field `isPhysics` narrows on.
19
19
  import * as Predicate from "effect/Predicate"
20
20