@wardic/core 1.3.1 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/dist/BaseValue.d.ts +1 -1
  2. package/dist/BaseValue.js +2 -2
  3. package/dist/BaseValue.js.map +1 -1
  4. package/dist/Batch.d.ts +5 -9
  5. package/dist/Batch.d.ts.map +1 -1
  6. package/dist/Batch.js +166 -20
  7. package/dist/Batch.js.map +1 -1
  8. package/dist/Clock.d.ts +16 -0
  9. package/dist/Clock.d.ts.map +1 -1
  10. package/dist/Clock.js +13 -0
  11. package/dist/Clock.js.map +1 -1
  12. package/dist/DragRecognizer.d.ts +10 -5
  13. package/dist/DragRecognizer.d.ts.map +1 -1
  14. package/dist/DragRecognizer.js +60 -24
  15. package/dist/DragRecognizer.js.map +1 -1
  16. package/dist/Frame.js +1 -1
  17. package/dist/Frame.js.map +1 -1
  18. package/dist/InputFeed.d.ts +10 -7
  19. package/dist/InputFeed.d.ts.map +1 -1
  20. package/dist/InputFeed.js +16 -35
  21. package/dist/InputFeed.js.map +1 -1
  22. package/dist/LiveValue.d.ts +9 -3
  23. package/dist/LiveValue.d.ts.map +1 -1
  24. package/dist/LiveValue.js +160 -14
  25. package/dist/LiveValue.js.map +1 -1
  26. package/dist/Machine.d.ts +16 -6
  27. package/dist/Machine.d.ts.map +1 -1
  28. package/dist/Machine.js +50 -27
  29. package/dist/Machine.js.map +1 -1
  30. package/dist/Playback.d.ts.map +1 -1
  31. package/dist/Playback.js +10 -8
  32. package/dist/Playback.js.map +1 -1
  33. package/dist/Recognizer.d.ts +1 -1
  34. package/dist/Recognizer.d.ts.map +1 -1
  35. package/dist/Recognizer.js +9 -4
  36. package/dist/Recognizer.js.map +1 -1
  37. package/dist/internal/frameLoop.d.ts +10 -2
  38. package/dist/internal/frameLoop.d.ts.map +1 -1
  39. package/dist/internal/frameLoop.js +148 -18
  40. package/dist/internal/frameLoop.js.map +1 -1
  41. package/dist/internal/guarded.d.ts +16 -0
  42. package/dist/internal/guarded.d.ts.map +1 -0
  43. package/dist/internal/guarded.js +68 -0
  44. package/dist/internal/guarded.js.map +1 -0
  45. package/dist/internal/stepper.d.ts +9 -3
  46. package/dist/internal/stepper.d.ts.map +1 -1
  47. package/dist/internal/stepper.js +5 -4
  48. package/dist/internal/stepper.js.map +1 -1
  49. package/dist/unstable/AnimationControls.d.ts +15 -8
  50. package/dist/unstable/AnimationControls.d.ts.map +1 -1
  51. package/dist/unstable/AnimationControls.js +36 -15
  52. package/dist/unstable/AnimationControls.js.map +1 -1
  53. package/dist/unstable/AppearHandoff.d.ts +6 -0
  54. package/dist/unstable/AppearHandoff.d.ts.map +1 -1
  55. package/dist/unstable/AppearHandoff.js.map +1 -1
  56. package/dist/unstable/Config.d.ts +5 -1
  57. package/dist/unstable/Config.d.ts.map +1 -1
  58. package/dist/unstable/Config.js.map +1 -1
  59. package/dist/unstable/FlushGate.d.ts +4 -2
  60. package/dist/unstable/FlushGate.d.ts.map +1 -1
  61. package/dist/unstable/FlushGate.js +155 -38
  62. package/dist/unstable/FlushGate.js.map +1 -1
  63. package/dist/unstable/FrameEffects.d.ts +4 -1
  64. package/dist/unstable/FrameEffects.d.ts.map +1 -1
  65. package/dist/unstable/FrameEffects.js +12 -1
  66. package/dist/unstable/FrameEffects.js.map +1 -1
  67. package/dist/unstable/Imperative.d.ts +13 -4
  68. package/dist/unstable/Imperative.d.ts.map +1 -1
  69. package/dist/unstable/Imperative.js +202 -43
  70. package/dist/unstable/Imperative.js.map +1 -1
  71. package/dist/unstable/MeasuredPairs.d.ts +6 -2
  72. package/dist/unstable/MeasuredPairs.d.ts.map +1 -1
  73. package/dist/unstable/MeasuredPairs.js.map +1 -1
  74. package/dist/unstable/PresenceRecord.d.ts +4 -0
  75. package/dist/unstable/PresenceRecord.d.ts.map +1 -1
  76. package/dist/unstable/PresenceRecord.js.map +1 -1
  77. package/dist/unstable/Report.d.ts +7 -0
  78. package/dist/unstable/Report.d.ts.map +1 -1
  79. package/dist/unstable/Report.js +17 -1
  80. package/dist/unstable/Report.js.map +1 -1
  81. package/dist/unstable/RetargetLegs.d.ts +6 -0
  82. package/dist/unstable/RetargetLegs.d.ts.map +1 -1
  83. package/dist/unstable/RetargetLegs.js +15 -0
  84. package/dist/unstable/RetargetLegs.js.map +1 -1
  85. package/dist/unstable/Spine.d.ts +53 -7
  86. package/dist/unstable/Spine.d.ts.map +1 -1
  87. package/dist/unstable/Spine.js +190 -40
  88. package/dist/unstable/Spine.js.map +1 -1
  89. package/dist/unstable/VariantTarget.d.ts +25 -3
  90. package/dist/unstable/VariantTarget.d.ts.map +1 -1
  91. package/dist/unstable/VariantTarget.js +66 -13
  92. package/dist/unstable/VariantTarget.js.map +1 -1
  93. package/dist/unstable/VariantTree.d.ts +37 -3
  94. package/dist/unstable/VariantTree.d.ts.map +1 -1
  95. package/dist/unstable/VariantTree.js +54 -7
  96. package/dist/unstable/VariantTree.js.map +1 -1
  97. package/package.json +1 -1
  98. package/src/BaseValue.ts +2 -2
  99. package/src/Batch.ts +167 -17
  100. package/src/Clock.ts +20 -0
  101. package/src/DragRecognizer.ts +82 -30
  102. package/src/Frame.ts +1 -1
  103. package/src/InputFeed.ts +37 -40
  104. package/src/LiveValue.ts +140 -14
  105. package/src/Machine.ts +67 -34
  106. package/src/Playback.ts +10 -8
  107. package/src/Recognizer.ts +9 -3
  108. package/src/internal/frameLoop.ts +165 -19
  109. package/src/internal/guarded.ts +69 -0
  110. package/src/internal/stepper.ts +14 -5
  111. package/src/unstable/AnimationControls.ts +49 -21
  112. package/src/unstable/AppearHandoff.ts +7 -0
  113. package/src/unstable/Config.ts +7 -1
  114. package/src/unstable/FlushGate.ts +169 -37
  115. package/src/unstable/FrameEffects.ts +15 -2
  116. package/src/unstable/Imperative.ts +219 -48
  117. package/src/unstable/MeasuredPairs.ts +7 -1
  118. package/src/unstable/PresenceRecord.ts +4 -0
  119. package/src/unstable/Report.ts +19 -1
  120. package/src/unstable/RetargetLegs.ts +14 -0
  121. package/src/unstable/Spine.ts +235 -40
  122. package/src/unstable/VariantTarget.ts +93 -14
  123. package/src/unstable/VariantTree.ts +87 -6
