@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
@@ -52,7 +52,8 @@ import * as Cause from "effect/Cause"
52
52
  import * as Effect from "effect/Effect"
53
53
  import * as Exit from "effect/Exit"
54
54
  import type * as Fiber from "effect/Fiber"
55
- import { constVoid, identity } from "effect/Function"
55
+ import { constTrue, constVoid, identity } from "effect/Function"
56
+ import * as Option from "effect/Option"
56
57
  import * as Predicate from "effect/Predicate"
57
58
  import * as Rec from "effect/Record"
58
59
  import * as UndefinedOr from "effect/UndefinedOr"
@@ -142,6 +143,8 @@ export interface BatchCall {
142
143
  readonly skipAnimations: boolean
143
144
  readonly head: Playback.Playhead
144
145
  readonly settled: () => void
146
+ /** It ended with a key a later change took, never landing (#951). */
147
+ readonly taken: () => void
145
148
  /** The members it moves, in order: every member when left out. */
146
149
  readonly members: ReadonlyArray<number> | undefined
147
150
  }
@@ -154,6 +157,8 @@ export interface BatchSequenceCall {
154
157
  readonly skipAnimations: boolean
155
158
  readonly head: Playback.Playhead
156
159
  readonly settled: () => void
160
+ /** It ended with a key a later change took, never landing (#951). */
161
+ readonly taken: () => void
157
162
  }
158
163
 
159
164
  /** An `animate` call's parts for Subjects that are Batch members' (#665): each Batch's members, and each Subject's own keys. */
