@wardic/object 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +41 -2
- package/THIRD_PARTY_NOTICES.md +145 -0
- package/dist/ObjectBatch.d.ts +80 -0
- package/dist/ObjectBatch.d.ts.map +1 -0
- package/dist/ObjectBatch.js +191 -0
- package/dist/ObjectBatch.js.map +1 -0
- package/dist/ObjectRenderer.d.ts +22 -0
- package/dist/ObjectRenderer.d.ts.map +1 -0
- package/dist/ObjectRenderer.js +38 -0
- package/dist/ObjectRenderer.js.map +1 -0
- package/dist/ObjectValues.d.ts +23 -0
- package/dist/ObjectValues.d.ts.map +1 -0
- package/dist/ObjectValues.js +90 -0
- package/dist/ObjectValues.js.map +1 -0
- package/dist/PumpedFrames.d.ts +39 -0
- package/dist/PumpedFrames.d.ts.map +1 -0
- package/dist/PumpedFrames.js +51 -0
- package/dist/PumpedFrames.js.map +1 -0
- package/dist/WardicObject.d.ts +148 -0
- package/dist/WardicObject.d.ts.map +1 -0
- package/dist/WardicObject.js +208 -0
- package/dist/WardicObject.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/package.json +63 -4
- package/src/ObjectBatch.ts +266 -0
- package/src/ObjectRenderer.ts +42 -0
- package/src/ObjectValues.ts +109 -0
- package/src/PumpedFrames.ts +70 -0
- package/src/WardicObject.ts +386 -0
- package/src/index.ts +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wardic/object",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.3.1",
|
|
4
|
+
"description": "Wardic for any object's properties: Motion's animate on plain objects, on frames the host pumps, for canvas and WebGL.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"animation",
|
|
7
|
+
"motion",
|
|
8
|
+
"spring",
|
|
9
|
+
"effect",
|
|
10
|
+
"canvas",
|
|
11
|
+
"webgl"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://wardic.dev",
|
|
14
|
+
"bugs": "https://github.com/wardiclabs/wardic/issues",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/wardiclabs/wardic.git",
|
|
18
|
+
"directory": "packages/object"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": {
|
|
22
|
+
"name": "The Wardic contributors",
|
|
23
|
+
"email": "hello@wardic.dev",
|
|
24
|
+
"url": "https://wardic.dev"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist/**/*.js",
|
|
28
|
+
"dist/**/*.d.ts",
|
|
29
|
+
"dist/**/*.map",
|
|
30
|
+
"src",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE",
|
|
33
|
+
"THIRD_PARTY_NOTICES.md"
|
|
34
|
+
],
|
|
35
|
+
"type": "module",
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"exports": {
|
|
41
|
+
"./package.json": "./package.json",
|
|
42
|
+
".": {
|
|
43
|
+
"@wardic/source": "./src/index.ts",
|
|
44
|
+
"types": "./dist/index.d.ts",
|
|
45
|
+
"default": "./dist/index.js"
|
|
46
|
+
},
|
|
47
|
+
"./*": {
|
|
48
|
+
"@wardic/source": "./src/*.ts",
|
|
49
|
+
"types": "./dist/*.d.ts",
|
|
50
|
+
"default": "./dist/*.js"
|
|
51
|
+
},
|
|
52
|
+
"./internal/*": null
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "../../node_modules/typescript-7/bin/tsc --build",
|
|
56
|
+
"typecheck": "../../node_modules/typescript-7/bin/tsc --build",
|
|
57
|
+
"typecheck:tests": "../../node_modules/typescript-7/bin/tsc -p tsconfig.test.json"
|
|
58
|
+
},
|
|
59
|
+
"dependencies": {
|
|
60
|
+
"@wardic/core": "1.3.1"
|
|
61
|
+
},
|
|
62
|
+
"peerDependencies": {
|
|
63
|
+
"effect": "^4.1.4"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Many objects animated on the same keys as one run (a **Batch**): hundreds of sprites, a chart's points,
|
|
3
|
+
* a grid of meshes that move together. `wardic.batch(objects)` makes one; `animate(batch, target)` moves
|
|
4
|
+
* every member, each key's value a number, two stops (its first and its target), or a function of a
|
|
5
|
+
* member's index and the count; `animate(batch.at(i), target)` moves member `i`. A new target turns each
|
|
6
|
+
* member toward it at the speed it has, for a fraction of what an animation per object costs.
|
|
7
|
+
*
|
|
8
|
+
* - **Its keys** are fixed by its first `animate` (or `keys` when it's made). Each member starts from
|
|
9
|
+
* its own values for them, numbers each.
|
|
10
|
+
* - **Its members** are reached through it: an object of a Batch given to `animate` itself is an error.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const swarm = wardic.batch(sprites)
|
|
14
|
+
* wardic.animate(swarm, { x: (i) => i * 12, y: [0, 40] }, { type: "spring", delay: stagger(0.01) })
|
|
15
|
+
* wardic.animate(swarm.at(3), { y: 0 })
|
|
16
|
+
*/
|
|
17
|
+
// `@wardic/object/ObjectBatch` (#882; ADR-0069 decisions 10–18, ADR-0073, *Amendment (#882)*, PR 3): core's
|
|
18
|
+
// Batch with a property writer, its calls core's `BatchCalls`, the DOM's. Its keys at its first call or
|
|
19
|
+
// `keys` (the DOM's keyed `createBatch`'s rule, #665's PR 3a); a member object animated itself is an error
|
|
20
|
+
// until a driver's hold (ADR-0069 decision 17) comes with `@wardic/three`.
|
|
21
|
+
import * as Batch from "@wardic/core/Batch"
|
|
22
|
+
import type * as DefaultTransition from "@wardic/core/DefaultTransition"
|
|
23
|
+
import type * as FrameLoop from "@wardic/core/FrameLoop"
|
|
24
|
+
import type * as ReducedMotion from "@wardic/core/ReducedMotion"
|
|
25
|
+
import type * as UnitScale from "@wardic/core/UnitScale"
|
|
26
|
+
import * as BatchCalls from "@wardic/core/unstable/BatchCalls"
|
|
27
|
+
import type * as Imperative from "@wardic/core/unstable/Imperative"
|
|
28
|
+
import type * as Spine from "@wardic/core/unstable/Spine"
|
|
29
|
+
import * as Arr from "effect/Array"
|
|
30
|
+
import type * as Effect from "effect/Effect"
|
|
31
|
+
import type * as Fiber from "effect/Fiber"
|
|
32
|
+
import * as Predicate from "effect/Predicate"
|
|
33
|
+
import * as Rec from "effect/Record"
|
|
34
|
+
import * as UndefinedOr from "effect/UndefinedOr"
|
|
35
|
+
|
|
36
|
+
/** A stop of a Batch's `animate`: a number for every member, or a function of a member's index and the count. */
|
|
37
|
+
export type Stop = number | ((i: number, n: number) => number)
|
|
38
|
+
|
|
39
|
+
/** A key of `T` a Batch animates: one whose value is a number. */
|
|
40
|
+
export type NumberKey<T> = { readonly [K in keyof T]-?: T[K] extends number ? K : never }[keyof T] & string
|
|
41
|
+
|
|
42
|
+
/** A Batch's targets for an `animate` call: each key a stop, two (its first and its target), or a function giving them. */
|
|
43
|
+
export type Target<T, Key extends string = NumberKey<T>> = {
|
|
44
|
+
readonly [K in Key]?: Stop | readonly [Stop, Stop] | ((i: number, n: number) => number | readonly [number, number])
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A Batch's key in a sequence's Segment of it: a number, `null` for where it is, stops of them, or a function giving them. */
|
|
48
|
+
export type SegmentValue = number | null | ReadonlyArray<number | null> | ((i: number, n: number) => number | null | ReadonlyArray<number | null>)
|
|
49
|
+
|
|
50
|
+
/** A member's handle: `animate` and a sequence's Segment move member `i` alone. */
|
|
51
|
+
export interface Member<T extends object, Key extends string = NumberKey<T>> {
|
|
52
|
+
readonly i: number
|
|
53
|
+
readonly batch: ObjectBatch<T, Key>
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A Batch of objects `T`. */
|
|
57
|
+
// `Key`: the keys its members take, their own numbers for plain objects; `@wardic/three`'s scene keys (#883).
|
|
58
|
+
export interface ObjectBatch<T extends object, Key extends string = NumberKey<T>> {
|
|
59
|
+
/** Its members, in order. */
|
|
60
|
+
readonly members: ReadonlyArray<T>
|
|
61
|
+
/** Member `i`'s handle, for `animate` and a sequence's Segment. */
|
|
62
|
+
readonly at: (i: number) => Member<T, Key>
|
|
63
|
+
/** Member `i`'s value for `key` now: as drawn, once the Batch has moved; its own before. */
|
|
64
|
+
readonly get: (i: number, key: Key) => number
|
|
65
|
+
/** Member `i`'s velocity for `key` now, per second: 0 before the Batch has moved. */
|
|
66
|
+
readonly velocity: (i: number, key: Key) => number
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** `wardic.batch`'s options. */
|
|
70
|
+
export interface Options<T, Key extends string = NumberKey<T>> {
|
|
71
|
+
/** Its keys, fixed now: else its first `animate`'s, or its first Segment's. */
|
|
72
|
+
readonly keys?: ReadonlyArray<Key> | undefined
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** What a runtime gives its Batches: its fork, and its Environment's Positional keys and Default transitions. */
|
|
76
|
+
export interface Context {
|
|
77
|
+
readonly runFork: <A, E>(effect: Effect.Effect<A, E, FrameLoop.FrameLoop>) => Fiber.Fiber<A, E>
|
|
78
|
+
/** The Environment's unit scale: each key's units for its Batches' rest thresholds. */
|
|
79
|
+
// ADR-0069 decision 8.
|
|
80
|
+
readonly unitScale: UnitScale.Rule
|
|
81
|
+
/** How the Environment draws a Batch's members: plain objects' {@link writer}, or another Environment's. */
|
|
82
|
+
readonly writer: Batch.Writer<object>
|
|
83
|
+
/** How it reads a member's key before the Batch moves it: plain objects' {@link read}, the property. */
|
|
84
|
+
readonly read: (subject: object, key: string) => unknown
|
|
85
|
+
readonly positionalKeys: ReducedMotion.PositionalKeys["Service"]
|
|
86
|
+
readonly defaultTransition: DefaultTransition.DefaultTransition["Service"]
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The property writer for a Batch's keys: each member's values onto its properties, by name. */
|
|
90
|
+
// Per frame: a plain loop over the keys, nothing allocated.
|
|
91
|
+
export const writer: Batch.Writer<object> = {
|
|
92
|
+
writes: () => true,
|
|
93
|
+
forKeys: (keys) => (subject, positions, base) => {
|
|
94
|
+
const object = subject as Record<string, number>
|
|
95
|
+
for (let k = 0; k < keys.length; k++) object[keys[k]] = positions[base + k]
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** A plain object's key, as a Batch reads it before moving it: the property. */
|
|
100
|
+
export const read = (subject: object, key: string): unknown => (subject as Readonly<Record<string, unknown>>)[key]
|
|
101
|
+
|
|
102
|
+
/** Each Batch's and member handle's entry, so `animate` knows them. */
|
|
103
|
+
// Module state (#460, Q7): held weakly by the handle, so it goes with it.
|
|
104
|
+
const ENTRIES = new WeakMap<object, Imperative.BatchEntry>()
|
|
105
|
+
|
|
106
|
+
/** Each member object's place in its Batch, so `animate` of it itself is an error naming its handle. */
|
|
107
|
+
// Module state (#460, Q7): held weakly by the object.
|
|
108
|
+
const MEMBERS = new WeakMap<object, number>()
|
|
109
|
+
|
|
110
|
+
/** The Batch or member handle `given` is, for `animate`: `undefined` for anything else. */
|
|
111
|
+
export const entryOf = (given: unknown): Imperative.BatchEntry | undefined => {
|
|
112
|
+
if (!Predicate.isObject(given)) return undefined
|
|
113
|
+
return ENTRIES.get(given)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** `object`'s member number in a Batch, or `undefined` when it's no Batch's. */
|
|
117
|
+
export const memberOf = (object: object): number | undefined => MEMBERS.get(object)
|
|
118
|
+
|
|
119
|
+
/** A Batch call's error, naming what it got. */
|
|
120
|
+
const fail = (message: string): never => {
|
|
121
|
+
throw new Error(`wardic.batch: ${message}`)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** `values`' keys that it gives: a target's or a Segment's. */
|
|
125
|
+
const givenKeys = (values: Readonly<Record<string, unknown>>): ReadonlyArray<string> => Arr.filter(Rec.keys(values), (key) => Predicate.isNotUndefined(values[key]))
|
|
126
|
+
|
|
127
|
+
/** A Batch of `objects` for a runtime, its keys `options.keys`, else its first call's. */
|
|
128
|
+
export const make = <T extends object>(context: Context, objects: ReadonlyArray<T>, options: Options<T> = {}): ObjectBatch<T> => {
|
|
129
|
+
let keys: ReadonlyArray<string> = []
|
|
130
|
+
let keyIndex: ReadonlyMap<string, number> = new Map()
|
|
131
|
+
let batch: Batch.Batch<object> | undefined
|
|
132
|
+
// Each member's slot in the core Batch, by its number.
|
|
133
|
+
let slots: ReadonlyArray<number> = []
|
|
134
|
+
const memberAt: Array<number> = []
|
|
135
|
+
const count = (): number => objects.length
|
|
136
|
+
const all: ReadonlyArray<number> = Arr.makeBy(objects.length, (i) => i)
|
|
137
|
+
|
|
138
|
+
const planning = (): BatchCalls.Planning => ({
|
|
139
|
+
keys,
|
|
140
|
+
positionalKeys: context.positionalKeys,
|
|
141
|
+
defaultFor: (k, target) => context.defaultTransition.transitionFor(keys[k] as string, target) as Batch.Transition,
|
|
142
|
+
fail,
|
|
143
|
+
})
|
|
144
|
+
const bySlot: BatchCalls.Members = { indexOf: (slot) => memberAt[slot] as number, count, nameOf: (slot) => `member ${memberAt[slot]}` }
|
|
145
|
+
const byMember: BatchCalls.Members = { indexOf: (i) => i, count, nameOf: (i) => `member ${i}` }
|
|
146
|
+
|
|
147
|
+
/** Member `i`'s start on the Batch's keys: its own values, numbers each, else an error naming it. */
|
|
148
|
+
const startOf = (object: T, i: number): Readonly<Record<string, number>> =>
|
|
149
|
+
Rec.fromEntries(
|
|
150
|
+
Arr.map(keys, (key) => {
|
|
151
|
+
const value = context.read(object, key)
|
|
152
|
+
if (!Predicate.isNumber(value)) fail(`member ${i}'s "${key}" is ${JSON.stringify(value)}: a Batch's keys are numbers on each of its objects`)
|
|
153
|
+
return [key, value as number] as const
|
|
154
|
+
}),
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
/** The core Batch, made on `given` keys at its first need, each member entered at its own values. */
|
|
158
|
+
const made = (given: ReadonlyArray<string>): Batch.Batch<object> => {
|
|
159
|
+
if (Predicate.isNotUndefined(batch)) return batch
|
|
160
|
+
keys = given
|
|
161
|
+
keyIndex = new Map(Arr.map(keys, (key, k) => [key, k] as const))
|
|
162
|
+
const transition = Rec.fromEntries(Arr.map(keys, (key) => [key, context.defaultTransition.transitionFor(key, 1) as Batch.Transition] as const))
|
|
163
|
+
const created = Batch.make({ keys, writer: context.writer, transition, unitScale: context.unitScale })
|
|
164
|
+
slots = Arr.map(objects, (object, i) => {
|
|
165
|
+
const slot = Batch.enter(created, object as object, startOf(object, i))
|
|
166
|
+
memberAt[slot] = i
|
|
167
|
+
return slot
|
|
168
|
+
})
|
|
169
|
+
batch = created
|
|
170
|
+
return created
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** `given`'s keys, each one of the Batch's, else an error naming it and the way to give more. */
|
|
174
|
+
const checked = (given: ReadonlyArray<string>): void =>
|
|
175
|
+
Arr.forEach(given, (key) => {
|
|
176
|
+
if (keyIndex.has(key)) return
|
|
177
|
+
fail(`"${key}" isn't one of its keys (${keys.join(", ")}): a Batch's keys are its first animate's, so give them all as \`keys\` when it's made, wardic.batch(objects, { keys })`)
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
/** Its run on the Frame loop, while a member moves. */
|
|
181
|
+
const wake = (running: Batch.Batch<object>): void => {
|
|
182
|
+
if (Batch.isMoving(running)) void context.runFork(Batch.run(running))
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** A driven change's controls, as an `animate` call's track. */
|
|
186
|
+
const controlsOf = (running: Batch.Batch<object>, animation: Batch.Animation): Spine.ImperativeTrack => ({
|
|
187
|
+
run: () => {
|
|
188
|
+
Batch.play(animation)
|
|
189
|
+
wake(running)
|
|
190
|
+
},
|
|
191
|
+
stop: () => {
|
|
192
|
+
Batch.stop(animation)
|
|
193
|
+
wake(running)
|
|
194
|
+
},
|
|
195
|
+
cancel: () => {
|
|
196
|
+
Batch.cancel(animation)
|
|
197
|
+
wake(running)
|
|
198
|
+
},
|
|
199
|
+
duration: () => Batch.durationOf(animation),
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
/** The slots `named` members hold, every member's when it names none. */
|
|
203
|
+
const slotsOf = (named: ReadonlyArray<number> | undefined): ReadonlyArray<number> =>
|
|
204
|
+
Arr.map(UndefinedOr.match(named, { onUndefined: () => all, onDefined: (given) => given }), (i) => slots[i] as number)
|
|
205
|
+
|
|
206
|
+
const binding: Imperative.BatchCalls = {
|
|
207
|
+
track: (call) => {
|
|
208
|
+
const given = call.target as Readonly<Record<string, unknown>>
|
|
209
|
+
const running = made(givenKeys(given))
|
|
210
|
+
checked(givenKeys(given))
|
|
211
|
+
const moving = slotsOf(call.members)
|
|
212
|
+
const { toOf, fromOf } = BatchCalls.callTargets(planning(), keyIndex, bySlot, moving, given)
|
|
213
|
+
return controlsOf(running, Batch.animate(running, moving, toOf, BatchCalls.animateOptions(planning(), call, toOf, fromOf)))
|
|
214
|
+
},
|
|
215
|
+
namedOf: (entry) => UndefinedOr.map(entry.member, (i) => [i]),
|
|
216
|
+
segmentOf: (named, target) => {
|
|
217
|
+
made(givenKeys(target))
|
|
218
|
+
const moving = UndefinedOr.match(named, { onUndefined: () => all, onDefined: (given) => given })
|
|
219
|
+
return { subjects: Arr.map(moving, (i) => memberEntries[i] as Imperative.BatchMember), values: BatchCalls.segmentValues(planning(), (key) => keyIndex.has(key), byMember, moving, target) }
|
|
220
|
+
},
|
|
221
|
+
sequenceTrack: (call) => {
|
|
222
|
+
const running = made(keys)
|
|
223
|
+
const moving: Array<number> = []
|
|
224
|
+
const animations: Array<(typeof call.animations)[number] | undefined> = []
|
|
225
|
+
Arr.forEach(call.members, (i, n) => {
|
|
226
|
+
const slot = slots[i] as number
|
|
227
|
+
moving.push(slot)
|
|
228
|
+
animations[slot] = call.animations[n]
|
|
229
|
+
})
|
|
230
|
+
const keyframesOf = BatchCalls.keyframesFor(planning(), call, (slot) => UndefinedOr.getOrThrow(animations[slot]))
|
|
231
|
+
return controlsOf(running, Batch.keyframes(running, moving, keyframesOf, BatchCalls.keyframesOptions(call)))
|
|
232
|
+
},
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const memberEntries: ReadonlyArray<Imperative.BatchMember> = Arr.map(objects, (_, i) => ({ binding, member: i }))
|
|
236
|
+
const handles: Array<Member<T> | undefined> = []
|
|
237
|
+
const handle: ObjectBatch<T> = {
|
|
238
|
+
members: objects,
|
|
239
|
+
at: (i) => {
|
|
240
|
+
const known = handles[i]
|
|
241
|
+
if (Predicate.isNotUndefined(known)) return known
|
|
242
|
+
if (Predicate.isUndefined(objects[i])) fail(`it has ${objects.length} members: there's no member ${i}`)
|
|
243
|
+
const made: Member<T> = { i, batch: handle }
|
|
244
|
+
ENTRIES.set(made, memberEntries[i] as Imperative.BatchMember)
|
|
245
|
+
handles[i] = made
|
|
246
|
+
return made
|
|
247
|
+
},
|
|
248
|
+
get: (i, key) =>
|
|
249
|
+
UndefinedOr.match(batch, {
|
|
250
|
+
onUndefined: () => context.read(objects[i] as object, key) as number,
|
|
251
|
+
onDefined: (running) => Batch.get(running, slots[i] as number, key),
|
|
252
|
+
}),
|
|
253
|
+
velocity: (i, key) =>
|
|
254
|
+
UndefinedOr.match(batch, {
|
|
255
|
+
onUndefined: () => 0,
|
|
256
|
+
onDefined: (running) => Batch.velocity(running, slots[i] as number, key),
|
|
257
|
+
}),
|
|
258
|
+
}
|
|
259
|
+
ENTRIES.set(handle, { binding, member: undefined })
|
|
260
|
+
Arr.forEach(objects, (object, i) => {
|
|
261
|
+
if (Predicate.isNotUndefined(MEMBERS.get(object))) fail(`member ${i} is already a member of a Batch: an object is one Batch's`)
|
|
262
|
+
MEMBERS.set(object, i)
|
|
263
|
+
})
|
|
264
|
+
if (Predicate.isNotUndefined(options.keys)) made(options.keys)
|
|
265
|
+
return handle
|
|
266
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The property writer: Wardic's values written onto a plain object's properties, by their names, as
|
|
3
|
+
* they are (`object[key] = value`), once a frame, in the frame that computed them. `layer` is what
|
|
4
|
+
* `WardicObject` provides; `GATE` is what its Flush gate reads of an object.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* import * as ObjectRenderer from "@wardic/object/ObjectRenderer"
|
|
8
|
+
*
|
|
9
|
+
* ObjectRenderer.commit(material, { opacity: 0.5 }) // material.opacity === 0.5
|
|
10
|
+
*/
|
|
11
|
+
// `@wardic/object/ObjectRenderer` (#882, ADR-0068 decision 2; ADR-0073, *Amendment (#882)*, Q5): core's
|
|
12
|
+
// `Renderer`, as ADR-0041 names an Environment's implementations. No grouping: `x` is the property `x`
|
|
13
|
+
// (three's keys are `@wardic/three`'s, step 2).
|
|
14
|
+
import * as Renderer from "@wardic/core/Renderer"
|
|
15
|
+
import type * as FlushGate from "@wardic/core/unstable/FlushGate"
|
|
16
|
+
import * as Layer from "effect/Layer"
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What the gate saves of a property before its first write, to put back if the key ever drops out:
|
|
20
|
+
* nothing. A plain object's values stay where Wardic leaves them, so a commit skips a key given this.
|
|
21
|
+
*/
|
|
22
|
+
const NOTHING_SAVED = ""
|
|
23
|
+
|
|
24
|
+
/** A plain object's patch: its values as they are, one property each. */
|
|
25
|
+
export const project = (values: Readonly<Record<string, unknown>>): Renderer.Patch => values as Renderer.Patch
|
|
26
|
+
|
|
27
|
+
/** `patch` written onto `target`'s properties: each key's value, a key the gate puts back with nothing saved left as it is. */
|
|
28
|
+
// Per frame: a plain loop over the patch's keys.
|
|
29
|
+
export const commit = (target: unknown, patch: Renderer.Patch): void => {
|
|
30
|
+
const object = target as Record<string, unknown>
|
|
31
|
+
for (const key in patch) {
|
|
32
|
+
const value = patch[key]
|
|
33
|
+
if (value === NOTHING_SAVED) continue
|
|
34
|
+
object[key] = value
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The Flush gate's reads of an object: nothing saved before a write, and a template key no object has. */
|
|
39
|
+
export const GATE: FlushGate.GateEnvironment = { valueBeforeOf: () => NOTHING_SAVED, templateKey: "@wardic/object/template" }
|
|
40
|
+
|
|
41
|
+
/** The property writer as the Environment's `Renderer`. */
|
|
42
|
+
export const layer: Layer.Layer<Renderer.Renderer> = /*#__PURE__*/ Layer.succeed(Renderer.Renderer, { project, commit })
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A plain object's values for Wardic: where each key it animates starts (the property's own value), and
|
|
3
|
+
* the ports core reads an Environment's values through, with no style, no layout to measure and no
|
|
4
|
+
* variables. A key the object doesn't have, or whose value is neither a number nor a string, is an
|
|
5
|
+
* error naming it: Wardic writes no property the object didn't have.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* import * as ObjectValues from "@wardic/object/ObjectValues"
|
|
9
|
+
*
|
|
10
|
+
* const links = ObjectValues.startLinksOf({ opacity: 1 })
|
|
11
|
+
* links.ownValue("opacity") // Option.some(1)
|
|
12
|
+
*/
|
|
13
|
+
// `@wardic/object/ObjectValues` (#882; ADR-0073, *Amendment (#882)*, Q5): core's `OwnValues`,
|
|
14
|
+
// `MeasuredPairs` and `VariableValues` ports, and a Subject's start links for core's Imperative track.
|
|
15
|
+
import * as BaseValue from "@wardic/core/BaseValue"
|
|
16
|
+
import type * as InterpolatorRegistry from "@wardic/core/InterpolatorRegistry"
|
|
17
|
+
import type * as MeasuredPairs from "@wardic/core/unstable/MeasuredPairs"
|
|
18
|
+
import type * as OwnValues from "@wardic/core/unstable/OwnValues"
|
|
19
|
+
import type * as Spine from "@wardic/core/unstable/Spine"
|
|
20
|
+
import type * as VariableValues from "@wardic/core/unstable/VariableValues"
|
|
21
|
+
import * as Bool from "effect/Boolean"
|
|
22
|
+
import * as Option from "effect/Option"
|
|
23
|
+
import * as Predicate from "effect/Predicate"
|
|
24
|
+
|
|
25
|
+
/** Whether `name` is a class's own: a name, not `Object`'s. */
|
|
26
|
+
const isClassName = (name: unknown): name is string => Bool.and(Predicate.isString(name), Bool.and(name !== "", name !== "Object"))
|
|
27
|
+
|
|
28
|
+
/** What `subject` is called in an error: its class's name, else "the object". */
|
|
29
|
+
const nameOf = (subject: object): string =>
|
|
30
|
+
Option.fromNullishOr((subject as { readonly constructor?: { readonly name?: unknown } }).constructor).pipe(
|
|
31
|
+
Option.map((made) => made.name),
|
|
32
|
+
Option.filter(isClassName),
|
|
33
|
+
Option.match({ onNone: () => "the object", onSome: (name) => `a ${name}` }),
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
/** `subject`'s own value for `key`: a number or a string, else an error naming the key. */
|
|
37
|
+
const ownValueOf = (subject: object, key: string): Option.Option<number | string> => {
|
|
38
|
+
const value = (subject as Readonly<Record<string, unknown>>)[key]
|
|
39
|
+
if (Bool.or(Predicate.isNumber(value), Predicate.isString(value))) return Option.some(value as number | string)
|
|
40
|
+
if (Predicate.isUndefined(value)) {
|
|
41
|
+
throw new Error(`wardic.animate: ${nameOf(subject)} has no "${key}" to animate. Give a key it has, a number or a string; Wardic adds no property to an object.`)
|
|
42
|
+
}
|
|
43
|
+
throw new Error(`wardic.animate: ${nameOf(subject)}'s "${key}" is ${typeof value}, which can't be animated: a key's value is a number or a string.`)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** `raw` in the shape of `like`: a number for a number, a string for a string; none otherwise. */
|
|
47
|
+
const valueLike = (raw: unknown, like: number | string): Option.Option<number | string> => {
|
|
48
|
+
if (Predicate.isNumber(like)) {
|
|
49
|
+
if (Predicate.isNumber(raw)) return Option.some(raw)
|
|
50
|
+
return Option.none()
|
|
51
|
+
}
|
|
52
|
+
if (Predicate.isString(raw)) return Option.some(raw)
|
|
53
|
+
return Option.none()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** No style: a plain object has none. */
|
|
57
|
+
const noStyle = (): Option.Option<number | string> => Option.none()
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Base-value links on `read`: each key starts from what `read` gives for it when the animation
|
|
61
|
+
* starts, an error it throws naming the key; there's no style to start from. Another Environment of
|
|
62
|
+
* objects reads its own way: `@wardic/three`'s finds a key on a mesh's material.
|
|
63
|
+
*/
|
|
64
|
+
// ADR-0022.
|
|
65
|
+
// #883 (ADR-0073, *Amendment (#883)*, Q1): the start links `makeWith`'s Environments share.
|
|
66
|
+
export const startLinksWith = (read: (subject: object, key: string) => Option.Option<number | string>) => (subject: object): Spine.StartLinks => {
|
|
67
|
+
const ownValue = (key: string): Option.Option<number | string> => read(subject, key)
|
|
68
|
+
const linksFor = (like: number | string) => ({
|
|
69
|
+
style: noStyle,
|
|
70
|
+
own: (key: string) => Option.flatMap(ownValue(key), (raw) => valueLike(raw, like)),
|
|
71
|
+
})
|
|
72
|
+
const startLinkOf = (key: string, like: number | string) => BaseValue.start(key, linksFor(like))
|
|
73
|
+
return { ownValue, linksFor, startLinkOf }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* `subject`'s Base-value links: each key starts from its own value, the property as it is
|
|
78
|
+
* when the animation starts; there's no style to start from.
|
|
79
|
+
*/
|
|
80
|
+
// ADR-0022.
|
|
81
|
+
export const startLinksOf: (subject: object) => Spine.StartLinks = /*#__PURE__*/ startLinksWith(ownValueOf)
|
|
82
|
+
|
|
83
|
+
/** No record: a plain object's binding gives no style. */
|
|
84
|
+
const NO_RECORD: Readonly<Record<string, unknown>> = {}
|
|
85
|
+
|
|
86
|
+
/** The Own values port: values in the shape of their targets, and no style. */
|
|
87
|
+
export const ownValues: OwnValues.Shape = {
|
|
88
|
+
valueLike,
|
|
89
|
+
styleValueOf: () => Option.none(),
|
|
90
|
+
recordOf: () => NO_RECORD,
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** No Measured pairs: an object has no layout to measure a value in. */
|
|
94
|
+
const NO_PAIRS: Readonly<Record<string, MeasuredPairs.Requested>> = {}
|
|
95
|
+
|
|
96
|
+
/** The Measured pairs port: none. */
|
|
97
|
+
export const measuredPairs: MeasuredPairs.Shape = {
|
|
98
|
+
needs: () => false,
|
|
99
|
+
measure: () => NO_PAIRS,
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** No Variable values' Key rule. */
|
|
103
|
+
const NO_VARIABLES: InterpolatorRegistry.KeyRule = { name: "no variable values", blend: () => Option.none() }
|
|
104
|
+
|
|
105
|
+
/** The Variable values port: none. */
|
|
106
|
+
export const variableValues: VariableValues.Shape = {
|
|
107
|
+
isValue: () => false,
|
|
108
|
+
values: () => NO_VARIABLES,
|
|
109
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Refresh frames the host pumps: Wardic's frames are the host loop's, so its writes land in the frame
|
|
3
|
+
* that draws them (a scene's **Host loop**: three's `setAnimationLoop`, an XR session's, a game loop).
|
|
4
|
+
* The host calls {@link PumpedFrames.frame} once a frame, before it renders, with the frame's time in
|
|
5
|
+
* ms, and a loop that draws only on demand asks {@link PumpedFrames.wantsFrame} whether Wardic wants
|
|
6
|
+
* another.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* import * as PumpedFrames from "@wardic/object/PumpedFrames"
|
|
10
|
+
*
|
|
11
|
+
* const frames = PumpedFrames.make()
|
|
12
|
+
* renderer.setAnimationLoop((time) => {
|
|
13
|
+
* frames.frame(time)
|
|
14
|
+
* renderer.render(scene, camera)
|
|
15
|
+
* })
|
|
16
|
+
*/
|
|
17
|
+
// `@wardic/object/PumpedFrames` (#882, ADR-0068 decision 5; ADR-0073, *Amendment (#882)*, Q4): core's
|
|
18
|
+
// `RefreshFrameSource`, keeping its three rules. The canvas note's P1 prototyped it, and P5 found frames
|
|
19
|
+
// pumped before the render never a frame late, where window rAF is whenever the scene's loop asked first.
|
|
20
|
+
import * as RefreshFrameSource from "@wardic/core/RefreshFrameSource"
|
|
21
|
+
import type * as Layer from "effect/Layer"
|
|
22
|
+
|
|
23
|
+
/** A callback a frame runs, with its time in ms: its own handle, as `cancelFrame` takes it. */
|
|
24
|
+
type Callback = (timestampMs: number) => void
|
|
25
|
+
|
|
26
|
+
/** Refresh frames the host pumps: core's frame source, and what the host calls. */
|
|
27
|
+
export interface PumpedFrames extends RefreshFrameSource.Shape<Callback> {
|
|
28
|
+
/**
|
|
29
|
+
* Run the frame: every callback asked for before it, with `time` in ms, on the same timebase as
|
|
30
|
+
* `now`. Call it once a frame, before rendering.
|
|
31
|
+
*/
|
|
32
|
+
readonly frame: (time: number) => void
|
|
33
|
+
/** Whether a callback waits for the next frame: what a loop that draws on demand checks to go on. */
|
|
34
|
+
readonly wantsFrame: () => boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** What `performance.now()` is, where the host has one. */
|
|
38
|
+
// @wardic/object compiles against ES2022 alone, which doesn't declare `performance`: a host global
|
|
39
|
+
// wherever Wardic runs (see core's `refresh-frame-clock.ts`).
|
|
40
|
+
declare const performance: { readonly now: () => number }
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Frames the host pumps. `now` is the host's steady clock on its frames' timebase, `performance.now()`
|
|
44
|
+
* when left out; core's Clock reads it to time a frame whose time repeats the one before.
|
|
45
|
+
*/
|
|
46
|
+
export const make = (now: () => number = () => performance.now()): PumpedFrames => {
|
|
47
|
+
// This frame's callbacks and the next's, swapped each frame: nothing allocated a frame.
|
|
48
|
+
let queue: Array<Callback> = []
|
|
49
|
+
let running: Array<Callback> = []
|
|
50
|
+
const requestFrame = (callback: Callback): Callback => {
|
|
51
|
+
queue.push(callback)
|
|
52
|
+
return callback
|
|
53
|
+
}
|
|
54
|
+
const cancelFrame = (handle: Callback): void => {
|
|
55
|
+
const at = queue.indexOf(handle)
|
|
56
|
+
if (at >= 0) queue.splice(at, 1)
|
|
57
|
+
}
|
|
58
|
+
const frame = (time: number): void => {
|
|
59
|
+
// A callback asked for while this frame's run waits for the next (the source's third rule).
|
|
60
|
+
const due = queue
|
|
61
|
+
queue = running
|
|
62
|
+
running = due
|
|
63
|
+
for (let i = 0; i < due.length; i++) due[i](time)
|
|
64
|
+
due.length = 0
|
|
65
|
+
}
|
|
66
|
+
return { requestFrame, cancelFrame, now, frame, wantsFrame: () => queue.length > 0 }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** `frames` as the Environment's Refresh frame source. */
|
|
70
|
+
export const layer = (frames: PumpedFrames): Layer.Layer<RefreshFrameSource.RefreshFrameSource> => RefreshFrameSource.layer(frames as RefreshFrameSource.Shape)
|