@@ -53,6 +53,7 @@ import * as Effect from "effect/Effect"
53
53
  import * as Exit from "effect/Exit"
54
54
  import type * as Fiber from "effect/Fiber"
55
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"
@@ -254,7 +255,7 @@ const plainOf = <S extends object>(env: AnimateEnv, subjects: Subjects<S>, subje
254
255
  ownValues: env.ownValues,
255
256
  measuredPairs: env.measuredPairs,
256
257
  variableValues: env.variableValues,
257
- start: { initialValues: {}, initialLink: {}, startedLink: {}, quiet: false },
258
+ start: { initialValues: {}, initialLink: {}, startedLink: {}, quiet: false, failure: Option.none() },
258
259
  snapshot,
259
260
  reuse: Spine.makeReuse(),
260
261
  links: subjects.startLinksOf(subject),
@@ -305,8 +306,12 @@ const lastTargetsOf = (host: Spine.SpineHost): Values.Values => UndefinedOr.matc
305
306
  /**
306
307
  * `key` taken by an Imperative animation from whatever runs it (#433): noted in `taking` with `noted`,
307
308
  * the target the Override stack has for it now, no longer returning, its Timeline value stopped, given
308
- * up by its owner, owned, and started where it's drawn toward `like`, noted in `started`.
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.
309
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.
310
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 => {
311
316
  taking.set(key, noted)
312
317
  host.returning().delete(key)
@@ -315,10 +320,18 @@ const takeKey = (host: Spine.SpineHost, taking: Map<string, number | string | un
315
320
  if (Predicate.isNotUndefined(timelineKeys)) timelineKeys.taken(key)
316
321
  const owner = host.owners.get(key)
317
322
  if (Predicate.isNotUndefined(owner)) host.giveUp(key, owner)
318
- if (!Object.hasOwn(host.initialValues, key)) host.ownValue(key)
319
323
  host.startKey(key, like, started)
320
324
  }
321
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
+
322
335
  /** Whether a Transition end may set a key drawn by `owner`: none draws it, or its leg is done. */
323
336
  const endFree = (owner: Spine.Running | undefined): boolean => {
324
337
  if (Predicate.isUndefined(owner)) return true
@@ -332,7 +345,9 @@ const endFree = (owner: Spine.Running | undefined): boolean => {
332
345
  * Its legs start where the keys are drawn, with their velocity, on `given` (a key with none of its own
333
346
  * on its Default transition), reduced motion as the provider says, and run through `head` once
334
347
  * {@link Spine.ImperativeTrack.run} is called. Once they land, none taken, its target's Transition
335
- * end is set (#896), and put back by a cancel or a run again.
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).
336
351
  */
337
352
  // #746 (ADR-0067, *Amendment (#746)*, decision 10): here, not in the spine, so an app that never animates
338
353
  // imperatively ships none of it; the spine hands over what it takes (`SpineHost`).
@@ -348,6 +363,7 @@ const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target,
348
363
  })))
