@wardic/core 1.3.0 → 1.3.1

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.
@@ -206,9 +206,26 @@ export interface ChangeCall {
206
206
  readonly skipAnimations: boolean
207
207
  readonly head: Playback.Playhead
208
208
  readonly settled: () => void
209
+ /** It ended with a key a later change took, never landing (#951). */
210
+ readonly taken?: (() => void) | undefined
209
211
  readonly onMemberLanded?: ((slot: number) => void) | undefined
210
212
  }
211
213
 
214
+ /**
215
+ * A Batch change's options `O`, `Batch.animate`'s or `Batch.keyframes`', with `onTaken`: called once
216
+ * the change ends with a key a later change took while it moved it, which it then never lands.
217
+ */
218
+ // #951: Motion's never resolves then; the scope it was started in hears it end. Here, in an unstable
219
+ // module, not on Batch's own options: Batch reads it at a change's end (`checkLanding`'s TakenHook).
220
+ export type Taken<O> = O & { readonly onTaken?: (() => void) | undefined }
221
+
222
+ /** A sequence's call's options for `Batch.keyframes`: its Playhead, its landing, and its `onTaken`. */
223
+ export const keyframesOptions = (call: { readonly head: Playback.Playhead; readonly settled: () => void; readonly taken: () => void }): Taken<Batch.KeyframesOptions> => ({
224
+ head: call.head,
225
+ onLanded: call.settled,
226
+ onTaken: call.taken,
227
+ })
228
+
212
229
  /**
213
230
  * A driven change's options for core's `Batch.animate`, toward `targetOf`, from `fromOf`'s first stops
214
231
  * when it gives them, on its call's transitions: each key's own, else its Default transition toward the
@@ -220,7 +237,7 @@ export const animateOptions = (
220
237
  call: ChangeCall,
221
238
  targetOf: (slot: number, k: number) => number,
222
239
  fromOf: ((slot: number, k: number) => number) | undefined,
223
- ): Batch.AnimateOptions => {
240
+ ): Taken<Batch.AnimateOptions> => {
224
241
  const transition = Bool.match(call.skipAnimations, {
225
242
  onTrue: () => (): Batch.Transition => INSTANT,
226
243
  onFalse: () => {
@@ -232,8 +249,9 @@ export const animateOptions = (
232
249
  }
233
250
  },
234
251
  })
235
- const options: { -readonly [K in keyof Batch.AnimateOptions]: Batch.AnimateOptions[K] } = { head: call.head, transition, onLanded: call.settled }
252
+ const options: { -readonly [K in keyof Taken<Batch.AnimateOptions>]: Taken<Batch.AnimateOptions>[K] } = { head: call.head, transition, onLanded: call.settled }
236
253
  if (Predicate.isNotUndefined(fromOf)) options.from = fromOf
254
+ if (Predicate.isNotUndefined(call.taken)) options.onTaken = call.taken
237
255
  if (Predicate.isNotUndefined(call.onMemberLanded)) options.onMemberLanded = call.onMemberLanded
238
256
  const delay = Bool.match(call.skipAnimations, { onTrue: () => undefined, onFalse: () => staggerOf(call.transition) })
239
257
  if (Predicate.isNotUndefined(delay)) options.delay = delay
@@ -208,6 +208,23 @@ export type WardicTransitionEntries = WardicValueEntries<WardicValueKey> & { rea
208
208
  /** Entries for the values `K`, and `default` for the rest (#301). */
209
209
  export type WardicValueEntries<K extends string> = { readonly default?: WardicValueTransition } & { readonly [P in K]?: WardicValueTransition }
210
210
 
