@wardic/core 1.2.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.
- package/dist/BaseValue.d.ts.map +1 -1
- package/dist/BaseValue.js +2 -0
- package/dist/BaseValue.js.map +1 -1
- package/dist/Batch.d.ts +6 -0
- package/dist/Batch.d.ts.map +1 -1
- package/dist/Batch.js +40 -10
- package/dist/Batch.js.map +1 -1
- package/dist/DefaultTransition.d.ts +21 -12
- package/dist/DefaultTransition.d.ts.map +1 -1
- package/dist/DefaultTransition.js +43 -2
- package/dist/DefaultTransition.js.map +1 -1
- package/dist/FrameLoop.d.ts +5 -1
- package/dist/FrameLoop.d.ts.map +1 -1
- package/dist/FrameLoop.js.map +1 -1
- package/dist/Machine.d.ts +3 -0
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +4 -3
- package/dist/Machine.js.map +1 -1
- package/dist/Spring.d.ts +7 -0
- package/dist/Spring.d.ts.map +1 -1
- package/dist/Spring.js +16 -0
- package/dist/Spring.js.map +1 -1
- package/dist/UnitScale.d.ts +39 -0
- package/dist/UnitScale.d.ts.map +1 -0
- package/dist/UnitScale.js +45 -0
- package/dist/UnitScale.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/frameLoop.js +5 -5
- package/dist/internal/frameLoop.js.map +1 -1
- package/dist/unstable/BatchCalls.d.ts +150 -0
- package/dist/unstable/BatchCalls.d.ts.map +1 -0
- package/dist/unstable/BatchCalls.js +319 -0
- package/dist/unstable/BatchCalls.js.map +1 -0
- package/dist/unstable/Config.d.ts +11 -0
- package/dist/unstable/Config.d.ts.map +1 -1
- package/dist/unstable/Config.js.map +1 -1
- package/dist/unstable/FrameEffects.d.ts +59 -0
- package/dist/unstable/FrameEffects.d.ts.map +1 -0
- package/dist/unstable/FrameEffects.js +142 -0
- package/dist/unstable/FrameEffects.js.map +1 -0
- package/dist/unstable/FrameTime.d.ts +1 -4
- package/dist/unstable/FrameTime.d.ts.map +1 -1
- package/dist/unstable/FrameTime.js +1 -4
- package/dist/unstable/FrameTime.js.map +1 -1
- package/dist/unstable/Imperative.d.ts +292 -0
- package/dist/unstable/Imperative.d.ts.map +1 -0
- package/dist/unstable/Imperative.js +922 -0
- package/dist/unstable/Imperative.js.map +1 -0
- package/dist/unstable/RetargetLegs.d.ts +2 -1
- package/dist/unstable/RetargetLegs.d.ts.map +1 -1
- package/dist/unstable/RetargetLegs.js +23 -15
- package/dist/unstable/RetargetLegs.js.map +1 -1
- package/dist/unstable/Spine.d.ts +17 -14
- package/dist/unstable/Spine.d.ts.map +1 -1
- package/dist/unstable/Spine.js +33 -16
- package/dist/unstable/Spine.js.map +1 -1
- package/package.json +1 -1
- package/src/BaseValue.ts +2 -0
- package/src/Batch.ts +62 -9
- package/src/DefaultTransition.ts +53 -2
- package/src/FrameLoop.ts +6 -2
- package/src/Machine.ts +7 -3
- package/src/Spring.ts +16 -0
- package/src/UnitScale.ts +56 -0
- package/src/index.ts +1 -0
- package/src/internal/frameLoop.ts +5 -5
- package/src/unstable/BatchCalls.ts +419 -0
- package/src/unstable/Config.ts +17 -0
- package/src/unstable/FrameEffects.ts +172 -0
- package/src/unstable/FrameTime.ts +1 -5
- package/src/unstable/Imperative.ts +1232 -0
- package/src/unstable/RetargetLegs.ts +20 -11
- package/src/unstable/Spine.ts +48 -21
|
@@ -0,0 +1,1232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* **Imperative animation** (#433, CONTEXT.md; Motion's `animate`), for every Environment: an
|
|
3
|
+
* **Animation scope**'s `animate`, the Subjects it animates, and the **Playback controls** it returns.
|
|
4
|
+
* An Environment says what its Subjects are and how a plain one starts ({@link Subjects}), and what
|
|
5
|
+
* its Batches are ({@link Batches}); each binding's `createAnimate()` builds on it.
|
|
6
|
+
*
|
|
7
|
+
* - **A Wardic element** animates through its own spine, registered here while it's mounted: the
|
|
8
|
+
* call takes the keys it animates, which stay where it leaves them until the element's own target
|
|
9
|
+
* for them changes (Motion's latest-wins).
|
|
10
|
+
* - **A plain Subject** animates through a spine of its own (#433's Q4), with no config, presence or
|
|
11
|
+
* layout node: it starts from the Subject's Own value (ADR-0022) and keeps its values for the next
|
|
12
|
+
* call, velocity included. It's in the gate only while it animates, so the gate keeps nothing of a
|
|
13
|
+
* Subject it isn't animating.
|
|
14
|
+
* - **The controls** are Motion's (`AnimationPlaybackControls`): awaitable, with `stop`, `cancel`,
|
|
15
|
+
* `complete`, `pause`, `play`, `speed`, `time` and `duration`, every Subject's legs run through one
|
|
16
|
+
* `Playback.Playhead`. Its `finished` resolves at a landing or `complete()`, never at a `stop()` or a
|
|
17
|
+
* `cancel()`, as Motion's.
|
|
18
|
+
* - **A sequence** (#433's PR 2, Motion's) is compiled by core's `Sequence` to each Subject's
|
|
19
|
+
* Keyframes over the whole sequence, run on the same controls: each Subject animates as a call
|
|
20
|
+
* does, and a function Segment's value, from 0, is told to its callback each frame.
|
|
21
|
+
*
|
|
22
|
+
* Unstable, for Wardic's own packages: public, with no semver promise.
|
|
23
|
+
*
|
|
24
|
+
* @module
|
|
25
|
+
*/
|
|
26
|
+
// Unstable: ADR-0071, decision 5. #882 (ADR-0073, *Amendment (#882)*, Q1): dom's `unstable/Imperative`
|
|
27
|
+
// (#672's move of solid's `imperative.ts`), its environment-agnostic part moved here behind two ports, so
|
|
28
|
+
// `@wardic/object` animates plain objects with no DOM code; dom fills the ports with its elements,
|
|
29
|
+
// selectors and Batch members.
|
|
30
|
+
import type * as DefaultTransition from "../DefaultTransition.ts"
|
|
31
|
+
import type * as Frame from "../Frame.ts"
|
|
32
|
+
import * as InterpolatorRegistry from "../InterpolatorRegistry.ts"
|
|
33
|
+
import * as Machine from "../Machine.ts"
|
|
34
|
+
import * as Playback from "../Playback.ts"
|
|
35
|
+
import * as ReducedMotion from "../ReducedMotion.ts"
|
|
36
|
+
import * as Sequence from "../Sequence.ts"
|
|
37
|
+
import type * as Values from "../Values.ts"
|
|
38
|
+
import type * as Config from "./Config.ts"
|
|
39
|
+
import type * as FlushGate from "./FlushGate.ts"
|
|
40
|
+
import type * as MeasuredPairs from "./MeasuredPairs.ts"
|
|
41
|
+
import type * as OwnValues from "./OwnValues.ts"
|
|
42
|
+
import * as Report from "./Report.ts"
|
|
43
|
+
import * as RetargetLegs from "./RetargetLegs.ts"
|
|
44
|
+
import * as Spine from "./Spine.ts"
|
|
45
|
+
import type * as VariableValues from "./VariableValues.ts"
|
|
46
|
+
import * as VariantTarget from "./VariantTarget.ts"
|
|
47
|
+
import * as VariantTree from "./VariantTree.ts"
|
|
48
|
+
import type * as WardicServices from "./WardicServices.ts"
|
|
49
|
+
import * as Arr from "effect/Array"
|
|
50
|
+
import * as Bool from "effect/Boolean"
|
|
51
|
+
import * as Cause from "effect/Cause"
|
|
52
|
+
import * as Effect from "effect/Effect"
|
|
53
|
+
import * as Exit from "effect/Exit"
|
|
54
|
+
import type * as Fiber from "effect/Fiber"
|
|
55
|
+
import { constTrue, constVoid, identity } from "effect/Function"
|
|
56
|
+
import * as Predicate from "effect/Predicate"
|
|
57
|
+
import * as Rec from "effect/Record"
|
|
58
|
+
import * as UndefinedOr from "effect/UndefinedOr"
|
|
59
|
+
|
|
60
|
+
/** What an Imperative animation animates a Wardic element through: what its spine hands over (`host`). */
|
|
61
|
+
type Animatable = Pick<Spine.ElementSpine, "host">
|
|
62
|
+
|
|
63
|
+
/** Each mounted Wardic element's spine (Motion's `visualElementStore`), by its Subject. */
|
|
64
|
+
// Module state (#460, Q7): each Wardic element's animatable, held weakly by its Subject.
|
|
65
|
+
const wardicElements = new WeakMap<object, Animatable>()
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Register a mounted Wardic element's spine for the Imperative animations of its Subject (#433): until
|
|
69
|
+
* the returned function, which its unmount calls.
|
|
70
|
+
*/
|
|
71
|
+
export const registerAnimatable = (subject: object, spine: Animatable): (() => void) => {
|
|
72
|
+
wardicElements.set(subject, spine)
|
|
73
|
+
return () => {
|
|
74
|
+
if (wardicElements.get(subject) === spine) wardicElements.delete(subject)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** What a scope animates plain Subjects with: its provider's. */
|
|
79
|
+
export interface AnimateEnv {
|
|
80
|
+
readonly runtime: WardicServices.Runner<WardicServices.WardicServices>
|
|
81
|
+
readonly gate: FlushGate.Shape
|
|
82
|
+
readonly reducedMotion: () => boolean
|
|
83
|
+
/**
|
|
84
|
+
* Whether its animations are skipped (#468): its scope's nearest `WardicConfig`'s, read as each
|
|
85
|
+
* starts, and handed to each Subject's animation in its transition, as Motion's `useAnimate` does.
|
|
86
|
+
*/
|
|
87
|
+
readonly skipAnimations: () => boolean
|
|
88
|
+
readonly positionalKeys: ReducedMotion.PositionalKeys["Service"]
|
|
89
|
+
readonly defaultTransition: DefaultTransition.DefaultTransition["Service"]
|
|
90
|
+
/** Its Environment's reads of its Subjects' own values (#672). */
|
|
91
|
+
readonly ownValues: OwnValues.Shape
|
|
92
|
+
/** Its Environment's Measured pairs (#430, #672). */
|
|
93
|
+
readonly measuredPairs: MeasuredPairs.Shape
|
|
94
|
+
/** Its Environment's Variable values (#55, #672). */
|
|
95
|
+
readonly variableValues: VariableValues.Shape
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* An Environment's Subjects, for `animate`: what its first argument names, and where a plain one
|
|
100
|
+
* starts. The DOM's are elements, selectors looked up in the scope and lists of them; a scene's, its
|
|
101
|
+
* objects.
|
|
102
|
+
*/
|
|
103
|
+
export interface Subjects<S extends object> {
|
|
104
|
+
/** The Subjects `given` names, inside `scope` (a binding's scope, or `undefined`): none when it names none. */
|
|
105
|
+
readonly resolve: (given: unknown, scope: unknown) => ReadonlyArray<S>
|
|
106
|
+
/** A plain Subject's Base-value links (ADR-0022): its Own values, and the DOM's Starting style. */
|
|
107
|
+
readonly startLinksOf: (subject: S) => Spine.StartLinks
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** What `animate` asks of a Batch's binding (#665): its tracks, and its Segments in a sequence. */
|
|
111
|
+
// Methods, not properties: a binding's own calls take its Environment's targets, a narrower type.
|
|
112
|
+
export interface BatchCalls {
|
|
113
|
+
/** An `animate` call's track over its members, or the members `members` names, driven by `head`. */
|
|
114
|
+
track(call: BatchCall): Spine.ImperativeTrack
|
|
115
|
+
/** The members a Batch or a member's handle names: `undefined` for every member, none once a handle's member has left. */
|
|
116
|
+
namedOf(entry: BatchEntry): ReadonlyArray<number> | undefined
|
|
117
|
+
/** A sequence's Segment of the Batch, or of the members `named` names: its members' entries, in member order, and its values for core. */
|
|
118
|
+
segmentOf(named: ReadonlyArray<number> | undefined, target: Readonly<Record<string, unknown>>): {
|
|
119
|
+
readonly subjects: ReadonlyArray<BatchMember>
|
|
120
|
+
readonly values: Readonly<Record<string, Sequence.Value>>
|
|
121
|
+
}
|
|
122
|
+
/** A sequence's track over its members, each through its own Batch keys' stops, driven by `head`. */
|
|
123
|
+
sequenceTrack(call: BatchSequenceCall): Spine.ImperativeTrack
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** What `animate` knows of a Batch or a member's handle: its binding, and a handle's member by its index. */
|
|
127
|
+
export interface BatchEntry {
|
|
128
|
+
readonly binding: BatchCalls
|
|
129
|
+
readonly member: number | undefined
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** A member's entry: a member's handle's, or a member's Subject's. */
|
|
133
|
+
export interface BatchMember extends BatchEntry {
|
|
134
|
+
readonly member: number
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** An `animate` call of a Batch: its targets, its transition, its scope's settings, its Playhead and landing. */
|
|
138
|
+
export interface BatchCall {
|
|
139
|
+
readonly target: Readonly<Record<string, unknown>>
|
|
140
|
+
readonly transition: Config.WardicTransition | undefined
|
|
141
|
+
readonly reducedMotion: boolean
|
|
142
|
+
readonly skipAnimations: boolean
|
|
143
|
+
readonly head: Playback.Playhead
|
|
144
|
+
readonly settled: () => void
|
|
145
|
+
/** It ended with a key a later change took, never landing (#951). */
|
|
146
|
+
readonly taken: () => void
|
|
147
|
+
/** The members it moves, in order: every member when left out. */
|
|
148
|
+
readonly members: ReadonlyArray<number> | undefined
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** A sequence's part for a Batch: its members, in order, each one's compiled animation, its scope's settings, its Playhead and landing. */
|
|
152
|
+
export interface BatchSequenceCall {
|
|
153
|
+
readonly members: ReadonlyArray<number>
|
|
154
|
+
readonly animations: ReadonlyArray<Sequence.Animation>
|
|
155
|
+
readonly reducedMotion: boolean
|
|
156
|
+
readonly skipAnimations: boolean
|
|
157
|
+
readonly head: Playback.Playhead
|
|
158
|
+
readonly settled: () => void
|
|
159
|
+
/** It ended with a key a later change took, never landing (#951). */
|
|
160
|
+
readonly taken: () => void
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** An `animate` call's parts for Subjects that are Batch members' (#665): each Batch's members, and each Subject's own keys. */
|
|
164
|
+
export interface CallParts<S extends object> {
|
|
165
|
+
readonly members: ReadonlyArray<{ readonly binding: BatchCalls; readonly members: ReadonlyArray<number>; readonly target: Readonly<Record<string, unknown>> }>
|
|
166
|
+
readonly subjects: ReadonlyArray<{ readonly subject: S; readonly index: number; readonly target: object }>
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** A compiled sequence's part for one Batch: its members, in the order they first appear, and each one's animation. */
|
|
170
|
+
export interface SequencePart {
|
|
171
|
+
readonly binding: BatchCalls
|
|
172
|
+
readonly members: ReadonlyArray<number>
|
|
173
|
+
readonly animations: ReadonlyArray<Sequence.Animation>
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** An Environment's Batches, for `animate`: its handles, and the Subjects that are its members'. */
|
|
177
|
+
// Methods, as `BatchCalls`' are: an Environment's entries carry its own binding, a wider type.
|
|
178
|
+
export interface Batches<S extends object> {
|
|
179
|
+
/** The Batch or member handle `given` is: `undefined` for anything else. */
|
|
180
|
+
entryOf(given: unknown): BatchEntry | undefined
|
|
181
|
+
/** Whether any of `subjects` is a Batch member's: when none is, a call keeps the plain path. */
|
|
182
|
+
hasMembers(subjects: ReadonlyArray<S>): boolean
|
|
183
|
+
/** A call's parts, when some of `subjects` are members': each Batch's members as one part, with its keys. */
|
|
184
|
+
callPartsOf(subjects: ReadonlyArray<S>, target: object): CallParts<S>
|
|
185
|
+
/** A Segment's subjects: each member's as its member's entry, each Batch checking the Segment's values once. */
|
|
186
|
+
segmentSubjectsOf(subjects: ReadonlyArray<S>, target: Readonly<Record<string, unknown>>): ReadonlyArray<S | BatchMember>
|
|
187
|
+
/** A compiled sequence's parts: each Batch's members as one part, the other keys of a member's its Subject's. */
|
|
188
|
+
sequencePartsOf<X>(compiled: ReadonlyArray<readonly [X | BatchMember, Sequence.Animation]>): {
|
|
189
|
+
readonly batches: ReadonlyArray<SequencePart>
|
|
190
|
+
readonly others: ReadonlyArray<readonly [X | S, Sequence.Animation]>
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** An Environment with no Batches. */
|
|
195
|
+
export const noBatches: Batches<object> = {
|
|
196
|
+
entryOf: () => undefined,
|
|
197
|
+
hasMembers: () => false,
|
|
198
|
+
callPartsOf: (subjects, target) => ({ members: [], subjects: Arr.map(subjects, (subject, index) => ({ subject, index, target })) }),
|
|
199
|
+
segmentSubjectsOf: identity,
|
|
200
|
+
sequencePartsOf: (compiled) => ({ batches: [], others: compiled as ReadonlyArray<readonly [never, Sequence.Animation]> }),
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** What `animate` works with: its Environment's Subjects and Batches. */
|
|
204
|
+
export interface Ports<S extends object> {
|
|
205
|
+
readonly subjects: Subjects<S>
|
|
206
|
+
readonly batches: Batches<S>
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** A plain Subject's spine (#433's Q4), its values, and how many of its animations run, which keep it in the gate. */
|
|
210
|
+
interface Plain {
|
|
211
|
+
readonly spine: Spine.ElementSpine
|
|
212
|
+
readonly snapshot: Spine.Snapshot
|
|
213
|
+
readonly id: string
|
|
214
|
+
running: number
|
|
215
|
+
unregister: () => void
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Each gate's plain Subjects: a provider's own, kept while the Subject is. */
|
|
219
|
+
// Module state (#460, Q7): each gate's plain Subjects, held weakly by the gate.
|
|
220
|
+
const plainByGate = new WeakMap<FlushGate.Shape, WeakMap<object, Plain>>()
|
|
221
|
+
|
|
222
|
+
/** The plain Subjects' gate ids, counted. */
|
|
223
|
+
// Module state (#460, Q7): an id counter, unique across every gate.
|
|
224
|
+
let plainIds = 0
|
|
225
|
+
|
|
226
|
+
/** No config: a plain Subject's. */
|
|
227
|
+
const NO_CONFIG = {}
|
|
228
|
+
|
|
229
|
+
/** `subject`'s plain spine under `env`'s gate, made at its first animation. */
|
|
230
|
+
const plainOf = <S extends object>(env: AnimateEnv, subjects: Subjects<S>, subject: S): Plain => {
|
|
231
|
+
let bySubject = plainByGate.get(env.gate)
|
|
232
|
+
if (Predicate.isUndefined(bySubject)) {
|
|
233
|
+
bySubject = new WeakMap()
|
|
234
|
+
plainByGate.set(env.gate, bySubject)
|
|
235
|
+
}
|
|
236
|
+
const known = bySubject.get(subject)
|
|
237
|
+
if (Predicate.isNotUndefined(known)) return known
|
|
238
|
+
const id = `wardic-animate-${plainIds++}`
|
|
239
|
+
const snapshot: Spine.Snapshot = Spine.emptySnapshotOf({})
|
|
240
|
+
const spine = Spine.makeElementSpine({
|
|
241
|
+
runtime: env.runtime,
|
|
242
|
+
gate: env.gate,
|
|
243
|
+
id,
|
|
244
|
+
element: subject,
|
|
245
|
+
tag: undefined,
|
|
246
|
+
config: () => NO_CONFIG,
|
|
247
|
+
parent: undefined,
|
|
248
|
+
canStart: undefined,
|
|
249
|
+
extraDelay: undefined,
|
|
250
|
+
onFrame: undefined,
|
|
251
|
+
reducedMotion: env.reducedMotion,
|
|
252
|
+
positionalKeys: env.positionalKeys,
|
|
253
|
+
defaultTransition: env.defaultTransition,
|
|
254
|
+
ownValues: env.ownValues,
|
|
255
|
+
measuredPairs: env.measuredPairs,
|
|
256
|
+
variableValues: env.variableValues,
|
|
257
|
+
start: { initialValues: {}, initialLink: {}, startedLink: {}, quiet: false },
|
|
258
|
+
snapshot,
|
|
259
|
+
reuse: Spine.makeReuse(),
|
|
260
|
+
links: subjects.startLinksOf(subject),
|
|
261
|
+
styleNow: undefined,
|
|
262
|
+
live: undefined,
|
|
263
|
+
presence: undefined,
|
|
264
|
+
projected: false,
|
|
265
|
+
dragAxis: undefined,
|
|
266
|
+
handoff: undefined,
|
|
267
|
+
})
|
|
268
|
+
const plain: Plain = { spine, snapshot, id, running: 0, unregister: constVoid }
|
|
269
|
+
bySubject.set(subject, plain)
|
|
270
|
+
return plain
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* `transition` with `skipAnimations` when its scope's animations are skipped (#468, Motion's
|
|
275
|
+
* `useAnimate`, which hands its config's to each animation): a Wardic element's own config can skip it
|
|
276
|
+
* too, and a plain Subject has none.
|
|
277
|
+
*/
|
|
278
|
+
const skippedIn = (env: AnimateEnv, transition: Config.WardicTransition | undefined): Config.WardicTransition | undefined => {
|
|
279
|
+
if (Bool.not(env.skipAnimations())) return transition
|
|
280
|
+
return { ...transition, skipAnimations: true } as Config.WardicTransition
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The resolution an **Imperative animation**'s legs run under (#433): none of the Override stack's,
|
|
285
|
+
* so a Retarget that takes a key back never carries one as its own Keyframes or names.
|
|
286
|
+
*/
|
|
287
|
+
const IMPERATIVE: Spine.Resolution = {
|
|
288
|
+
resolved: {},
|
|
289
|
+
base: {},
|
|
290
|
+
dragging: false,
|
|
291
|
+
variant: undefined,
|
|
292
|
+
active: { inView: false, focused: false, hovered: false, pressed: false, dragged: false, dragging: false },
|
|
293
|
+
transition: undefined,
|
|
294
|
+
layers: [],
|
|
295
|
+
names: VariantTree.NO_GESTURE_NAMES,
|
|
296
|
+
layersOn: VariantTree.NO_GESTURE_STATES,
|
|
297
|
+
ends: undefined,
|
|
298
|
+
keyframes: undefined,
|
|
299
|
+
change: undefined,
|
|
300
|
+
}
|
|
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
|
+
|
|
328
|
+
/**
|
|
329
|
+
* An **Imperative animation** of a Subject (#433), through its spine's `host`: it takes `target`'s
|
|
330
|
+
* keys from whatever runs them, each noted with the target the Override stack has for it now, so a
|
|
331
|
+
* Retarget leaves it where the animation leaves it until that target changes (Motion's latest-wins).
|
|
332
|
+
* Its legs start where the keys are drawn, with their velocity, on `given` (a key with none of its own
|
|
333
|
+
* on its Default transition), reduced motion as the provider says, and run through `head` once
|
|
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.
|
|
336
|
+
*/
|
|
337
|
+
// #746 (ADR-0067, *Amendment (#746)*, decision 10): here, not in the spine, so an app that never animates
|
|
338
|
+
// imperatively ships none of it; the spine hands over what it takes (`SpineHost`).
|
|
339
|
+
const imperativeTrackOf = (host: Spine.SpineHost, target: VariantTarget.Target, given: Config.WardicTransition | undefined, head: Playback.Playhead, hooks: Spine.ImperativeHooks): Spine.ImperativeTrack => {
|
|
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
|
|
341
|
+
/** A leg of an Imperative animation, forked as a Retarget's are; it lands on its track. */
|
|
342
|
+
// Data-first: a pipe's curried forms are two closures more for every leg (#618, H1).
|
|
343
|
+
const runImperative = (running: Spine.Running, program: Effect.Effect<unknown, never, WardicServices.WardicServices>): void => {
|
|
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
|
+
})))
|
|
349
|
+
}
|
|
350
|
+
const to = target.values
|
|
351
|
+
const before = lastTargetsOf(host)
|
|
352
|
+
const started: Record<string, string> = {}
|
|
353
|
+
let measuring: Array<string> | undefined
|
|
354
|
+
// Each key taken from whatever runs it, noted with its target now, and started where it's drawn.
|
|
355
|
+
// Its velocity read now (#464), as its legs are built.
|
|
356
|
+
const takenNow = host.taking()
|
|
357
|
+
// Where a Path's axis this target leaves out rests (#574), before its keys are taken below.
|
|
358
|
+
const rests = Spine.restsOf(owners, to)
|
|
359
|
+
Arr.forEach(Rec.keys(to), (key) => {
|
|
360
|
+
const like = UndefinedOr.getOrThrow(to[key])
|
|
361
|
+
takeKey(host, takenNow, key, before[key], like, started)
|
|
362
|
+
if (measuredPairs.needs(key, UndefinedOr.getOrThrow(snapshot.values[key]), like)) {
|
|
363
|
+
if (Predicate.isUndefined(measuring)) measuring = []
|
|
364
|
+
measuring.push(key)
|
|
365
|
+
}
|
|
366
|
+
})
|
|
367
|
+
keepTheRest()
|
|
368
|
+
let from: Values.Values = Rec.map(to, (_, key) => UndefinedOr.getOrThrow(snapshot.values[key]))
|
|
369
|
+
let stops: RetargetLegs.StopsByKey | undefined
|
|
370
|
+
if (Predicate.isNotUndefined(target.keyframes)) {
|
|
371
|
+
const begun = RetargetLegs.keyframesFrom(from, to, target.keyframes)
|
|
372
|
+
from = begun.from
|
|
373
|
+
stops = begun.stops
|
|
374
|
+
}
|
|
375
|
+
const reduce = host.reducedMotion()
|
|
376
|
+
const skipped = host.skipAnimations()
|
|
377
|
+
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)
|
|
379
|
+
// Built once: a seek or a replay runs the same legs, from the same start, with the same velocities.
|
|
380
|
+
const built = RetargetLegs.legsOf(from, to, given, reduce, skipped, positionalKeys, defaultTransition, readVelocity, undefined, stops, measured, snapshot.values, rests, unitScale)
|
|
381
|
+
const variables = Spine.holdsVariable(variableValues, from, to, stops)
|
|
382
|
+
const legs = Arr.map(built, (leg) => RetargetLegs.mapMachine(leg, Playback.drive(head)))
|
|
383
|
+
let runnings: ReadonlyArray<Spine.Running> = Spine.NO_RUNNING
|
|
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
|
+
}
|
|
478
|
+
const landing: Spine.Landing = {
|
|
479
|
+
report: constVoid,
|
|
480
|
+
settled: legDone,
|
|
481
|
+
failed: hooks.failed,
|
|
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
|
+
}
|
|
497
|
+
// Its legs ended where they are: the keys they draw are drawn by none.
|
|
498
|
+
const end = (): void =>
|
|
499
|
+
Arr.forEach(runnings, (running) => {
|
|
500
|
+
forget(running)
|
|
501
|
+
if (Bool.not(running.done)) endLeg(running)
|
|
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))
|
|
512
|
+
const run = (): void => {
|
|
513
|
+
runs++
|
|
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()
|
|
520
|
+
left = legs.length
|
|
521
|
+
Spine.tellStarted(live, legs)
|
|
522
|
+
runnings = Arr.map(legs, (leg) => {
|
|
523
|
+
const running = Spine.runningOf(leg.to, IMPERATIVE, landing, leg._tag)
|
|
524
|
+
own(running)
|
|
525
|
+
runImperative(running, legProgram(leg, running, variables))
|
|
526
|
+
return running
|
|
527
|
+
})
|
|
528
|
+
// A target with no key lands at once.
|
|
529
|
+
if (left === 0) landed()
|
|
530
|
+
}
|
|
531
|
+
const cancel = (): void => {
|
|
532
|
+
runs++
|
|
533
|
+
end()
|
|
534
|
+
turnBack()
|
|
535
|
+
putBack()
|
|
536
|
+
// Put back at once, its keys where it started them.
|
|
537
|
+
setAtRest(from)
|
|
538
|
+
}
|
|
539
|
+
// Each leg's machine, run from its start to rest, apart from the Playhead, which it would move: a
|
|
540
|
+
// blend's built with the Subject's Key rules.
|
|
541
|
+
const duration = (): number =>
|
|
542
|
+
Arr.reduce(built, 0, (longest, leg) => Math.max(longest, Machine.durationOf(machineOf(leg))))
|
|
543
|
+
const machineOf = (leg: RetargetLegs.Leg): Machine.Machine<Frame.Frame<unknown>> => {
|
|
544
|
+
if (leg._tag === "Blend") return runtime.runSync(Effect.suspend(leg.machine).pipe(InterpolatorRegistry.withKeyRules(elementRules)))
|
|
545
|
+
return leg.machine() as Machine.Machine<Frame.Frame<unknown>>
|
|
546
|
+
}
|
|
547
|
+
const stop = (): void => {
|
|
548
|
+
runs++
|
|
549
|
+
end()
|
|
550
|
+
turnBack()
|
|
551
|
+
}
|
|
552
|
+
return { run, stop, cancel, duration }
|
|
553
|
+
}
|
|
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
|
+
|
|
568
|
+
/**
|
|
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.
|
|
571
|
+
*/
|
|
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)
|
|
574
|
+
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)
|
|
578
|
+
const plain = plainOf(env, subjects, subject)
|
|
579
|
+
// In the gate while it runs; out once its last animation is done, after the flush of its last frame.
|
|
580
|
+
let live = false
|
|
581
|
+
const enter = (): void => {
|
|
582
|
+
if (live) return
|
|
583
|
+
live = true
|
|
584
|
+
// Back in the gate with every value it holds, so a transform it draws keeps the keys it isn't animating.
|
|
585
|
+
if (plain.running === 0) {
|
|
586
|
+
plain.unregister = env.gate.register(plain.id, { target: subject })
|
|
587
|
+
if (Bool.not(Rec.isEmptyRecord(plain.snapshot.values))) env.gate.write(plain.id, plain.snapshot.values)
|
|
588
|
+
}
|
|
589
|
+
plain.running++
|
|
590
|
+
}
|
|
591
|
+
const leave = (): void => {
|
|
592
|
+
if (Bool.not(live)) return
|
|
593
|
+
live = false
|
|
594
|
+
plain.running--
|
|
595
|
+
if (plain.running === 0) env.gate.afterFlush(() => {
|
|
596
|
+
if (plain.running === 0) plain.unregister()
|
|
597
|
+
})
|
|
598
|
+
}
|
|
599
|
+
const track = imperativeTrackOf(plain.spine.host(), resolved, transition, head, {
|
|
600
|
+
settled: (landed) => {
|
|
601
|
+
leave()
|
|
602
|
+
settled(landed)
|
|
603
|
+
},
|
|
604
|
+
failed: () => leave(),
|
|
605
|
+
})
|
|
606
|
+
return {
|
|
607
|
+
run: () => {
|
|
608
|
+
enter()
|
|
609
|
+
track.run()
|
|
610
|
+
},
|
|
611
|
+
stop: () => {
|
|
612
|
+
track.stop()
|
|
613
|
+
leave()
|
|
614
|
+
},
|
|
615
|
+
cancel: () => {
|
|
616
|
+
enter()
|
|
617
|
+
track.cancel()
|
|
618
|
+
leave()
|
|
619
|
+
},
|
|
620
|
+
duration: track.duration,
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
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
|
+
}
|
|
634
|
+
|
|
635
|
+
/** A `delay` that may be each Subject's (Motion's `delay(i, n)`; `stagger()` is #436). */
|
|
636
|
+
interface EachDelay {
|
|
637
|
+
readonly delay?: number | ((index: number, count: number) => number) | undefined
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
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.
|
|
645
|
+
*/
|
|
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">>>
|
|
654
|
+
|
|
655
|
+
/** A call's options without its `onComplete`: its transition. */
|
|
656
|
+
const withoutComplete = (given: WardicAnimateOptions): Readonly<Record<string, unknown>> => {
|
|
657
|
+
const { onComplete: _onComplete, ...rest } = given as Readonly<Record<string, unknown>>
|
|
658
|
+
return rest
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/** Each Subject's transition: its `delay` worked out for it (Motion's `delay(i, n)`). */
|
|
662
|
+
const transitionFor = (options: WardicAnimateOptions | undefined, index: number, count: number): Config.WardicTransition | undefined =>
|
|
663
|
+
UndefinedOr.map(options, (given) => {
|
|
664
|
+
const { delay, ...rest } = withoutComplete(given)
|
|
665
|
+
if (Predicate.isFunction(delay)) return { ...rest, delay: (delay as (index: number, count: number) => number)(index, count) } as Config.WardicTransition
|
|
666
|
+
if (Predicate.isUndefined(delay)) return rest as Config.WardicTransition
|
|
667
|
+
return { ...rest, delay } as Config.WardicTransition
|
|
668
|
+
})
|
|
669
|
+
|
|
670
|
+
/** A Batch call's transition (#665): the options without `onComplete`, a `stagger()` delay kept for the Batch to count its movers. */
|
|
671
|
+
const batchTransitionOf = (options: WardicAnimateOptions | undefined): Config.WardicTransition | undefined =>
|
|
672
|
+
UndefinedOr.map(options, (given) => withoutComplete(given) as Config.WardicTransition)
|
|
673
|
+
|
|
674
|
+
/** A Batch call's track over `members` (every member when left out), driven by `head`: the call's options its transition. */
|
|
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 =>
|
|
676
|
+
binding.track({
|
|
677
|
+
target,
|
|
678
|
+
transition: batchTransitionOf(options),
|
|
679
|
+
reducedMotion: env.reducedMotion(),
|
|
680
|
+
skipAnimations: skipsBatchCall(env, options),
|
|
681
|
+
head,
|
|
682
|
+
settled: () => settled(true),
|
|
683
|
+
taken: () => settled(false),
|
|
684
|
+
members,
|
|
685
|
+
})
|
|
686
|
+
|
|
687
|
+
/** Whether a Batch call lands at once: its scope's animations are skipped, or its own `skipAnimations` (#468). */
|
|
688
|
+
const skipsBatchCall = (env: AnimateEnv, options: WardicAnimateOptions | undefined): boolean => {
|
|
689
|
+
if (env.skipAnimations()) return true
|
|
690
|
+
return UndefinedOr.match(options, { onUndefined: () => false, onDefined: (given) => given.skipAnimations === true })
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/** Where an animation is: `"idle"`, `"running"`, `"paused"` or `"finished"`. Motion's `AnimationPlayState`. */
|
|
694
|
+
export type WardicPlayState = "idle" | "running" | "paused" | "finished"
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* What `animate` returns: an animation's controls, and a promise of its landing, so it can be awaited.
|
|
698
|
+
* Motion's `AnimationPlaybackControls`.
|
|
699
|
+
*/
|
|
700
|
+
// #433: every Subject's legs run through one Playhead.
|
|
701
|
+
export interface WardicPlaybackControls extends PromiseLike<void> {
|
|
702
|
+
/** Seconds played, a delay included. Set, it seeks: each key starts again there, holding while paused. */
|
|
703
|
+
time: number
|
|
704
|
+
/** How fast it plays: 1 as given, 2 twice as fast, 0 held, below 0 backwards. */
|
|
705
|
+
speed: number
|
|
706
|
+
/** How long it runs, in seconds: its longest Subject's longest key, a spring's to rest. */
|
|
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
|
+
*/
|
|
712
|
+
readonly state: WardicPlayState
|
|
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
|
+
*/
|
|
717
|
+
readonly finished: Promise<void>
|
|
718
|
+
/** Play it on: from where it's paused, or again from its start once it's finished or cancelled. Not after `stop()`. */
|
|
719
|
+
readonly play: () => void
|
|
720
|
+
/** Hold it where it is. */
|
|
721
|
+
readonly pause: () => void
|
|
722
|
+
/** End it where it is, for good: its keys stay there. */
|
|
723
|
+
readonly stop: () => void
|
|
724
|
+
/** End it, and put its keys back where it started them. */
|
|
725
|
+
readonly cancel: () => void
|
|
726
|
+
/** Go to its end at once, and finish. */
|
|
727
|
+
readonly complete: () => void
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/** What a scope hears of its animations: each one's start and its end, however it ended. */
|
|
731
|
+
export interface AnimationWatch {
|
|
732
|
+
readonly started: (controls: WardicPlaybackControls) => void
|
|
733
|
+
/** It's no longer in flight: `landed` when it finished. */
|
|
734
|
+
readonly ended: (controls: WardicPlaybackControls, landed: boolean) => void
|
|
735
|
+
}
|
|
736
|
+
|
|
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): {
|
|
739
|
+
readonly controls: WardicPlaybackControls
|
|
740
|
+
readonly settledOne: (landed: boolean) => void
|
|
741
|
+
} => {
|
|
742
|
+
let state: WardicPlayState = "running"
|
|
743
|
+
let stopped = false
|
|
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
|
|
747
|
+
let resolve: (() => void) | undefined
|
|
748
|
+
let finished: Promise<void> | undefined
|
|
749
|
+
let isResolved = false
|
|
750
|
+
let duration: number | undefined
|
|
751
|
+
const durationNow = (): number => {
|
|
752
|
+
if (Predicate.isUndefined(duration)) duration = Arr.reduce(tracks, 0, (longest, track) => Math.max(longest, track.duration()))
|
|
753
|
+
return duration
|
|
754
|
+
}
|
|
755
|
+
const runAll = (): void => {
|
|
756
|
+
landed = 0
|
|
757
|
+
anyTaken = false
|
|
758
|
+
Arr.forEach(tracks, (track) => track.run())
|
|
759
|
+
}
|
|
760
|
+
const fresh = (): void => {
|
|
761
|
+
finished = undefined
|
|
762
|
+
resolve = undefined
|
|
763
|
+
isResolved = false
|
|
764
|
+
}
|
|
765
|
+
const end = (how: WardicPlayState, landedNow: boolean): void => {
|
|
766
|
+
const was = state
|
|
767
|
+
state = how
|
|
768
|
+
if (Bool.or(was === "running", was === "paused")) watch.ended(controls, landedNow)
|
|
769
|
+
}
|
|
770
|
+
const restart = (): void => {
|
|
771
|
+
const was = state
|
|
772
|
+
state = "running"
|
|
773
|
+
if (Bool.or(was === "finished", was === "idle")) watch.started(controls)
|
|
774
|
+
}
|
|
775
|
+
const controls: WardicPlaybackControls = {
|
|
776
|
+
get time() {
|
|
777
|
+
return head.time
|
|
778
|
+
},
|
|
779
|
+
set time(at: number) {
|
|
780
|
+
Playback.seek(head, at)
|
|
781
|
+
// A seek of one that isn't running holds where it goes, as Motion's.
|
|
782
|
+
if (state !== "running") {
|
|
783
|
+
Playback.pause(head)
|
|
784
|
+
if (Bool.or(state === "finished", state === "idle")) {
|
|
785
|
+
fresh()
|
|
786
|
+
watch.started(controls)
|
|
787
|
+
}
|
|
788
|
+
state = "paused"
|
|
789
|
+
}
|
|
790
|
+
runAll()
|
|
791
|
+
},
|
|
792
|
+
get speed() {
|
|
793
|
+
return head.speed
|
|
794
|
+
},
|
|
795
|
+
set speed(speed: number) {
|
|
796
|
+
Playback.setSpeed(head, speed)
|
|
797
|
+
},
|
|
798
|
+
get duration() {
|
|
799
|
+
return durationNow()
|
|
800
|
+
},
|
|
801
|
+
get state() {
|
|
802
|
+
return state
|
|
803
|
+
},
|
|
804
|
+
get finished() {
|
|
805
|
+
if (Predicate.isUndefined(finished)) {
|
|
806
|
+
finished = Bool.match(isResolved, {
|
|
807
|
+
onTrue: () => Promise.resolve(),
|
|
808
|
+
onFalse: () =>
|
|
809
|
+
new Promise<void>((done) => {
|
|
810
|
+
resolve = done
|
|
811
|
+
}),
|
|
812
|
+
})
|
|
813
|
+
}
|
|
814
|
+
return finished
|
|
815
|
+
},
|
|
816
|
+
then: (onResolve, onReject) => controls.finished.then(onResolve, onReject),
|
|
817
|
+
play: () => {
|
|
818
|
+
if (stopped) return
|
|
819
|
+
if (state === "paused") {
|
|
820
|
+
Playback.play(head)
|
|
821
|
+
state = "running"
|
|
822
|
+
return
|
|
823
|
+
}
|
|
824
|
+
if (state === "running") return
|
|
825
|
+
// Again from its start: its end when it plays backwards.
|
|
826
|
+
fresh()
|
|
827
|
+
Playback.play(head)
|
|
828
|
+
Playback.seek(head, Bool.match(head.speed < 0, { onTrue: durationNow, onFalse: () => 0 }))
|
|
829
|
+
restart()
|
|
830
|
+
runAll()
|
|
831
|
+
},
|
|
832
|
+
pause: () => {
|
|
833
|
+
if (stopped) return
|
|
834
|
+
Playback.pause(head)
|
|
835
|
+
if (state === "running") state = "paused"
|
|
836
|
+
},
|
|
837
|
+
stop: () => {
|
|
838
|
+
stopped = true
|
|
839
|
+
Arr.forEach(tracks, (track) => track.stop())
|
|
840
|
+
end("idle", false)
|
|
841
|
+
},
|
|
842
|
+
cancel: () => {
|
|
843
|
+
Arr.forEach(tracks, (track) => track.cancel())
|
|
844
|
+
Playback.seek(head, 0)
|
|
845
|
+
end("idle", false)
|
|
846
|
+
},
|
|
847
|
+
complete: () => {
|
|
848
|
+
if (stopped) return
|
|
849
|
+
if (state !== "running") restart()
|
|
850
|
+
Playback.play(head)
|
|
851
|
+
Playback.seek(head, durationNow())
|
|
852
|
+
runAll()
|
|
853
|
+
},
|
|
854
|
+
}
|
|
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 => {
|
|
858
|
+
landed++
|
|
859
|
+
if (Bool.not(landedOne)) anyTaken = true
|
|
860
|
+
if (landed < tracks.length) return
|
|
861
|
+
if (anyTaken) {
|
|
862
|
+
stopped = true
|
|
863
|
+
return end("idle", false)
|
|
864
|
+
}
|
|
865
|
+
isResolved = true
|
|
866
|
+
UndefinedOr.map(resolve, (done) => done())
|
|
867
|
+
UndefinedOr.map(onComplete, (complete) => complete())
|
|
868
|
+
end("finished", true)
|
|
869
|
+
}
|
|
870
|
+
return { controls, settledOne }
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/** An animation of no Subject: finished at once (a server's, or a subject that matched none). */
|
|
874
|
+
export const finishedControls = (): WardicPlaybackControls => {
|
|
875
|
+
const done = Promise.resolve()
|
|
876
|
+
return {
|
|
877
|
+
time: 0,
|
|
878
|
+
speed: 1,
|
|
879
|
+
duration: 0,
|
|
880
|
+
state: "finished",
|
|
881
|
+
finished: done,
|
|
882
|
+
then: (onResolve, onReject) => done.then(onResolve, onReject),
|
|
883
|
+
play: constVoid,
|
|
884
|
+
pause: constVoid,
|
|
885
|
+
stop: constVoid,
|
|
886
|
+
cancel: constVoid,
|
|
887
|
+
complete: constVoid,
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
/**
|
|
892
|
+
* Where a Segment starts in a sequence: seconds in, `1.5`; after or before the end of the Segment
|
|
893
|
+
* before, `"+0.5"` or `"-0.5"`; at its start, `"<"`, or around it, `"<+0.5"` or `"<-0.5"`; or a label's
|
|
894
|
+
* name. Motion's `SequenceTime`.
|
|
895
|
+
*/
|
|
896
|
+
export type WardicSequenceTime = number | "<" | `+${number}` | `-${number}` | `<+${number}` | `<-${number}` | (string & {})
|
|
897
|
+
|
|
898
|
+
/** A label in a sequence: a name for where the Segment before ends, or `{ name, at }` for a time of its own. Motion's. */
|
|
899
|
+
export type WardicSequenceLabel = string | { readonly name: string; readonly at: WardicSequenceTime }
|
|
900
|
+
|
|
901
|
+
/**
|
|
902
|
+
* A Segment's options: a transition, with a value's own entries laid over it; its `delay`, a number or
|
|
903
|
+
* a function of each Subject's index and count, such as `stagger(0.05)`; and `at`, where it starts. A
|
|
904
|
+
* spring through two stops or fewer plays as an easing over its time to rest. Motion's.
|
|
905
|
+
*/
|
|
906
|
+
export type WardicSegmentOptions = WithoutDelay & EachDelay & {
|
|
907
|
+
readonly at?: WardicSequenceTime | undefined
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
/** A Segment's values: each a value or a list of keyframes, as a target's. */
|
|
911
|
+
export type WardicSegmentTarget = Omit<Config.WardicTargetValues, "transition">
|
|
912
|
+
|
|
913
|
+
/**
|
|
914
|
+
* A function Segment, `[callback, value?, options?]`: its callback is given a value each frame, from 0
|
|
915
|
+
* 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.
|
|
917
|
+
*/
|
|
918
|
+
export type WardicFunctionSegment =
|
|
919
|
+
| readonly [callback: (value: number) => void]
|
|
920
|
+
| 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]
|
|
923
|
+
|
|
924
|
+
/**
|
|
925
|
+
* A sequence's options: `duration`, the whole sequence scaled to it; `delay`; `repeat`, `repeatType`
|
|
926
|
+
* and `repeatDelay`; `defaultTransition`, each Segment's when it gives none (a 0.3 s `easeOut` tween
|
|
927
|
+
* otherwise); and `onComplete`, called as it lands. Motion's `SequenceOptions`.
|
|
928
|
+
*/
|
|
929
|
+
export interface WardicSequenceOptions {
|
|
930
|
+
readonly duration?: number | undefined
|
|
931
|
+
readonly delay?: number | undefined
|
|
932
|
+
readonly repeat?: number | undefined
|
|
933
|
+
readonly repeatType?: "loop" | "reverse" | "mirror" | undefined
|
|
934
|
+
readonly repeatDelay?: number | undefined
|
|
935
|
+
readonly defaultTransition?: Config.WardicTransition | undefined
|
|
936
|
+
readonly onComplete?: (() => void) | undefined
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
/**
|
|
940
|
+
* An animation of `count` tracks started (#433): its controls over them, through one Playhead, told
|
|
941
|
+
* to `watch`, each track run. Finished at once with none.
|
|
942
|
+
*/
|
|
943
|
+
const started = (
|
|
944
|
+
watch: AnimationWatch,
|
|
945
|
+
count: number,
|
|
946
|
+
onComplete: (() => void) | undefined,
|
|
947
|
+
tracksOf: (head: Playback.Playhead, settled: (landed: boolean) => void) => ReadonlyArray<Spine.ImperativeTrack>,
|
|
948
|
+
): WardicPlaybackControls => {
|
|
949
|
+
if (count === 0) return finishedControls()
|
|
950
|
+
const head = Playback.make()
|
|
951
|
+
let settledOne: (landed: boolean) => void = constVoid
|
|
952
|
+
const tracks = tracksOf(head, (landedOne) => settledOne(landedOne))
|
|
953
|
+
const made = controlsOf(tracks, head, watch, onComplete)
|
|
954
|
+
settledOne = made.settledOne
|
|
955
|
+
watch.started(made.controls)
|
|
956
|
+
Arr.forEach(tracks, (track) => track.run())
|
|
957
|
+
return made.controls
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/**
|
|
961
|
+
* An **Animation scope**'s `animate` (#433), over an Environment's {@link Ports}: `animate(subject,
|
|
962
|
+
* target, options?)`, a Batch's `animate(batch, target, options?)` and `animate(sequence, options?)`,
|
|
963
|
+
* each returning its Playback controls. `scope()` is handed to the Environment's {@link Subjects.resolve}
|
|
964
|
+
* (the DOM's selectors are looked up inside it); each animation is told to `watch` as it starts and
|
|
965
|
+
* ends. Every Subject's legs start in the call, on the loop by the end of its task. An Environment
|
|
966
|
+
* types its own overloads over it.
|
|
967
|
+
*/
|
|
968
|
+
export const make = <S extends object>(env: AnimateEnv, ports: Ports<S>, scope: () => unknown, watch: AnimationWatch) =>
|
|
969
|
+
(first: unknown, second?: unknown, third?: WardicAnimateOptions): WardicPlaybackControls => {
|
|
970
|
+
if (isSequence(first)) return sequenceOf(env, ports, scope(), watch, first, second as WardicSequenceOptions | undefined)
|
|
971
|
+
// A Batch, or a member's handle (#665): one track over its members, on the Batch's own run.
|
|
972
|
+
const batch = ports.batches.entryOf(first)
|
|
973
|
+
if (Predicate.isNotUndefined(batch)) {
|
|
974
|
+
const members = batch.binding.namedOf(batch)
|
|
975
|
+
return started(watch, 1, UndefinedOr.map(third, (given) => given.onComplete), (head, settled) => [
|
|
976
|
+
batchTrackOf(env, batch.binding, second as Readonly<Record<string, unknown>>, members, third, head, settled),
|
|
977
|
+
])
|
|
978
|
+
}
|
|
979
|
+
const subjects = ports.subjects.resolve(first, scope())
|
|
980
|
+
const target = second as Config.WardicVariant
|
|
981
|
+
const onComplete = UndefinedOr.map(third, (given) => given.onComplete)
|
|
982
|
+
// No member's Subject among them: each Subject's track, building no parts (bench:solid --batch's
|
|
983
|
+
// per-element page, a call per element: 8.2 -> 8.5 ms at 1,000 with the parts built for every call; #665).
|
|
984
|
+
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)))
|
|
987
|
+
}
|
|
988
|
+
// 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
|
+
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
|
+
])
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
// --- Sequences (#433's PR 2) ------------------------------------------------------------------
|
|
997
|
+
|
|
998
|
+
/** Whether `given` is a sequence rather than a subject (#433, Motion's `isSequence`): a list with a Segment, itself a list, in it; a list of Subjects has none. A binding whose `animate` is its own tells the forms apart by it (#798). */
|
|
999
|
+
export const isSequence = (given: unknown): given is ReadonlyArray<unknown> =>
|
|
1000
|
+
Bool.match(Arr.isArray(given), { onTrue: () => Arr.some(given as ReadonlyArray<unknown>, Arr.isArray), onFalse: () => false })
|
|
1001
|
+
|
|
1002
|
+
/** A function Segment's value (Motion's motion value): its own subject, its callback told each frame. */
|
|
1003
|
+
interface Callback {
|
|
1004
|
+
readonly call: (value: number | string) => void
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
/** Whether a sequence's subject is a function Segment's value. */
|
|
1008
|
+
const isCallback = (subject: unknown): subject is Callback => Predicate.hasProperty(subject, "call")
|
|
1009
|
+
|
|
1010
|
+
/** The key a function Segment's value animates under. */
|
|
1011
|
+
const VALUE = "value"
|
|
1012
|
+
|
|
1013
|
+
/** A function Segment's stops with none given (Motion's). */
|
|
1014
|
+
const ZERO_TO_ONE: ReadonlyArray<number> = [0, 1]
|
|
1015
|
+
|
|
1016
|
+
/** No fields. */
|
|
1017
|
+
const NO_FIELDS: Readonly<Record<string, unknown>> = {}
|
|
1018
|
+
|
|
1019
|
+
/**
|
|
1020
|
+
* What a Segment's transition doesn't take from the sequence's `defaultTransition` (Motion's): its
|
|
1021
|
+
* delay, times and repeat, which are each Segment's own, and a value's own transitions.
|
|
1022
|
+
*/
|
|
1023
|
+
const PLACING: ReadonlySet<string> = new Set(["delay", "times", "repeat", "repeatType", "repeatDelay", "reverse", "at"])
|
|
1024
|
+
|
|
1025
|
+
/** A Segment's default transition (Motion's): a 0.3 s `easeOut` tween. */
|
|
1026
|
+
const SEGMENT_TWEEN: Sequence.Transition = { type: "tween" }
|
|
1027
|
+
|
|
1028
|
+
/** The fields of a sequence's options laid over each key's animation: `duration`, `delay` and the repeat. */
|
|
1029
|
+
const WHOLE = ["duration", "delay", "repeat", "repeatType", "repeatDelay"] as const
|
|
1030
|
+
|
|
1031
|
+
/**
|
|
1032
|
+
* The fields of a sequence's `defaultTransition` laid over each key's animation, under its own
|
|
1033
|
+
* options' (Motion's): its delay and repeat. Its `duration` is a Segment's, never the sequence's.
|
|
1034
|
+
*/
|
|
1035
|
+
const WHOLE_DEFAULTS = ["delay", "repeat", "repeatType", "repeatDelay"] as const
|
|
1036
|
+
|
|
1037
|
+
/** `fields`' `keys`, those it has. */
|
|
1038
|
+
const picked = (fields: Readonly<Record<string, unknown>>, keys: ReadonlyArray<string>): Readonly<Record<string, unknown>> =>
|
|
1039
|
+
Rec.filter(fields, (value, key) => Bool.and(Arr.contains(keys, key), Predicate.isNotUndefined(value)))
|
|
1040
|
+
|
|
1041
|
+
/**
|
|
1042
|
+
* `key`'s transition in a Segment (Motion's `getValueTransition` for sequences): the Segment's,
|
|
1043
|
+
* the value's own laid over it, over the sequence's `defaultTransition` less its placing; a spring
|
|
1044
|
+
* or a tween as Wardic's transitions are (ADR-0051), Motion's 0.3 s `easeOut` tween when it defines
|
|
1045
|
+
* neither; its `delay` kept as given, each Subject's when it's a function.
|
|
1046
|
+
*/
|
|
1047
|
+
const segmentTransitionOf = (options: WardicSegmentOptions | undefined, key: string, defaults: Config.WardicTransition | undefined): Sequence.Transition => {
|
|
1048
|
+
const own = UndefinedOr.match(options as Readonly<Record<string, unknown>> | undefined, {
|
|
1049
|
+
onUndefined: () => NO_FIELDS,
|
|
1050
|
+
onDefined: (given) => ({ ...given, ...UndefinedOr.match(given[key], { onUndefined: () => NO_FIELDS, onDefined: (entry) => Bool.match(Predicate.isObject(entry), { onTrue: () => entry as Readonly<Record<string, unknown>>, onFalse: () => NO_FIELDS }) }) }),
|
|
1051
|
+
})
|
|
1052
|
+
const base = UndefinedOr.match(defaults as Readonly<Record<string, unknown>> | undefined, {
|
|
1053
|
+
onUndefined: () => NO_FIELDS,
|
|
1054
|
+
onDefined: (given) => Rec.filter(given, (_, field) => Bool.not(PLACING.has(field))),
|
|
1055
|
+
})
|
|
1056
|
+
const { delay, at: _at, ...fields } = own
|
|
1057
|
+
// An inertia (#435) isn't a sequence's: a Segment's time is placed on its timeline, and a glide has none to place.
|
|
1058
|
+
const run = UndefinedOr.match(RetargetLegs.runOf({ ...base, ...fields } as Config.WardicTransition), {
|
|
1059
|
+
onUndefined: () => SEGMENT_TWEEN,
|
|
1060
|
+
onDefined: (made) => Bool.match(made.type === "inertia", { onTrue: () => SEGMENT_TWEEN, onFalse: () => made as Sequence.Transition }),
|
|
1061
|
+
})
|
|
1062
|
+
return Bool.match(Predicate.isUndefined(delay), { onTrue: () => run, onFalse: () => ({ ...run, delay }) as Sequence.Transition })
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
/** 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))
|
|
1067
|
+
|
|
1068
|
+
/** A Segment's values: those that are a value, `null` or Keyframes. */
|
|
1069
|
+
const segmentValuesOf = (target: WardicSegmentTarget): Readonly<Record<string, Sequence.Stop | ReadonlyArray<Sequence.Stop>>> =>
|
|
1070
|
+
Rec.filter(target as Readonly<Record<string, unknown>>, isSegmentValue) as Readonly<Record<string, Sequence.Stop | ReadonlyArray<Sequence.Stop>>>
|
|
1071
|
+
|
|
1072
|
+
/** Each of `values`' keys' transition in a Segment. */
|
|
1073
|
+
const segmentTransitionsOf = (values: Readonly<Record<string, unknown>>, options: WardicSegmentOptions | undefined, defaults: Config.WardicTransition | undefined): Readonly<Record<string, Sequence.Transition>> =>
|
|
1074
|
+
Rec.map(values, (_, key) => segmentTransitionOf(options, key, defaults))
|
|
1075
|
+
|
|
1076
|
+
/** A Segment's `at`, when it gives one. */
|
|
1077
|
+
const atOf = (options: WardicSegmentOptions | undefined): Sequence.Time | undefined => UndefinedOr.map(options, (given) => given.at)
|
|
1078
|
+
|
|
1079
|
+
/**
|
|
1080
|
+
* A Segment's subjects (#665, Q40): each Subject, a member's as its member's entry, so its Batch keys
|
|
1081
|
+
* run in its Batch, as one subject with the member a Segment of the Batch names.
|
|
1082
|
+
*/
|
|
1083
|
+
const subjectsIn = <S extends object>(batches: Batches<S>, subjects: ReadonlyArray<S>, target: Readonly<Record<string, unknown>>): ReadonlyArray<S | BatchMember> => {
|
|
1084
|
+
if (Bool.not(batches.hasMembers(subjects))) return subjects
|
|
1085
|
+
return batches.segmentSubjectsOf(subjects, target)
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
/**
|
|
1089
|
+
* A sequence's steps for core (#433): labels as they are; a Segment of Subjects' Subjects (a string
|
|
1090
|
+
* the Environment resolves, a DOM selector, looked up once a sequence, inside `scope`), values and each
|
|
1091
|
+
* value's transition; a function Segment's own value, from 0 to 1 unless it gives its stops.
|
|
1092
|
+
*/
|
|
1093
|
+
const stepsOf = <S extends object>(ports: Ports<S>, sequence: ReadonlyArray<unknown>, scope: unknown, defaults: Config.WardicTransition | undefined): ReadonlyArray<Sequence.Step<S | Callback | BatchMember>> => {
|
|
1094
|
+
const named = new Map<string, ReadonlyArray<S>>()
|
|
1095
|
+
const subjectsOf = (subject: unknown): ReadonlyArray<S> => {
|
|
1096
|
+
if (Bool.not(Predicate.isString(subject))) return ports.subjects.resolve(subject, scope)
|
|
1097
|
+
const name = subject as string
|
|
1098
|
+
const known = named.get(name)
|
|
1099
|
+
if (Predicate.isNotUndefined(known)) return known
|
|
1100
|
+
const found = ports.subjects.resolve(name, scope)
|
|
1101
|
+
named.set(name, found)
|
|
1102
|
+
return found
|
|
1103
|
+
}
|
|
1104
|
+
return Arr.map(sequence, (segment): Sequence.Step<S | Callback | BatchMember> => {
|
|
1105
|
+
if (Predicate.isString(segment)) return segment
|
|
1106
|
+
if (Bool.not(Arr.isArray(segment))) return segment as { readonly name: string; readonly at: Sequence.Time }
|
|
1107
|
+
const parts = segment as ReadonlyArray<unknown>
|
|
1108
|
+
// A Batch, or a member's handle (#665, Q38, Q39): its members, each through its own stops in the Batch.
|
|
1109
|
+
const batch = ports.batches.entryOf(parts[0])
|
|
1110
|
+
if (Predicate.isNotUndefined(batch)) {
|
|
1111
|
+
const options = parts[2] as WardicSegmentOptions | undefined
|
|
1112
|
+
const { subjects, values } = batch.binding.segmentOf(batch.binding.namedOf(batch), parts[1] as Readonly<Record<string, unknown>>)
|
|
1113
|
+
return { subjects, target: values, transitions: segmentTransitionsOf(values, options, defaults), at: atOf(options) }
|
|
1114
|
+
}
|
|
1115
|
+
if (Predicate.isFunction(parts[0])) {
|
|
1116
|
+
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
|
|
1120
|
+
const target = { [VALUE]: given }
|
|
1121
|
+
return { subjects: [{ call }], target, transitions: segmentTransitionsOf(target, options, defaults), at: atOf(options) }
|
|
1122
|
+
}
|
|
1123
|
+
const [subject, values, options] = segment as readonly [unknown, WardicSegmentTarget, WardicSegmentOptions | undefined]
|
|
1124
|
+
const target = segmentValuesOf(values)
|
|
1125
|
+
return { subjects: subjectsIn(ports.batches, subjectsOf(subject), target), target, transitions: segmentTransitionsOf(target, options, defaults), at: atOf(options) }
|
|
1126
|
+
})
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
/**
|
|
1130
|
+
* Whether a Segment animates anything under `scope` (#798): a function Segment does, and a Batch's; a
|
|
1131
|
+
* Segment of Subjects when its subject names one; a label (a string, or `{ name, at }`) never. What a
|
|
1132
|
+
* binding asks of a sequence before it plays it, since one that animates nothing lands at once.
|
|
1133
|
+
*/
|
|
1134
|
+
export const segmentAnimates = <S extends object>(ports: Ports<S>, segment: unknown, scope: unknown): boolean => {
|
|
1135
|
+
if (Predicate.isString(segment)) return false
|
|
1136
|
+
if (Bool.not(Arr.isArray(segment))) return false
|
|
1137
|
+
const parts = segment as ReadonlyArray<unknown>
|
|
1138
|
+
if (Predicate.isNotUndefined(ports.batches.entryOf(parts[0]))) return true
|
|
1139
|
+
if (Predicate.isFunction(parts[0])) return true
|
|
1140
|
+
return ports.subjects.resolve(parts[0], scope).length > 0
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
/**
|
|
1144
|
+
* A function Segment's value animated (#433): its stops, a first `null` 0, through the
|
|
1145
|
+
* 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.
|
|
1147
|
+
*/
|
|
1148
|
+
const callbackTrackOf = (env: AnimateEnv, callback: Callback, animation: Sequence.Animation, head: Playback.Playhead, settled: (landed: boolean) => void): Spine.ImperativeTrack => {
|
|
1149
|
+
const stops = RetargetLegs.filledStops(UndefinedOr.getOrThrow(animation.keyframes[VALUE]), 0)
|
|
1150
|
+
const options = UndefinedOr.getOrThrow(animation.transition[VALUE])
|
|
1151
|
+
// Skipped (#468): its callback told the value it would end on, on the first tick.
|
|
1152
|
+
const machine = Bool.match(env.skipAnimations(), {
|
|
1153
|
+
onTrue: () => Effect.succeed(ReducedMotion.snap<Values.Values>({ [VALUE]: UndefinedOr.getOrThrow(stops[0]) }, { [VALUE]: ReducedMotion.endOf(UndefinedOr.getOrThrow(stops[0]), UndefinedOr.getOrThrow(stops[stops.length - 1]), options) })),
|
|
1154
|
+
onFalse: () => Machine.keyframesValues({ [VALUE]: stops }, options),
|
|
1155
|
+
})
|
|
1156
|
+
let fiber: Fiber.Fiber<unknown> | undefined
|
|
1157
|
+
const end = (): void => {
|
|
1158
|
+
if (Predicate.isUndefined(fiber)) return
|
|
1159
|
+
const running = fiber
|
|
1160
|
+
fiber = undefined
|
|
1161
|
+
// Nothing waits on its end: interrupted here, not by a fiber forked for it (#618, L2).
|
|
1162
|
+
if (Predicate.isUndefined(running.pollUnsafe())) running.interruptUnsafe()
|
|
1163
|
+
}
|
|
1164
|
+
const landed = (exit: Exit.Exit<unknown>): Effect.Effect<void> =>
|
|
1165
|
+
Exit.match(exit, {
|
|
1166
|
+
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) }),
|
|
1168
|
+
})
|
|
1169
|
+
const run = (): void => {
|
|
1170
|
+
end()
|
|
1171
|
+
fiber = env.runtime.runFork(
|
|
1172
|
+
machine.pipe(
|
|
1173
|
+
Effect.map(Playback.drive(head)),
|
|
1174
|
+
Effect.flatMap(Machine.run((frame) => {
|
|
1175
|
+
callback.call(UndefinedOr.getOrThrow(frame.value[VALUE]))
|
|
1176
|
+
return undefined
|
|
1177
|
+
})),
|
|
1178
|
+
Effect.onExit(landed),
|
|
1179
|
+
),
|
|
1180
|
+
)
|
|
1181
|
+
}
|
|
1182
|
+
return {
|
|
1183
|
+
run,
|
|
1184
|
+
stop: end,
|
|
1185
|
+
cancel: () => {
|
|
1186
|
+
end()
|
|
1187
|
+
callback.call(UndefinedOr.getOrThrow(stops[0]))
|
|
1188
|
+
},
|
|
1189
|
+
duration: () => Machine.durationOf(env.runtime.runSync(machine)),
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
/**
|
|
1194
|
+
* A sequence started (#433's PR 2, Motion's): compiled by core's `Sequence`, each Subject animated
|
|
1195
|
+
* through its Keyframes on its spine or its plain one, as a call animates it, and each function
|
|
1196
|
+
* Segment's value told to its callback; one Playhead for all, so it seeks, pauses and awaits as one.
|
|
1197
|
+
*/
|
|
1198
|
+
const sequenceOf = <S extends object>(env: AnimateEnv, ports: Ports<S>, scope: unknown, watch: AnimationWatch, sequence: ReadonlyArray<unknown>, options: WardicSequenceOptions | undefined): WardicPlaybackControls => {
|
|
1199
|
+
const defaults = UndefinedOr.map(options, (given) => given.defaultTransition)
|
|
1200
|
+
// The sequence's own options over its `defaultTransition`'s, as Motion's.
|
|
1201
|
+
const whole = {
|
|
1202
|
+
...picked(UndefinedOr.match(defaults as Readonly<Record<string, unknown>> | undefined, { onUndefined: () => NO_FIELDS, onDefined: identity }), WHOLE_DEFAULTS),
|
|
1203
|
+
...picked(UndefinedOr.match(options as Readonly<Record<string, unknown>> | undefined, { onUndefined: () => NO_FIELDS, onDefined: identity }), WHOLE),
|
|
1204
|
+
} as Sequence.Options
|
|
1205
|
+
const compiled = Arr.fromIterable(Sequence.compile(stepsOf(ports, sequence, scope, defaults), whole))
|
|
1206
|
+
// Each Batch's members as one track, their other keys their Subjects' (#665, Q40).
|
|
1207
|
+
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
|
+
])
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
/** `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). */
|
|
1219
|
+
export const alsoTold = (watch: AnimationWatch, also: AnimationWatch | undefined): AnimationWatch =>
|
|
1220
|
+
UndefinedOr.match(also, {
|
|
1221
|
+
onUndefined: () => watch,
|
|
1222
|
+
onDefined: (more) => ({
|
|
1223
|
+
started: (controls) => {
|
|
1224
|
+
watch.started(controls)
|
|
1225
|
+
more.started(controls)
|
|
1226
|
+
},
|
|
1227
|
+
ended: (controls, landed) => {
|
|
1228
|
+
watch.ended(controls, landed)
|
|
1229
|
+
more.ended(controls, landed)
|
|
1230
|
+
},
|
|
1231
|
+
}),
|
|
1232
|
+
})
|