349
364
  }
350
365
  const to = target.values
366
+ const ends = target.transitionEnd
351
367
  const before = lastTargetsOf(host)
352
368
  const started: Record<string, string> = {}
353
369
  let measuring: Array<string> | undefined
@@ -375,14 +391,13 @@ const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target,
375
391
  const reduce = host.reducedMotion()
376
392
  const skipped = host.skipAnimations()
377
393
  let measured: RetargetLegs.MeasuredPairs | undefined
378
- 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)
379
395
  // Built once: a seek or a replay runs the same legs, from the same start, with the same velocities.
380
396
  const built = RetargetLegs.legsOf(from, to, given, reduce, skipped, positionalKeys, defaultTransition, readVelocity, undefined, stops, measured, snapshot.values, rests, unitScale)
381
397
  const variables = Spine.holdsVariable(variableValues, from, to, stops)
382
398
  const legs = Arr.map(built, (leg) => RetargetLegs.mapMachine(leg, Playback.drive(head)))
383
399
  let runnings: ReadonlyArray<Spine.Running> = Spine.NO_RUNNING
384
400
  let left = 0
385
- const ends = target.transitionEnd
386
401
  // What its Transition end's keys were before it set them (Q5): `undefined` until it has.
387
402
  let overwritten: Values.Values | undefined
388
403
  // Its runs, stops and cancels, counted: what a landing waiting on a flush checks it's still for.
@@ -412,8 +427,20 @@ const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target,
412
427
  if (only(key)) takeKey(host, takingNow, key, targetsNow[key], UndefinedOr.getOrThrow(values[key]), startedNow)
413
428
  })