211
+ /** Each member's keys, as one union: a union's own `keyof` is only the keys its members share. */
212
+ type KeysOfEach<T> = T extends unknown ? keyof T : never
213
+
214
+ /** A transition's own fields' names, on any arm, and its `layout` entry's. */
215
+ type TransitionFieldName = KeysOfEach<WardicTransitionWith<unknown>> | "layout"
216
+
217
+ /**
218
+ * A transition for a Subject whose keys are `K`: an entry under each of `K`, and `default`; no
219
+ * `layout`. A key named like one of the transition's own fields (`duration`, `type`, `delay`…) has
220
+ * no entry, since that name is the field's: it runs on `default` or the transition's own fields.
221
+ */
222
+ // #952: an Environment whose keys depend on its Subject (`@wardic/object`'s, `@wardic/three`'s) types
223
+ // its transitions so; one whose keys are global, `ValueKeys`' (`@wardic/dom`'s), `WardicTransition`.
224
+ // A name holds the field or the entry, never both, and the runtime reads any object under it as an
225
+ // entry (RetargetLegs.entriesOf): a field's name is left out of the entries, so it stays the field's.
226
+ export type WardicTransitionFor<K extends string> = WardicTransitionWith<WardicValueEntries<Exclude<K, TransitionFieldName>>>
227
+
211
228
  /**
212
229
  * A variant's own transition, or a target's: it runs the animation that variant starts, in place of
213
230
  * the element's `transition`, unless `inherit: true` lays it over the element's, its own fields winning.
@@ -52,7 +52,7 @@ 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
56
  import * as Predicate from "effect/Predicate"
57
57
  import * as Rec from "effect/Record"
58
58
  import * as UndefinedOr from "effect/UndefinedOr"
@@ -142,6 +142,8 @@ export interface BatchCall {
142
142
  readonly skipAnimations: boolean
143
143
  readonly head: Playback.Playhead
144
144
  readonly settled: () => void
145
+ /** It ended with a key a later change took, never landing (#951). */
146
+ readonly taken: () => void
145
147
  /** The members it moves, in order: every member when left out. */
146
148
  readonly members: ReadonlyArray<number> | undefined
147
149
  }
@@ -154,6 +156,8 @@ export interface BatchSequenceCall {
154
156
  readonly skipAnimations: boolean
155
157
  readonly head: Playback.Playhead
156
158
  readonly settled: () => void
159
+ /** It ended with a key a later change took, never landing (#951). */
160
+ readonly taken: () => void
157
161
  }
158
162
 
159
163
  /** An `animate` call's parts for Subjects that are Batch members' (#665): each Batch's members, and each Subject's own keys. */
@@ -295,25 +299,56 @@ const IMPERATIVE: Spine.Resolution = {
295
299
  change: undefined,
296
300
  }
297
301
 
302
+ /** The target the Override stack had for each key at the spine's last Retarget: what a taken key is noted with. */
303
+ const lastTargetsOf = (host: Spine.SpineHost): Values.Values => UndefinedOr.match(host.lastResolution(), { onUndefined: () => Spine.NO_VALUES, onDefined: (last) => last.resolved })
304
+
305
+ /**
306
+ * `key` taken by an Imperative animation from whatever runs it (#433): noted in `taking` with `noted`,
307
+ * 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
+ */
310
+ 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
+ taking.set(key, noted)
312
+ host.returning().delete(key)
313
+ // Its Timeline value stops while the animation draws the key (#240).
314
+ const timelineKeys = host.timelineKeys()
315
+ if (Predicate.isNotUndefined(timelineKeys)) timelineKeys.taken(key)
316
+ const owner = host.owners.get(key)
317
+ if (Predicate.isNotUndefined(owner)) host.giveUp(key, owner)
318
+ if (!Object.hasOwn(host.initialValues, key)) host.ownValue(key)
319
+ host.startKey(key, like, started)
320
+ }
321
+
322
+ /** Whether a Transition end may set a key drawn by `owner`: none draws it, or its leg is done. */
323
+ const endFree = (owner: Spine.Running | undefined): boolean => {
324
+ if (Predicate.isUndefined(owner)) return true
325
+ return owner.done
326
+ }
327
+
298
328
  /**
299
329
  * An **Imperative animation** of a Subject (#433), through its spine's `host`: it takes `target`'s
300
330
  * keys from whatever runs them, each noted with the target the Override stack has for it now, so a
301
331
  * Retarget leaves it where the animation leaves it until that target changes (Motion's latest-wins).
302
332
  * Its legs start where the keys are drawn, with their velocity, on `given` (a key with none of its own
303
333
  * on its Default transition), reduced motion as the provider says, and run through `head` once
304
- * {@link Spine.ImperativeTrack.run} is called.
334
+ * {@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.
305
336
  */