@@ -250,7 +255,7 @@ const plainOf = <S extends object>(env: AnimateEnv, subjects: Subjects<S>, subje
250
255
  ownValues: env.ownValues,
251
256
  measuredPairs: env.measuredPairs,
252
257
  variableValues: env.variableValues,
253
- start: { initialValues: {}, initialLink: {}, startedLink: {}, quiet: false },
258
+ start: { initialValues: {}, initialLink: {}, startedLink: {}, quiet: false, failure: Option.none() },
254
259
  snapshot,
255
260
  reuse: Spine.makeReuse(),
256
261
  links: subjects.startLinksOf(subject),
@@ -295,25 +300,71 @@ const IMPERATIVE: Spine.Resolution = {
295
300
  change: undefined,
296
301
  }
297
302
 
303
+ /** The target the Override stack had for each key at the spine's last Retarget: what a taken key is noted with. */
304
+ const lastTargetsOf = (host: Spine.SpineHost): Values.Values => UndefinedOr.match(host.lastResolution(), { onUndefined: () => Spine.NO_VALUES, onDefined: (last) => last.resolved })
305
+
306
+ /**
307
+ * `key` taken by an Imperative animation from whatever runs it (#433): noted in `taking` with `noted`,
308
+ * the target the Override stack has for it now, no longer returning, its Timeline value stopped, given
309
+ * up by its owner, owned, and started where it's drawn toward `like`, noted in `started`. Its Own value
310
+ * is read before it's taken, by the call (`readiedOf`) or the landing (`settableOrReported`); a run's
311
+ * retake at a replay reads none, the call having read it.
312
+ */
313
+ // #973 (ADR-0061, *Amendment (#973)*, Q3): read here, a key refused at the call had already taken the keys
314
+ // before it, of its Subject and of a list's earlier Subjects, ending the animations that drew them.
315
+ const takeKey = (host: Spine.SpineHost, taking: Map<string, number | string | undefined>, key: string, noted: number | string | undefined, like: number | string, started: Record<string, string>): void => {
316
+ taking.set(key, noted)
317
+ host.returning().delete(key)
318
+ // Its Timeline value stops while the animation draws the key (#240).
319
+ const timelineKeys = host.timelineKeys()
320
+ if (Predicate.isNotUndefined(timelineKeys)) timelineKeys.taken(key)
321
+ const owner = host.owners.get(key)
322
+ if (Predicate.isNotUndefined(owner)) host.giveUp(key, owner)
323
+ host.startKey(key, like, started)
324
+ }
325
+
326
+ /**
327
+ * `key`'s Own value read, where it has no `initial` value: a key the Subject lacks, or can't write, is
328
+ * its Environment's error, naming it.
329
+ */
330
+ // Per key at a call and at a landing, the hot path (effect-first.md): a statement.
331
+ const readOwn = (host: Spine.SpineHost, key: string): void => {
332
+ if (!Object.hasOwn(host.initialValues, key)) host.ownValue(key)
333
+ }
334
+
335
+ /** Whether a Transition end may set a key drawn by `owner`: none draws it, or its leg is done. */
336
+ const endFree = (owner: Spine.Running | undefined): boolean => {
337
+ if (Predicate.isUndefined(owner)) return true
338
+ return owner.done
339
+ }
340
+
298
341
  /**
299
342
  * An **Imperative animation** of a Subject (#433), through its spine's `host`: it takes `target`'s
300
343
  * keys from whatever runs them, each noted with the target the Override stack has for it now, so a
301
344
  * Retarget leaves it where the animation leaves it until that target changes (Motion's latest-wins).
302
345
  * Its legs start where the keys are drawn, with their velocity, on `given` (a key with none of its own
303
346
  * on its Default transition), reduced motion as the provider says, and run through `head` once
304
- * {@link Spine.ImperativeTrack.run} is called.
347
+ * {@link Spine.ImperativeTrack.run} is called. Once they land, none taken, its target's Transition
348
+ * end is set (#896), and put back by a cancel or a run again. Its keys, the end's too, were read before
349
+ * it's made (`readiedOf`); one the Subject can no longer set by a landing is reported there and left as it
350
+ * is, and the rest of the end set (#970).
305
351
  */
306
352
  // #746 (ADR-0067, *Amendment (#746)*, decision 10): here, not in the spine, so an app that never animates
307
353
  // imperatively ships none of it; the spine hands over what it takes (`SpineHost`).
308
354
  const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target, given: Config.WardicTransition | undefined, head: Playback.Playhead, hooks: Spine.ImperativeHooks): Spine.ImperativeTrack => {
309
- const { snapshot, owners, giveUp, initialValues, ownValue, startKey, keepTheRest, element, positionalKeys, defaultTransition, unitScale, readVelocity, gate, forget, endLeg, live, own, legProgram, legEnded, turnBack, id, recordFrame, runtime, elementRules, measuredPairs, variableValues } = host
355
+ const { snapshot, owners, keepTheRest, element, positionalKeys, defaultTransition, unitScale, readVelocity, gate, forget, endLeg, live, own, legProgram, legEnded, turnBack, id, recordFrame, runtime, elementRules, measuredPairs, variableValues } = host
310
356
  /** A leg of an Imperative animation, forked as a Retarget's are; it lands on its track. */
311
357
  // Data-first: a pipe's curried forms are two closures more for every leg (#618, H1).
312
358
  const runImperative = (running: Spine.Running, program: Effect.Effect<unknown, never, WardicServices.WardicServices>): void => {
313
- running.fiber = runtime.runFork(Effect.asVoid(Effect.onExit(program, legEnded(running))))
359
+ const at = runs
360
+ running.fiber = runtime.runFork(Effect.asVoid(Effect.onExit(program, (exit) => {
361
+ if (at === runs) supersededAt(running)
362
+ return legEnded(running)(exit)
363
+ })))
314
364
  }
315
365
  const to = target.values
316
- const before = UndefinedOr.match(host.lastResolution(), { onUndefined: () => Spine.NO_VALUES, onDefined: (last) => last.resolved })
366
+ const ends = target.transitionEnd
367
+ const before = lastTargetsOf(host)
317
368
  const started: Record<string, string> = {}
318
369
  let measuring: Array<string> | undefined
319
370
  // Each key taken from whatever runs it, noted with its target now, and started where it's drawn.
@@ -322,16 +373,8 @@ const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target,
322
373
  // Where a Path's axis this target leaves out rests (#574), before its keys are taken below.
323
374
  const rests = Spine.restsOf(owners, to)
324
375
  Arr.forEach(Rec.keys(to), (key) => {
325
- takenNow.set(key, before[key])
326
- host.returning().delete(key)
327
- // Its Timeline value stops while the animation draws the key (#240).
328
- const timelineKeys = host.timelineKeys()
329
- if (Predicate.isNotUndefined(timelineKeys)) timelineKeys.taken(key)
330
- const owner = owners.get(key)
331
- if (Predicate.isNotUndefined(owner)) giveUp(key, owner)
332
- if (!Object.hasOwn(initialValues, key)) ownValue(key)
333
376
  const like = UndefinedOr.getOrThrow(to[key])
334
- startKey(key, like, started)
377
+ takeKey(host, takenNow, key, before[key], like, started)
335
378
  if (measuredPairs.needs(key, UndefinedOr.getOrThrow(snapshot.values[key]), like)) {
336
379
  if (Predicate.isUndefined(measuring)) measuring = []
337
380
  measuring.push(key)
@@ -348,29 +391,159 @@ const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target,
348
391
  const reduce = host.reducedMotion()
349
392
  const skipped = host.skipAnimations()
350
393
  let measured: RetargetLegs.MeasuredPairs | undefined
351
- if (Bool.and(Bool.not(Bool.or(reduce, skipped)), Predicate.isNotUndefined(measuring))) measured = Spine.measuredOf(measuredPairs, element, UndefinedOr.getOrThrow(measuring), from, to, snapshot.values, stops)
394
+ if (Bool.and(Bool.not(Bool.or(reduce, skipped)), Predicate.isNotUndefined(measuring))) measured = Spine.measuredOf(measuredPairs, element, UndefinedOr.getOrThrow(measuring), from, to, snapshot.values, stops, runtime, id)
352
395
  // Built once: a seek or a replay runs the same legs, from the same start, with the same velocities.
353
396
  const built = RetargetLegs.legsOf(from, to, given, reduce, skipped, positionalKeys, defaultTransition, readVelocity, undefined, stops, measured, snapshot.values, rests, unitScale)
354
397
  const variables = Spine.holdsVariable(variableValues, from, to, stops)
355
398
  const legs = Arr.map(built, (leg) => RetargetLegs.mapMachine(leg, Playback.drive(head)))
356
399
  let runnings: ReadonlyArray<Spine.Running> = Spine.NO_RUNNING
357
400
  let left = 0
401
+ // What its Transition end's keys were before it set them (Q5): `undefined` until it has.
402
+ let overwritten: Values.Values | undefined
403
+ // Its runs, stops and cancels, counted: what a landing waiting on a flush checks it's still for.
404
+ let runs = 0
405
+ // `values` drawn at once, at rest: newer than the legs that let them go (#464).
406
+ const setAtRest = (values: Values.Values): void => {
407
+ snapshot.values = { ...snapshot.values, ...values }
408
+ snapshot.velocities = { ...snapshot.velocities, ...Rec.map(values, () => 0) }
409
+ const now = snapshot.now()
410
+ Arr.forEach(Rec.keys(values), (key) => {
411
+ snapshot.drawnAt[key] = now
412
+ })
413
+ gate.write(id, values)
414
+ recordFrame(values)
415
+ }
416
+ /**
417
+ * Its **Transition end** set (#896, ADR-0061 *Amendment (#896)*, Q2 and Q4): each key no leg draws
418
+ * now taken as the keys it animates are, started where it's drawn, then set at once.
419
+ */
420
+ // Each of `values`' keys `only` picks taken as the call took its own (`takeKey`), noted with the
421
+ // Override stack's targets now: an end's keys as it's set, a run's again as it starts (#951).
422
+ const takeEach = (values: Values.Values, only: (key: string) => boolean): void => {
423
+ const takingNow = host.taking()
424
+ const targetsNow = lastTargetsOf(host)
425
+ const startedNow: Record<string, string> = {}
426
+ Arr.forEach(Rec.keys(values), (key) => {
427
+ if (only(key)) takeKey(host, takingNow, key, targetsNow[key], UndefinedOr.getOrThrow(values[key]), startedNow)
428
+ })
429
+ }
430
+ // Whether an end's `key` can still be set, else reported: one its Subject has lost, or can no longer
431
+ // write, since the call read it is reported at the landing and left as it is, and the rest of the end is
432
+ // set (#970).
433
+ const settableOrReported = (key: string): boolean => {
434
+ try {
435
+ readOwn(host, key)
436
+ return true
437
+ } catch (defect) {
438
+ runtime.runFork(Report.failure(`wardic.animate: a Transition end's "${key}" couldn't be set, the rest of it set and the animation landed`, Cause.die(defect)))
439
+ return false
440
+ }
441
+ }
442
+ const writeEnds = (values: Values.Values): void => {
443
+ const free = Rec.filter(values, (_, key) => endFree(owners.get(key)) && settableOrReported(key))
444
+ if (Rec.isEmptyRecord(free)) return
445
+ takeEach(free, constTrue)
446
+ // Kept from an earlier landing a run from its end didn't put back: what they were before any.
447
+ if (Predicate.isUndefined(overwritten)) overwritten = Rec.map(free, (_, key) => UndefinedOr.getOrThrow(snapshot.values[key]))
448
+ setAtRest(free)
449
+ }
450
+ /**
451
+ * Its Transition end's keys put back as they were before it was set (Q5): at a cancel, a seek or a
452
+ * replay, each a key no leg has drawn since, a later call's or the element's own. The next landing
453
+ * sets them again.
454
+ */
455
+ const putBack = (): void => {
456
+ if (Predicate.isUndefined(overwritten)) return
457
+ // Its take left each without an owner: one since is a later animation's, which wins.
458
+ const back = Rec.filter(overwritten, (_, key) => Predicate.isUndefined(owners.get(key)))
459
+ overwritten = undefined
460
+ if (Bool.not(Rec.isEmptyRecord(back))) setAtRest(back)
461
+ }
462
+ // Whether its `i`th leg snaps (#468's skip, reduced motion's positional keys): it lands on its next
463
+ // frame, its keys landed from its start, so a take before then takes nothing in flight, as Motion's
464
+ // skipped value makes no animation. Its machine read only for a leg that lost a key.
465
+ const snaps = (i: number): boolean => Machine.durationOf(machineOf(UndefinedOr.getOrThrow(built[i]))) === 0
466
+ // A leg the element's own Retarget carried on, toward the same target, as its own (Q7): the call has lost it.
467
+ let carried = false
468
+ // Taken (#896's Q3, #951): a key it animates taken from its leg mid-flight, by a later animation or the
469
+ // element's own target, or a leg carried off whole. A leg that lost one has fewer than it started
470
+ // with; one taken after its leg landed doesn't count, as Motion's, nor one that snaps.
471
+ const taken = (): boolean => {
472
+ if (carried) return true
473
+ return Arr.some(runnings, (running, i) => Bool.and(running.count < Rec.size(running.targets), Bool.not(snaps(i))))
474
+ }
475
+ // Landed: its Transition end set at the flush after its last leg's frame, then the landing told
476
+ // after the flush that draws it, so the landing includes it (ADR-0057 decision 4). Taken, it ends
477
+ // without landing, its end dropped, as Motion's call never resolves (ADR-0061 *Amendment (#951)*).
478
+ // A run, a stop or a cancel since makes each wait stale: a take reported synchronously, then a seek
479
+ // in the same task, or a stop between its end and its landing, tells none.
480
+ const landed = (): void => {
481
+ const at = runs
482
+ if (taken()) {
483
+ return gate.afterFlush(() => {
484
+ if (at === runs) hooks.settled(false)
485
+ })
486
+ }
487
+ if (Predicate.isUndefined(ends)) {
488
+ return gate.afterFlush(() => {
489
+ if (at === runs) hooks.settled(true)
490
+ })
491
+ }
492
+ gate.afterFlush(() => {
493
+ if (at !== runs) return
494
+ writeEnds(ends)
495
+ gate.afterFlush(() => {
496
+ if (at === runs) hooks.settled(true)
497
+ })
498
+ })
499
+ }
500
+ // A leg landed, or was Superseded (#951): the last lands the track, or finds it taken.
501
+ const legDone = (): void => {
502
+ left--
503
+ if (left === 0) landed()
504
+ }
358
505
  const landing: Spine.Landing = {
359
506
  report: constVoid,
360
- settled: () => {
361
- left--
362
- if (left === 0) gate.afterFlush(hooks.settled)
363
- },
507
+ settled: legDone,
364
508
  failed: hooks.failed,
365
509
  }
510
+ /**
511
+ * A leg of this run ended **Superseded** (#951), told at its end, as it settles nothing of this
512
+ * track's: carried on by the element's own Retarget as its own, its landing the Retarget's now (Q7);
513
+ * or with every key taken, a count of 0 (only a take lowers it), which `taken` reads. Either counts
514
+ * toward the track's landing, which finds it taken.
515
+ */
516
+ // Here, not in the spine, so an app with no Imperative animation pays nothing for it.
517
+ const supersededAt = (running: Spine.Running): void => {
518
+ if (running.landing !== landing) {
519
+ carried = true
520
+ return legDone()
521
+ }
522
+ if (running.count === 0) legDone()
523
+ }
366
524
  // Its legs ended where they are: the keys they draw are drawn by none.
367
525
  const end = (): void =>
368
526
  Arr.forEach(runnings, (running) => {
369
527
  forget(running)
370
528
  if (Bool.not(running.done)) endLeg(running)
371
529
  })
530
+ // Whether a run starts at its end, going forward, its end set: it lands at once, so its end stays,
531
+ // with no frame without it (a seek to the end, `complete()`). Its duration read only then.
532
+ const fromItsEnd = (): boolean => {
533
+ if (Predicate.isUndefined(overwritten)) return false
534
+ if (head.speed < 0) return false
535
+ return head.time >= duration()
536
+ }
537
+ // A key another leg draws: a later animation's, or the element's own.
538
+ const drawnByAnother = (key: string): boolean => Predicate.isNotUndefined(owners.get(key))
372
539
  const run = (): void => {
540
+ runs++
373
541
  end()
542
+ carried = false
543
+ // Run again, it takes each of its keys from whatever draws it now, as the call took them (#951): one
544
+ // leg draws a key. The first run's are taken already.
545
+ if (runs > 1) takeEach(to, drawnByAnother)
546
+ if (Bool.not(fromItsEnd())) putBack()
374
547
  left = legs.length
375
548
  Spine.tellStarted(live, legs)
376
549
  runnings = Arr.map(legs, (leg) => {
@@ -380,20 +553,15 @@ const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target,
380
553
  return running
381
554
  })
382
555
  // A target with no key lands at once.
383
- if (left === 0) gate.afterFlush(hooks.settled)
556
+ if (left === 0) landed()
384
557
  }
385
558
  const cancel = (): void => {
559
+ runs++
386
560
  end()
387
561
  turnBack()
388
- snapshot.values = { ...snapshot.values, ...from }
389
- snapshot.velocities = { ...snapshot.velocities, ...Rec.map(from, () => 0) }
390
- // Put back at once: at rest, newer than the legs that let them go (#464).
391
- const now = snapshot.now()
392
- Arr.forEach(Rec.keys(from), (key) => {
393
- snapshot.drawnAt[key] = now
394
- })
395
- gate.write(id, from)
396
- recordFrame(from)
562
+ putBack()
563
+ // Put back at once, its keys where it started them.
564
+ setAtRest(from)
397
565
  }
398
566
  // Each leg's machine, run from its start to rest, apart from the Playhead, which it would move: a
399
567
  // blend's built with the Subject's Key rules.
@@ -404,6 +572,7 @@ const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target,
404
572
  return leg.machine() as Machine.Machine<Frame.Frame<unknown>>
405
573
  }
406
574
  const stop = (): void => {
575
+ runs++
407
576
  end()
408
577
  turnBack()
409
578
  }
@@ -411,16 +580,69 @@ const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target,
411
580
  }
412
581
 
413
582
  /**
414
- * A Subject's animation of `target`: through its spine when it's a Wardic element's, else through its
415
- * plain spine, in the gate while any of its animations runs.
583
+ * A call's transition on its target's own (#896, ADR-0061 *Amendment (#896)*, Q1): the options' fields
584
+ * laid over the target's, shallowly, as `inherit: true` lays a target's over its element's. Motion's
585
+ * `animate` reads the options alone.
416
586
  */
417
- const trackOf = <S extends object>(env: AnimateEnv, subjects: Subjects<S>, subject: S, target: Config.WardicVariant, given: Config.WardicTransition | undefined, head: Playback.Playhead, settled: () => void): Spine.ImperativeTrack => {
418
- const resolved = VariantTarget.resolveTarget(target, undefined, undefined, VariantTarget.NO_VARIANT_STATE)
587
+ // Its own handed back as it is with no options, so the legs' decode cache (#66) holds for it. Its
588
+ // `inherit` has nothing above it in a call: the legs read none (`RetargetLegs`' `NOT_DEFINING`).
589
+ const overOwn = (own: Config.WardicVariantTransition | undefined, given: Config.WardicTransition | undefined): Config.WardicTransition | undefined => {
590
+ if (Predicate.isUndefined(own)) return given
591
+ if (Predicate.isUndefined(given)) return own as Config.WardicTransition
592
+ return { ...own, ...given } as Config.WardicTransition
593
+ }
594
+
595
+ /** A Subject's animation, its keys read and none taken yet: its target, and the spine it runs through. */
596
+ interface Readied<S extends object> {
597
+ readonly subject: S
598
+ readonly target: VariantTarget.Target
599
+ readonly host: Spine.SpineHost
600
+ /** Its plain spine: `undefined` for a Wardic element's, which runs through the element's own. */
601
+ readonly plain: Plain | undefined
602
+ }
603
+
604
+ /**
605
+ * `subject`'s animation of `variant`, readied: each key the target names, its Transition end's then its
606
+ * values', read as its Environment reads it, so one the Subject can't take is an error now, naming it.
607
+ * A call readies each of its Subjects before it builds any track ({@link trackOf}), so a call refused here
608
+ * has taken nothing: an animation it would have taken a key from runs on.
609
+ */
610
+ // #973 (ADR-0061, *Amendment (#973)*, Q1): tracks took their keys as each was built, so a later key's
611
+ // refusal, its Subject's or a later Subject's, ended the animations that drew the keys taken before it.
612
+ const readiedOf = <S extends object>(env: AnimateEnv, subjects: Subjects<S>, subject: S, variant: Config.WardicVariant): Readied<S> => {
613
+ const target = VariantTarget.resolveTarget(variant, undefined, undefined, VariantTarget.NO_VARIANT_STATE)
614
+ // A function given as the target (past the types) that throws is an error at the call, as a key it
615
+ // can't take is: the call takes nothing (#979; ADR-0074 keeps a refusal at the call a throw).
616
+ if (Option.isSome(target.failure)) throw target.failure.value.defect
419
617
  const own = wardicElements.get(subject)
420
- const hooks = { settled, failed: constVoid }
421
- const transition = skippedIn(env, given)
422
- if (Predicate.isNotUndefined(own)) return imperativeTrackOf(own.host(), resolved, transition, head, hooks)
618
+ if (Predicate.isNotUndefined(own)) return readOwnValues({ subject, target, host: own.host(), plain: undefined })
423
619
  const plain = plainOf(env, subjects, subject)
620
+ return readOwnValues({ subject, target, host: plain.spine.host(), plain })
621
+ }
622
+
623
+ /**
624
+ * `readied`'s Own values read, its Transition end's keys then its target's: an error the Environment's
625
+ * read throws is thrown, naming the first key it can't take.
626
+ */
627
+ // Its end's first, as #970 read them, so a key refused in both names the end's. Per key at a call, the hot
628
+ // path (effect-first.md): statements, a loop's body its closure.
629
+ const readOwnValues = <S extends object>(readied: Readied<S>): Readied<S> => {
630
+ const { host, target } = readied
631
+ const ends = target.transitionEnd
632
+ if (Predicate.isNotUndefined(ends)) Arr.forEach(Rec.keys(ends), (key) => readOwn(host, key))
633
+ Arr.forEach(Rec.keys(target.values), (key) => readOwn(host, key))
634
+ return readied
635
+ }
636
+
637
+ /**
638
+ * A readied Subject's animation (`readiedOf`): through its spine when it's a Wardic element's, else through
639
+ * its plain spine, in the gate while any of its animations runs.
640
+ */
641
+ const trackOf = <S extends object>(env: AnimateEnv, readied: Readied<S>, given: Config.WardicTransition | undefined, head: Playback.Playhead, settled: (landed: boolean) => void): Spine.ImperativeTrack => {
642
+ const { subject, target, host, plain } = readied
643
+ const hooks = { settled, failed: constVoid }
644
+ const transition = skippedIn(env, overOwn(target.transition, given))
645
+ if (Predicate.isUndefined(plain)) return imperativeTrackOf(host, target, transition, head, hooks)
424
646
  // In the gate while it runs; out once its last animation is done, after the flush of its last frame.
425
647
  let live = false
426
648
  const enter = (): void => {
@@ -441,10 +663,10 @@ const trackOf = <S extends object>(env: AnimateEnv, subjects: Subjects<S>, subje
441
663
  if (plain.running === 0) plain.unregister()
442
664
  })
443
665
  }
444
- const track = imperativeTrackOf(plain.spine.host(), resolved, transition, head, {
445
- settled: () => {
666
+ const track = imperativeTrackOf(host, target, transition, head, {
667
+ settled: (landed) => {
446
668
  leave()
447
- settled()
669
+ settled(landed)
448
670
  },
449
671
  failed: () => leave(),
450
672
  })
@@ -466,8 +688,16 @@ const trackOf = <S extends object>(env: AnimateEnv, subjects: Subjects<S>, subje
466
688
  }
467
689
  }
468
690
 
469
- /** A transition without its `delay`, each arm: `animate`'s and a Segment's take their own. */
470
- type WithoutDelay = Config.WardicTransition extends infer T ? (T extends unknown ? { readonly [K in keyof T as Exclude<K, "delay">]: T[K] } : never) : never
691
+ /** The transition `T` without its `delay`, each arm: `animate`'s and a Segment's take their own. */
692
+ type WithoutDelayOf<T> = T extends unknown ? { readonly [K in keyof T as Exclude<K, "delay">]: T[K] } : never
693
+
694
+ /** A transition without its `delay`, each arm. */
695
+ type WithoutDelay = WithoutDelayOf<Config.WardicTransition>
696
+
697
+ /** `animate`'s options over the transition `T`: its `delay` each Subject's, and `onComplete`. */
698
+ type AnimateOptionsOver<T> = WithoutDelayOf<T> & EachDelay & {
699
+ readonly onComplete?: (() => void) | undefined
700
+ }
471
701
 
472
702
  /** A `delay` that may be each Subject's (Motion's `delay(i, n)`; `stagger()` is #436). */
473
703
  interface EachDelay {
@@ -475,12 +705,20 @@ interface EachDelay {
475
705
  }
476
706
 
477
707
  /**
478
- * `animate`'s options: a transition, its `delay` a number, or a function of each Subject's index and
479
- * their count, such as `stagger(0.05)`; and `onComplete`, called as it lands. Motion's.
708
+ * `animate`'s options: a transition, laid field by field over the target's own, its `delay` a number,
709
+ * or a function of each Subject's index and their count, such as `stagger(0.05)`; and `onComplete`,
710
+ * called as it lands, each Subject's `transitionEnd` already set and the animation already `"finished"`,
711
+ * so one that throws is reported and leaves it finished. Motion's, though Motion's `animate` runs on the
712
+ * options alone.
480
713
  */
481
- export type WardicAnimateOptions = WithoutDelay & EachDelay & {
482
- readonly onComplete?: (() => void) | undefined
483
- }
714
+ export type WardicAnimateOptions = AnimateOptionsOver<Config.WardicTransition>
715
+
716
+ /**
717
+ * {@link WardicAnimateOptions} for a Subject whose keys are `K`: its transition's entries are under
718
+ * `K`'s names, as {@link Config.WardicTransitionFor}'s, `default` for the rest.
719
+ */
720
+ // #952: `@wardic/object`'s and `@wardic/three`'s `animate`, by the Subject the call names.
721
+ export type WardicAnimateOptionsFor<K extends string> = AnimateOptionsOver<Config.WardicTransitionFor<Exclude<K, "onComplete">>>
484
722
 
485
723
  /** A call's options without its `onComplete`: its transition. */
486
724
  const withoutComplete = (given: WardicAnimateOptions): Readonly<Record<string, unknown>> => {
@@ -502,14 +740,15 @@ const batchTransitionOf = (options: WardicAnimateOptions | undefined): Config.Wa
502
740
  UndefinedOr.map(options, (given) => withoutComplete(given) as Config.WardicTransition)
503
741
 
504
742
  /** A Batch call's track over `members` (every member when left out), driven by `head`: the call's options its transition. */
505
- const batchTrackOf = (env: AnimateEnv, binding: BatchCalls, target: Readonly<Record<string, unknown>>, members: ReadonlyArray<number> | undefined, options: WardicAnimateOptions | undefined, head: Playback.Playhead, settled: () => void): Spine.ImperativeTrack =>
743
+ const batchTrackOf = (env: AnimateEnv, binding: BatchCalls, target: Readonly<Record<string, unknown>>, members: ReadonlyArray<number> | undefined, options: WardicAnimateOptions | undefined, head: Playback.Playhead, settled: (landed: boolean) => void): Spine.ImperativeTrack =>
506
744
  binding.track({
507
745
  target,
508
746
  transition: batchTransitionOf(options),
509
747
  reducedMotion: env.reducedMotion(),
510
748
  skipAnimations: skipsBatchCall(env, options),
511
749
  head,
512
- settled,
750
+ settled: () => settled(true),
751
+ taken: () => settled(false),
513
752
  members,
514
753
  })
515
754
 
@@ -534,8 +773,15 @@ export interface WardicPlaybackControls extends PromiseLike<void> {
534
773
  speed: number
535
774
  /** How long it runs, in seconds: its longest Subject's longest key, a spring's to rest. */
536
775
  readonly duration: number
776
+ /**
777
+ * Where it is: `"running"`, `"paused"`, `"finished"` once it lands, and `"idle"` after `stop()` or
778
+ * `cancel()`, or once a later animation has taken one of its keys and the rest have landed.
779
+ */
537
780
  readonly state: WardicPlayState
538
- /** Resolves when it lands, or at `complete()`; never at `stop()` or `cancel()`, as Motion's. */
781
+ /**
782
+ * Resolves when it lands, or at `complete()`; never at `stop()` or `cancel()`, nor once a later
783
+ * animation takes one of its keys, as Motion's.
784
+ */
539
785
  readonly finished: Promise<void>
540
786
  /** Play it on: from where it's paused, or again from its start once it's finished or cancelled. Not after `stop()`. */
541
787
  readonly play: () => void
@@ -556,14 +802,22 @@ export interface AnimationWatch {
556
802
  readonly ended: (controls: WardicPlaybackControls, landed: boolean) => void
557
803
  }
558
804
 
559
- /** Playback controls over `tracks`, all run through `head`, started now. */
560
- const controlsOf = (tracks: ReadonlyArray<Spine.ImperativeTrack>, head: Playback.Playhead, watch: AnimationWatch, onComplete: (() => void) | undefined): {
805
+ /** Playback controls over `tracks`, all run through `head`, started now: a throw from `onComplete` reported on `runtime`. */
806
+ const controlsOf = (
807
+ tracks: ReadonlyArray<Spine.ImperativeTrack>,
808
+ head: Playback.Playhead,
809
+ watch: AnimationWatch,
810
+ onComplete: (() => void) | undefined,
811
+ runtime: AnimateEnv["runtime"],
812
+ ): {
561
813
  readonly controls: WardicPlaybackControls
562
- readonly settledOne: () => void
814
+ readonly settledOne: (landed: boolean) => void
563
815
  } => {
564
816
  let state: WardicPlayState = "running"
565
817
  let stopped = false
566
818
  let landed = 0
819
+ // Whether a Subject's part ended without landing, a key of it taken by a later animation (#951).
820
+ let anyTaken = false
567
821
  let resolve: (() => void) | undefined
568
822
  let finished: Promise<void> | undefined
569
823
  let isResolved = false
@@ -574,6 +828,7 @@ const controlsOf = (tracks: ReadonlyArray<Spine.ImperativeTrack>, head: Playback
574
828
  }
575
829
  const runAll = (): void => {
576
830
  landed = 0
831
+ anyTaken = false
577
832
  Arr.forEach(tracks, (track) => track.run())
578
833
  }
579
834
  const fresh = (): void => {
@@ -671,14 +926,28 @@ const controlsOf = (tracks: ReadonlyArray<Spine.ImperativeTrack>, head: Playback
671
926
  runAll()
672
927
  },
673
928
  }
674
- // Each Subject's legs landed: the last lands the animation.
675
- const settledOne = (): void => {
929
+ // Each Subject's legs landed, or ended with a key taken: the last lands the animation, or, with one
930
+ // taken, ends it without landing, as a stop does (#951, Q1 and Q5). Motion's never resolves then.
931
+ const settledOne = (landedOne: boolean): void => {
676
932
  landed++
933
+ if (Bool.not(landedOne)) anyTaken = true
677
934
  if (landed < tracks.length) return
935
+ if (anyTaken) {
936
+ stopped = true
937
+ return end("idle", false)
938
+ }
678
939
  isResolved = true
679
940
  UndefinedOr.map(resolve, (done) => done())
680
- UndefinedOr.map(onComplete, (complete) => complete())
941
+ // Finished before its onComplete runs, as Motion's `finish()`: a throw there leaves it finished, and
942
+ // is reported each time it lands (#977; ADR-0074, *Amendment (#979, group A)*).
681
943
  end("finished", true)
944
+ UndefinedOr.map(onComplete, (complete) => {
945
+ try {
946
+ complete()
947
+ } catch (defect) {
948
+ runtime.runFork(Report.failure("wardic.animate: an onComplete failed", Cause.die(defect)))
949
+ }
950
+ })
682
951
  }
683
952
  return { controls, settledOne }
684
953
  }
@@ -726,13 +995,14 @@ export type WardicSegmentTarget = Omit<Config.WardicTargetValues, "transition">
726
995
  /**
727
996
  * A function Segment, `[callback, value?, options?]`: its callback is given a value each frame, from 0
728
997
  * to 1 unless it gives its own stops, for what isn't a Subject's own value. Motion's, in place of its
729
- * motion value Segments.
998
+ * motion value Segments. A callback that throws is reported, once until it returns, and given the next
999
+ * frame's value all the same: its Segment, and its sequence, land as their stops do.
730
1000
  */
731
1001
  export type WardicFunctionSegment =
732
1002
  | readonly [callback: (value: number) => void]
733
1003
  | readonly [callback: (value: number) => void, options: WardicSegmentOptions]
734
- | readonly [callback: (value: number) => void, value: number | Config.WardicKeyframes<number>, options: WardicSegmentOptions]
735
- | readonly [callback: (value: string) => void, value: string | Config.WardicKeyframes<string>, options: WardicSegmentOptions]
1004
+ | readonly [callback: (value: number) => void, value: number | Config.WardicKeyframes<number>, options?: WardicSegmentOptions]
1005
+ | readonly [callback: (value: string) => void, value: string | Config.WardicKeyframes<string>, options?: WardicSegmentOptions]
736
1006
 
737
1007
  /**
738
1008
  * A sequence's options: `duration`, the whole sequence scaled to it; `delay`; `repeat`, `repeatType`
@@ -754,22 +1024,49 @@ export interface WardicSequenceOptions {
754
1024
  * to `watch`, each track run. Finished at once with none.
755
1025
  */
756
1026
  const started = (
1027
+ runtime: AnimateEnv["runtime"],
757
1028
  watch: AnimationWatch,
758
1029
  count: number,
759
1030
  onComplete: (() => void) | undefined,
760
- tracksOf: (head: Playback.Playhead, settled: () => void) => ReadonlyArray<Spine.ImperativeTrack>,
1031
+ tracksOf: (head: Playback.Playhead, settled: (landed: boolean) => void) => ReadonlyArray<Spine.ImperativeTrack>,
761
1032
  ): WardicPlaybackControls => {
762
1033
  if (count === 0) return finishedControls()
763
1034
  const head = Playback.make()
764
- let settledOne = constVoid
765
- const tracks = tracksOf(head, () => settledOne())
766
- const made = controlsOf(tracks, head, watch, onComplete)
1035
+ let settledOne: (landed: boolean) => void = constVoid
1036
+ const tracks = tracksOf(head, (landedOne) => settledOne(landedOne))
1037
+ const made = controlsOf(tracks, head, watch, onComplete, runtime)
767
1038
  settledOne = made.settledOne
768
1039
  watch.started(made.controls)
769
- Arr.forEach(tracks, (track) => track.run())
1040
+ // A run that refuses the call (a Batch part's `delay` that throws, at its first play) stops it, so its
1041
+ // scope hears it end, and the throw goes on to the call (#979, row 20).
1042
+ try {
1043
+ Arr.forEach(tracks, (track) => track.run())
1044
+ } catch (defect) {
1045
+ made.controls.stop()
1046
+ throw defect
1047
+ }
770
1048
  return made.controls
771
1049
  }
772
1050
 
1051
+ /**
1052
+ * A track built as it first runs: a mixed call's plain Subject, whose keys are taken only once the Batch
1053
+ * parts before it have run, a Batch part's `delay` among what they call. Stopped or cancelled before it
1054
+ * runs, it has taken nothing.
1055
+ */
1056
+ const builtAtRun = (build: () => Spine.ImperativeTrack): Spine.ImperativeTrack => {
1057
+ let built: Spine.ImperativeTrack | undefined
1058
+ const track = (): Spine.ImperativeTrack => {
1059
+ if (Predicate.isUndefined(built)) built = build()
1060
+ return built
1061
+ }
1062
+ return {
1063
+ run: () => track().run(),
1064
+ stop: () => UndefinedOr.map(built, (made) => made.stop()),
1065
+ cancel: () => UndefinedOr.map(built, (made) => made.cancel()),
1066
+ duration: () => track().duration(),
1067
+ }
1068
+ }
1069
+
773
1070
  /**
774
1071
  * An **Animation scope**'s `animate` (#433), over an Environment's {@link Ports}: `animate(subject,
775
1072
  * target, options?)`, a Batch's `animate(batch, target, options?)` and `animate(sequence, options?)`,
@@ -777,6 +1074,13 @@ const started = (
777
1074
  * (the DOM's selectors are looked up inside it); each animation is told to `watch` as it starts and
778
1075
  * ends. Every Subject's legs start in the call, on the loop by the end of its task. An Environment
779
1076
  * types its own overloads over it.
1077
+ *
1078
+ * A key a target or its Transition end names that a Subject can't take (its Environment's own value
1079
+ * read throws: a key it lacks, say) is an error at the call, naming it, and so is a `delay` function that
1080
+ * throws. The call then takes nothing from any of its Subjects: an animation it would have taken a key
1081
+ * from runs on.
1082
+ * A Transition end's key a Subject can no longer set when it lands is reported, left as it is, and the
1083
+ * rest of its end set: the call lands.
780
1084
  */
781
1085
  export const make = <S extends object>(env: AnimateEnv, ports: Ports<S>, scope: () => unknown, watch: AnimationWatch) =>
782
1086
  (first: unknown, second?: unknown, third?: WardicAnimateOptions): WardicPlaybackControls => {
@@ -785,7 +1089,7 @@ export const make = <S extends object>(env: AnimateEnv, ports: Ports<S>, scope:
785
1089
  const batch = ports.batches.entryOf(first)
786
1090
  if (Predicate.isNotUndefined(batch)) {
787
1091
  const members = batch.binding.namedOf(batch)
788
- return started(watch, 1, UndefinedOr.map(third, (given) => given.onComplete), (head, settled) => [
1092
+ return started(env.runtime, watch, 1, UndefinedOr.map(third, (given) => given.onComplete), (head, settled) => [
789
1093
  batchTrackOf(env, batch.binding, second as Readonly<Record<string, unknown>>, members, third, head, settled),
790
1094
  ])
791
1095
  }
@@ -795,15 +1099,28 @@ export const make = <S extends object>(env: AnimateEnv, ports: Ports<S>, scope:
795
1099
  // No member's Subject among them: each Subject's track, building no parts (bench:solid --batch's
796
1100
  // per-element page, a call per element: 8.2 -> 8.5 ms at 1,000 with the parts built for every call; #665).
797
1101
  if (Bool.not(ports.batches.hasMembers(subjects))) {
798
- return started(watch, subjects.length, onComplete, (head, settled) =>
799
- Arr.map(subjects, (subject, index) => trackOf(env, ports.subjects, subject, target, transitionFor(third, index, subjects.length), head, settled)))
1102
+ return started(env.runtime, watch, subjects.length, onComplete, (head, settled) => {
1103
+ // One Subject: readied, then its track, which takes its keys once they're all read.
1104
+ if (subjects.length === 1) return [trackOf(env, readiedOf(env, ports.subjects, subjects[0], target), transitionFor(third, 0, 1), head, settled)]
1105
+ // Every Subject readied, and its transition worked out (a `delay(i, n)` called), before any track takes a key (#973; #979, row 20).
1106
+ const readied = Arr.map(subjects, (subject) => readiedOf(env, ports.subjects, subject, target))
1107
+ const transitions = Arr.map(subjects, (_, index) => transitionFor(third, index, subjects.length))
1108
+ return Arr.zipWith(readied, transitions, (each, transition) => trackOf(env, each, transition, head, settled))
1109
+ })
800
1110
  }
801
1111
  // A Batch's members' Subjects as one call over them, with its keys; the rest of each Subject's on its own (#665, Q14, Q35).
802
1112
  const parts = ports.batches.callPartsOf(subjects, target)
803
- return started(watch, parts.members.length + parts.subjects.length, onComplete, (head, settled) => [
804
- ...Arr.map(parts.members, (part) => batchTrackOf(env, part.binding, part.target, part.members, third, head, settled)),
805
- ...Arr.map(parts.subjects, (part) => trackOf(env, ports.subjects, part.subject, part.target as Config.WardicVariant, transitionFor(third, part.index, subjects.length), head, settled)),
806
- ])
1113
+ return started(env.runtime, watch, parts.members.length + parts.subjects.length, onComplete, (head, settled) => {
1114
+ // A Batch's track takes nothing until it runs; every other Subject readied, and its transition worked
1115
+ // out, before any track is built (#973). The Batch parts run first, and each other Subject's track is
1116
+ // built as it runs, so a Batch part's `delay` that throws takes nothing either (#979, row 20).
1117
+ const readied = Arr.map(parts.subjects, (part) => readiedOf(env, ports.subjects, part.subject, part.target as Config.WardicVariant))
1118
+ const transitions = Arr.map(parts.subjects, (part) => transitionFor(third, part.index, subjects.length))
1119
+ return [
1120
+ ...Arr.map(parts.members, (part) => batchTrackOf(env, part.binding, part.target, part.members, third, head, settled)),
1121
+ ...Arr.zipWith(readied, transitions, (each, transition) => builtAtRun(() => trackOf(env, each, transition, head, settled))),
1122
+ ]
1123
+ })
807
1124
  }
808
1125
 
809
1126
  // --- Sequences (#433's PR 2) ------------------------------------------------------------------
@@ -829,6 +1146,9 @@ const ZERO_TO_ONE: ReadonlyArray<number> = [0, 1]
829
1146
  /** No fields. */
830
1147
  const NO_FIELDS: Readonly<Record<string, unknown>> = {}
831
1148
 
1149
+ /** What a function Segment's callback, or its run, failing is reported as. */
1150
+ const SEGMENT_FAILED = "wardic.animate: a function Segment failed"
1151
+
832
1152
  /**
833
1153
  * What a Segment's transition doesn't take from the sequence's `defaultTransition` (Motion's): its
834
1154
  * delay, times and repeat, which are each Segment's own, and a value's own transitions.
@@ -875,8 +1195,11 @@ const segmentTransitionOf = (options: WardicSegmentOptions | undefined, key: str
875
1195
  return Bool.match(Predicate.isUndefined(delay), { onTrue: () => run, onFalse: () => ({ ...run, delay }) as Sequence.Transition })
876
1196
  }
877
1197
 
1198
+ /** Whether a function Segment's second part is its value: a number, a string or a list of stops. Anything else is its options. */
1199
+ const isFunctionSegmentValue: Predicate.Predicate<unknown> = /*#__PURE__*/ Predicate.or(/*#__PURE__*/ Predicate.or(Predicate.isNumber, Predicate.isString), Arr.isArray)
1200
+
878
1201
  /** Whether a Segment's value is one: a number, a string, `null` or Keyframes. */
879
- const isSegmentValue: Predicate.Predicate<unknown> = /*#__PURE__*/ Predicate.or(/*#__PURE__*/ Predicate.or(Predicate.isNumber, Predicate.isString), /*#__PURE__*/ Predicate.or(Predicate.isNull, Arr.isArray))
1202
+ const isSegmentValue: Predicate.Predicate<unknown> = /*#__PURE__*/ Predicate.or(isFunctionSegmentValue, Predicate.isNull)
880
1203
 
881
1204
  /** A Segment's values: those that are a value, `null` or Keyframes. */
882
1205
  const segmentValuesOf = (target: WardicSegmentTarget): Readonly<Record<string, Sequence.Stop | ReadonlyArray<Sequence.Stop>>> =>
@@ -927,9 +1250,12 @@ const stepsOf = <S extends object>(ports: Ports<S>, sequence: ReadonlyArray<unkn
927
1250
  }
928
1251
  if (Predicate.isFunction(parts[0])) {
929
1252
  const call = parts[0] as (value: number | string) => void
930
- const three = parts.length === 3
931
- const given = Bool.match(three, { onTrue: () => parts[1] as Sequence.Stop | ReadonlyArray<Sequence.Stop>, onFalse: () => ZERO_TO_ONE })
932
- const options = Bool.match(three, { onTrue: () => parts[2], onFalse: () => parts[1] }) as WardicSegmentOptions | undefined
1253
+ // #975 (ADR-0061, *Amendment (#975)*): two parts are a value and no options when the second is one,
1254
+ // as Motion's `[value, keyframes]`; read as options, a number was dropped, and an array's `at` threw.
1255
+ const [given, options] = Bool.match(Bool.or(parts.length === 3, isFunctionSegmentValue(parts[1])), {
1256
+ onTrue: () => [parts[1] as Sequence.Stop | ReadonlyArray<Sequence.Stop>, parts[2] as WardicSegmentOptions | undefined] as const,
1257
+ onFalse: () => [ZERO_TO_ONE, parts[1] as WardicSegmentOptions | undefined] as const,
1258
+ })
933
1259
  const target = { [VALUE]: given }
934
1260
  return { subjects: [{ call }], target, transitions: segmentTransitionsOf(target, options, defaults), at: atOf(options) }
935
1261
  }
@@ -956,9 +1282,11 @@ export const segmentAnimates = <S extends object>(ports: Ports<S>, segment: unkn
956
1282
  /**
957
1283
  * A function Segment's value animated (#433): its stops, a first `null` 0, through the
958
1284
  * Interpolators on its tween over the sequence, each frame told to its callback. Cancelled, the
959
- * callback hears its first stop again, as Motion's motion value is set back.
1285
+ * callback hears its first stop again, as Motion's motion value is set back. A callback that throws is
1286
+ * reported, once a run of throws, and told the next frame's value all the same; one that throws on a
1287
+ * cancel, each time, the call's cancel going on.
960
1288
  */
961
- const callbackTrackOf = (env: AnimateEnv, callback: Callback, animation: Sequence.Animation, head: Playback.Playhead, settled: () => void): Spine.ImperativeTrack => {
1289
+ const callbackTrackOf = (env: AnimateEnv, callback: Callback, animation: Sequence.Animation, head: Playback.Playhead, settled: (landed: boolean) => void): Spine.ImperativeTrack => {
962
1290
  const stops = RetargetLegs.filledStops(UndefinedOr.getOrThrow(animation.keyframes[VALUE]), 0)
963
1291
  const options = UndefinedOr.getOrThrow(animation.transition[VALUE])
964
1292
  // Skipped (#468): its callback told the value it would end on, on the first tick.
@@ -967,6 +1295,8 @@ const callbackTrackOf = (env: AnimateEnv, callback: Callback, animation: Sequenc
967
1295
  onFalse: () => Machine.keyframesValues({ [VALUE]: stops }, options),
968
1296
  })
969
1297
  let fiber: Fiber.Fiber<unknown> | undefined
1298
+ // Its callback threw, reported, and hasn't returned since (#979, row 18).
1299
+ let failing = false
970
1300
  const end = (): void => {
971
1301
  if (Predicate.isUndefined(fiber)) return
972
1302
  const running = fiber
@@ -976,16 +1306,28 @@ const callbackTrackOf = (env: AnimateEnv, callback: Callback, animation: Sequenc
976
1306
  }
977
1307
  const landed = (exit: Exit.Exit<unknown>): Effect.Effect<void> =>
978
1308
  Exit.match(exit, {
979
- onSuccess: () => Effect.sync(settled),
980
- onFailure: (cause) => Bool.match(Cause.hasInterruptsOnly(cause), { onTrue: () => Effect.void, onFalse: () => Report.failure("wardic.animate: a function Segment failed", cause) }),
1309
+ onSuccess: () => Effect.sync(() => settled(true)),
1310
+ onFailure: (cause) => Bool.match(Cause.hasInterruptsOnly(cause), { onTrue: () => Effect.void, onFalse: () => Report.failure(SEGMENT_FAILED, cause) }),
981
1311
  })
982
1312
  const run = (): void => {
983
1313
  end()
1314
+ // A run of its own: a throw in it is reported, whatever the last run's last call did.
1315
+ failing = false
984
1316
  fiber = env.runtime.runFork(
985
1317
  machine.pipe(
986
1318
  Effect.map(Playback.drive(head)),
987
1319
  Effect.flatMap(Machine.run((frame) => {
988
- callback.call(UndefinedOr.getOrThrow(frame.value[VALUE]))
1320
+ const value = UndefinedOr.getOrThrow(frame.value[VALUE])
1321
+ // Its callback's throw is its own: told again next frame, its Segment landing as its stops do (ADR-0074).
1322
+ try {
1323
+ callback.call(value)
1324
+ if (failing) failing = false
1325
+ } catch (defect) {
1326
+ if (!failing) {
1327
+ failing = true
1328
+ env.runtime.runFork(Report.failure(SEGMENT_FAILED, Cause.die(defect)))
1329
+ }
1330
+ }
989
1331
  return undefined
990
1332
  })),
991
1333
  Effect.onExit(landed),
@@ -997,7 +1339,13 @@ const callbackTrackOf = (env: AnimateEnv, callback: Callback, animation: Sequenc
997
1339
  stop: end,
998
1340
  cancel: () => {
999
1341
  end()
1000
- callback.call(UndefinedOr.getOrThrow(stops[0]))
1342
+ const first = UndefinedOr.getOrThrow(stops[0])
1343
+ // Told its first stop once its run has ended: a throw there is reported, and the call's cancel goes on (#979, row 19).
1344
+ try {
1345
+ callback.call(first)
1346
+ } catch (defect) {
1347
+ env.runtime.runFork(Report.failure("wardic.animate: a function Segment's cancel failed", Cause.die(defect)))
1348
+ }
1001
1349
  },
1002
1350
  duration: () => Machine.durationOf(env.runtime.runSync(machine)),
1003
1351
  }
@@ -1018,14 +1366,24 @@ const sequenceOf = <S extends object>(env: AnimateEnv, ports: Ports<S>, scope: u
1018
1366
  const compiled = Arr.fromIterable(Sequence.compile(stepsOf(ports, sequence, scope, defaults), whole))
1019
1367
  // Each Batch's members as one track, their other keys their Subjects' (#665, Q40).
1020
1368
  const parts = ports.batches.sequencePartsOf<S | Callback>(compiled)
1021
- return started(watch, compiled.length, UndefinedOr.map(options, (given) => given.onComplete), (head, settled) => [
1022
- ...Arr.map(parts.others, ([subject, animation]) => {
1023
- if (isCallback(subject)) return callbackTrackOf(env, subject, animation, head, settled)
1024
- return trackOf(env, ports.subjects, subject as S, animation.keyframes as Config.WardicVariant, animation.transition as Config.WardicTransition, head, settled)
1025
- }),
1026
- ...Arr.map(parts.batches, (part) =>
1027
- part.binding.sequenceTrack({ members: part.members, animations: part.animations, reducedMotion: env.reducedMotion(), skipAnimations: env.skipAnimations(), head, settled })),
1028
- ])
1369
+ return started(env.runtime, watch, compiled.length, UndefinedOr.map(options, (given) => given.onComplete), (head, settled) => {
1370
+ // Every Segment's Subject readied before any track takes a key, a later one's refusal taking nothing (#973).
1371
+ const readied = Arr.map(parts.others, ([subject, animation]) => segmentReadiedOf(env, ports.subjects, subject, animation))
1372
+ return [
1373
+ ...Arr.map(parts.others, ([subject, animation], i) => {
1374
+ if (isCallback(subject)) return callbackTrackOf(env, subject, animation, head, settled)
1375
+ return trackOf(env, UndefinedOr.getOrThrow(readied[i]), animation.transition as Config.WardicTransition, head, settled)
1376
+ }),
1377
+ ...Arr.map(parts.batches, (part) =>
1378
+ part.binding.sequenceTrack({ members: part.members, animations: part.animations, reducedMotion: env.reducedMotion(), skipAnimations: env.skipAnimations(), head, settled: () => settled(true), taken: () => settled(false) })),
1379
+ ]
1380
+ })
1381
+ }
1382
+
1383
+ /** A sequence's Subject readied (`readiedOf`) for its compiled animation: none for a function Segment's callback, which has no keys. */
1384
+ const segmentReadiedOf = <S extends object>(env: AnimateEnv, subjects: Subjects<S>, subject: S | Callback, animation: Sequence.Animation): Readied<S> | undefined => {
1385
+ if (isCallback(subject)) return undefined
1386
+ return readiedOf(env, subjects, subject as S, animation.keyframes as Config.WardicVariant)
1029
1387
  }
1030
1388
 
1031
1389
  /** `watch`, with `also` told each start and end after it, when given: a scope's own watch beside its owner's (#433), a binding's beside a scope's (#798). */