414
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
+ }
415
442
  const writeEnds = (values: Values.Values): void => {
416
- const free = Rec.filter(values, (_, key) => endFree(owners.get(key)))
443
+ const free = Rec.filter(values, (_, key) => endFree(owners.get(key)) && settableOrReported(key))
417
444
  if (Rec.isEmptyRecord(free)) return
418
445
  takeEach(free, constTrue)
419
446
  // Kept from an earlier landing a run from its end didn't put back: what they were before any.
@@ -565,17 +592,57 @@ const overOwn = (own: Config.WardicVariantTransition | undefined, given: Config.
565
592
  return { ...own, ...given } as Config.WardicTransition
566
593
  }
567
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
+
568
604
  /**
569
- * A Subject's animation of `target`: through its spine when it's a Wardic element's, else through its
570
- * plain spine, in the gate while any of its animations runs.
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.
571
609
  */
572
- const trackOf = <S extends object>(env: AnimateEnv, subjects: Subjects<S>, subject: S, target: Config.WardicVariant, given: Config.WardicTransition | undefined, head: Playback.Playhead, settled: (landed: boolean) => void): Spine.ImperativeTrack => {
573
- const resolved = VariantTarget.resolveTarget(target, undefined, undefined, VariantTarget.NO_VARIANT_STATE)
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
574
617
  const own = wardicElements.get(subject)
575
- const hooks = { settled, failed: constVoid }
576
- const transition = skippedIn(env, overOwn(resolved.transition, given))
577
- 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 })
578
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)
579
646
  // In the gate while it runs; out once its last animation is done, after the flush of its last frame.
580
647
  let live = false
581
648
  const enter = (): void => {
@@ -596,7 +663,7 @@ const trackOf = <S extends object>(env: AnimateEnv, subjects: Subjects<S>, subje
596
663
  if (plain.running === 0) plain.unregister()
597
664
  })
598
665
  }
599
- const track = imperativeTrackOf(plain.spine.host(), resolved, transition, head, {
666
+ const track = imperativeTrackOf(host, target, transition, head, {
600
667
  settled: (landed) => {
601
668
  leave()
602
669
  settled(landed)
@@ -640,8 +707,9 @@ interface EachDelay {
640
707
  /**
641
708
  * `animate`'s options: a transition, laid field by field over the target's own, its `delay` a number,
642
709
  * or a function of each Subject's index and their count, such as `stagger(0.05)`; and `onComplete`,
643
- * called as it lands, each Subject's `transitionEnd` already set. Motion's, though Motion's `animate`
644
- * runs on the options alone.
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.
645
713
  */
646
714
  export type WardicAnimateOptions = AnimateOptionsOver<Config.WardicTransition>
647
715
 
@@ -734,8 +802,14 @@ export interface AnimationWatch {
734
802
  readonly ended: (controls: WardicPlaybackControls, landed: boolean) => void
735
803
  }
736
804
 
737
- /** Playback controls over `tracks`, all run through `head`, started now. */
738
- 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
+ ): {
739
813
  readonly controls: WardicPlaybackControls
740
814
  readonly settledOne: (landed: boolean) => void
741
815
  } => {
@@ -864,8 +938,16 @@ const controlsOf = (tracks: ReadonlyArray<Spine.ImperativeTrack>, head: Playback
864
938
  }
865
939
  isResolved = true
866
940
  UndefinedOr.map(resolve, (done) => done())
867
- 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)*).
868
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
+ })
869
951
  }
870
952
  return { controls, settledOne }
871
953
  }
@@ -913,13 +995,14 @@ export type WardicSegmentTarget = Omit<Config.WardicTargetValues, "transition">
913
995
  /**
914
996
  * A function Segment, `[callback, value?, options?]`: its callback is given a value each frame, from 0
915
997
  * to 1 unless it gives its own stops, for what isn't a Subject's own value. Motion's, in place of its
916
- * 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.
917
1000
  */
918
1001
  export type WardicFunctionSegment =
919
1002
  | readonly [callback: (value: number) => void]
920
1003
  | readonly [callback: (value: number) => void, options: WardicSegmentOptions]
921
- | readonly [callback: (value: number) => void, value: number | Config.WardicKeyframes<number>, options: WardicSegmentOptions]
922
- | 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]
923
1006
 