306
337
  // #746 (ADR-0067, *Amendment (#746)*, decision 10): here, not in the spine, so an app that never animates
307
338
  // imperatively ships none of it; the spine hands over what it takes (`SpineHost`).
308
339
  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
340
+ 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
341
  /** A leg of an Imperative animation, forked as a Retarget's are; it lands on its track. */
311
342
  // Data-first: a pipe's curried forms are two closures more for every leg (#618, H1).
312
343
  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))))
344
+ const at = runs
345
+ running.fiber = runtime.runFork(Effect.asVoid(Effect.onExit(program, (exit) => {
346
+ if (at === runs) supersededAt(running)
347
+ return legEnded(running)(exit)
348
+ })))
314
349
  }
315
350
  const to = target.values
316
- const before = UndefinedOr.match(host.lastResolution(), { onUndefined: () => Spine.NO_VALUES, onDefined: (last) => last.resolved })
351
+ const before = lastTargetsOf(host)
317
352
  const started: Record<string, string> = {}
318
353
  let measuring: Array<string> | undefined
319
354
  // Each key taken from whatever runs it, noted with its target now, and started where it's drawn.
@@ -322,16 +357,8 @@ const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target,
322
357
  // Where a Path's axis this target leaves out rests (#574), before its keys are taken below.
323
358
  const rests = Spine.restsOf(owners, to)
324
359
  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
360
  const like = UndefinedOr.getOrThrow(to[key])
