@wardic/core 1.2.0 → 1.3.0
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 +11 -6
- 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 +135 -0
- package/dist/unstable/BatchCalls.d.ts.map +1 -0
- package/dist/unstable/BatchCalls.js +311 -0
- package/dist/unstable/BatchCalls.js.map +1 -0
- 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 +270 -0
- package/dist/unstable/Imperative.d.ts.map +1 -0
- package/dist/unstable/Imperative.js +745 -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 +8 -11
- 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 +19 -6
- 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 +401 -0
- package/src/unstable/FrameEffects.ts +172 -0
- package/src/unstable/FrameTime.ts +1 -5
- package/src/unstable/Imperative.ts +1045 -0
- package/src/unstable/RetargetLegs.ts +20 -11
- package/src/unstable/Spine.ts +39 -18
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an Imperative animation's call of a Batch turns into, for every Environment: an `animate` call's
|
|
3
|
+
* targets for each member (a number, two stops, or a function of a member's index and the count), each
|
|
4
|
+
* key's transition for the change (its own, its Default transition held by its delay, reduced motion's
|
|
5
|
+
* Snap, `skipAnimations`' landing at once), a `stagger()` counting the members it moves, and a sequence's
|
|
6
|
+
* Segment of the Batch, each member's key through its own stops. An Environment's binding names its
|
|
7
|
+
* members and its errors; this works out the rest over core's `Batch`.
|
|
8
|
+
*
|
|
9
|
+
* Unstable, for Wardic's own packages: public, with no semver promise.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
// Unstable: ADR-0071, decision 5. #882 (ADR-0073, *Amendment (#882)*, PR 3): dom's `BatchMembers`, its
|
|
14
|
+
// environment-agnostic part moved here, so `@wardic/object`'s Batch runs the DOM's calls; #665's rules
|
|
15
|
+
// (ADR-0069, *Amendment (#665)*; ADR-0070, *Amendment (#665)*).
|
|
16
|
+
import type * as Batch from "../Batch.ts"
|
|
17
|
+
import type * as Machine from "../Machine.ts"
|
|
18
|
+
import type * as Playback from "../Playback.ts"
|
|
19
|
+
import * as ReducedMotion from "../ReducedMotion.ts"
|
|
20
|
+
import type * as Sequence from "../Sequence.ts"
|
|
21
|
+
import type * as Config from "./Config.ts"
|
|
22
|
+
import type * as Imperative from "./Imperative.ts"
|
|
23
|
+
import * as RetargetLegs from "./RetargetLegs.ts"
|
|
24
|
+
import * as Arr from "effect/Array"
|
|
25
|
+
import * as Bool from "effect/Boolean"
|
|
26
|
+
import * as Option from "effect/Option"
|
|
27
|
+
import * as Predicate from "effect/Predicate"
|
|
28
|
+
import * as Rec from "effect/Record"
|
|
29
|
+
import * as UndefinedOr from "effect/UndefinedOr"
|
|
30
|
+
|
|
31
|
+
/** How an Environment reports a Batch call's error: it throws, naming what it got. */
|
|
32
|
+
export type Fail = (message: string) => never
|
|
33
|
+
|
|
34
|
+
/** One change that lands every key at once: `skipAnimations`'. */
|
|
35
|
+
export const INSTANT: Batch.Transition = { type: "tween", duration: 0 }
|
|
36
|
+
|
|
37
|
+
/** A sequence's run that lands a key at once after `delay`: `skipAnimations`', and reduced motion's Snap of a positional key. */
|
|
38
|
+
// Module state (#460, Q7): a cache built once, keyed by what it caches.
|
|
39
|
+
const LANDINGS = new Map<number, Machine.KeyframesOptions>()
|
|
40
|
+
|
|
41
|
+
/** A run of keyframes that lands at once after `delay`, one object each. */
|
|
42
|
+
export const landingAfter = (delay: number): Machine.KeyframesOptions => {
|
|
43
|
+
const known = LANDINGS.get(delay)
|
|
44
|
+
if (Predicate.isNotUndefined(known)) return known
|
|
45
|
+
const made: Machine.KeyframesOptions = { duration: 0, delay }
|
|
46
|
+
LANDINGS.set(delay, made)
|
|
47
|
+
return made
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A Batch key's stop in a sequence, as core's Batch takes it: a number, `NaN` for `null`. */
|
|
51
|
+
export const stopNumber = (stop: Sequence.Stop): number => {
|
|
52
|
+
if (Predicate.isNumber(stop)) return stop
|
|
53
|
+
return Number.NaN
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A key's last stop that's a number, `NaN` (where it is) for none: what its `null`s after it hold. */
|
|
57
|
+
export const lastNumber = (stops: ReadonlyArray<number>): number =>
|
|
58
|
+
Option.getOrElse(Arr.findLast(stops, (stop) => !Number.isNaN(stop)), () => Number.NaN)
|
|
59
|
+
|
|
60
|
+
/** Whether `value` is a Batch's key's first stop: a number, or `null` for where it is. */
|
|
61
|
+
export const isFirstStop: Predicate.Predicate<unknown> = /*#__PURE__*/ Predicate.or(Predicate.isNumber, Predicate.isNull)
|
|
62
|
+
|
|
63
|
+
/** Whether `value` is two stops. */
|
|
64
|
+
export const isPair = (value: unknown): value is readonly [unknown, unknown] => {
|
|
65
|
+
if (!Arr.isArray(value)) return false
|
|
66
|
+
return value.length === 2
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Whether `value` is a Batch key's value in a sequence: a number, `null`, or stops of them. */
|
|
70
|
+
const isSegmentStops = (value: unknown): boolean => {
|
|
71
|
+
if (isFirstStop(value)) return true
|
|
72
|
+
if (!Arr.isArray(value)) return false
|
|
73
|
+
return Arr.every(value, isFirstStop)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A Batch key's value in a sequence (Q38): a number, `null` or stops of them; else an error naming the key, and the member a function gave it. */
|
|
77
|
+
export const segmentValueOf = (key: string, value: unknown, memberName: string | undefined, fail: Fail): Sequence.Stop | ReadonlyArray<Sequence.Stop> => {
|
|
78
|
+
if (isSegmentStops(value)) return value as Sequence.Stop | ReadonlyArray<Sequence.Stop>
|
|
79
|
+
const whose = UndefinedOr.match(memberName, { onUndefined: () => "", onDefined: (name) => `${name}'s ` })
|
|
80
|
+
return fail(`a sequence gives ${whose}"${key}" as ${JSON.stringify(value)}: a Batch's key takes a number, \`null\`, or stops of them`)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Reduced motion's Snap after `delay`: a 0 s tween, kept by its delay so its keys share one. */
|
|
84
|
+
// Module state (#460, Q7): a cache built once, keyed by what it caches.
|
|
85
|
+
const SNAPS = new Map<number, Batch.Transition>()
|
|
86
|
+
|
|
87
|
+
const snapAfter = (delay: number): Batch.Transition => {
|
|
88
|
+
const known = SNAPS.get(delay)
|
|
89
|
+
if (Predicate.isNotUndefined(known)) return known
|
|
90
|
+
const made: Batch.Transition = { type: "tween", duration: 0, delay }
|
|
91
|
+
SNAPS.set(delay, made)
|
|
92
|
+
return made
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* A Default transition held by a key's own `delay`, as a Wardic element's is when its transition gives
|
|
97
|
+
* only a delay (ADR-0051): one object for each transition and delay, so the members on it share it.
|
|
98
|
+
*/
|
|
99
|
+
// Module state (#460, Q7): a cache held weakly by the object it's for, so it goes with it.
|
|
100
|
+
const DELAYED = new WeakMap<object, Map<number, Batch.Transition>>()
|
|
101
|
+
|
|
102
|
+
export const delayedBy = (transition: Batch.Transition, delay: number): Batch.Transition => {
|
|
103
|
+
if (delay === 0) return transition
|
|
104
|
+
let byDelay = DELAYED.get(transition)
|
|
105
|
+
if (Predicate.isUndefined(byDelay)) {
|
|
106
|
+
byDelay = new Map()
|
|
107
|
+
DELAYED.set(transition, byDelay)
|
|
108
|
+
}
|
|
109
|
+
const known = byDelay.get(delay)
|
|
110
|
+
if (Predicate.isNotUndefined(known)) return known
|
|
111
|
+
const made = { ...transition, delay } as Batch.Transition
|
|
112
|
+
byDelay.set(delay, made)
|
|
113
|
+
return made
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A transition without a `stagger()` delay, which a change gives each member by its place: one object
|
|
118
|
+
* each. A `delay` in seconds stays, held by the keys the transition's own fields reach, as a Wardic
|
|
119
|
+
* element's top-level delay is.
|
|
120
|
+
*/
|
|
121
|
+
// Module state (#460, Q7): a cache held weakly by the object it's for, so it goes with it.
|
|
122
|
+
const UNSTAGGERED = new WeakMap<object, Config.WardicTransition>()
|
|
123
|
+
|
|
124
|
+
export const unstaggered = (given: { readonly delay?: unknown }): Config.WardicTransition => {
|
|
125
|
+
if (!Predicate.isFunction(given.delay)) return given as Config.WardicTransition
|
|
126
|
+
const known = UNSTAGGERED.get(given)
|
|
127
|
+
if (Predicate.isNotUndefined(known)) return known
|
|
128
|
+
const made = { ...given, delay: undefined } as unknown as Config.WardicTransition
|
|
129
|
+
UNSTAGGERED.set(given, made)
|
|
130
|
+
return made
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** A change's `stagger()` delay, for the members it moves; `undefined` for none. */
|
|
134
|
+
export const staggerOf = (given: Config.WardicTransition | undefined): ((rank: number, n: number) => number) | undefined => {
|
|
135
|
+
if (Predicate.isUndefined(given)) return undefined
|
|
136
|
+
const delay: unknown = given.delay
|
|
137
|
+
if (Predicate.isFunction(delay)) return delay as (rank: number, n: number) => number
|
|
138
|
+
return undefined
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A key's transition as a Batch runs it: its run, which can't be an inertia or repeat. */
|
|
142
|
+
const batchRunOf = (run: RetargetLegs.Run, key: string, fail: Fail): Batch.Transition => {
|
|
143
|
+
if (run.type === "inertia") fail(`"${key}" has an inertia transition, which a Batch can't run: give it a spring or a tween`)
|
|
144
|
+
if (Predicate.hasProperty(run, "repeat")) fail(`"${key}"'s transition repeats, which a Batch can't run`)
|
|
145
|
+
return run as Batch.Transition
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** A change's plan for one key: its own transition, or `undefined` for its Default transition held by `delay`. */
|
|
149
|
+
export interface KeyPlan {
|
|
150
|
+
readonly own: Batch.Transition | undefined
|
|
151
|
+
readonly delay: number
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Each key's plan for a change, by its index, as a Wardic element's keys run (#301): its entry, else
|
|
156
|
+
* `default`, else the transition's own fields; reduced motion's Snap for a positional key that doesn't
|
|
157
|
+
* opt out; its Default transition, held by its own `delay`, for a key whose transition isn't defined.
|
|
158
|
+
*/
|
|
159
|
+
// Once a change, per key.
|
|
160
|
+
export const plannedOf = (positional: ReducedMotion.PositionalKeys["Service"], keys: ReadonlyArray<string>, given: Config.WardicTransition | undefined, reduced: boolean, fail: Fail): ReadonlyArray<KeyPlan> =>
|
|
161
|
+
Arr.map(keys, (key): KeyPlan => {
|
|
162
|
+
const transition = RetargetLegs.transitionForKey(given, key)
|
|
163
|
+
let delay = 0
|
|
164
|
+
let respects = true
|
|
165
|
+
if (Predicate.isNotUndefined(transition)) {
|
|
166
|
+
if (Predicate.isNumber(transition.delay)) delay = transition.delay
|
|
167
|
+
respects = transition.respectReducedMotion !== false
|
|
168
|
+
}
|
|
169
|
+
if (reduced && respects && positional.isPositional(key)) return { own: snapAfter(delay), delay }
|
|
170
|
+
const run = RetargetLegs.runOf(transition)
|
|
171
|
+
if (Predicate.isUndefined(run)) return { own: undefined, delay }
|
|
172
|
+
return { own: batchRunOf(run, key, fail), delay }
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* A member's transitions on `keys` for a change planned as `planned`: each key's own, else its Default
|
|
177
|
+
* transition (`defaultFor`) toward its target there, `targetAt(k)`; every key at once when it's skipped.
|
|
178
|
+
*/
|
|
179
|
+
export const memberTransitionsOf = (
|
|
180
|
+
keys: ReadonlyArray<string>,
|
|
181
|
+
planned: ReadonlyArray<KeyPlan>,
|
|
182
|
+
defaultFor: (key: string, target: number) => Batch.Transition,
|
|
183
|
+
targetAt: (k: number) => number,
|
|
184
|
+
skipped: boolean,
|
|
185
|
+
): ReadonlyArray<Batch.Transition> =>
|
|
186
|
+
Arr.map(keys, (key, k) => {
|
|
187
|
+
if (skipped) return INSTANT
|
|
188
|
+
const plan = planned[k] as KeyPlan
|
|
189
|
+
if (Predicate.isNotUndefined(plan.own)) return plan.own
|
|
190
|
+
return delayedBy(defaultFor(key, targetAt(k)), plan.delay)
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
/** What a binding tells these of its Batch: its keys, its Environment's Positional keys and Default transitions, and its errors. */
|
|
194
|
+
export interface Planning {
|
|
195
|
+
readonly keys: ReadonlyArray<string>
|
|
196
|
+
readonly positionalKeys: ReducedMotion.PositionalKeys["Service"]
|
|
197
|
+
/** A key's Default transition toward `target`, by the key's index. */
|
|
198
|
+
readonly defaultFor: (k: number, target: number) => Batch.Transition
|
|
199
|
+
readonly fail: Fail
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** A change's call: its transition, its scope's settings, its Playhead and its landing. */
|
|
203
|
+
export interface ChangeCall {
|
|
204
|
+
readonly transition: Config.WardicTransition | undefined
|
|
205
|
+
readonly reducedMotion: boolean
|
|
206
|
+
readonly skipAnimations: boolean
|
|
207
|
+
readonly head: Playback.Playhead
|
|
208
|
+
readonly settled: () => void
|
|
209
|
+
readonly onMemberLanded?: ((slot: number) => void) | undefined
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* A driven change's options for core's `Batch.animate`, toward `targetOf`, from `fromOf`'s first stops
|
|
214
|
+
* when it gives them, on its call's transitions: each key's own, else its Default transition toward the
|
|
215
|
+
* member's target, held by the key's delay; every key at once when the call skips; its `stagger()`
|
|
216
|
+
* counting the members it moves.
|
|
217
|
+
*/
|
|
218
|
+
export const animateOptions = (
|
|
219
|
+
planning: Planning,
|
|
220
|
+
call: ChangeCall,
|
|
221
|
+
targetOf: (slot: number, k: number) => number,
|
|
222
|
+
fromOf: ((slot: number, k: number) => number) | undefined,
|
|
223
|
+
): Batch.AnimateOptions => {
|
|
224
|
+
const transition = Bool.match(call.skipAnimations, {
|
|
225
|
+
onTrue: () => (): Batch.Transition => INSTANT,
|
|
226
|
+
onFalse: () => {
|
|
227
|
+
const planned = plannedOf(planning.positionalKeys, planning.keys, UndefinedOr.map(call.transition, unstaggered), call.reducedMotion, planning.fail)
|
|
228
|
+
return (slot: number, k: number): Batch.Transition => {
|
|
229
|
+
const plan = planned[k] as KeyPlan
|
|
230
|
+
if (Predicate.isNotUndefined(plan.own)) return plan.own
|
|
231
|
+
return delayedBy(planning.defaultFor(k, targetOf(slot, k)), plan.delay)
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
})
|
|
235
|
+
const options: { -readonly [K in keyof Batch.AnimateOptions]: Batch.AnimateOptions[K] } = { head: call.head, transition, onLanded: call.settled }
|
|
236
|
+
if (Predicate.isNotUndefined(fromOf)) options.from = fromOf
|
|
237
|
+
if (Predicate.isNotUndefined(call.onMemberLanded)) options.onMemberLanded = call.onMemberLanded
|
|
238
|
+
const delay = Bool.match(call.skipAnimations, { onTrue: () => undefined, onFalse: () => staggerOf(call.transition) })
|
|
239
|
+
if (Predicate.isNotUndefined(delay)) options.delay = delay
|
|
240
|
+
return options
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* How a binding names its members for a call, by the number it passes for each (its slot in the Batch
|
|
245
|
+
* for an `animate` call, the binding's own number for a Segment's): their place in its list, their count,
|
|
246
|
+
* and what an error calls them.
|
|
247
|
+
*/
|
|
248
|
+
export interface Members {
|
|
249
|
+
/** A member's index in its list: what a function of the index is given. */
|
|
250
|
+
readonly indexOf: (id: number) => number
|
|
251
|
+
/** How many members the Batch has: what a function of the index is given as the count. */
|
|
252
|
+
readonly count: () => number
|
|
253
|
+
/** What an error calls the member. */
|
|
254
|
+
readonly nameOf: (id: number) => string
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** An `animate` call's targets for each member's key, and its first stops when it gives any: `NaN` for none. */
|
|
258
|
+
export interface CallTargets {
|
|
259
|
+
readonly toOf: (slot: number, k: number) => number
|
|
260
|
+
readonly fromOf: ((slot: number, k: number) => number) | undefined
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* An `animate` call's `given` targets for `slots`' members, worked out once: each key one of the Batch's
|
|
265
|
+
* (`keyIndex`), its value a number, two stops (its first and its target), or a function of a member's
|
|
266
|
+
* index and the count that gives one; else an error naming it.
|
|
267
|
+
*/
|
|
268
|
+
// Per member per key at a call (#665): statements.
|
|
269
|
+
export const callTargets = (planning: Planning, keyIndex: ReadonlyMap<string, number>, members: Members, slots: ReadonlyArray<number>, given: Readonly<Record<string, unknown>>): CallTargets => {
|
|
270
|
+
const { keys, fail } = planning
|
|
271
|
+
const stride = keys.length
|
|
272
|
+
const top = Arr.reduce(slots, 0, (highest, slot) => Math.max(highest, slot))
|
|
273
|
+
const tos = new Float64Array((top + 1) * stride).fill(Number.NaN)
|
|
274
|
+
const froms = new Float64Array((top + 1) * stride).fill(Number.NaN)
|
|
275
|
+
let firsts = false
|
|
276
|
+
/** One stop's value for the member in `slot`: a number, or a function's of its index and the count. */
|
|
277
|
+
const stopAt = (key: string, stop: unknown, slot: number): number => {
|
|
278
|
+
let value = stop
|
|
279
|
+
if (Predicate.isFunction(stop)) value = stop(members.indexOf(slot), members.count())
|
|
280
|
+
if (Predicate.isNumber(value)) return value
|
|
281
|
+
return fail(`an animate call's "${key}" gives ${members.nameOf(slot)} ${JSON.stringify(value)}: a Batch takes numbers`)
|
|
282
|
+
}
|
|
283
|
+
Arr.forEach(Rec.keys(given), (key) => {
|
|
284
|
+
const k = keyIndex.get(key)
|
|
285
|
+
if (Predicate.isUndefined(k)) return fail(`an animate call gives "${key}", which isn't one of the Batch's keys (${keys.join(", ")})`)
|
|
286
|
+
const value = given[key]
|
|
287
|
+
Arr.forEach(slots, (slot) => {
|
|
288
|
+
const at = slot * stride + k
|
|
289
|
+
let stops = value
|
|
290
|
+
if (Predicate.isFunction(value)) stops = value(members.indexOf(slot), members.count())
|
|
291
|
+
if (isPair(stops)) {
|
|
292
|
+
froms[at] = stopAt(key, stops[0], slot)
|
|
293
|
+
tos[at] = stopAt(key, stops[1], slot)
|
|
294
|
+
firsts = true
|
|
295
|
+
return
|
|
296
|
+
}
|
|
297
|
+
if (Predicate.isNumber(stops)) {
|
|
298
|
+
tos[at] = stops
|
|
299
|
+
return
|
|
300
|
+
}
|
|
301
|
+
return fail(`an animate call's "${key}" is ${JSON.stringify(stops)}: a Batch takes a number or two, its first and its target, or a function of a member's index that gives them`)
|
|
302
|
+
})
|
|
303
|
+
})
|
|
304
|
+
const toOf = (slot: number, k: number): number => tos[slot * stride + k] as number
|
|
305
|
+
const fromOf = (slot: number, k: number): number => froms[slot * stride + k] as number
|
|
306
|
+
return { toOf, fromOf: Bool.match(firsts, { onTrue: () => fromOf, onFalse: () => undefined }) }
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* A sequence's Segment of a Batch's values (Q38, Q39), for the members moving there, in order: each key
|
|
311
|
+
* one of the Batch's (`hasKey`), its value a number, `null` or stops, or a function of a member's index
|
|
312
|
+
* and the count that gives them, called for each member as core's Sequence compiles it.
|
|
313
|
+
*/
|
|
314
|
+
export const segmentValues = (
|
|
315
|
+
planning: Planning,
|
|
316
|
+
hasKey: (key: string) => boolean,
|
|
317
|
+
members: Members,
|
|
318
|
+
moving: ReadonlyArray<number>,
|
|
319
|
+
target: Readonly<Record<string, unknown>>,
|
|
320
|
+
): Readonly<Record<string, Sequence.Value>> => {
|
|
321
|
+
const { keys, fail } = planning
|
|
322
|
+
const values: Record<string, Sequence.Value> = {}
|
|
323
|
+
Arr.forEach(Rec.keys(target), (key) => {
|
|
324
|
+
const value = target[key]
|
|
325
|
+
if (Predicate.isUndefined(value)) return
|
|
326
|
+
if (!hasKey(key)) return fail(`a sequence's Segment of a Batch gives "${key}", which isn't one of its keys (${keys.join(", ")})`)
|
|
327
|
+
if (Predicate.isFunction(value)) {
|
|
328
|
+
values[key] = (index: number) => segmentValueOf(key, value(members.indexOf(moving[index] as number), members.count()), members.nameOf(moving[index] as number), fail)
|
|
329
|
+
return
|
|
330
|
+
}
|
|
331
|
+
values[key] = segmentValueOf(key, value, undefined, fail)
|
|
332
|
+
})
|
|
333
|
+
return values
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** A sequence's call of a Batch's members: its scope's settings. */
|
|
337
|
+
export interface SequenceCall {
|
|
338
|
+
readonly reducedMotion: boolean
|
|
339
|
+
readonly skipAnimations: boolean
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* A sequence's keyframes for each member's key (#665, Q36), from the animation core's Sequence compiled
|
|
344
|
+
* for the member in `slot` (`animationAt`): its stops and tween; reduced motion's Snap of a positional
|
|
345
|
+
* key, or `skipAnimations`, landing it at once where its run would end.
|
|
346
|
+
*/
|
|
347
|
+
// Per member per key, once at its first play (#665): statements.
|
|
348
|
+
export const keyframesFor = (planning: Planning, call: SequenceCall, animationAt: (slot: number) => Sequence.Animation) =>
|
|
349
|
+
(slot: number, k: number): Batch.Keyframes | undefined => {
|
|
350
|
+
const key = planning.keys[k] as string
|
|
351
|
+
const animation = animationAt(slot)
|
|
352
|
+
const given = animation.keyframes[key]
|
|
353
|
+
if (Predicate.isUndefined(given)) return undefined
|
|
354
|
+
const transition = UndefinedOr.getOrThrow(animation.transition[key])
|
|
355
|
+
const stops = Arr.map(given, stopNumber)
|
|
356
|
+
const snaps = call.reducedMotion && planning.positionalKeys.isPositional(key)
|
|
357
|
+
if (!call.skipAnimations && !snaps) return { stops, transition }
|
|
358
|
+
// Where its run would end, from where it is: after its delay for reduced motion's Snap, at once when
|
|
359
|
+
// skipped. Its last stop is its last number: a sequence ends a key with a `null` when its last Segment ends first.
|
|
360
|
+
const end = ReducedMotion.endOf(stops[0] as number, lastNumber(stops), transition)
|
|
361
|
+
let delay = 0
|
|
362
|
+
if (!call.skipAnimations && Predicate.isNotUndefined(transition.delay)) delay = transition.delay
|
|
363
|
+
return { stops: [Number.NaN, end], transition: landingAfter(delay) }
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** Whether a sequence's subject is a Batch member's entry: a binding and its member's number. */
|
|
367
|
+
export const isMember: Predicate.Refinement<unknown, Imperative.BatchMember> = /*#__PURE__*/ Predicate.and(
|
|
368
|
+
/*#__PURE__*/ Predicate.hasProperty("binding"),
|
|
369
|
+
/*#__PURE__*/ Predicate.hasProperty("member"),
|
|
370
|
+
) as Predicate.Refinement<unknown, Imperative.BatchMember>
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* A compiled sequence's parts (Q40): each Batch's members as one part, in the order they first appear,
|
|
374
|
+
* with their animations; every other subject as it is. `restOf` gives what of a member's animation its
|
|
375
|
+
* Subject takes itself (the DOM's member element's keys the Batch doesn't draw), when it takes any.
|
|
376
|
+
*/
|
|
377
|
+
export const sequencePartsOf = <X, S>(
|
|
378
|
+
compiled: ReadonlyArray<readonly [X | Imperative.BatchMember, Sequence.Animation]>,
|
|
379
|
+
restOf?: (member: Imperative.BatchMember, animation: Sequence.Animation) => readonly [S, Sequence.Animation] | undefined,
|
|
380
|
+
): { readonly batches: ReadonlyArray<Imperative.SequencePart>; readonly others: ReadonlyArray<readonly [X | S, Sequence.Animation]> } => {
|
|
381
|
+
const byBinding = new Map<Imperative.BatchCalls, { readonly members: Array<number>; readonly animations: Array<Sequence.Animation> }>()
|
|
382
|
+
const others: Array<readonly [X | S, Sequence.Animation]> = []
|
|
383
|
+
Arr.forEach(compiled, ([subject, animation]) => {
|
|
384
|
+
if (!isMember(subject)) {
|
|
385
|
+
others.push([subject as X, animation])
|
|
386
|
+
return
|
|
387
|
+
}
|
|
388
|
+
let part = byBinding.get(subject.binding)
|
|
389
|
+
if (Predicate.isUndefined(part)) {
|
|
390
|
+
part = { members: [], animations: [] }
|
|
391
|
+
byBinding.set(subject.binding, part)
|
|
392
|
+
}
|
|
393
|
+
part.members.push(subject.member)
|
|
394
|
+
part.animations.push(animation)
|
|
395
|
+
if (Predicate.isUndefined(restOf)) return
|
|
396
|
+
const rest = restOf(subject, animation)
|
|
397
|
+
if (Predicate.isNotUndefined(rest)) others.push(rest)
|
|
398
|
+
})
|
|
399
|
+
return { batches: Arr.map(Arr.fromIterable(byBinding), ([binding, part]): Imperative.SequencePart => ({ binding, ...part })), others }
|
|
400
|
+
}
|
|
401
|
+
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An Environment's **Frame effects** (CONTEXT.md): an app's own code, run at the end of each frame the
|
|
3
|
+
* Environment draws, after Wardic's writes for it, in the order the effects were added. A scene runs
|
|
4
|
+
* them in its Host loop before it renders; the DOM before paint. The Environment calls {@link
|
|
5
|
+
* FrameEffects.run} at its frame's end, and gives `make` its way to ask for a frame, so an effect whose
|
|
6
|
+
* inputs changed is run on a frame nothing else asked for.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* import * as FrameEffects from "@wardic/core/unstable/FrameEffects"
|
|
10
|
+
*
|
|
11
|
+
* const effects = FrameEffects.make({ requestFrame: () => host.wantFrame(), report: console.error })
|
|
12
|
+
* const remove = effects.add((time) => controls.update())
|
|
13
|
+
* effects.run(time) // the Environment's, at its frame's end
|
|
14
|
+
*
|
|
15
|
+
* Unstable, for Wardic's own packages: public, with no semver promise.
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
// Unstable: ADR-0071, decision 5. #882 (ADR-0073, *Amendment (#882)*, Q3): the slot for decision 19's Frame
|
|
20
|
+
// effects, one for every Environment. Not a phase of the Frame loop or of the gate: the loop ticks only
|
|
21
|
+
// while an animation runs, and the gate flushes only what was written, so a frame where an effect's only
|
|
22
|
+
// input moved (`createTime()`, an every-frame `controls.update()`) has neither.
|
|
23
|
+
import * as Effect from "effect/Effect"
|
|
24
|
+
import type * as Scope from "effect/Scope"
|
|
25
|
+
import * as Clock from "../Clock.ts"
|
|
26
|
+
import * as FrameLoop from "../FrameLoop.ts"
|
|
27
|
+
import * as RefreshFrameSource from "../RefreshFrameSource.ts"
|
|
28
|
+
|
|
29
|
+
/** What an Environment gives its Frame effects. */
|
|
30
|
+
export interface FrameEffectsOptions {
|
|
31
|
+
/** Asks the Environment for a frame, whose end runs the effects. Called once a request, until the run. */
|
|
32
|
+
readonly requestFrame: () => void
|
|
33
|
+
/** What an effect that throws reports: the rest of the frame's effects still run. */
|
|
34
|
+
readonly report: (defect: unknown) => void
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** An Environment's Frame effects: an ordered list it runs at each frame's end. */
|
|
38
|
+
export interface FrameEffects {
|
|
39
|
+
/** Run `effect` at the end of each frame from now on, after those added before it. Returns its remover. */
|
|
40
|
+
readonly add: (effect: (time: number) => void) => () => void
|
|
41
|
+
/** Ask for a frame whose end runs the effects, though nothing else asks for one: an input of one changed. */
|
|
42
|
+
readonly request: () => void
|
|
43
|
+
/** Whether a frame is asked for: a `request()` since the last `run`. */
|
|
44
|
+
readonly requested: () => boolean
|
|
45
|
+
/** The Environment's call at its frame's end, `time` in ms: every effect added before it, in order. */
|
|
46
|
+
readonly run: (time: number) => void
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** An effect in the list, and whether it's still in it. */
|
|
50
|
+
interface Entry {
|
|
51
|
+
readonly effect: (time: number) => void
|
|
52
|
+
live: boolean
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* An Environment's Frame effects, asking it for frames through `options.requestFrame`. An effect added
|
|
57
|
+
* while they run waits for the next frame's run, as a Refresh frame's callbacks do; one removed while
|
|
58
|
+
* they run, before its turn, isn't run.
|
|
59
|
+
*/
|
|
60
|
+
export const make = (options: FrameEffectsOptions): FrameEffects => {
|
|
61
|
+
const { requestFrame, report } = options
|
|
62
|
+
// In order: the per-frame path, a plain array and loops, nothing allocated a frame.
|
|
63
|
+
const entries: Array<Entry> = []
|
|
64
|
+
let running = false
|
|
65
|
+
let removedInRun = 0
|
|
66
|
+
let asked = false
|
|
67
|
+
|
|
68
|
+
/** The entries no longer live taken out, in place, keeping the rest's order. */
|
|
69
|
+
const compact = (): void => {
|
|
70
|
+
let kept = 0
|
|
71
|
+
for (let i = 0; i < entries.length; i++) {
|
|
72
|
+
const entry = entries[i]
|
|
73
|
+
if (entry.live) entries[kept++] = entry
|
|
74
|
+
}
|
|
75
|
+
entries.length = kept
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const add: FrameEffects["add"] = (effect) => {
|
|
79
|
+
const entry: Entry = { effect, live: true }
|
|
80
|
+
entries.push(entry)
|
|
81
|
+
return () => {
|
|
82
|
+
if (!entry.live) return
|
|
83
|
+
entry.live = false
|
|
84
|
+
if (running) {
|
|
85
|
+
removedInRun++
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
compact()
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const request: FrameEffects["request"] = () => {
|
|
93
|
+
if (asked) return
|
|
94
|
+
asked = true
|
|
95
|
+
requestFrame()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const run: FrameEffects["run"] = (time) => {
|
|
99
|
+
asked = false
|
|
100
|
+
running = true
|
|
101
|
+
// Those added before the run: one added in it waits for the next frame.
|
|
102
|
+
const count = entries.length
|
|
103
|
+
try {
|
|
104
|
+
for (let i = 0; i < count; i++) {
|
|
105
|
+
const entry = entries[i]
|
|
106
|
+
if (!entry.live) continue
|
|
107
|
+
try {
|
|
108
|
+
entry.effect(time)
|
|
109
|
+
} catch (defect) {
|
|
110
|
+
report(defect)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
} finally {
|
|
114
|
+
running = false
|
|
115
|
+
if (removedInRun > 0) {
|
|
116
|
+
removedInRun = 0
|
|
117
|
+
compact()
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return { add, request, requested: () => asked, run }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Frame effects for an Environment on core's Frame loop and Refresh frames, the DOM's: run at the end
|
|
127
|
+
* of each tick, after the tick's hooks added before them (the Flush gate's flush, so after Wardic's
|
|
128
|
+
* writes for the frame), and, on a frame with no tick, on a Refresh frame of their own that `request()`
|
|
129
|
+
* asks for, which a tick withdraws. For as long as the Scope is open.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* const effects = yield* FrameEffects.onFrameLoop((defect) => console.error(defect))
|
|
133
|
+
* effects.add(() => camera.lookAt(target))
|
|
134
|
+
*/
|
|
135
|
+
// #884's PR 1 (ADR-0073 decision 19, *Amendment (#882)*'s Q3): the DOM's slot, as `@wardic/object`'s
|
|
136
|
+
// `frame(t)` runs its own. The tick's hook as the gate's `flushTicked` is: its own frame withdrawn, run now.
|
|
137
|
+
export const onFrameLoop = (report: (defect: unknown) => void): Effect.Effect<FrameEffects, never, FrameLoop.FrameLoop | RefreshFrameSource.RefreshFrameSource | Clock.Clock | Scope.Scope> =>
|
|
138
|
+
Effect.gen(function* () {
|
|
139
|
+
const loop = yield* FrameLoop.FrameLoop
|
|
140
|
+
const { requestFrame, cancelFrame } = yield* RefreshFrameSource.RefreshFrameSource
|
|
141
|
+
const clock = yield* Clock.Clock
|
|
142
|
+
// The Refresh frame a request asked for, while it's pending.
|
|
143
|
+
let pending = false
|
|
144
|
+
let frame: unknown = undefined
|
|
145
|
+
const effects: FrameEffects = make({
|
|
146
|
+
requestFrame: () => {
|
|
147
|
+
pending = true
|
|
148
|
+
frame = requestFrame((time) => {
|
|
149
|
+
pending = false
|
|
150
|
+
effects.run(time)
|
|
151
|
+
})
|
|
152
|
+
},
|
|
153
|
+
report,
|
|
154
|
+
})
|
|
155
|
+
const ticked = (): undefined => {
|
|
156
|
+
if (pending) {
|
|
157
|
+
cancelFrame(frame)
|
|
158
|
+
pending = false
|
|
159
|
+
}
|
|
160
|
+
effects.run(clock.frameAt())
|
|
161
|
+
return undefined
|
|
162
|
+
}
|
|
163
|
+
yield* loop.afterTick(ticked)
|
|
164
|
+
yield* Effect.addFinalizer(() =>
|
|
165
|
+
Effect.sync(() => {
|
|
166
|
+
if (pending) cancelFrame(frame)
|
|
167
|
+
pending = false
|
|
168
|
+
})
|
|
169
|
+
)
|
|
170
|
+
return effects
|
|
171
|
+
})
|
|
172
|
+
|
|
@@ -17,14 +17,10 @@ import type * as WardicServices from "./WardicServices.ts"
|
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* How long a key's velocity holds once nothing has moved it, in ms (Motion's `MAX_VELOCITY_DELTA`):
|
|
20
|
-
* past it, the key is at rest. A velocity worked out
|
|
21
|
-
* time apart.
|
|
20
|
+
* past it, the key is at rest. A velocity worked out over a longer step takes this as the step's time.
|
|
22
21
|
*/
|
|
23
22
|
export const MAX_VELOCITY_DELTA = 30
|
|
24
23
|
|
|
25
|
-
/** A frame at 60 Hz, in ms: the time a leg's first frame is read over, from where it started (Motion's first-frame delta). */
|
|
26
|
-
export const NOMINAL_FRAME = 1000 / 60
|
|
27
|
-
|
|
28
24
|
// #672: declared here, as core's `Clock.ts` declares it, since core builds with no DOM in its lib.
|
|
29
25
|
declare const performance: { readonly now: () => number }
|
|
30
26
|
|