924
1007
  /**
925
1008
  * A sequence's options: `duration`, the whole sequence scaled to it; `delay`; `repeat`, `repeatType`
@@ -941,6 +1024,7 @@ export interface WardicSequenceOptions {
941
1024
  * to `watch`, each track run. Finished at once with none.
942
1025
  */
943
1026
  const started = (
1027
+ runtime: AnimateEnv["runtime"],
944
1028
  watch: AnimationWatch,
945
1029
  count: number,
946
1030
  onComplete: (() => void) | undefined,
@@ -950,13 +1034,39 @@ const started = (
950
1034
  const head = Playback.make()
951
1035
  let settledOne: (landed: boolean) => void = constVoid
952
1036
  const tracks = tracksOf(head, (landedOne) => settledOne(landedOne))
953
- const made = controlsOf(tracks, head, watch, onComplete)
1037
+ const made = controlsOf(tracks, head, watch, onComplete, runtime)
954
1038
  settledOne = made.settledOne
955
1039
  watch.started(made.controls)
956
- 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
+ }
957
1048
  return made.controls
958
1049
  }
959
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
+
960
1070
  /**
961
1071
  * An **Animation scope**'s `animate` (#433), over an Environment's {@link Ports}: `animate(subject,
962
1072
  * target, options?)`, a Batch's `animate(batch, target, options?)` and `animate(sequence, options?)`,
@@ -964,6 +1074,13 @@ const started = (
964
1074
  * (the DOM's selectors are looked up inside it); each animation is told to `watch` as it starts and
965
1075
  * ends. Every Subject's legs start in the call, on the loop by the end of its task. An Environment
966
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.
967
1084
  */
968
1085
  export const make = <S extends object>(env: AnimateEnv, ports: Ports<S>, scope: () => unknown, watch: AnimationWatch) =>
969
1086
  (first: unknown, second?: unknown, third?: WardicAnimateOptions): WardicPlaybackControls => {
@@ -972,7 +1089,7 @@ export const make = <S extends object>(env: AnimateEnv, ports: Ports<S>, scope:
972
1089
  const batch = ports.batches.entryOf(first)
973
1090
  if (Predicate.isNotUndefined(batch)) {
974
1091
  const members = batch.binding.namedOf(batch)
975
- 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) => [
976
1093
  batchTrackOf(env, batch.binding, second as Readonly<Record<string, unknown>>, members, third, head, settled),
977
1094
  ])
978
1095
  }
@@ -982,15 +1099,28 @@ export const make = <S extends object>(env: AnimateEnv, ports: Ports<S>, scope:
982
1099
  // No member's Subject among them: each Subject's track, building no parts (bench:solid --batch's
983
1100
  // per-element page, a call per element: 8.2 -> 8.5 ms at 1,000 with the parts built for every call; #665).
984
1101
  if (Bool.not(ports.batches.hasMembers(subjects))) {
985
- return started(watch, subjects.length, onComplete, (head, settled) =>
986
- 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
+ })
987
1110
  }
988
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).
989
1112
  const parts = ports.batches.callPartsOf(subjects, target)
990
- return started(watch, parts.members.length + parts.subjects.length, onComplete, (head, settled) => [
991
- ...Arr.map(parts.members, (part) => batchTrackOf(env, part.binding, part.target, part.members, third, head, settled)),
992
- ...Arr.map(parts.subjects, (part) => trackOf(env, ports.subjects, part.subject, part.target as Config.WardicVariant, transitionFor(third, part.index, subjects.length), head, settled)),
993
- ])
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
+ })
994
1124
  }
995
1125
 
996
1126
  // --- Sequences (#433's PR 2) ------------------------------------------------------------------
@@ -1016,6 +1146,9 @@ const ZERO_TO_ONE: ReadonlyArray<number> = [0, 1]
1016
1146
  /** No fields. */
1017
1147
  const NO_FIELDS: Readonly<Record<string, unknown>> = {}