334
- startKey(key, like, started)
361
+ takeKey(host, takenNow, key, before[key], like, started)
335
362
  if (measuredPairs.needs(key, UndefinedOr.getOrThrow(snapshot.values[key]), like)) {
336
363
  if (Predicate.isUndefined(measuring)) measuring = []
337
364
  measuring.push(key)
@@ -355,22 +382,141 @@ const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target,
355
382
  const legs = Arr.map(built, (leg) => RetargetLegs.mapMachine(leg, Playback.drive(head)))
356
383
  let runnings: ReadonlyArray<Spine.Running> = Spine.NO_RUNNING
357
384
  let left = 0
385
+ const ends = target.transitionEnd
386
+ // What its Transition end's keys were before it set them (Q5): `undefined` until it has.
387
+ let overwritten: Values.Values | undefined
388
+ // Its runs, stops and cancels, counted: what a landing waiting on a flush checks it's still for.
389
+ let runs = 0
390
+ // `values` drawn at once, at rest: newer than the legs that let them go (#464).
391
+ const setAtRest = (values: Values.Values): void => {
392
+ snapshot.values = { ...snapshot.values, ...values }
393
+ snapshot.velocities = { ...snapshot.velocities, ...Rec.map(values, () => 0) }
394
+ const now = snapshot.now()
395
+ Arr.forEach(Rec.keys(values), (key) => {
396
+ snapshot.drawnAt[key] = now
397
+ })
398
+ gate.write(id, values)
399
+ recordFrame(values)
400
+ }
401
+ /**
402
+ * Its **Transition end** set (#896, ADR-0061 *Amendment (#896)*, Q2 and Q4): each key no leg draws
403
+ * now taken as the keys it animates are, started where it's drawn, then set at once.
404
+ */
405
+ // Each of `values`' keys `only` picks taken as the call took its own (`takeKey`), noted with the
406
+ // Override stack's targets now: an end's keys as it's set, a run's again as it starts (#951).
407
+ const takeEach = (values: Values.Values, only: (key: string) => boolean): void => {
408
+ const takingNow = host.taking()
409
+ const targetsNow = lastTargetsOf(host)
410
+ const startedNow: Record<string, string> = {}
411
+ Arr.forEach(Rec.keys(values), (key) => {
412
+ if (only(key)) takeKey(host, takingNow, key, targetsNow[key], UndefinedOr.getOrThrow(values[key]), startedNow)
413
+ })
414
+ }
415
+ const writeEnds = (values: Values.Values): void => {
416
+ const free = Rec.filter(values, (_, key) => endFree(owners.get(key)))
417
+ if (Rec.isEmptyRecord(free)) return
418
+ takeEach(free, constTrue)
419
+ // Kept from an earlier landing a run from its end didn't put back: what they were before any.
420
+ if (Predicate.isUndefined(overwritten)) overwritten = Rec.map(free, (_, key) => UndefinedOr.getOrThrow(snapshot.values[key]))
421
+ setAtRest(free)
422
+ }
423
+ /**
424
+ * Its Transition end's keys put back as they were before it was set (Q5): at a cancel, a seek or a
425
+ * replay, each a key no leg has drawn since, a later call's or the element's own. The next landing
426
+ * sets them again.
427
+ */
428
+ const putBack = (): void => {
429
+ if (Predicate.isUndefined(overwritten)) return
430
+ // Its take left each without an owner: one since is a later animation's, which wins.
431
+ const back = Rec.filter(overwritten, (_, key) => Predicate.isUndefined(owners.get(key)))
432
+ overwritten = undefined
433
+ if (Bool.not(Rec.isEmptyRecord(back))) setAtRest(back)
434
+ }
435
+ // Whether its `i`th leg snaps (#468's skip, reduced motion's positional keys): it lands on its next
436
+ // frame, its keys landed from its start, so a take before then takes nothing in flight, as Motion's
437
+ // skipped value makes no animation. Its machine read only for a leg that lost a key.
438
+ const snaps = (i: number): boolean => Machine.durationOf(machineOf(UndefinedOr.getOrThrow(built[i]))) === 0
439
+ // A leg the element's own Retarget carried on, toward the same target, as its own (Q7): the call has lost it.
440
+ let carried = false
441
+ // Taken (#896's Q3, #951): a key it animates taken from its leg mid-flight, by a later animation or the
442
+ // element's own target, or a leg carried off whole. A leg that lost one has fewer than it started
443
+ // with; one taken after its leg landed doesn't count, as Motion's, nor one that snaps.
444
+ const taken = (): boolean => {
445
+ if (carried) return true
446
+ return Arr.some(runnings, (running, i) => Bool.and(running.count < Rec.size(running.targets), Bool.not(snaps(i))))
447
+ }
448
+ // Landed: its Transition end set at the flush after its last leg's frame, then the landing told
449
+ // after the flush that draws it, so the landing includes it (ADR-0057 decision 4). Taken, it ends
450
+ // without landing, its end dropped, as Motion's call never resolves (ADR-0061 *Amendment (#951)*).
451
+ // A run, a stop or a cancel since makes each wait stale: a take reported synchronously, then a seek
452
+ // in the same task, or a stop between its end and its landing, tells none.
453
+ const landed = (): void => {
454
+ const at = runs
455
+ if (taken()) {
456
+ return gate.afterFlush(() => {
457
+ if (at === runs) hooks.settled(false)
458
+ })
459
+ }
460
+ if (Predicate.isUndefined(ends)) {
461
+ return gate.afterFlush(() => {
462
+ if (at === runs) hooks.settled(true)
463
+ })
464
+ }
465
+ gate.afterFlush(() => {
466
+ if (at !== runs) return
467
+ writeEnds(ends)
468
+ gate.afterFlush(() => {
469
+ if (at === runs) hooks.settled(true)
470
+ })
471
+ })
472
+ }
473
+ // A leg landed, or was Superseded (#951): the last lands the track, or finds it taken.
474
+ const legDone = (): void => {
475
+ left--
476
+ if (left === 0) landed()
477
+ }
358
478
  const landing: Spine.Landing = {
359
479
  report: constVoid,
360
- settled: () => {
361
- left--
362
- if (left === 0) gate.afterFlush(hooks.settled)
363
- },
480
+ settled: legDone,
364
481
  failed: hooks.failed,
365
482
  }
483
+ /**
484
+ * A leg of this run ended **Superseded** (#951), told at its end, as it settles nothing of this
485
+ * track's: carried on by the element's own Retarget as its own, its landing the Retarget's now (Q7);
486
+ * or with every key taken, a count of 0 (only a take lowers it), which `taken` reads. Either counts
487
+ * toward the track's landing, which finds it taken.
488
+ */
489
+ // Here, not in the spine, so an app with no Imperative animation pays nothing for it.
490
+ const supersededAt = (running: Spine.Running): void => {
491
+ if (running.landing !== landing) {
492
+ carried = true
493
+ return legDone()
494
+ }
495
+ if (running.count === 0) legDone()
496
+ }
366
497
  // Its legs ended where they are: the keys they draw are drawn by none.
367
498
  const end = (): void =>
368
499
  Arr.forEach(runnings, (running) => {
369
500
  forget(running)
370
501
  if (Bool.not(running.done)) endLeg(running)
371
502
  })
503
+ // Whether a run starts at its end, going forward, its end set: it lands at once, so its end stays,
504
+ // with no frame without it (a seek to the end, `complete()`). Its duration read only then.
505
+ const fromItsEnd = (): boolean => {
506
+ if (Predicate.isUndefined(overwritten)) return false
507
+ if (head.speed < 0) return false
508
+ return head.time >= duration()
509
+ }
510
+ // A key another leg draws: a later animation's, or the element's own.
511
+ const drawnByAnother = (key: string): boolean => Predicate.isNotUndefined(owners.get(key))
372
512
  const run = (): void => {
513
+ runs++
373
514
  end()
515
+ carried = false
516
+ // Run again, it takes each of its keys from whatever draws it now, as the call took them (#951): one
517
+ // leg draws a key. The first run's are taken already.
518
+ if (runs > 1) takeEach(to, drawnByAnother)
519
+ if (Bool.not(fromItsEnd())) putBack()
374
520
  left = legs.length
375
521
  Spine.tellStarted(live, legs)
376
522
  runnings = Arr.map(legs, (leg) => {
@@ -380,20 +526,15 @@ const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target,
380
526
  return running
381
527
  })
382
528
  // A target with no key lands at once.
383
- if (left === 0) gate.afterFlush(hooks.settled)
529
+ if (left === 0) landed()
384
530
  }
385
531
  const cancel = (): void => {
532
+ runs++
386
533
  end()
387
534
  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)
535
+ putBack()
536
+ // Put back at once, its keys where it started them.
537
+ setAtRest(from)
397
538
  }
