@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,922 @@
|
|
|
1
|
+
import * as InterpolatorRegistry from "../InterpolatorRegistry.js";
|
|
2
|
+
import * as Machine from "../Machine.js";
|
|
3
|
+
import * as Playback from "../Playback.js";
|
|
4
|
+
import * as ReducedMotion from "../ReducedMotion.js";
|
|
5
|
+
import * as Sequence from "../Sequence.js";
|
|
6
|
+
import * as Report from "./Report.js";
|
|
7
|
+
import * as RetargetLegs from "./RetargetLegs.js";
|
|
8
|
+
import * as Spine from "./Spine.js";
|
|
9
|
+
import * as VariantTarget from "./VariantTarget.js";
|
|
10
|
+
import * as VariantTree from "./VariantTree.js";
|
|
11
|
+
import * as Arr from "effect/Array";
|
|
12
|
+
import * as Bool from "effect/Boolean";
|
|
13
|
+
import * as Cause from "effect/Cause";
|
|
14
|
+
import * as Effect from "effect/Effect";
|
|
15
|
+
import * as Exit from "effect/Exit";
|
|
16
|
+
import { constTrue, constVoid, identity } from "effect/Function";
|
|
17
|
+
import * as Predicate from "effect/Predicate";
|
|
18
|
+
import * as Rec from "effect/Record";
|
|
19
|
+
import * as UndefinedOr from "effect/UndefinedOr";
|
|
20
|
+
/** Each mounted Wardic element's spine (Motion's `visualElementStore`), by its Subject. */
|
|
21
|
+
// Module state (#460, Q7): each Wardic element's animatable, held weakly by its Subject.
|
|
22
|
+
const wardicElements = new WeakMap();
|
|
23
|
+
/**
|
|
24
|
+
* Register a mounted Wardic element's spine for the Imperative animations of its Subject (#433): until
|
|
25
|
+
* the returned function, which its unmount calls.
|
|
26
|
+
*/
|
|
27
|
+
export const registerAnimatable = (subject, spine) => {
|
|
28
|
+
wardicElements.set(subject, spine);
|
|
29
|
+
return () => {
|
|
30
|
+
if (wardicElements.get(subject) === spine)
|
|
31
|
+
wardicElements.delete(subject);
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
/** An Environment with no Batches. */
|
|
35
|
+
export const noBatches = {
|
|
36
|
+
entryOf: () => undefined,
|
|
37
|
+
hasMembers: () => false,
|
|
38
|
+
callPartsOf: (subjects, target) => ({ members: [], subjects: Arr.map(subjects, (subject, index) => ({ subject, index, target })) }),
|
|
39
|
+
segmentSubjectsOf: identity,
|
|
40
|
+
sequencePartsOf: (compiled) => ({ batches: [], others: compiled }),
|
|
41
|
+
};
|
|
42
|
+
/** Each gate's plain Subjects: a provider's own, kept while the Subject is. */
|
|
43
|
+
// Module state (#460, Q7): each gate's plain Subjects, held weakly by the gate.
|
|
44
|
+
const plainByGate = new WeakMap();
|
|
45
|
+
/** The plain Subjects' gate ids, counted. */
|
|
46
|
+
// Module state (#460, Q7): an id counter, unique across every gate.
|
|
47
|
+
let plainIds = 0;
|
|
48
|
+
/** No config: a plain Subject's. */
|
|
49
|
+
const NO_CONFIG = {};
|
|
50
|
+
/** `subject`'s plain spine under `env`'s gate, made at its first animation. */
|
|
51
|
+
const plainOf = (env, subjects, subject) => {
|
|
52
|
+
let bySubject = plainByGate.get(env.gate);
|
|
53
|
+
if (Predicate.isUndefined(bySubject)) {
|
|
54
|
+
bySubject = new WeakMap();
|
|
55
|
+
plainByGate.set(env.gate, bySubject);
|
|
56
|
+
}
|
|
57
|
+
const known = bySubject.get(subject);
|
|
58
|
+
if (Predicate.isNotUndefined(known))
|
|
59
|
+
return known;
|
|
60
|
+
const id = `wardic-animate-${plainIds++}`;
|
|
61
|
+
const snapshot = Spine.emptySnapshotOf({});
|
|
62
|
+
const spine = Spine.makeElementSpine({
|
|
63
|
+
runtime: env.runtime,
|
|
64
|
+
gate: env.gate,
|
|
65
|
+
id,
|
|
66
|
+
element: subject,
|
|
67
|
+
tag: undefined,
|
|
68
|
+
config: () => NO_CONFIG,
|
|
69
|
+
parent: undefined,
|
|
70
|
+
canStart: undefined,
|
|
71
|
+
extraDelay: undefined,
|
|
72
|
+
onFrame: undefined,
|
|
73
|
+
reducedMotion: env.reducedMotion,
|
|
74
|
+
positionalKeys: env.positionalKeys,
|
|
75
|
+
defaultTransition: env.defaultTransition,
|
|
76
|
+
ownValues: env.ownValues,
|
|
77
|
+
measuredPairs: env.measuredPairs,
|
|
78
|
+
variableValues: env.variableValues,
|
|
79
|
+
start: { initialValues: {}, initialLink: {}, startedLink: {}, quiet: false },
|
|
80
|
+
snapshot,
|
|
81
|
+
reuse: Spine.makeReuse(),
|
|
82
|
+
links: subjects.startLinksOf(subject),
|
|
83
|
+
styleNow: undefined,
|
|
84
|
+
live: undefined,
|
|
85
|
+
presence: undefined,
|
|
86
|
+
projected: false,
|
|
87
|
+
dragAxis: undefined,
|
|
88
|
+
handoff: undefined,
|
|
89
|
+
});
|
|
90
|
+
const plain = { spine, snapshot, id, running: 0, unregister: constVoid };
|
|
91
|
+
bySubject.set(subject, plain);
|
|
92
|
+
return plain;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* `transition` with `skipAnimations` when its scope's animations are skipped (#468, Motion's
|
|
96
|
+
* `useAnimate`, which hands its config's to each animation): a Wardic element's own config can skip it
|
|
97
|
+
* too, and a plain Subject has none.
|
|
98
|
+
*/
|
|
99
|
+
const skippedIn = (env, transition) => {
|
|
100
|
+
if (Bool.not(env.skipAnimations()))
|
|
101
|
+
return transition;
|
|
102
|
+
return { ...transition, skipAnimations: true };
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* The resolution an **Imperative animation**'s legs run under (#433): none of the Override stack's,
|
|
106
|
+
* so a Retarget that takes a key back never carries one as its own Keyframes or names.
|
|
107
|
+
*/
|
|
108
|
+
const IMPERATIVE = {
|
|
109
|
+
resolved: {},
|
|
110
|
+
base: {},
|
|
111
|
+
dragging: false,
|
|
112
|
+
variant: undefined,
|
|
113
|
+
active: { inView: false, focused: false, hovered: false, pressed: false, dragged: false, dragging: false },
|
|
114
|
+
transition: undefined,
|
|
115
|
+
layers: [],
|
|
116
|
+
names: VariantTree.NO_GESTURE_NAMES,
|
|
117
|
+
layersOn: VariantTree.NO_GESTURE_STATES,
|
|
118
|
+
ends: undefined,
|
|
119
|
+
keyframes: undefined,
|
|
120
|
+
change: undefined,
|
|
121
|
+
};
|
|
122
|
+
/** The target the Override stack had for each key at the spine's last Retarget: what a taken key is noted with. */
|
|
123
|
+
const lastTargetsOf = (host) => UndefinedOr.match(host.lastResolution(), { onUndefined: () => Spine.NO_VALUES, onDefined: (last) => last.resolved });
|
|
124
|
+
/**
|
|
125
|
+
* `key` taken by an Imperative animation from whatever runs it (#433): noted in `taking` with `noted`,
|
|
126
|
+
* the target the Override stack has for it now, no longer returning, its Timeline value stopped, given
|
|
127
|
+
* up by its owner, owned, and started where it's drawn toward `like`, noted in `started`.
|
|
128
|
+
*/
|
|
129
|
+
const takeKey = (host, taking, key, noted, like, started) => {
|
|
130
|
+
taking.set(key, noted);
|
|
131
|
+
host.returning().delete(key);
|
|
132
|
+
// Its Timeline value stops while the animation draws the key (#240).
|
|
133
|
+
const timelineKeys = host.timelineKeys();
|
|
134
|
+
if (Predicate.isNotUndefined(timelineKeys))
|
|
135
|
+
timelineKeys.taken(key);
|
|
136
|
+
const owner = host.owners.get(key);
|
|
137
|
+
if (Predicate.isNotUndefined(owner))
|
|
138
|
+
host.giveUp(key, owner);
|
|
139
|
+
if (!Object.hasOwn(host.initialValues, key))
|
|
140
|
+
host.ownValue(key);
|
|
141
|
+
host.startKey(key, like, started);
|
|
142
|
+
};
|
|
143
|
+
/** Whether a Transition end may set a key drawn by `owner`: none draws it, or its leg is done. */
|
|
144
|
+
const endFree = (owner) => {
|
|
145
|
+
if (Predicate.isUndefined(owner))
|
|
146
|
+
return true;
|
|
147
|
+
return owner.done;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* An **Imperative animation** of a Subject (#433), through its spine's `host`: it takes `target`'s
|
|
151
|
+
* keys from whatever runs them, each noted with the target the Override stack has for it now, so a
|
|
152
|
+
* Retarget leaves it where the animation leaves it until that target changes (Motion's latest-wins).
|
|
153
|
+
* Its legs start where the keys are drawn, with their velocity, on `given` (a key with none of its own
|
|
154
|
+
* on its Default transition), reduced motion as the provider says, and run through `head` once
|
|
155
|
+
* {@link Spine.ImperativeTrack.run} is called. Once they land, none taken, its target's Transition
|
|
156
|
+
* end is set (#896), and put back by a cancel or a run again.
|
|
157
|
+
*/
|
|
158
|
+
// #746 (ADR-0067, *Amendment (#746)*, decision 10): here, not in the spine, so an app that never animates
|
|
159
|
+
// imperatively ships none of it; the spine hands over what it takes (`SpineHost`).
|
|
160
|
+
const imperativeTrackOf = (host, target, given, head, hooks) => {
|
|
161
|
+
const { snapshot, owners, keepTheRest, element, positionalKeys, defaultTransition, unitScale, readVelocity, gate, forget, endLeg, live, own, legProgram, legEnded, turnBack, id, recordFrame, runtime, elementRules, measuredPairs, variableValues } = host;
|
|
162
|
+
/** A leg of an Imperative animation, forked as a Retarget's are; it lands on its track. */
|
|
163
|
+
// Data-first: a pipe's curried forms are two closures more for every leg (#618, H1).
|
|
164
|
+
const runImperative = (running, program) => {
|
|
165
|
+
const at = runs;
|
|
166
|
+
running.fiber = runtime.runFork(Effect.asVoid(Effect.onExit(program, (exit) => {
|
|
167
|
+
if (at === runs)
|
|
168
|
+
supersededAt(running);
|
|
169
|
+
return legEnded(running)(exit);
|
|
170
|
+
})));
|
|
171
|
+
};
|
|
172
|
+
const to = target.values;
|
|
173
|
+
const before = lastTargetsOf(host);
|
|
174
|
+
const started = {};
|
|
175
|
+
let measuring;
|
|
176
|
+
// Each key taken from whatever runs it, noted with its target now, and started where it's drawn.
|
|
177
|
+
// Its velocity read now (#464), as its legs are built.
|
|
178
|
+
const takenNow = host.taking();
|
|
179
|
+
// Where a Path's axis this target leaves out rests (#574), before its keys are taken below.
|
|
180
|
+
const rests = Spine.restsOf(owners, to);
|
|
181
|
+
Arr.forEach(Rec.keys(to), (key) => {
|
|
182
|
+
const like = UndefinedOr.getOrThrow(to[key]);
|
|
183
|
+
takeKey(host, takenNow, key, before[key], like, started);
|
|
184
|
+
if (measuredPairs.needs(key, UndefinedOr.getOrThrow(snapshot.values[key]), like)) {
|
|
185
|
+
if (Predicate.isUndefined(measuring))
|
|
186
|
+
measuring = [];
|
|
187
|
+
measuring.push(key);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
keepTheRest();
|
|
191
|
+
let from = Rec.map(to, (_, key) => UndefinedOr.getOrThrow(snapshot.values[key]));
|
|
192
|
+
let stops;
|
|
193
|
+
if (Predicate.isNotUndefined(target.keyframes)) {
|
|
194
|
+
const begun = RetargetLegs.keyframesFrom(from, to, target.keyframes);
|
|
195
|
+
from = begun.from;
|
|
196
|
+
stops = begun.stops;
|
|
197
|
+
}
|
|
198
|
+
const reduce = host.reducedMotion();
|
|
199
|
+
const skipped = host.skipAnimations();
|
|
200
|
+
let measured;
|
|
201
|
+
if (Bool.and(Bool.not(Bool.or(reduce, skipped)), Predicate.isNotUndefined(measuring)))
|
|
202
|
+
measured = Spine.measuredOf(measuredPairs, element, UndefinedOr.getOrThrow(measuring), from, to, snapshot.values, stops);
|
|
203
|
+
// Built once: a seek or a replay runs the same legs, from the same start, with the same velocities.
|
|
204
|
+
const built = RetargetLegs.legsOf(from, to, given, reduce, skipped, positionalKeys, defaultTransition, readVelocity, undefined, stops, measured, snapshot.values, rests, unitScale);
|
|
205
|
+
const variables = Spine.holdsVariable(variableValues, from, to, stops);
|
|
206
|
+
const legs = Arr.map(built, (leg) => RetargetLegs.mapMachine(leg, Playback.drive(head)));
|
|
207
|
+
let runnings = Spine.NO_RUNNING;
|
|
208
|
+
let left = 0;
|
|
209
|
+
const ends = target.transitionEnd;
|
|
210
|
+
// What its Transition end's keys were before it set them (Q5): `undefined` until it has.
|
|
211
|
+
let overwritten;
|
|
212
|
+
// Its runs, stops and cancels, counted: what a landing waiting on a flush checks it's still for.
|
|
213
|
+
let runs = 0;
|
|
214
|
+
// `values` drawn at once, at rest: newer than the legs that let them go (#464).
|
|
215
|
+
const setAtRest = (values) => {
|
|
216
|
+
snapshot.values = { ...snapshot.values, ...values };
|
|
217
|
+
snapshot.velocities = { ...snapshot.velocities, ...Rec.map(values, () => 0) };
|
|
218
|
+
const now = snapshot.now();
|
|
219
|
+
Arr.forEach(Rec.keys(values), (key) => {
|
|
220
|
+
snapshot.drawnAt[key] = now;
|
|
221
|
+
});
|
|
222
|
+
gate.write(id, values);
|
|
223
|
+
recordFrame(values);
|
|
224
|
+
};
|
|
225
|
+
/**
|
|
226
|
+
* Its **Transition end** set (#896, ADR-0061 *Amendment (#896)*, Q2 and Q4): each key no leg draws
|
|
227
|
+
* now taken as the keys it animates are, started where it's drawn, then set at once.
|
|
228
|
+
*/
|
|
229
|
+
// Each of `values`' keys `only` picks taken as the call took its own (`takeKey`), noted with the
|
|
230
|
+
// Override stack's targets now: an end's keys as it's set, a run's again as it starts (#951).
|
|
231
|
+
const takeEach = (values, only) => {
|
|
232
|
+
const takingNow = host.taking();
|
|
233
|
+
const targetsNow = lastTargetsOf(host);
|
|
234
|
+
const startedNow = {};
|
|
235
|
+
Arr.forEach(Rec.keys(values), (key) => {
|
|
236
|
+
if (only(key))
|
|
237
|
+
takeKey(host, takingNow, key, targetsNow[key], UndefinedOr.getOrThrow(values[key]), startedNow);
|
|
238
|
+
});
|
|
239
|
+
};
|
|
240
|
+
const writeEnds = (values) => {
|
|
241
|
+
const free = Rec.filter(values, (_, key) => endFree(owners.get(key)));
|
|
242
|
+
if (Rec.isEmptyRecord(free))
|
|
243
|
+
return;
|
|
244
|
+
takeEach(free, constTrue);
|
|
245
|
+
// Kept from an earlier landing a run from its end didn't put back: what they were before any.
|
|
246
|
+
if (Predicate.isUndefined(overwritten))
|
|
247
|
+
overwritten = Rec.map(free, (_, key) => UndefinedOr.getOrThrow(snapshot.values[key]));
|
|
248
|
+
setAtRest(free);
|
|
249
|
+
};
|
|
250
|
+
/**
|
|
251
|
+
* Its Transition end's keys put back as they were before it was set (Q5): at a cancel, a seek or a
|
|
252
|
+
* replay, each a key no leg has drawn since, a later call's or the element's own. The next landing
|
|
253
|
+
* sets them again.
|
|
254
|
+
*/
|
|
255
|
+
const putBack = () => {
|
|
256
|
+
if (Predicate.isUndefined(overwritten))
|
|
257
|
+
return;
|
|
258
|
+
// Its take left each without an owner: one since is a later animation's, which wins.
|
|
259
|
+
const back = Rec.filter(overwritten, (_, key) => Predicate.isUndefined(owners.get(key)));
|
|
260
|
+
overwritten = undefined;
|
|
261
|
+
if (Bool.not(Rec.isEmptyRecord(back)))
|
|
262
|
+
setAtRest(back);
|
|
263
|
+
};
|
|
264
|
+
// Whether its `i`th leg snaps (#468's skip, reduced motion's positional keys): it lands on its next
|
|
265
|
+
// frame, its keys landed from its start, so a take before then takes nothing in flight, as Motion's
|
|
266
|
+
// skipped value makes no animation. Its machine read only for a leg that lost a key.
|
|
267
|
+
const snaps = (i) => Machine.durationOf(machineOf(UndefinedOr.getOrThrow(built[i]))) === 0;
|
|
268
|
+
// A leg the element's own Retarget carried on, toward the same target, as its own (Q7): the call has lost it.
|
|
269
|
+
let carried = false;
|
|
270
|
+
// Taken (#896's Q3, #951): a key it animates taken from its leg mid-flight, by a later animation or the
|
|
271
|
+
// element's own target, or a leg carried off whole. A leg that lost one has fewer than it started
|
|
272
|
+
// with; one taken after its leg landed doesn't count, as Motion's, nor one that snaps.
|
|
273
|
+
const taken = () => {
|
|
274
|
+
if (carried)
|
|
275
|
+
return true;
|
|
276
|
+
return Arr.some(runnings, (running, i) => Bool.and(running.count < Rec.size(running.targets), Bool.not(snaps(i))));
|
|
277
|
+
};
|
|
278
|
+
// Landed: its Transition end set at the flush after its last leg's frame, then the landing told
|
|
279
|
+
// after the flush that draws it, so the landing includes it (ADR-0057 decision 4). Taken, it ends
|
|
280
|
+
// without landing, its end dropped, as Motion's call never resolves (ADR-0061 *Amendment (#951)*).
|
|
281
|
+
// A run, a stop or a cancel since makes each wait stale: a take reported synchronously, then a seek
|
|
282
|
+
// in the same task, or a stop between its end and its landing, tells none.
|
|
283
|
+
const landed = () => {
|
|
284
|
+
const at = runs;
|
|
285
|
+
if (taken()) {
|
|
286
|
+
return gate.afterFlush(() => {
|
|
287
|
+
if (at === runs)
|
|
288
|
+
hooks.settled(false);
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
if (Predicate.isUndefined(ends)) {
|
|
292
|
+
return gate.afterFlush(() => {
|
|
293
|
+
if (at === runs)
|
|
294
|
+
hooks.settled(true);
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
gate.afterFlush(() => {
|
|
298
|
+
if (at !== runs)
|
|
299
|
+
return;
|
|
300
|
+
writeEnds(ends);
|
|
301
|
+
gate.afterFlush(() => {
|
|
302
|
+
if (at === runs)
|
|
303
|
+
hooks.settled(true);
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
};
|
|
307
|
+
// A leg landed, or was Superseded (#951): the last lands the track, or finds it taken.
|
|
308
|
+
const legDone = () => {
|
|
309
|
+
left--;
|
|
310
|
+
if (left === 0)
|
|
311
|
+
landed();
|
|
312
|
+
};
|
|
313
|
+
const landing = {
|
|
314
|
+
report: constVoid,
|
|
315
|
+
settled: legDone,
|
|
316
|
+
failed: hooks.failed,
|
|
317
|
+
};
|
|
318
|
+
/**
|
|
319
|
+
* A leg of this run ended **Superseded** (#951), told at its end, as it settles nothing of this
|
|
320
|
+
* track's: carried on by the element's own Retarget as its own, its landing the Retarget's now (Q7);
|
|
321
|
+
* or with every key taken, a count of 0 (only a take lowers it), which `taken` reads. Either counts
|
|
322
|
+
* toward the track's landing, which finds it taken.
|
|
323
|
+
*/
|
|
324
|
+
// Here, not in the spine, so an app with no Imperative animation pays nothing for it.
|
|
325
|
+
const supersededAt = (running) => {
|
|
326
|
+
if (running.landing !== landing) {
|
|
327
|
+
carried = true;
|
|
328
|
+
return legDone();
|
|
329
|
+
}
|
|
330
|
+
if (running.count === 0)
|
|
331
|
+
legDone();
|
|
332
|
+
};
|
|
333
|
+
// Its legs ended where they are: the keys they draw are drawn by none.
|
|
334
|
+
const end = () => Arr.forEach(runnings, (running) => {
|
|
335
|
+
forget(running);
|
|
336
|
+
if (Bool.not(running.done))
|
|
337
|
+
endLeg(running);
|
|
338
|
+
});
|
|
339
|
+
// Whether a run starts at its end, going forward, its end set: it lands at once, so its end stays,
|
|
340
|
+
// with no frame without it (a seek to the end, `complete()`). Its duration read only then.
|
|
341
|
+
const fromItsEnd = () => {
|
|
342
|
+
if (Predicate.isUndefined(overwritten))
|
|
343
|
+
return false;
|
|
344
|
+
if (head.speed < 0)
|
|
345
|
+
return false;
|
|
346
|
+
return head.time >= duration();
|
|
347
|
+
};
|
|
348
|
+
// A key another leg draws: a later animation's, or the element's own.
|
|
349
|
+
const drawnByAnother = (key) => Predicate.isNotUndefined(owners.get(key));
|
|
350
|
+
const run = () => {
|
|
351
|
+
runs++;
|
|
352
|
+
end();
|
|
353
|
+
carried = false;
|
|
354
|
+
// Run again, it takes each of its keys from whatever draws it now, as the call took them (#951): one
|
|
355
|
+
// leg draws a key. The first run's are taken already.
|
|
356
|
+
if (runs > 1)
|
|
357
|
+
takeEach(to, drawnByAnother);
|
|
358
|
+
if (Bool.not(fromItsEnd()))
|
|
359
|
+
putBack();
|
|
360
|
+
left = legs.length;
|
|
361
|
+
Spine.tellStarted(live, legs);
|
|
362
|
+
runnings = Arr.map(legs, (leg) => {
|
|
363
|
+
const running = Spine.runningOf(leg.to, IMPERATIVE, landing, leg._tag);
|
|
364
|
+
own(running);
|
|
365
|
+
runImperative(running, legProgram(leg, running, variables));
|
|
366
|
+
return running;
|
|
367
|
+
});
|
|
368
|
+
// A target with no key lands at once.
|
|
369
|
+
if (left === 0)
|
|
370
|
+
landed();
|
|
371
|
+
};
|
|
372
|
+
const cancel = () => {
|
|
373
|
+
runs++;
|
|
374
|
+
end();
|
|
375
|
+
turnBack();
|
|
376
|
+
putBack();
|
|
377
|
+
// Put back at once, its keys where it started them.
|
|
378
|
+
setAtRest(from);
|
|
379
|
+
};
|
|
380
|
+
// Each leg's machine, run from its start to rest, apart from the Playhead, which it would move: a
|
|
381
|
+
// blend's built with the Subject's Key rules.
|
|
382
|
+
const duration = () => Arr.reduce(built, 0, (longest, leg) => Math.max(longest, Machine.durationOf(machineOf(leg))));
|
|
383
|
+
const machineOf = (leg) => {
|
|
384
|
+
if (leg._tag === "Blend")
|
|
385
|
+
return runtime.runSync(Effect.suspend(leg.machine).pipe(InterpolatorRegistry.withKeyRules(elementRules)));
|
|
386
|
+
return leg.machine();
|
|
387
|
+
};
|
|
388
|
+
const stop = () => {
|
|
389
|
+
runs++;
|
|
390
|
+
end();
|
|
391
|
+
turnBack();
|
|
392
|
+
};
|
|
393
|
+
return { run, stop, cancel, duration };
|
|
394
|
+
};
|
|
395
|
+
/**
|
|
396
|
+
* A call's transition on its target's own (#896, ADR-0061 *Amendment (#896)*, Q1): the options' fields
|
|
397
|
+
* laid over the target's, shallowly, as `inherit: true` lays a target's over its element's. Motion's
|
|
398
|
+
* `animate` reads the options alone.
|
|
399
|
+
*/
|
|
400
|
+
// Its own handed back as it is with no options, so the legs' decode cache (#66) holds for it. Its
|
|
401
|
+
// `inherit` has nothing above it in a call: the legs read none (`RetargetLegs`' `NOT_DEFINING`).
|
|
402
|
+
const overOwn = (own, given) => {
|
|
403
|
+
if (Predicate.isUndefined(own))
|
|
404
|
+
return given;
|
|
405
|
+
if (Predicate.isUndefined(given))
|
|
406
|
+
return own;
|
|
407
|
+
return { ...own, ...given };
|
|
408
|
+
};
|
|
409
|
+
/**
|
|
410
|
+
* A Subject's animation of `target`: through its spine when it's a Wardic element's, else through its
|
|
411
|
+
* plain spine, in the gate while any of its animations runs.
|
|
412
|
+
*/
|
|
413
|
+
const trackOf = (env, subjects, subject, target, given, head, settled) => {
|
|
414
|
+
const resolved = VariantTarget.resolveTarget(target, undefined, undefined, VariantTarget.NO_VARIANT_STATE);
|
|
415
|
+
const own = wardicElements.get(subject);
|
|
416
|
+
const hooks = { settled, failed: constVoid };
|
|
417
|
+
const transition = skippedIn(env, overOwn(resolved.transition, given));
|
|
418
|
+
if (Predicate.isNotUndefined(own))
|
|
419
|
+
return imperativeTrackOf(own.host(), resolved, transition, head, hooks);
|
|
420
|
+
const plain = plainOf(env, subjects, subject);
|
|
421
|
+
// In the gate while it runs; out once its last animation is done, after the flush of its last frame.
|
|
422
|
+
let live = false;
|
|
423
|
+
const enter = () => {
|
|
424
|
+
if (live)
|
|
425
|
+
return;
|
|
426
|
+
live = true;
|
|
427
|
+
// Back in the gate with every value it holds, so a transform it draws keeps the keys it isn't animating.
|
|
428
|
+
if (plain.running === 0) {
|
|
429
|
+
plain.unregister = env.gate.register(plain.id, { target: subject });
|
|
430
|
+
if (Bool.not(Rec.isEmptyRecord(plain.snapshot.values)))
|
|
431
|
+
env.gate.write(plain.id, plain.snapshot.values);
|
|
432
|
+
}
|
|
433
|
+
plain.running++;
|
|
434
|
+
};
|
|
435
|
+
const leave = () => {
|
|
436
|
+
if (Bool.not(live))
|
|
437
|
+
return;
|
|
438
|
+
live = false;
|
|
439
|
+
plain.running--;
|
|
440
|
+
if (plain.running === 0)
|
|
441
|
+
env.gate.afterFlush(() => {
|
|
442
|
+
if (plain.running === 0)
|
|
443
|
+
plain.unregister();
|
|
444
|
+
});
|
|
445
|
+
};
|
|
446
|
+
const track = imperativeTrackOf(plain.spine.host(), resolved, transition, head, {
|
|
447
|
+
settled: (landed) => {
|
|
448
|
+
leave();
|
|
449
|
+
settled(landed);
|
|
450
|
+
},
|
|
451
|
+
failed: () => leave(),
|
|
452
|
+
});
|
|
453
|
+
return {
|
|
454
|
+
run: () => {
|
|
455
|
+
enter();
|
|
456
|
+
track.run();
|
|
457
|
+
},
|
|
458
|
+
stop: () => {
|
|
459
|
+
track.stop();
|
|
460
|
+
leave();
|
|
461
|
+
},
|
|
462
|
+
cancel: () => {
|
|
463
|
+
enter();
|
|
464
|
+
track.cancel();
|
|
465
|
+
leave();
|
|
466
|
+
},
|
|
467
|
+
duration: track.duration,
|
|
468
|
+
};
|
|
469
|
+
};
|
|
470
|
+
/** A call's options without its `onComplete`: its transition. */
|
|
471
|
+
const withoutComplete = (given) => {
|
|
472
|
+
const { onComplete: _onComplete, ...rest } = given;
|
|
473
|
+
return rest;
|
|
474
|
+
};
|
|
475
|
+
/** Each Subject's transition: its `delay` worked out for it (Motion's `delay(i, n)`). */
|
|
476
|
+
const transitionFor = (options, index, count) => UndefinedOr.map(options, (given) => {
|
|
477
|
+
const { delay, ...rest } = withoutComplete(given);
|
|
478
|
+
if (Predicate.isFunction(delay))
|
|
479
|
+
return { ...rest, delay: delay(index, count) };
|
|
480
|
+
if (Predicate.isUndefined(delay))
|
|
481
|
+
return rest;
|
|
482
|
+
return { ...rest, delay };
|
|
483
|
+
});
|
|
484
|
+
/** A Batch call's transition (#665): the options without `onComplete`, a `stagger()` delay kept for the Batch to count its movers. */
|
|
485
|
+
const batchTransitionOf = (options) => UndefinedOr.map(options, (given) => withoutComplete(given));
|
|
486
|
+
/** A Batch call's track over `members` (every member when left out), driven by `head`: the call's options its transition. */
|
|
487
|
+
const batchTrackOf = (env, binding, target, members, options, head, settled) => binding.track({
|
|
488
|
+
target,
|
|
489
|
+
transition: batchTransitionOf(options),
|
|
490
|
+
reducedMotion: env.reducedMotion(),
|
|
491
|
+
skipAnimations: skipsBatchCall(env, options),
|
|
492
|
+
head,
|
|
493
|
+
settled: () => settled(true),
|
|
494
|
+
taken: () => settled(false),
|
|
495
|
+
members,
|
|
496
|
+
});
|
|
497
|
+
/** Whether a Batch call lands at once: its scope's animations are skipped, or its own `skipAnimations` (#468). */
|
|
498
|
+
const skipsBatchCall = (env, options) => {
|
|
499
|
+
if (env.skipAnimations())
|
|
500
|
+
return true;
|
|
501
|
+
return UndefinedOr.match(options, { onUndefined: () => false, onDefined: (given) => given.skipAnimations === true });
|
|
502
|
+
};
|
|
503
|
+
/** Playback controls over `tracks`, all run through `head`, started now. */
|
|
504
|
+
const controlsOf = (tracks, head, watch, onComplete) => {
|
|
505
|
+
let state = "running";
|
|
506
|
+
let stopped = false;
|
|
507
|
+
let landed = 0;
|
|
508
|
+
// Whether a Subject's part ended without landing, a key of it taken by a later animation (#951).
|
|
509
|
+
let anyTaken = false;
|
|
510
|
+
let resolve;
|
|
511
|
+
let finished;
|
|
512
|
+
let isResolved = false;
|
|
513
|
+
let duration;
|
|
514
|
+
const durationNow = () => {
|
|
515
|
+
if (Predicate.isUndefined(duration))
|
|
516
|
+
duration = Arr.reduce(tracks, 0, (longest, track) => Math.max(longest, track.duration()));
|
|
517
|
+
return duration;
|
|
518
|
+
};
|
|
519
|
+
const runAll = () => {
|
|
520
|
+
landed = 0;
|
|
521
|
+
anyTaken = false;
|
|
522
|
+
Arr.forEach(tracks, (track) => track.run());
|
|
523
|
+
};
|
|
524
|
+
const fresh = () => {
|
|
525
|
+
finished = undefined;
|
|
526
|
+
resolve = undefined;
|
|
527
|
+
isResolved = false;
|
|
528
|
+
};
|
|
529
|
+
const end = (how, landedNow) => {
|
|
530
|
+
const was = state;
|
|
531
|
+
state = how;
|
|
532
|
+
if (Bool.or(was === "running", was === "paused"))
|
|
533
|
+
watch.ended(controls, landedNow);
|
|
534
|
+
};
|
|
535
|
+
const restart = () => {
|
|
536
|
+
const was = state;
|
|
537
|
+
state = "running";
|
|
538
|
+
if (Bool.or(was === "finished", was === "idle"))
|
|
539
|
+
watch.started(controls);
|
|
540
|
+
};
|
|
541
|
+
const controls = {
|
|
542
|
+
get time() {
|
|
543
|
+
return head.time;
|
|
544
|
+
},
|
|
545
|
+
set time(at) {
|
|
546
|
+
Playback.seek(head, at);
|
|
547
|
+
// A seek of one that isn't running holds where it goes, as Motion's.
|
|
548
|
+
if (state !== "running") {
|
|
549
|
+
Playback.pause(head);
|
|
550
|
+
if (Bool.or(state === "finished", state === "idle")) {
|
|
551
|
+
fresh();
|
|
552
|
+
watch.started(controls);
|
|
553
|
+
}
|
|
554
|
+
state = "paused";
|
|
555
|
+
}
|
|
556
|
+
runAll();
|
|
557
|
+
},
|
|
558
|
+
get speed() {
|
|
559
|
+
return head.speed;
|
|
560
|
+
},
|
|
561
|
+
set speed(speed) {
|
|
562
|
+
Playback.setSpeed(head, speed);
|
|
563
|
+
},
|
|
564
|
+
get duration() {
|
|
565
|
+
return durationNow();
|
|
566
|
+
},
|
|
567
|
+
get state() {
|
|
568
|
+
return state;
|
|
569
|
+
},
|
|
570
|
+
get finished() {
|
|
571
|
+
if (Predicate.isUndefined(finished)) {
|
|
572
|
+
finished = Bool.match(isResolved, {
|
|
573
|
+
onTrue: () => Promise.resolve(),
|
|
574
|
+
onFalse: () => new Promise((done) => {
|
|
575
|
+
resolve = done;
|
|
576
|
+
}),
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
return finished;
|
|
580
|
+
},
|
|
581
|
+
then: (onResolve, onReject) => controls.finished.then(onResolve, onReject),
|
|
582
|
+
play: () => {
|
|
583
|
+
if (stopped)
|
|
584
|
+
return;
|
|
585
|
+
if (state === "paused") {
|
|
586
|
+
Playback.play(head);
|
|
587
|
+
state = "running";
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
if (state === "running")
|
|
591
|
+
return;
|
|
592
|
+
// Again from its start: its end when it plays backwards.
|
|
593
|
+
fresh();
|
|
594
|
+
Playback.play(head);
|
|
595
|
+
Playback.seek(head, Bool.match(head.speed < 0, { onTrue: durationNow, onFalse: () => 0 }));
|
|
596
|
+
restart();
|
|
597
|
+
runAll();
|
|
598
|
+
},
|
|
599
|
+
pause: () => {
|
|
600
|
+
if (stopped)
|
|
601
|
+
return;
|
|
602
|
+
Playback.pause(head);
|
|
603
|
+
if (state === "running")
|
|
604
|
+
state = "paused";
|
|
605
|
+
},
|
|
606
|
+
stop: () => {
|
|
607
|
+
stopped = true;
|
|
608
|
+
Arr.forEach(tracks, (track) => track.stop());
|
|
609
|
+
end("idle", false);
|
|
610
|
+
},
|
|
611
|
+
cancel: () => {
|
|
612
|
+
Arr.forEach(tracks, (track) => track.cancel());
|
|
613
|
+
Playback.seek(head, 0);
|
|
614
|
+
end("idle", false);
|
|
615
|
+
},
|
|
616
|
+
complete: () => {
|
|
617
|
+
if (stopped)
|
|
618
|
+
return;
|
|
619
|
+
if (state !== "running")
|
|
620
|
+
restart();
|
|
621
|
+
Playback.play(head);
|
|
622
|
+
Playback.seek(head, durationNow());
|
|
623
|
+
runAll();
|
|
624
|
+
},
|
|
625
|
+
};
|
|
626
|
+
// Each Subject's legs landed, or ended with a key taken: the last lands the animation, or, with one
|
|
627
|
+
// taken, ends it without landing, as a stop does (#951, Q1 and Q5). Motion's never resolves then.
|
|
628
|
+
const settledOne = (landedOne) => {
|
|
629
|
+
landed++;
|
|
630
|
+
if (Bool.not(landedOne))
|
|
631
|
+
anyTaken = true;
|
|
632
|
+
if (landed < tracks.length)
|
|
633
|
+
return;
|
|
634
|
+
if (anyTaken) {
|
|
635
|
+
stopped = true;
|
|
636
|
+
return end("idle", false);
|
|
637
|
+
}
|
|
638
|
+
isResolved = true;
|
|
639
|
+
UndefinedOr.map(resolve, (done) => done());
|
|
640
|
+
UndefinedOr.map(onComplete, (complete) => complete());
|
|
641
|
+
end("finished", true);
|
|
642
|
+
};
|
|
643
|
+
return { controls, settledOne };
|
|
644
|
+
};
|
|
645
|
+
/** An animation of no Subject: finished at once (a server's, or a subject that matched none). */
|
|
646
|
+
export const finishedControls = () => {
|
|
647
|
+
const done = Promise.resolve();
|
|
648
|
+
return {
|
|
649
|
+
time: 0,
|
|
650
|
+
speed: 1,
|
|
651
|
+
duration: 0,
|
|
652
|
+
state: "finished",
|
|
653
|
+
finished: done,
|
|
654
|
+
then: (onResolve, onReject) => done.then(onResolve, onReject),
|
|
655
|
+
play: constVoid,
|
|
656
|
+
pause: constVoid,
|
|
657
|
+
stop: constVoid,
|
|
658
|
+
cancel: constVoid,
|
|
659
|
+
complete: constVoid,
|
|
660
|
+
};
|
|
661
|
+
};
|
|
662
|
+
/**
|
|
663
|
+
* An animation of `count` tracks started (#433): its controls over them, through one Playhead, told
|
|
664
|
+
* to `watch`, each track run. Finished at once with none.
|
|
665
|
+
*/
|
|
666
|
+
const started = (watch, count, onComplete, tracksOf) => {
|
|
667
|
+
if (count === 0)
|
|
668
|
+
return finishedControls();
|
|
669
|
+
const head = Playback.make();
|
|
670
|
+
let settledOne = constVoid;
|
|
671
|
+
const tracks = tracksOf(head, (landedOne) => settledOne(landedOne));
|
|
672
|
+
const made = controlsOf(tracks, head, watch, onComplete);
|
|
673
|
+
settledOne = made.settledOne;
|
|
674
|
+
watch.started(made.controls);
|
|
675
|
+
Arr.forEach(tracks, (track) => track.run());
|
|
676
|
+
return made.controls;
|
|
677
|
+
};
|
|
678
|
+
/**
|
|
679
|
+
* An **Animation scope**'s `animate` (#433), over an Environment's {@link Ports}: `animate(subject,
|
|
680
|
+
* target, options?)`, a Batch's `animate(batch, target, options?)` and `animate(sequence, options?)`,
|
|
681
|
+
* each returning its Playback controls. `scope()` is handed to the Environment's {@link Subjects.resolve}
|
|
682
|
+
* (the DOM's selectors are looked up inside it); each animation is told to `watch` as it starts and
|
|
683
|
+
* ends. Every Subject's legs start in the call, on the loop by the end of its task. An Environment
|
|
684
|
+
* types its own overloads over it.
|
|
685
|
+
*/
|
|
686
|
+
export const make = (env, ports, scope, watch) => (first, second, third) => {
|
|
687
|
+
if (isSequence(first))
|
|
688
|
+
return sequenceOf(env, ports, scope(), watch, first, second);
|
|
689
|
+
// A Batch, or a member's handle (#665): one track over its members, on the Batch's own run.
|
|
690
|
+
const batch = ports.batches.entryOf(first);
|
|
691
|
+
if (Predicate.isNotUndefined(batch)) {
|
|
692
|
+
const members = batch.binding.namedOf(batch);
|
|
693
|
+
return started(watch, 1, UndefinedOr.map(third, (given) => given.onComplete), (head, settled) => [
|
|
694
|
+
batchTrackOf(env, batch.binding, second, members, third, head, settled),
|
|
695
|
+
]);
|
|
696
|
+
}
|
|
697
|
+
const subjects = ports.subjects.resolve(first, scope());
|
|
698
|
+
const target = second;
|
|
699
|
+
const onComplete = UndefinedOr.map(third, (given) => given.onComplete);
|
|
700
|
+
// No member's Subject among them: each Subject's track, building no parts (bench:solid --batch's
|
|
701
|
+
// per-element page, a call per element: 8.2 -> 8.5 ms at 1,000 with the parts built for every call; #665).
|
|
702
|
+
if (Bool.not(ports.batches.hasMembers(subjects))) {
|
|
703
|
+
return started(watch, subjects.length, onComplete, (head, settled) => Arr.map(subjects, (subject, index) => trackOf(env, ports.subjects, subject, target, transitionFor(third, index, subjects.length), head, settled)));
|
|
704
|
+
}
|
|
705
|
+
// 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).
|
|
706
|
+
const parts = ports.batches.callPartsOf(subjects, target);
|
|
707
|
+
return started(watch, parts.members.length + parts.subjects.length, onComplete, (head, settled) => [
|
|
708
|
+
...Arr.map(parts.members, (part) => batchTrackOf(env, part.binding, part.target, part.members, third, head, settled)),
|
|
709
|
+
...Arr.map(parts.subjects, (part) => trackOf(env, ports.subjects, part.subject, part.target, transitionFor(third, part.index, subjects.length), head, settled)),
|
|
710
|
+
]);
|
|
711
|
+
};
|
|
712
|
+
// --- Sequences (#433's PR 2) ------------------------------------------------------------------
|
|
713
|
+
/** 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). */
|
|
714
|
+
export const isSequence = (given) => Bool.match(Arr.isArray(given), { onTrue: () => Arr.some(given, Arr.isArray), onFalse: () => false });
|
|
715
|
+
/** Whether a sequence's subject is a function Segment's value. */
|
|
716
|
+
const isCallback = (subject) => Predicate.hasProperty(subject, "call");
|
|
717
|
+
/** The key a function Segment's value animates under. */
|
|
718
|
+
const VALUE = "value";
|
|
719
|
+
/** A function Segment's stops with none given (Motion's). */
|
|
720
|
+
const ZERO_TO_ONE = [0, 1];
|
|
721
|
+
/** No fields. */
|
|
722
|
+
const NO_FIELDS = {};
|
|
723
|
+
/**
|
|
724
|
+
* What a Segment's transition doesn't take from the sequence's `defaultTransition` (Motion's): its
|
|
725
|
+
* delay, times and repeat, which are each Segment's own, and a value's own transitions.
|
|
726
|
+
*/
|
|
727
|
+
const PLACING = new Set(["delay", "times", "repeat", "repeatType", "repeatDelay", "reverse", "at"]);
|
|
728
|
+
/** A Segment's default transition (Motion's): a 0.3 s `easeOut` tween. */
|
|
729
|
+
const SEGMENT_TWEEN = { type: "tween" };
|
|
730
|
+
/** The fields of a sequence's options laid over each key's animation: `duration`, `delay` and the repeat. */
|
|
731
|
+
const WHOLE = ["duration", "delay", "repeat", "repeatType", "repeatDelay"];
|
|
732
|
+
/**
|
|
733
|
+
* The fields of a sequence's `defaultTransition` laid over each key's animation, under its own
|
|
734
|
+
* options' (Motion's): its delay and repeat. Its `duration` is a Segment's, never the sequence's.
|
|
735
|
+
*/
|
|
736
|
+
const WHOLE_DEFAULTS = ["delay", "repeat", "repeatType", "repeatDelay"];
|
|
737
|
+
/** `fields`' `keys`, those it has. */
|
|
738
|
+
const picked = (fields, keys) => Rec.filter(fields, (value, key) => Bool.and(Arr.contains(keys, key), Predicate.isNotUndefined(value)));
|
|
739
|
+
/**
|
|
740
|
+
* `key`'s transition in a Segment (Motion's `getValueTransition` for sequences): the Segment's,
|
|
741
|
+
* the value's own laid over it, over the sequence's `defaultTransition` less its placing; a spring
|
|
742
|
+
* or a tween as Wardic's transitions are (ADR-0051), Motion's 0.3 s `easeOut` tween when it defines
|
|
743
|
+
* neither; its `delay` kept as given, each Subject's when it's a function.
|
|
744
|
+
*/
|
|
745
|
+
const segmentTransitionOf = (options, key, defaults) => {
|
|
746
|
+
const own = UndefinedOr.match(options, {
|
|
747
|
+
onUndefined: () => NO_FIELDS,
|
|
748
|
+
onDefined: (given) => ({ ...given, ...UndefinedOr.match(given[key], { onUndefined: () => NO_FIELDS, onDefined: (entry) => Bool.match(Predicate.isObject(entry), { onTrue: () => entry, onFalse: () => NO_FIELDS }) }) }),
|
|
749
|
+
});
|
|
750
|
+
const base = UndefinedOr.match(defaults, {
|
|
751
|
+
onUndefined: () => NO_FIELDS,
|
|
752
|
+
onDefined: (given) => Rec.filter(given, (_, field) => Bool.not(PLACING.has(field))),
|
|
753
|
+
});
|
|
754
|
+
const { delay, at: _at, ...fields } = own;
|
|
755
|
+
// An inertia (#435) isn't a sequence's: a Segment's time is placed on its timeline, and a glide has none to place.
|
|
756
|
+
const run = UndefinedOr.match(RetargetLegs.runOf({ ...base, ...fields }), {
|
|
757
|
+
onUndefined: () => SEGMENT_TWEEN,
|
|
758
|
+
onDefined: (made) => Bool.match(made.type === "inertia", { onTrue: () => SEGMENT_TWEEN, onFalse: () => made }),
|
|
759
|
+
});
|
|
760
|
+
return Bool.match(Predicate.isUndefined(delay), { onTrue: () => run, onFalse: () => ({ ...run, delay }) });
|
|
761
|
+
};
|
|
762
|
+
/** Whether a Segment's value is one: a number, a string, `null` or Keyframes. */
|
|
763
|
+
const isSegmentValue = /*#__PURE__*/ Predicate.or(/*#__PURE__*/ Predicate.or(Predicate.isNumber, Predicate.isString), /*#__PURE__*/ Predicate.or(Predicate.isNull, Arr.isArray));
|
|
764
|
+
/** A Segment's values: those that are a value, `null` or Keyframes. */
|
|
765
|
+
const segmentValuesOf = (target) => Rec.filter(target, isSegmentValue);
|
|
766
|
+
/** Each of `values`' keys' transition in a Segment. */
|
|
767
|
+
const segmentTransitionsOf = (values, options, defaults) => Rec.map(values, (_, key) => segmentTransitionOf(options, key, defaults));
|
|
768
|
+
/** A Segment's `at`, when it gives one. */
|
|
769
|
+
const atOf = (options) => UndefinedOr.map(options, (given) => given.at);
|
|
770
|
+
/**
|
|
771
|
+
* A Segment's subjects (#665, Q40): each Subject, a member's as its member's entry, so its Batch keys
|
|
772
|
+
* run in its Batch, as one subject with the member a Segment of the Batch names.
|
|
773
|
+
*/
|
|
774
|
+
const subjectsIn = (batches, subjects, target) => {
|
|
775
|
+
if (Bool.not(batches.hasMembers(subjects)))
|
|
776
|
+
return subjects;
|
|
777
|
+
return batches.segmentSubjectsOf(subjects, target);
|
|
778
|
+
};
|
|
779
|
+
/**
|
|
780
|
+
* A sequence's steps for core (#433): labels as they are; a Segment of Subjects' Subjects (a string
|
|
781
|
+
* the Environment resolves, a DOM selector, looked up once a sequence, inside `scope`), values and each
|
|
782
|
+
* value's transition; a function Segment's own value, from 0 to 1 unless it gives its stops.
|
|
783
|
+
*/
|
|
784
|
+
const stepsOf = (ports, sequence, scope, defaults) => {
|
|
785
|
+
const named = new Map();
|
|
786
|
+
const subjectsOf = (subject) => {
|
|
787
|
+
if (Bool.not(Predicate.isString(subject)))
|
|
788
|
+
return ports.subjects.resolve(subject, scope);
|
|
789
|
+
const name = subject;
|
|
790
|
+
const known = named.get(name);
|
|
791
|
+
if (Predicate.isNotUndefined(known))
|
|
792
|
+
return known;
|
|
793
|
+
const found = ports.subjects.resolve(name, scope);
|
|
794
|
+
named.set(name, found);
|
|
795
|
+
return found;
|
|
796
|
+
};
|
|
797
|
+
return Arr.map(sequence, (segment) => {
|
|
798
|
+
if (Predicate.isString(segment))
|
|
799
|
+
return segment;
|
|
800
|
+
if (Bool.not(Arr.isArray(segment)))
|
|
801
|
+
return segment;
|
|
802
|
+
const parts = segment;
|
|
803
|
+
// A Batch, or a member's handle (#665, Q38, Q39): its members, each through its own stops in the Batch.
|
|
804
|
+
const batch = ports.batches.entryOf(parts[0]);
|
|
805
|
+
if (Predicate.isNotUndefined(batch)) {
|
|
806
|
+
const options = parts[2];
|
|
807
|
+
const { subjects, values } = batch.binding.segmentOf(batch.binding.namedOf(batch), parts[1]);
|
|
808
|
+
return { subjects, target: values, transitions: segmentTransitionsOf(values, options, defaults), at: atOf(options) };
|
|
809
|
+
}
|
|
810
|
+
if (Predicate.isFunction(parts[0])) {
|
|
811
|
+
const call = parts[0];
|
|
812
|
+
const three = parts.length === 3;
|
|
813
|
+
const given = Bool.match(three, { onTrue: () => parts[1], onFalse: () => ZERO_TO_ONE });
|
|
814
|
+
const options = Bool.match(three, { onTrue: () => parts[2], onFalse: () => parts[1] });
|
|
815
|
+
const target = { [VALUE]: given };
|
|
816
|
+
return { subjects: [{ call }], target, transitions: segmentTransitionsOf(target, options, defaults), at: atOf(options) };
|
|
817
|
+
}
|
|
818
|
+
const [subject, values, options] = segment;
|
|
819
|
+
const target = segmentValuesOf(values);
|
|
820
|
+
return { subjects: subjectsIn(ports.batches, subjectsOf(subject), target), target, transitions: segmentTransitionsOf(target, options, defaults), at: atOf(options) };
|
|
821
|
+
});
|
|
822
|
+
};
|
|
823
|
+
/**
|
|
824
|
+
* Whether a Segment animates anything under `scope` (#798): a function Segment does, and a Batch's; a
|
|
825
|
+
* Segment of Subjects when its subject names one; a label (a string, or `{ name, at }`) never. What a
|
|
826
|
+
* binding asks of a sequence before it plays it, since one that animates nothing lands at once.
|
|
827
|
+
*/
|
|
828
|
+
export const segmentAnimates = (ports, segment, scope) => {
|
|
829
|
+
if (Predicate.isString(segment))
|
|
830
|
+
return false;
|
|
831
|
+
if (Bool.not(Arr.isArray(segment)))
|
|
832
|
+
return false;
|
|
833
|
+
const parts = segment;
|
|
834
|
+
if (Predicate.isNotUndefined(ports.batches.entryOf(parts[0])))
|
|
835
|
+
return true;
|
|
836
|
+
if (Predicate.isFunction(parts[0]))
|
|
837
|
+
return true;
|
|
838
|
+
return ports.subjects.resolve(parts[0], scope).length > 0;
|
|
839
|
+
};
|
|
840
|
+
/**
|
|
841
|
+
* A function Segment's value animated (#433): its stops, a first `null` 0, through the
|
|
842
|
+
* Interpolators on its tween over the sequence, each frame told to its callback. Cancelled, the
|
|
843
|
+
* callback hears its first stop again, as Motion's motion value is set back.
|
|
844
|
+
*/
|
|
845
|
+
const callbackTrackOf = (env, callback, animation, head, settled) => {
|
|
846
|
+
const stops = RetargetLegs.filledStops(UndefinedOr.getOrThrow(animation.keyframes[VALUE]), 0);
|
|
847
|
+
const options = UndefinedOr.getOrThrow(animation.transition[VALUE]);
|
|
848
|
+
// Skipped (#468): its callback told the value it would end on, on the first tick.
|
|
849
|
+
const machine = Bool.match(env.skipAnimations(), {
|
|
850
|
+
onTrue: () => Effect.succeed(ReducedMotion.snap({ [VALUE]: UndefinedOr.getOrThrow(stops[0]) }, { [VALUE]: ReducedMotion.endOf(UndefinedOr.getOrThrow(stops[0]), UndefinedOr.getOrThrow(stops[stops.length - 1]), options) })),
|
|
851
|
+
onFalse: () => Machine.keyframesValues({ [VALUE]: stops }, options),
|
|
852
|
+
});
|
|
853
|
+
let fiber;
|
|
854
|
+
const end = () => {
|
|
855
|
+
if (Predicate.isUndefined(fiber))
|
|
856
|
+
return;
|
|
857
|
+
const running = fiber;
|
|
858
|
+
fiber = undefined;
|
|
859
|
+
// Nothing waits on its end: interrupted here, not by a fiber forked for it (#618, L2).
|
|
860
|
+
if (Predicate.isUndefined(running.pollUnsafe()))
|
|
861
|
+
running.interruptUnsafe();
|
|
862
|
+
};
|
|
863
|
+
const landed = (exit) => Exit.match(exit, {
|
|
864
|
+
onSuccess: () => Effect.sync(() => settled(true)),
|
|
865
|
+
onFailure: (cause) => Bool.match(Cause.hasInterruptsOnly(cause), { onTrue: () => Effect.void, onFalse: () => Report.failure("wardic.animate: a function Segment failed", cause) }),
|
|
866
|
+
});
|
|
867
|
+
const run = () => {
|
|
868
|
+
end();
|
|
869
|
+
fiber = env.runtime.runFork(machine.pipe(Effect.map(Playback.drive(head)), Effect.flatMap(Machine.run((frame) => {
|
|
870
|
+
callback.call(UndefinedOr.getOrThrow(frame.value[VALUE]));
|
|
871
|
+
return undefined;
|
|
872
|
+
})), Effect.onExit(landed)));
|
|
873
|
+
};
|
|
874
|
+
return {
|
|
875
|
+
run,
|
|
876
|
+
stop: end,
|
|
877
|
+
cancel: () => {
|
|
878
|
+
end();
|
|
879
|
+
callback.call(UndefinedOr.getOrThrow(stops[0]));
|
|
880
|
+
},
|
|
881
|
+
duration: () => Machine.durationOf(env.runtime.runSync(machine)),
|
|
882
|
+
};
|
|
883
|
+
};
|
|
884
|
+
/**
|
|
885
|
+
* A sequence started (#433's PR 2, Motion's): compiled by core's `Sequence`, each Subject animated
|
|
886
|
+
* through its Keyframes on its spine or its plain one, as a call animates it, and each function
|
|
887
|
+
* Segment's value told to its callback; one Playhead for all, so it seeks, pauses and awaits as one.
|
|
888
|
+
*/
|
|
889
|
+
const sequenceOf = (env, ports, scope, watch, sequence, options) => {
|
|
890
|
+
const defaults = UndefinedOr.map(options, (given) => given.defaultTransition);
|
|
891
|
+
// The sequence's own options over its `defaultTransition`'s, as Motion's.
|
|
892
|
+
const whole = {
|
|
893
|
+
...picked(UndefinedOr.match(defaults, { onUndefined: () => NO_FIELDS, onDefined: identity }), WHOLE_DEFAULTS),
|
|
894
|
+
...picked(UndefinedOr.match(options, { onUndefined: () => NO_FIELDS, onDefined: identity }), WHOLE),
|
|
895
|
+
};
|
|
896
|
+
const compiled = Arr.fromIterable(Sequence.compile(stepsOf(ports, sequence, scope, defaults), whole));
|
|
897
|
+
// Each Batch's members as one track, their other keys their Subjects' (#665, Q40).
|
|
898
|
+
const parts = ports.batches.sequencePartsOf(compiled);
|
|
899
|
+
return started(watch, compiled.length, UndefinedOr.map(options, (given) => given.onComplete), (head, settled) => [
|
|
900
|
+
...Arr.map(parts.others, ([subject, animation]) => {
|
|
901
|
+
if (isCallback(subject))
|
|
902
|
+
return callbackTrackOf(env, subject, animation, head, settled);
|
|
903
|
+
return trackOf(env, ports.subjects, subject, animation.keyframes, animation.transition, head, settled);
|
|
904
|
+
}),
|
|
905
|
+
...Arr.map(parts.batches, (part) => part.binding.sequenceTrack({ members: part.members, animations: part.animations, reducedMotion: env.reducedMotion(), skipAnimations: env.skipAnimations(), head, settled: () => settled(true), taken: () => settled(false) })),
|
|
906
|
+
]);
|
|
907
|
+
};
|
|
908
|
+
/** `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). */
|
|
909
|
+
export const alsoTold = (watch, also) => UndefinedOr.match(also, {
|
|
910
|
+
onUndefined: () => watch,
|
|
911
|
+
onDefined: (more) => ({
|
|
912
|
+
started: (controls) => {
|
|
913
|
+
watch.started(controls);
|
|
914
|
+
more.started(controls);
|
|
915
|
+
},
|
|
916
|
+
ended: (controls, landed) => {
|
|
917
|
+
watch.ended(controls, landed);
|
|
918
|
+
more.ended(controls, landed);
|
|
919
|
+
},
|
|
920
|
+
}),
|
|
921
|
+
});
|
|
922
|
+
//# sourceMappingURL=Imperative.js.map
|