1018
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
+
1019
1152
  /**
1020
1153
  * What a Segment's transition doesn't take from the sequence's `defaultTransition` (Motion's): its
1021
1154
  * delay, times and repeat, which are each Segment's own, and a value's own transitions.
@@ -1062,8 +1195,11 @@ const segmentTransitionOf = (options: WardicSegmentOptions | undefined, key: str
1062
1195
  return Bool.match(Predicate.isUndefined(delay), { onTrue: () => run, onFalse: () => ({ ...run, delay }) as Sequence.Transition })
1063
1196
  }
1064
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
+
1065
1201
  /** Whether a Segment's value is one: a number, a string, `null` or Keyframes. */
1066
- 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)
1067
1203
 
1068
1204
  /** A Segment's values: those that are a value, `null` or Keyframes. */
1069
1205
  const segmentValuesOf = (target: WardicSegmentTarget): Readonly<Record<string, Sequence.Stop | ReadonlyArray<Sequence.Stop>>> =>
@@ -1114,9 +1250,12 @@ const stepsOf = <S extends object>(ports: Ports<S>, sequence: ReadonlyArray<unkn
1114
1250
  }
1115
1251
  if (Predicate.isFunction(parts[0])) {
1116
1252
  const call = parts[0] as (value: number | string) => void
1117
- const three = parts.length === 3
1118
- const given = Bool.match(three, { onTrue: () => parts[1] as Sequence.Stop | ReadonlyArray<Sequence.Stop>, onFalse: () => ZERO_TO_ONE })
1119
- 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
+ })
1120
1259
  const target = { [VALUE]: given }
1121
1260
  return { subjects: [{ call }], target, transitions: segmentTransitionsOf(target, options, defaults), at: atOf(options) }
1122
1261
  }