398
539
  // Each leg's machine, run from its start to rest, apart from the Playhead, which it would move: a
399
540
  // blend's built with the Subject's Key rules.
@@ -404,21 +545,35 @@ const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target,
404
545
  return leg.machine() as Machine.Machine<Frame.Frame<unknown>>
405
546
  }
406
547
  const stop = (): void => {
548
+ runs++
407
549
  end()
408
550
  turnBack()
409
551
  }
410
552
  return { run, stop, cancel, duration }
411
553
  }
412
554
 
555
+ /**
556
+ * A call's transition on its target's own (#896, ADR-0061 *Amendment (#896)*, Q1): the options' fields
557
+ * laid over the target's, shallowly, as `inherit: true` lays a target's over its element's. Motion's
558
+ * `animate` reads the options alone.
559
+ */
560
+ // Its own handed back as it is with no options, so the legs' decode cache (#66) holds for it. Its
561
+ // `inherit` has nothing above it in a call: the legs read none (`RetargetLegs`' `NOT_DEFINING`).
562
+ const overOwn = (own: Config.WardicVariantTransition | undefined, given: Config.WardicTransition | undefined): Config.WardicTransition | undefined => {
563
+ if (Predicate.isUndefined(own)) return given
564
+ if (Predicate.isUndefined(given)) return own as Config.WardicTransition
565
+ return { ...own, ...given } as Config.WardicTransition
566
+ }
567
+
413
568
  /**
414
569
  * A Subject's animation of `target`: through its spine when it's a Wardic element's, else through its
415
570
  * plain spine, in the gate while any of its animations runs.
416
571
  */
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 => {
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 => {
418
573
  const resolved = VariantTarget.resolveTarget(target, undefined, undefined, VariantTarget.NO_VARIANT_STATE)
419
574
  const own = wardicElements.get(subject)
420
575
  const hooks = { settled, failed: constVoid }
421
- const transition = skippedIn(env, given)
576
+ const transition = skippedIn(env, overOwn(resolved.transition, given))
422
577
  if (Predicate.isNotUndefined(own)) return imperativeTrackOf(own.host(), resolved, transition, head, hooks)
423
578
  const plain = plainOf(env, subjects, subject)
424
579
  // In the gate while it runs; out once its last animation is done, after the flush of its last frame.
@@ -442,9 +597,9 @@ const trackOf = <S extends object>(env: AnimateEnv, subjects: Subjects<S>, subje
442
597
  })
443
598
  }