@@ -1143,7 +1282,9 @@ export const segmentAnimates = <S extends object>(ports: Ports<S>, segment: unkn
1143
1282
  /**
1144
1283
  * A function Segment's value animated (#433): its stops, a first `null` 0, through the
1145
1284
  * Interpolators on its tween over the sequence, each frame told to its callback. Cancelled, the
1146
- * 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.
1147
1288
  */
1148
1289
  const callbackTrackOf = (env: AnimateEnv, callback: Callback, animation: Sequence.Animation, head: Playback.Playhead, settled: (landed: boolean) => void): Spine.ImperativeTrack => {
1149
1290
  const stops = RetargetLegs.filledStops(UndefinedOr.getOrThrow(animation.keyframes[VALUE]), 0)
@@ -1154,6 +1295,8 @@ const callbackTrackOf = (env: AnimateEnv, callback: Callback, animation: Sequenc
1154
1295
  onFalse: () => Machine.keyframesValues({ [VALUE]: stops }, options),
1155
1296
  })
1156
1297
  let fiber: Fiber.Fiber<unknown> | undefined
1298
+ // Its callback threw, reported, and hasn't returned since (#979, row 18).
1299
+ let failing = false
1157
1300
  const end = (): void => {
1158
1301
  if (Predicate.isUndefined(fiber)) return
1159
1302
  const running = fiber
@@ -1164,15 +1307,27 @@ const callbackTrackOf = (env: AnimateEnv, callback: Callback, animation: Sequenc
1164
1307
  const landed = (exit: Exit.Exit<unknown>): Effect.Effect<void> =>
1165
1308
  Exit.match(exit, {
1166
1309
  onSuccess: () => Effect.sync(() => settled(true)),
1167
- onFailure: (cause) => Bool.match(Cause.hasInterruptsOnly(cause), { onTrue: () => Effect.void, onFalse: () => Report.failure("wardic.animate: a function Segment failed", cause) }),
1310
+ onFailure: (cause) => Bool.match(Cause.hasInterruptsOnly(cause), { onTrue: () => Effect.void, onFalse: () => Report.failure(SEGMENT_FAILED, cause) }),
1168
1311
  })
1169
1312
  const run = (): void => {
1170
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
1171
1316
  fiber = env.runtime.runFork(
1172
1317
  machine.pipe(
1173
1318
  Effect.map(Playback.drive(head)),
1174
1319
  Effect.flatMap(Machine.run((frame) => {
1175
- 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
+ }
1176
1331
  return undefined
1177
1332
  })),
1178
1333
  Effect.onExit(landed),
@@ -1184,7 +1339,13 @@ const callbackTrackOf = (env: AnimateEnv, callback: Callback, animation: Sequenc
1184
1339
  stop: end,
1185
1340
  cancel: () => {
1186
1341
  end()
1187
- 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
+ }
1188
1349
  },
1189
1350
  duration: () => Machine.durationOf(env.runtime.runSync(machine)),
1190
1351
  }
@@ -1205,14 +1366,24 @@ const sequenceOf = <S extends object>(env: AnimateEnv, ports: Ports<S>, scope: u
1205
1366
  const compiled = Arr.fromIterable(Sequence.compile(stepsOf(ports, sequence, scope, defaults), whole))
1206
1367
  // Each Batch's members as one track, their other keys their Subjects' (#665, Q40).
1207
1368
  const parts = ports.batches.sequencePartsOf<S | Callback>(compiled)
1208
- return started(watch, compiled.length, UndefinedOr.map(options, (given) => given.onComplete), (head, settled) => [
1209
- ...Arr.map(parts.others, ([subject, animation]) => {
1210
- if (isCallback(subject)) return callbackTrackOf(env, subject, animation, head, settled)
1211
- return trackOf(env, ports.subjects, subject as S, animation.keyframes as Config.WardicVariant, animation.transition as Config.WardicTransition, head, settled)
1212
- }),
1213
- ...Arr.map(parts.batches, (part) =>
1214
- part.binding.sequenceTrack({ members: part.members, animations: part.animations, reducedMotion: env.reducedMotion(), skipAnimations: env.skipAnimations(), head, settled: () => settled(true), taken: () => settled(false) })),
1215
- ])
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)
1216
1387
  }
1217
1388
 
1218
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). */
@@ -44,11 +44,17 @@ export interface Measured extends Pair {
44
44
  export interface Shape {
45
45
  /** Whether `key` from `from` to `to` needs its pair measured: its ends in units that don't blend. */
46
46
  readonly needs: (key: string, from: number | string, to: number | string) => boolean
47
- /** Asks `pairs` measured on `subject`, at `values`: each key's pair, whose result it reads once it's done. */
47
+ /**
48
+ * Asks `pairs` measured on `subject`, at `values`: each key's pair, whose result it reads once it's
49
+ * done. One the measure can't take (its write throws) is never measured, and `failed` is told, its
50
+ * Subject's alone.
51
+ */
52
+ // #979, row 8 (ADR-0074): `failed`, the asker's, which reports through its own runtime.
48
53
  readonly measure: (
49
54
  subject: object,
50
55
  pairs: Readonly<Record<string, { readonly from: number | string; readonly to: number | string }>>,
51
56
  values: Values.Values,
57
+ failed: (defect: unknown) => void,
52
58
  ) => Readonly<Record<string, Requested>>
53
59
  }
54
60
 
@@ -15,6 +15,8 @@ import type * as Subject from "./Subject.ts"
15
15
  import type * as Stops from "./Stops.ts"
16
16
  import type * as Config from "./Config.ts"
17
17
  import { constFalse } from "effect/Function"
18
+ import type * as Option from "effect/Option"
19
+ import type * as VariantTarget from "./VariantTarget.ts"
18
20
  import * as Predicate from "effect/Predicate"
19
21
  import * as UndefinedOr from "effect/UndefinedOr"
20
22
 
@@ -92,6 +94,8 @@ export interface PresenceExit {
92
94
  readonly groups?: ReadonlyArray<ExitGroup> | undefined
93
95
  /** Its Keyframes (#429): each such key's stops, its value in `values` their last. Left out with none. */
94
96
  readonly keyframes?: Readonly<Record<string, Stops.Stops>> | undefined
97
+ /** Its exit's resolver that threw, and its variant's name (#979): it has none of that exit's values. None when it returned. */
98
+ readonly failure: Option.Option<VariantTarget.VariantFailure>
95
99
  }
96
100
 
97
101
  /** One name of a list's exit (#297): its keys, and the transition they and its orchestration run on. */
@@ -3,7 +3,8 @@
3
3
  * `ErrorReporter`s. Core's engine doesn't report: the Frame loop hands each run's failure to its
4
4
  * caller, and a Tier 2 caller may recover. A binding boundary that ends a fiber nobody observes calls
5
5
  * this, so a failure there reaches the console and an app's reporters instead of stopping its feature
6
- * with nothing said.
6
+ * with nothing said. So does what outlives one Subject's failure, so the others go on: the Flush gate's
7
+ * flush, a Batch's run, on the services it runs with.
7
8
  *
8
9
  * An app installs its reporters on the Environment it hands the provider,
9
10
  * `ErrorReporter.layer([…])` merged into `env`; Wardic ships none. With none installed, a report
@@ -21,11 +22,13 @@
21
22
  // reports from core once it's lifted there.
22
23
  import * as Bool from "effect/Boolean"
23
24
  import * as Cause from "effect/Cause"
25
+ import type * as Context from "effect/Context"
24
26
  import * as Effect from "effect/Effect"
25
27
  import * as ErrorReporter from "effect/ErrorReporter"
26
28
  import * as LogLevel from "effect/LogLevel"
27
29
  import * as Predicate from "effect/Predicate"
28
30
  import * as References from "effect/References"
31
+ import * as UndefinedOr from "effect/UndefinedOr"
29
32
 
30
33
  /**
31
34
  * `cause` logged at Error as `message`, with the cause, and handed to the Environment's
@@ -38,6 +41,21 @@ export const failure = (message: string, cause: Cause.Cause<unknown>): Effect.Ef
38
41
  onFalse: () => Effect.logError(message, cause).pipe(Effect.andThen(ErrorReporter.report(cause))),
39
42
  })
40
43
 
44
+ /**
45
+ * {@link failure}, run where no fiber of a binding's is there to see it: on `context`, what the failing
46
+ * code was built or started in, so an app's logger and reporters hear it, else on Effect's default
47
+ * runtime. Forked, off the path that failed, with `annotations` on its log when given.
48
+ */
49
+ // #979 (ADR-0030, *Amendment (#979, groups D and G)*): a Live value's run, a scroll's reader, a pointer's
50
+ // value and a watch with no onDefect, each captured its context once and calls this only at a failure.
51
+ export const failureOn = (context: Context.Context<never> | undefined, message: string, cause: Cause.Cause<unknown>, annotations?: Readonly<Record<string, unknown>>): void => {
52
+ const report = UndefinedOr.match(annotations, { onUndefined: () => failure(message, cause), onDefined: (given) => Effect.annotateLogs(failure(message, cause), given) })
53
+ UndefinedOr.match(context, {
54
+ onUndefined: () => void Effect.runFork(report),
55
+ onDefined: (given) => void Effect.runForkWith(given)(report),
56
+ })
57
+ }
58
+
41
59
  /**
42
60
  * Whether a Debug log would be kept, as Effect's `MinimumLogLevel` says. Run it once (a runtime, a
43
61
  * build) and keep the answer, not once a log.
@@ -249,6 +249,20 @@ export const transitionForKey = (given: Config.WardicTransition | undefined, key
249
249
  return forKey(entries, key)
250
250
  }
251
251
 
252
+ /**
253
+ * Whether `given` gives `key` a velocity of its own, a number other than 0, on the transition it runs on:
254
+ * it kicks the key, so the key animates though it's already at its target, out and back on a spring or
255
+ * gliding on an inertia, as Motion's `valueTransition.velocity` does.
256
+ */
257
+ // #448 (ADR-0005, *Amendment (#448)*): the spine's, here for a server's Appear rule to read too (#1018, ADR-0044).
258
+ export const givesVelocity = (given: Config.WardicTransition | undefined, key: string): boolean => {
259
+ // Per key, per Retarget (the spine's): statements.
260
+ const own = transitionForKey(given, key) as { readonly velocity?: unknown } | undefined
261
+ if (Predicate.isUndefined(own)) return false
262
+ if (Bool.not(Predicate.isNumber(own.velocity))) return false
263
+ return own.velocity !== 0
264
+ }
265
+
252
266
  /**
253
267
  * `given` held back by `extra` more seconds (ADR-0007's stagger, #290's exit delays): its own `delay`,
254
268
  * and each entry's (#301), so a key on its entry waits as the rest do. An `inherit` entry with no