444
599
  const track = imperativeTrackOf(plain.spine.host(), resolved, transition, head, {
445
- settled: () => {
600
+ settled: (landed) => {
446
601
  leave()
447
- settled()
602
+ settled(landed)
448
603
  },
449
604
  failed: () => leave(),
450
605
  })
@@ -466,8 +621,16 @@ const trackOf = <S extends object>(env: AnimateEnv, subjects: Subjects<S>, subje
466
621
  }
467
622
  }
468
623
 
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
624
+ /** The transition `T` without its `delay`, each arm: `animate`'s and a Segment's take their own. */
625
+ type WithoutDelayOf<T> = T extends unknown ? { readonly [K in keyof T as Exclude<K, "delay">]: T[K] } : never
626
+
627
+ /** A transition without its `delay`, each arm. */
628
+ type WithoutDelay = WithoutDelayOf<Config.WardicTransition>
629
+
630
+ /** `animate`'s options over the transition `T`: its `delay` each Subject's, and `onComplete`. */
631
+ type AnimateOptionsOver<T> = WithoutDelayOf<T> & EachDelay & {
632
+ readonly onComplete?: (() => void) | undefined
633
+ }
471
634
 
472
635
  /** A `delay` that may be each Subject's (Motion's `delay(i, n)`; `stagger()` is #436). */
473
636
  interface EachDelay {
@@ -475,12 +638,19 @@ interface EachDelay {
475
638
  }
476
639
 
477
640
  /**
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.
641
+ * `animate`'s options: a transition, laid field by field over the target's own, its `delay` a number,
642
+ * 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.
480
645
  */
481
- export type WardicAnimateOptions = WithoutDelay & EachDelay & {
482
- readonly onComplete?: (() => void) | undefined
483
- }
646
+ export type WardicAnimateOptions = AnimateOptionsOver<Config.WardicTransition>
647
+
648
+ /**
649
+ * {@link WardicAnimateOptions} for a Subject whose keys are `K`: its transition's entries are under
650
+ * `K`'s names, as {@link Config.WardicTransitionFor}'s, `default` for the rest.
651
+ */
652
+ // #952: `@wardic/object`'s and `@wardic/three`'s `animate`, by the Subject the call names.
653
+ export type WardicAnimateOptionsFor<K extends string> = AnimateOptionsOver<Config.WardicTransitionFor<Exclude<K, "onComplete">>>
484
654
 
485
655
  /** A call's options without its `onComplete`: its transition. */
486
656
  const withoutComplete = (given: WardicAnimateOptions): Readonly<Record<string, unknown>> => {
@@ -502,14 +672,15 @@ const batchTransitionOf = (options: WardicAnimateOptions | undefined): Config.Wa
502
672
  UndefinedOr.map(options, (given) => withoutComplete(given) as Config.WardicTransition)
503
673
 
504
674
  /** 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 =>
675
+ 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
676
  binding.track({
507
677
  target,
508
678
  transition: batchTransitionOf(options),
509
679
  reducedMotion: env.reducedMotion(),
510
680
  skipAnimations: skipsBatchCall(env, options),
511
681
  head,
512
- settled,
682
+ settled: () => settled(true),
683
+ taken: () => settled(false),
513
684
  members,
514
685
  })
515
686
 
@@ -534,8 +705,15 @@ export interface WardicPlaybackControls extends PromiseLike<void> {
534
705
  speed: number
535
706
  /** How long it runs, in seconds: its longest Subject's longest key, a spring's to rest. */
536
707
  readonly duration: number
708
+ /**
709
+ * Where it is: `"running"`, `"paused"`, `"finished"` once it lands, and `"idle"` after `stop()` or
710
+ * `cancel()`, or once a later animation has taken one of its keys and the rest have landed.
711
+ */
537
712
  readonly state: WardicPlayState
538
- /** Resolves when it lands, or at `complete()`; never at `stop()` or `cancel()`, as Motion's. */
713
+ /**
714
+ * Resolves when it lands, or at `complete()`; never at `stop()` or `cancel()`, nor once a later
715
+ * animation takes one of its keys, as Motion's.
716
+ */
539
717
  readonly finished: Promise<void>
540
718
  /** Play it on: from where it's paused, or again from its start once it's finished or cancelled. Not after `stop()`. */
541
719
  readonly play: () => void
@@ -559,11 +737,13 @@ export interface AnimationWatch {
559
737
  /** Playback controls over `tracks`, all run through `head`, started now. */
560
738
  const controlsOf = (tracks: ReadonlyArray<Spine.ImperativeTrack>, head: Playback.Playhead, watch: AnimationWatch, onComplete: (() => void) | undefined): {
561
739
  readonly controls: WardicPlaybackControls
562
- readonly settledOne: () => void
740
+ readonly settledOne: (landed: boolean) => void
563
741
  } => {
564
742
  let state: WardicPlayState = "running"
565
743
  let stopped = false
566
744
  let landed = 0
745
+ // Whether a Subject's part ended without landing, a key of it taken by a later animation (#951).
746
+ let anyTaken = false
567
747
  let resolve: (() => void) | undefined
568
748
  let finished: Promise<void> | undefined
569
749
  let isResolved = false
@@ -574,6 +754,7 @@ const controlsOf = (tracks: ReadonlyArray<Spine.ImperativeTrack>, head: Playback
574
754
  }
575
755
  const runAll = (): void => {
576
756
  landed = 0
757
+ anyTaken = false
577
758
  Arr.forEach(tracks, (track) => track.run())
578
759
  }
579
760
  const fresh = (): void => {
@@ -671,10 +852,16 @@ const controlsOf = (tracks: ReadonlyArray<Spine.ImperativeTrack>, head: Playback
671
852
  runAll()
672
853
  },
673
854
  }
674
- // Each Subject's legs landed: the last lands the animation.
675
- const settledOne = (): void => {
855
+ // Each Subject's legs landed, or ended with a key taken: the last lands the animation, or, with one
856
+ // taken, ends it without landing, as a stop does (#951, Q1 and Q5). Motion's never resolves then.
857
+ const settledOne = (landedOne: boolean): void => {
676
858
  landed++
859
+ if (Bool.not(landedOne)) anyTaken = true
677
860
  if (landed < tracks.length) return
861
+ if (anyTaken) {
862
+ stopped = true
863
+ return end("idle", false)
864
+ }
678
865
  isResolved = true
679
866
  UndefinedOr.map(resolve, (done) => done())
680
867
  UndefinedOr.map(onComplete, (complete) => complete())
@@ -757,12 +944,12 @@ const started = (
757
944
  watch: AnimationWatch,
758
945
  count: number,
759
946
  onComplete: (() => void) | undefined,
760
- tracksOf: (head: Playback.Playhead, settled: () => void) => ReadonlyArray<Spine.ImperativeTrack>,
947
+ tracksOf: (head: Playback.Playhead, settled: (landed: boolean) => void) => ReadonlyArray<Spine.ImperativeTrack>,
761
948
  ): WardicPlaybackControls => {
762
949
  if (count === 0) return finishedControls()
763
950
  const head = Playback.make()
764
- let settledOne = constVoid
765
- const tracks = tracksOf(head, () => settledOne())
951
+ let settledOne: (landed: boolean) => void = constVoid
952
+ const tracks = tracksOf(head, (landedOne) => settledOne(landedOne))
766
953
  const made = controlsOf(tracks, head, watch, onComplete)
767
954
  settledOne = made.settledOne
768
955
  watch.started(made.controls)
@@ -958,7 +1145,7 @@ export const segmentAnimates = <S extends object>(ports: Ports<S>, segment: unkn
958
1145
  * Interpolators on its tween over the sequence, each frame told to its callback. Cancelled, the
959
1146
  * callback hears its first stop again, as Motion's motion value is set back.
960
1147
  */
961
- const callbackTrackOf = (env: AnimateEnv, callback: Callback, animation: Sequence.Animation, head: Playback.Playhead, settled: () => void): Spine.ImperativeTrack => {
1148
+ const callbackTrackOf = (env: AnimateEnv, callback: Callback, animation: Sequence.Animation, head: Playback.Playhead, settled: (landed: boolean) => void): Spine.ImperativeTrack => {
962
1149
  const stops = RetargetLegs.filledStops(UndefinedOr.getOrThrow(animation.keyframes[VALUE]), 0)
963
1150
  const options = UndefinedOr.getOrThrow(animation.transition[VALUE])
964
1151
  // Skipped (#468): its callback told the value it would end on, on the first tick.
@@ -976,7 +1163,7 @@ const callbackTrackOf = (env: AnimateEnv, callback: Callback, animation: Sequenc
976
1163
  }
977
1164
  const landed = (exit: Exit.Exit<unknown>): Effect.Effect<void> =>
978
1165
  Exit.match(exit, {
979
- onSuccess: () => Effect.sync(settled),
1166
+ onSuccess: () => Effect.sync(() => settled(true)),
980
1167
  onFailure: (cause) => Bool.match(Cause.hasInterruptsOnly(cause), { onTrue: () => Effect.void, onFalse: () => Report.failure("wardic.animate: a function Segment failed", cause) }),
981
1168
  })
982
1169
  const run = (): void => {
@@ -1024,7 +1211,7 @@ const sequenceOf = <S extends object>(env: AnimateEnv, ports: Ports<S>, scope: u
1024
1211
  return trackOf(env, ports.subjects, subject as S, animation.keyframes as Config.WardicVariant, animation.transition as Config.WardicTransition, head, settled)
1025
1212
  }),
1026
1213
  ...Arr.map(parts.batches, (part) =>
1027
- part.binding.sequenceTrack({ members: part.members, animations: part.animations, reducedMotion: env.reducedMotion(), skipAnimations: env.skipAnimations(), head, settled })),
1214
+ part.binding.sequenceTrack({ members: part.members, animations: part.animations, reducedMotion: env.reducedMotion(), skipAnimations: env.skipAnimations(), head, settled: () => settled(true), taken: () => settled(false) })),
1028
1215
  ])
1029
1216
  }
1030
1217
 
@@ -1971,15 +1971,21 @@ export interface FollowNow {
1971
1971
 
1972
1972
  /** What an **Imperative animation** tells of its legs on one element (#433). */
1973
1973
  export interface ImperativeHooks {
1974
- /** Every leg it started on the element landed, told after the gate's flush of their last frames. */
1975
- readonly settled: () => void
1974
+ /**
1975
+ * Every leg it started on the element landed, told after the gate's flush of their last frames:
1976
+ * `landed` false when a key of it was taken by a later animation, which it then never lands (#951).
1977
+ */
1978
+ readonly settled: (landed: boolean) => void
1976
1979
  /** A leg failed, reported already (ADR-0030). */
1977
1980
  readonly failed: (cause: Cause.Cause<unknown>) => void
1978
1981
  }
1979
1982
 
1980
1983
  /** An **Imperative animation** on one element (#433): its legs, built at the call, run through its Playhead. */
1981
1984
  export interface ImperativeTrack {
1982
- /** Run every leg from the Playhead's time: those running end first, so a seek starts them again there. */
1985
+ /**
1986
+ * Run every leg from the Playhead's time: those running end first, so a seek starts them again there,
1987
+ * and its keys are taken back from whatever draws them now.
1988
+ */
1983
1989
  readonly run: () => void
1984
1990
  /** End every leg where it is: its keys stay there, the animation's. */
1985
1991
  readonly stop: () => void