@rafaelaugusto/jolt 0.1.0-rc.12 → 0.1.0-rc.13
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/README.md +2 -2
- package/dist/core/circle-math.d.ts.map +1 -1
- package/dist/core/easing.d.ts +8 -0
- package/dist/core/easing.d.ts.map +1 -1
- package/dist/core/easing.js +12 -4
- package/dist/core/easing.js.map +1 -1
- package/dist/core/gesture-math.d.ts.map +1 -1
- package/dist/core/gradient-text.d.ts +24 -0
- package/dist/core/gradient-text.d.ts.map +1 -0
- package/dist/core/gradient-text.js +30 -0
- package/dist/core/gradient-text.js.map +1 -0
- package/dist/core/inspection-plot.d.ts +11 -0
- package/dist/core/inspection-plot.d.ts.map +1 -0
- package/dist/core/inspection-plot.js +57 -0
- package/dist/core/inspection-plot.js.map +1 -0
- package/dist/core/motion-behaviors.d.ts +68 -0
- package/dist/core/motion-behaviors.d.ts.map +1 -0
- package/dist/core/motion-behaviors.js +276 -0
- package/dist/core/motion-behaviors.js.map +1 -0
- package/dist/core/numeric-map.js.map +1 -1
- package/dist/core/particle-motion.d.ts +8 -0
- package/dist/core/particle-motion.d.ts.map +1 -0
- package/dist/core/particle-motion.js +11 -0
- package/dist/core/particle-motion.js.map +1 -0
- package/dist/core/pointer.d.ts +1 -1
- package/dist/core/preset-recipe.d.ts +20 -0
- package/dist/core/preset-recipe.d.ts.map +1 -0
- package/dist/core/preset-recipe.js +79 -0
- package/dist/core/preset-recipe.js.map +1 -0
- package/dist/core/reorder.d.ts +5 -0
- package/dist/core/reorder.d.ts.map +1 -1
- package/dist/core/reorder.js +17 -0
- package/dist/core/reorder.js.map +1 -1
- package/dist/core/rig.d.ts +73 -0
- package/dist/core/rig.d.ts.map +1 -0
- package/dist/core/rig.js +99 -0
- package/dist/core/rig.js.map +1 -0
- package/dist/core/scroll-header.js.map +1 -1
- package/dist/core/sensor-kinds.d.ts +1 -1
- package/dist/core/sensor-kinds.d.ts.map +1 -1
- package/dist/core/sensor-replay.d.ts +10 -0
- package/dist/core/sensor-replay.d.ts.map +1 -0
- package/dist/core/sensor-replay.js +32 -0
- package/dist/core/sensor-replay.js.map +1 -0
- package/dist/gradient-text/index.d.ts +2 -10
- package/dist/gradient-text/index.d.ts.map +1 -1
- package/dist/gradient-text/index.js +2 -25
- package/dist/gradient-text/index.js.map +1 -1
- package/dist/gradient-text/native.d.ts +2 -8
- package/dist/gradient-text/native.d.ts.map +1 -1
- package/dist/gradient-text/native.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/inspector/plot.d.ts +1 -3
- package/dist/inspector/plot.d.ts.map +1 -1
- package/dist/inspector/plot.js +1 -22
- package/dist/inspector/plot.js.map +1 -1
- package/dist/motion-behaviors.d.ts +5 -54
- package/dist/motion-behaviors.d.ts.map +1 -1
- package/dist/motion-behaviors.js +18 -267
- package/dist/motion-behaviors.js.map +1 -1
- package/dist/rig/index.d.ts +2 -27
- package/dist/rig/index.d.ts.map +1 -1
- package/dist/rig/index.js +5 -89
- package/dist/rig/index.js.map +1 -1
- package/dist/testing/sensors.d.ts +2 -9
- package/dist/testing/sensors.d.ts.map +1 -1
- package/dist/testing/sensors.js +3 -21
- package/dist/testing/sensors.js.map +1 -1
- package/dist/web/angular/index.d.ts +62 -0
- package/dist/web/angular/index.d.ts.map +1 -0
- package/dist/web/angular/index.js +190 -0
- package/dist/web/angular/index.js.map +1 -0
- package/dist/web/behaviors.d.ts +5 -0
- package/dist/web/behaviors.d.ts.map +1 -0
- package/dist/web/behaviors.js +2 -0
- package/dist/web/behaviors.js.map +1 -0
- package/dist/web/bottom-sheet.js.map +1 -1
- package/dist/web/compose.js.map +1 -1
- package/dist/web/curve.js.map +1 -1
- package/dist/web/decay.js.map +1 -1
- package/dist/web/diagnostics.d.ts +1 -1
- package/dist/web/diagnostics.d.ts.map +1 -1
- package/dist/web/diagnostics.js.map +1 -1
- package/dist/web/drag.d.ts +11 -11
- package/dist/web/drag.d.ts.map +1 -1
- package/dist/web/effect.d.ts +2 -1
- package/dist/web/effect.d.ts.map +1 -1
- package/dist/web/effect.js +11 -4
- package/dist/web/effect.js.map +1 -1
- package/dist/web/frameworks/binding.d.ts +39 -0
- package/dist/web/frameworks/binding.d.ts.map +1 -0
- package/dist/web/frameworks/binding.js +356 -0
- package/dist/web/frameworks/binding.js.map +1 -0
- package/dist/web/gestures.js.map +1 -1
- package/dist/web/gradient-css.d.ts +2 -0
- package/dist/web/gradient-css.d.ts.map +1 -0
- package/dist/web/gradient-css.js +5 -0
- package/dist/web/gradient-css.js.map +1 -0
- package/dist/web/gradient-text.d.ts +24 -0
- package/dist/web/gradient-text.d.ts.map +1 -0
- package/dist/web/gradient-text.js +179 -0
- package/dist/web/gradient-text.js.map +1 -0
- package/dist/web/gradient.d.ts.map +1 -1
- package/dist/web/gradient.js +2 -1
- package/dist/web/gradient.js.map +1 -1
- package/dist/web/image-surface.d.ts.map +1 -1
- package/dist/web/image-surface.js.map +1 -1
- package/dist/web/inspector.d.ts +24 -0
- package/dist/web/inspector.d.ts.map +1 -1
- package/dist/web/inspector.js +76 -5
- package/dist/web/inspector.js.map +1 -1
- package/dist/web/interaction.d.ts +4 -0
- package/dist/web/interaction.d.ts.map +1 -1
- package/dist/web/interaction.js.map +1 -1
- package/dist/web/keyboard.js.map +1 -1
- package/dist/web/layout.d.ts +75 -75
- package/dist/web/layout.d.ts.map +1 -1
- package/dist/web/layout.js.map +1 -1
- package/dist/web/particles.d.ts.map +1 -1
- package/dist/web/particles.js +5 -6
- package/dist/web/particles.js.map +1 -1
- package/dist/web/plan.d.ts +2 -0
- package/dist/web/plan.d.ts.map +1 -1
- package/dist/web/plan.js.map +1 -1
- package/dist/web/press.js.map +1 -1
- package/dist/web/progress.d.ts.map +1 -1
- package/dist/web/properties.d.ts +4 -4
- package/dist/web/properties.d.ts.map +1 -1
- package/dist/web/property.d.ts.map +1 -1
- package/dist/web/property.js.map +1 -1
- package/dist/web/public.d.ts +19 -3
- package/dist/web/public.d.ts.map +1 -1
- package/dist/web/public.js +9 -0
- package/dist/web/public.js.map +1 -1
- package/dist/web/react/index.d.ts +12 -0
- package/dist/web/react/index.d.ts.map +1 -0
- package/dist/web/react/index.js +119 -0
- package/dist/web/react/index.js.map +1 -0
- package/dist/web/reorder.d.ts.map +1 -1
- package/dist/web/reorder.js +23 -2
- package/dist/web/reorder.js.map +1 -1
- package/dist/web/rig.d.ts +43 -0
- package/dist/web/rig.d.ts.map +1 -0
- package/dist/web/rig.js +307 -0
- package/dist/web/rig.js.map +1 -0
- package/dist/web/runtime.d.ts +37 -25
- package/dist/web/runtime.d.ts.map +1 -1
- package/dist/web/runtime.js +111 -8
- package/dist/web/runtime.js.map +1 -1
- package/dist/web/scheduler.d.ts.map +1 -1
- package/dist/web/scheduler.js.map +1 -1
- package/dist/web/scope.d.ts +7 -1
- package/dist/web/scope.d.ts.map +1 -1
- package/dist/web/scope.js +16 -2
- package/dist/web/scope.js.map +1 -1
- package/dist/web/scroll-header.js.map +1 -1
- package/dist/web/scroll-range.d.ts.map +1 -1
- package/dist/web/scroll-range.js.map +1 -1
- package/dist/web/scroll.d.ts +1 -1
- package/dist/web/scroll.d.ts.map +1 -1
- package/dist/web/scroll.js.map +1 -1
- package/dist/web/sensor-replay-bridge.d.ts +16 -0
- package/dist/web/sensor-replay-bridge.d.ts.map +1 -0
- package/dist/web/sensor-replay-bridge.js +12 -0
- package/dist/web/sensor-replay-bridge.js.map +1 -0
- package/dist/web/sensor-replay.d.ts +22 -0
- package/dist/web/sensor-replay.d.ts.map +1 -0
- package/dist/web/sensor-replay.js +124 -0
- package/dist/web/sensor-replay.js.map +1 -0
- package/dist/web/sensors.d.ts +14 -5
- package/dist/web/sensors.d.ts.map +1 -1
- package/dist/web/sensors.js +159 -41
- package/dist/web/sensors.js.map +1 -1
- package/dist/web/shape.d.ts +2 -0
- package/dist/web/shape.d.ts.map +1 -1
- package/dist/web/shape.js +44 -12
- package/dist/web/shape.js.map +1 -1
- package/dist/web/shared-layout.js.map +1 -1
- package/dist/web/size.d.ts.map +1 -1
- package/dist/web/size.js.map +1 -1
- package/dist/web/solid/index.d.ts +16 -0
- package/dist/web/solid/index.d.ts.map +1 -0
- package/dist/web/solid/index.js +89 -0
- package/dist/web/solid/index.js.map +1 -0
- package/dist/web/spring-value.d.ts.map +1 -1
- package/dist/web/spring-value.js.map +1 -1
- package/dist/web/stroke.d.ts.map +1 -1
- package/dist/web/stroke.js.map +1 -1
- package/dist/web/style.d.ts.map +1 -1
- package/dist/web/style.js.map +1 -1
- package/dist/web/surface-effect.d.ts.map +1 -1
- package/dist/web/surface-effect.js.map +1 -1
- package/dist/web/svelte/index.d.ts +50 -0
- package/dist/web/svelte/index.d.ts.map +1 -0
- package/dist/web/svelte/index.js +68 -0
- package/dist/web/svelte/index.js.map +1 -0
- package/dist/web/testing.d.ts +1 -0
- package/dist/web/testing.d.ts.map +1 -1
- package/dist/web/testing.js +1 -0
- package/dist/web/testing.js.map +1 -1
- package/dist/web/timers.d.ts.map +1 -1
- package/dist/web/timers.js.map +1 -1
- package/dist/web/types.d.ts.map +1 -1
- package/dist/web/value-playback.d.ts +17 -17
- package/dist/web/value-playback.js.map +1 -1
- package/dist/web/value.d.ts.map +1 -1
- package/dist/web/value.js.map +1 -1
- package/dist/web/viewport.js.map +1 -1
- package/dist/web/vue/index.d.ts +12 -3
- package/dist/web/vue/index.d.ts.map +1 -1
- package/dist/web/vue/index.js +115 -35
- package/dist/web/vue/index.js.map +1 -1
- package/package.json +35 -6
- package/src/core/easing.ts +12 -4
- package/src/core/gradient-text.ts +56 -0
- package/src/core/inspection-plot.ts +74 -0
- package/src/core/motion-behaviors.ts +337 -0
- package/src/core/particle-motion.ts +20 -0
- package/src/core/preset-recipe.ts +111 -0
- package/src/core/reorder.ts +23 -0
- package/src/core/rig.ts +149 -0
- package/src/core/sensor-replay.ts +48 -0
- package/src/gradient-text/index.ts +7 -43
- package/src/gradient-text/native.ts +2 -7
- package/src/index.ts +6 -0
- package/src/inspector/plot.ts +1 -27
- package/src/motion-behaviors.ts +34 -316
- package/src/rig/index.ts +25 -124
- package/src/testing/sensors.ts +4 -36
- package/src/web/angular/index.ts +171 -0
- package/src/web/behaviors.ts +12 -0
- package/src/web/effect.ts +10 -5
- package/src/web/frameworks/binding.ts +408 -0
- package/src/web/gradient-css.ts +8 -0
- package/src/web/gradient-text.ts +234 -0
- package/src/web/gradient.ts +2 -1
- package/src/web/inspector.ts +80 -5
- package/src/web/interaction.ts +4 -0
- package/src/web/particles.ts +15 -9
- package/src/web/plan.ts +2 -0
- package/src/web/public.ts +51 -4
- package/src/web/react/index.ts +208 -0
- package/src/web/reorder.ts +21 -3
- package/src/web/rig.ts +377 -0
- package/src/web/runtime.ts +128 -11
- package/src/web/scope.ts +32 -4
- package/src/web/sensor-replay-bridge.ts +23 -0
- package/src/web/sensor-replay.ts +140 -0
- package/src/web/sensors.ts +179 -57
- package/src/web/shape.ts +42 -9
- package/src/web/solid/index.ts +102 -0
- package/src/web/svelte/index.ts +88 -0
- package/src/web/testing.ts +6 -0
- package/src/web/vue/index.ts +122 -31
package/src/motion-behaviors.ts
CHANGED
|
@@ -1,325 +1,43 @@
|
|
|
1
1
|
import { Application } from '@nativescript/core'
|
|
2
2
|
import { motionScope, type MotionScope } from './motion-scope.js'
|
|
3
|
+
import { createMotionBehaviors } from './core/motion-behaviors.js'
|
|
4
|
+
import type { MotionBehaviorsOptions, MotionBehaviors } from './core/motion-behaviors.js'
|
|
5
|
+
export type {
|
|
6
|
+
BehaviorStatus,
|
|
7
|
+
BehaviorResult,
|
|
8
|
+
BehaviorRun,
|
|
9
|
+
MotionBehaviorsOptions,
|
|
10
|
+
MotionBehaviors,
|
|
11
|
+
} from './core/motion-behaviors.js'
|
|
12
|
+
export type BehaviorContext =
|
|
13
|
+
import('./core/motion-behaviors.js').BehaviorContext<MotionScope>
|
|
14
|
+
export type MotionBehavior =
|
|
15
|
+
import('./core/motion-behaviors.js').MotionBehavior<MotionScope>
|
|
3
16
|
|
|
4
|
-
|
|
5
|
-
'completed' | 'interrupted' | 'cancelled' | 'failed' | 'blocked'
|
|
6
|
-
export interface BehaviorResult {
|
|
7
|
-
readonly status: BehaviorStatus
|
|
8
|
-
readonly error?: unknown
|
|
9
|
-
}
|
|
10
|
-
export interface BehaviorRun {
|
|
11
|
-
readonly id: number
|
|
12
|
-
readonly name: string
|
|
13
|
-
readonly finished: Promise<BehaviorResult>
|
|
14
|
-
cancel(): void
|
|
15
|
-
}
|
|
16
|
-
export interface BehaviorContext {
|
|
17
|
-
/** Private to this run; cancellation disposes it before the next run starts. */
|
|
18
|
-
readonly scope: MotionScope
|
|
19
|
-
readonly id: number
|
|
20
|
-
readonly active: boolean
|
|
21
|
-
/** Check after an external await, before starting work or changing application state. */
|
|
22
|
-
checkpoint(): void
|
|
23
|
-
/** Await work and check identity before continuing. False animation results cancel the run. */
|
|
24
|
-
wait<T>(work: PromiseLike<T>): Promise<T>
|
|
25
|
-
/** Guard a callback that may arrive after interruption (e.g. a service response). */
|
|
26
|
-
guard<A extends unknown[]>(callback: (...args: A) => void): (...args: A) => void
|
|
27
|
-
}
|
|
28
|
-
export interface MotionBehavior {
|
|
29
|
-
priority?: number
|
|
30
|
-
/** Evaluated on each request. Domain state stays in the application. */
|
|
31
|
-
when?: () => boolean
|
|
32
|
-
/** Explicit exclusions, including higher-priority requests. Defaults to none. */
|
|
33
|
-
blocks?: readonly string[]
|
|
34
|
-
/** Equal-priority requests, including the same name, reject by default. */
|
|
35
|
-
equal?: 'reject' | 'replace'
|
|
36
|
-
run(context: BehaviorContext): void | PromiseLike<unknown>
|
|
37
|
-
}
|
|
38
|
-
export interface MotionBehaviorsOptions<N extends string> {
|
|
39
|
-
/** Restart this declared rest behavior after success, cancellation or failure. No implicit resume. */
|
|
40
|
-
rest?: N
|
|
41
|
-
/** Receive failure after cleanup. Throwing here does not strand another run. */
|
|
42
|
-
onError?: (error: unknown, name: N) => void
|
|
43
|
-
}
|
|
44
|
-
export interface MotionBehaviors<N extends string = string> {
|
|
45
|
-
readonly current: N | undefined
|
|
46
|
-
readonly disposed: boolean
|
|
47
|
-
readonly paused: boolean
|
|
48
|
-
can(name: N): boolean
|
|
49
|
-
request(name: N): BehaviorRun
|
|
50
|
-
/** Cancel the active run and restore the declared rest behavior. */
|
|
51
|
-
cancel(): void
|
|
52
|
-
pause(): void
|
|
53
|
-
play(): void
|
|
54
|
-
/** Cancel without restoration; later requests may start again. */
|
|
55
|
-
stop(): void
|
|
56
|
-
dispose(): void
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** One exclusive interaction lane; other coordinators and unrelated input remain independent. */
|
|
17
|
+
/** One exclusive interaction lane, backed by native scopes and app lifecycle. */
|
|
60
18
|
export function motionBehaviors<const D extends Record<string, MotionBehavior>>(
|
|
61
19
|
definitions: D,
|
|
62
20
|
options: MotionBehaviorsOptions<Extract<keyof D, string>> = {},
|
|
63
21
|
): MotionBehaviors<Extract<keyof D, string>> {
|
|
64
|
-
type Name = Extract<keyof D, string>
|
|
65
|
-
// Copy policy so changing the caller's object cannot silently change an active run.
|
|
66
|
-
const policies = new Map<Name, MotionBehavior>()
|
|
67
|
-
for (const [name, definition] of Object.entries(definitions)) {
|
|
68
|
-
if (
|
|
69
|
-
!definition ||
|
|
70
|
-
typeof definition.run !== 'function' ||
|
|
71
|
-
!Number.isFinite(definition.priority ?? 0) ||
|
|
72
|
-
(definition.when !== undefined && typeof definition.when !== 'function') ||
|
|
73
|
-
(definition.equal !== undefined &&
|
|
74
|
-
!['reject', 'replace'].includes(definition.equal)) ||
|
|
75
|
-
(definition.blocks !== undefined &&
|
|
76
|
-
(!Array.isArray(definition.blocks) ||
|
|
77
|
-
definition.blocks.some((n) => !Object.hasOwn(definitions, n))))
|
|
78
|
-
)
|
|
79
|
-
throw new TypeError(`Invalid motion behavior "${name}".`)
|
|
80
|
-
policies.set(name as Name, { ...definition, blocks: [...(definition.blocks ?? [])] })
|
|
81
|
-
}
|
|
82
|
-
if (options.rest !== undefined && !policies.has(options.rest))
|
|
83
|
-
throw new TypeError('The rest behavior must be declared.')
|
|
84
|
-
const cancelled = Symbol('cancelled behavior')
|
|
85
|
-
let serial = 0,
|
|
86
|
-
disposed = false,
|
|
87
|
-
paused = false,
|
|
88
|
-
changing = false,
|
|
89
|
-
restPending = false
|
|
90
22
|
let background = Application.suspended
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
const finish = (run: Active, status: BehaviorStatus, error?: unknown) => {
|
|
114
|
-
if (run.done) return
|
|
115
|
-
run.done = true
|
|
116
|
-
run.wake?.()
|
|
117
|
-
if (active === run) active = undefined
|
|
118
|
-
try {
|
|
119
|
-
run.scope.dispose()
|
|
120
|
-
} catch (cleanup) {
|
|
121
|
-
error =
|
|
122
|
-
error === undefined
|
|
123
|
-
? cleanup
|
|
124
|
-
: new AggregateError([error, cleanup], 'Behavior cleanup failed.')
|
|
125
|
-
status = 'failed'
|
|
126
|
-
}
|
|
127
|
-
run.resolve({ status, ...(error === undefined ? {} : { error }) })
|
|
128
|
-
if (error !== undefined) report(error, run.name)
|
|
129
|
-
}
|
|
130
|
-
const restore = (previous?: Name) => {
|
|
131
|
-
if (!disposed && !active && options.rest !== undefined && previous !== options.rest) {
|
|
132
|
-
restPending = isPaused()
|
|
133
|
-
if (!isPaused()) {
|
|
134
|
-
try {
|
|
135
|
-
controller.request(options.rest)
|
|
136
|
-
} catch (error) {
|
|
137
|
-
report(error, options.rest)
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
const allowed = (name: Name) => {
|
|
143
|
-
const candidate = policy(name)
|
|
144
|
-
if (disposed || isPaused() || changing) return false
|
|
145
|
-
let permitted: boolean
|
|
146
|
-
changing = true
|
|
147
|
-
try {
|
|
148
|
-
permitted = candidate.when?.() !== false
|
|
149
|
-
} finally {
|
|
150
|
-
changing = false
|
|
151
|
-
}
|
|
152
|
-
if (!permitted || disposed || isPaused()) return false
|
|
153
|
-
if (!active) return true
|
|
154
|
-
const current = policy(active.name)
|
|
155
|
-
if (current.blocks?.includes(name)) return false
|
|
156
|
-
const delta = (candidate.priority ?? 0) - (current.priority ?? 0)
|
|
157
|
-
return delta > 0 || (delta === 0 && candidate.equal === 'replace')
|
|
158
|
-
}
|
|
159
|
-
const cancel = (restoreRest: boolean) => {
|
|
160
|
-
if (changing) return
|
|
161
|
-
restPending = false
|
|
162
|
-
const previous = active?.name
|
|
163
|
-
changing = true
|
|
164
|
-
try {
|
|
165
|
-
if (active) finish(active, 'cancelled')
|
|
166
|
-
} finally {
|
|
167
|
-
changing = false
|
|
168
|
-
}
|
|
169
|
-
if (restoreRest) restore(previous)
|
|
170
|
-
}
|
|
171
|
-
const controller: MotionBehaviors<Name> = {
|
|
172
|
-
get current() {
|
|
173
|
-
return active?.name
|
|
174
|
-
},
|
|
175
|
-
get disposed() {
|
|
176
|
-
return disposed
|
|
177
|
-
},
|
|
178
|
-
get paused() {
|
|
179
|
-
return isPaused()
|
|
180
|
-
},
|
|
181
|
-
can: allowed,
|
|
182
|
-
request(name) {
|
|
183
|
-
const id = ++serial
|
|
184
|
-
if (!allowed(name))
|
|
185
|
-
return { id, name, finished: Promise.resolve({ status: 'blocked' }), cancel() {} }
|
|
186
|
-
// Guard/exclusion callbacks may have changed ownership; evaluate them before mutation.
|
|
187
|
-
const definition = policy(name)
|
|
188
|
-
changing = true
|
|
189
|
-
try {
|
|
190
|
-
if (active) finish(active, 'interrupted')
|
|
191
|
-
} finally {
|
|
192
|
-
changing = false
|
|
193
|
-
}
|
|
194
|
-
if (disposed || isPaused())
|
|
195
|
-
return {
|
|
196
|
-
id,
|
|
197
|
-
name,
|
|
198
|
-
finished: Promise.resolve({ status: 'cancelled' }),
|
|
199
|
-
cancel() {},
|
|
200
|
-
}
|
|
201
|
-
restPending = false
|
|
202
|
-
let resolve!: (result: BehaviorResult) => void
|
|
203
|
-
const finished = new Promise<BehaviorResult>((r) => {
|
|
204
|
-
resolve = r
|
|
205
|
-
})
|
|
206
|
-
const run: Active = { id, name, scope: motionScope(), done: false, resolve }
|
|
207
|
-
active = run
|
|
208
|
-
const checkpoint = () => {
|
|
209
|
-
if (disposed || run.done || active !== run) throw cancelled
|
|
210
|
-
}
|
|
211
|
-
const context: BehaviorContext = {
|
|
212
|
-
id,
|
|
213
|
-
scope: run.scope,
|
|
214
|
-
get active() {
|
|
215
|
-
return !disposed && !run.done && active === run
|
|
216
|
-
},
|
|
217
|
-
checkpoint,
|
|
218
|
-
async wait(work) {
|
|
219
|
-
checkpoint()
|
|
220
|
-
const result = await work
|
|
221
|
-
checkpoint()
|
|
222
|
-
while (isPaused()) {
|
|
223
|
-
await new Promise<void>((resolve) => {
|
|
224
|
-
const previous = run.wake
|
|
225
|
-
run.wake = () => {
|
|
226
|
-
previous?.()
|
|
227
|
-
resolve()
|
|
228
|
-
}
|
|
229
|
-
})
|
|
230
|
-
checkpoint()
|
|
231
|
-
}
|
|
232
|
-
if (result === false) throw cancelled
|
|
233
|
-
return result
|
|
234
|
-
},
|
|
235
|
-
guard(callback) {
|
|
236
|
-
return (...args) => {
|
|
237
|
-
if (context.active) callback(...args)
|
|
238
|
-
}
|
|
239
|
-
},
|
|
240
|
-
}
|
|
241
|
-
const settle = (status: BehaviorStatus, error?: unknown) => {
|
|
242
|
-
if (run.done) return
|
|
243
|
-
changing = true
|
|
244
|
-
try {
|
|
245
|
-
finish(run, status, error)
|
|
246
|
-
} finally {
|
|
247
|
-
changing = false
|
|
248
|
-
}
|
|
249
|
-
restore(name)
|
|
250
|
-
}
|
|
251
|
-
try {
|
|
252
|
-
const work = definition.run(context)
|
|
253
|
-
void Promise.resolve(work).then(
|
|
254
|
-
() => settle('completed'),
|
|
255
|
-
(error) =>
|
|
256
|
-
error === cancelled ? settle('cancelled') : settle('failed', error),
|
|
257
|
-
)
|
|
258
|
-
} catch (error) {
|
|
259
|
-
settle(
|
|
260
|
-
error === cancelled ? 'cancelled' : 'failed',
|
|
261
|
-
error === cancelled ? undefined : error,
|
|
262
|
-
)
|
|
263
|
-
}
|
|
264
|
-
return {
|
|
265
|
-
id,
|
|
266
|
-
name,
|
|
267
|
-
finished,
|
|
268
|
-
cancel() {
|
|
269
|
-
if (active === run) cancel(true)
|
|
270
|
-
},
|
|
271
|
-
}
|
|
272
|
-
},
|
|
273
|
-
cancel() {
|
|
274
|
-
cancel(true)
|
|
275
|
-
},
|
|
276
|
-
pause() {
|
|
277
|
-
if (paused || disposed) return
|
|
278
|
-
paused = true
|
|
279
|
-
active?.scope.suspend()
|
|
280
|
-
},
|
|
281
|
-
play() {
|
|
282
|
-
if (!paused || disposed) return
|
|
283
|
-
paused = false
|
|
284
|
-
active?.scope.resume()
|
|
285
|
-
wake()
|
|
286
|
-
},
|
|
287
|
-
stop() {
|
|
288
|
-
cancel(false)
|
|
289
|
-
},
|
|
290
|
-
dispose() {
|
|
291
|
-
if (disposed) return
|
|
292
|
-
disposed = true
|
|
293
|
-
Application.off(Application.suspendEvent, suspend)
|
|
294
|
-
Application.off(Application.resumeEvent, resume)
|
|
295
|
-
restPending = false
|
|
296
|
-
// Guards and cleanup may dispose reentrantly. Invalidate the current run too.
|
|
297
|
-
const wasChanging = changing
|
|
298
|
-
changing = true
|
|
299
|
-
try {
|
|
300
|
-
if (active) finish(active, 'cancelled')
|
|
301
|
-
} finally {
|
|
302
|
-
changing = wasChanging
|
|
303
|
-
}
|
|
304
|
-
},
|
|
305
|
-
}
|
|
306
|
-
const wake = () => {
|
|
307
|
-
if (disposed || isPaused()) return
|
|
308
|
-
active?.wake?.()
|
|
309
|
-
if (active) active.wake = undefined
|
|
310
|
-
if (restPending) {
|
|
311
|
-
restPending = false
|
|
312
|
-
restore()
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
const suspend = () => {
|
|
316
|
-
background = true
|
|
317
|
-
}
|
|
318
|
-
const resume = () => {
|
|
319
|
-
background = false
|
|
320
|
-
wake()
|
|
321
|
-
}
|
|
322
|
-
Application.on(Application.suspendEvent, suspend)
|
|
323
|
-
Application.on(Application.resumeEvent, resume)
|
|
324
|
-
return controller
|
|
23
|
+
return createMotionBehaviors(definitions, options, {
|
|
24
|
+
scope: motionScope,
|
|
25
|
+
hidden: () => background,
|
|
26
|
+
visibility(callback) {
|
|
27
|
+
const suspend = () => {
|
|
28
|
+
background = true
|
|
29
|
+
callback()
|
|
30
|
+
}
|
|
31
|
+
const resume = () => {
|
|
32
|
+
background = false
|
|
33
|
+
callback()
|
|
34
|
+
}
|
|
35
|
+
Application.on(Application.suspendEvent, suspend)
|
|
36
|
+
Application.on(Application.resumeEvent, resume)
|
|
37
|
+
return () => {
|
|
38
|
+
Application.off(Application.suspendEvent, suspend)
|
|
39
|
+
Application.off(Application.resumeEvent, resume)
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
})
|
|
325
43
|
}
|
package/src/rig/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { getDriver } from '../driver/index.js'
|
|
|
5
5
|
import { motionScope } from '../motion-scope.js'
|
|
6
6
|
import { readValue } from '../properties.js'
|
|
7
7
|
import type { TimelineAnimation } from '../sequence.js'
|
|
8
|
-
import { createTrack
|
|
8
|
+
import { createTrack } from '../track.js'
|
|
9
9
|
import {
|
|
10
10
|
resolveTransition,
|
|
11
11
|
springs,
|
|
@@ -14,29 +14,18 @@ import {
|
|
|
14
14
|
} from '../transition.js'
|
|
15
15
|
import type { Target } from '../types.js'
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
export
|
|
29
|
-
part: string
|
|
30
|
-
/** Local DIPs, or fractions when units is 'relative'. */
|
|
31
|
-
x: number
|
|
32
|
-
y: number
|
|
33
|
-
units?: 'dip' | 'relative'
|
|
34
|
-
}
|
|
35
|
-
export interface RigDefinition {
|
|
36
|
-
parts: Record<string, RigPart>
|
|
37
|
-
poses: Record<string, Record<string, RigTransform>>
|
|
38
|
-
sockets?: Record<string, RigSocket>
|
|
39
|
-
}
|
|
17
|
+
import {
|
|
18
|
+
rigChannels as channels,
|
|
19
|
+
resolveRigDefinition,
|
|
20
|
+
rigDestination,
|
|
21
|
+
limitRigTrack,
|
|
22
|
+
type RigChannel as Channel,
|
|
23
|
+
type RigDefinition,
|
|
24
|
+
type RigPart,
|
|
25
|
+
type RigSocket,
|
|
26
|
+
type RigTransform,
|
|
27
|
+
} from '../core/rig.js'
|
|
28
|
+
export type { RigDefinition, RigPart, RigSocket, RigTransform } from '../core/rig.js'
|
|
40
29
|
export interface Rig {
|
|
41
30
|
readonly disposed: boolean
|
|
42
31
|
readonly paused: boolean
|
|
@@ -59,78 +48,13 @@ export interface Rig {
|
|
|
59
48
|
dispose(): void
|
|
60
49
|
}
|
|
61
50
|
const owners = new WeakMap<View, object>()
|
|
62
|
-
const shortest = (angle: number) => ((((angle + 180) % 360) + 360) % 360) - 180
|
|
63
|
-
const finite = (n: unknown): n is number => typeof n === 'number' && Number.isFinite(n)
|
|
64
51
|
|
|
65
52
|
/** Native view articulation, with no JavaScript frame loop or asset ownership. */
|
|
66
53
|
export function rig2d(
|
|
67
54
|
definition: RigDefinition,
|
|
68
55
|
targets: Record<string, Target | null> = {},
|
|
69
56
|
): Rig {
|
|
70
|
-
const parts =
|
|
71
|
-
Object.entries(definition.parts).map(([name, part]) => [
|
|
72
|
-
name,
|
|
73
|
-
{
|
|
74
|
-
...part,
|
|
75
|
-
pivot: part.pivot && { ...part.pivot },
|
|
76
|
-
rest: { ...part.rest },
|
|
77
|
-
rotation: part.rotation && ([...part.rotation] as [number, number]),
|
|
78
|
-
},
|
|
79
|
-
]),
|
|
80
|
-
)
|
|
81
|
-
if (!parts.size || parts.size > 64) throw new RangeError('A rig supports 1..64 parts.')
|
|
82
|
-
const validateTransform = (values: RigTransform) => {
|
|
83
|
-
for (const [key, value] of Object.entries(values))
|
|
84
|
-
if (!channels.includes(key as Channel) || !finite(value))
|
|
85
|
-
throw new TypeError('Invalid rig transform.')
|
|
86
|
-
}
|
|
87
|
-
const used = new Map<string, Set<Channel>>()
|
|
88
|
-
for (const [name, part] of parts) {
|
|
89
|
-
validateTransform(part.rest)
|
|
90
|
-
used.set(name, new Set(Object.keys(part.rest) as Channel[]))
|
|
91
|
-
if (part.pivot && (!finite(part.pivot.x) || !finite(part.pivot.y)))
|
|
92
|
-
throw new TypeError('Invalid rig pivot.')
|
|
93
|
-
if (
|
|
94
|
-
part.rotation &&
|
|
95
|
-
(part.rotation.length !== 2 ||
|
|
96
|
-
!part.rotation.every(finite) ||
|
|
97
|
-
part.rotation[0] > part.rotation[1])
|
|
98
|
-
)
|
|
99
|
-
throw new TypeError('Invalid rotation limits.')
|
|
100
|
-
const visited = new Set([name])
|
|
101
|
-
let parent = part.parent
|
|
102
|
-
while (parent !== undefined) {
|
|
103
|
-
if (!parts.has(parent) || visited.has(parent))
|
|
104
|
-
throw new TypeError('Rig hierarchy contains a cycle or missing parent.')
|
|
105
|
-
visited.add(parent)
|
|
106
|
-
parent = parts.get(parent)!.parent
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
const poses = new Map(
|
|
110
|
-
Object.entries(definition.poses).map(([name, pose]) => [
|
|
111
|
-
name,
|
|
112
|
-
Object.fromEntries(
|
|
113
|
-
Object.entries(pose).map(([part, values]) => {
|
|
114
|
-
if (!parts.has(part)) throw new TypeError(`Unknown rig part "${part}".`)
|
|
115
|
-
validateTransform(values)
|
|
116
|
-
Object.keys(values).forEach((key) => used.get(part)!.add(key as Channel))
|
|
117
|
-
return [part, { ...values }]
|
|
118
|
-
}),
|
|
119
|
-
),
|
|
120
|
-
]),
|
|
121
|
-
)
|
|
122
|
-
const sockets = new Map(
|
|
123
|
-
Object.entries(definition.sockets ?? {}).map(([name, socket]) => {
|
|
124
|
-
if (
|
|
125
|
-
!parts.has(socket.part) ||
|
|
126
|
-
!finite(socket.x) ||
|
|
127
|
-
!finite(socket.y) ||
|
|
128
|
-
(socket.units !== undefined && !['dip', 'relative'].includes(socket.units))
|
|
129
|
-
)
|
|
130
|
-
throw new TypeError('Invalid rig socket.')
|
|
131
|
-
return [name, { ...socket }]
|
|
132
|
-
}),
|
|
133
|
-
)
|
|
57
|
+
const { parts, used, sockets, select } = resolveRigDefinition(definition)
|
|
134
58
|
const scope = motionScope(),
|
|
135
59
|
identity = {}
|
|
136
60
|
type Binding = {
|
|
@@ -235,49 +159,26 @@ export function rig2d(
|
|
|
235
159
|
blend(layers, transition = 'smooth') {
|
|
236
160
|
alive()
|
|
237
161
|
if (paused) throw new Error('Resume the rig before starting a pose.')
|
|
238
|
-
const selected = layers
|
|
239
|
-
if (!finite(layer.weight) || layer.weight < 0 || !poses.has(layer.pose))
|
|
240
|
-
throw new TypeError('Unknown pose or invalid blend weight.')
|
|
241
|
-
return { pose: poses.get(layer.pose)!, weight: layer.weight }
|
|
242
|
-
})
|
|
162
|
+
const selected = select(layers)
|
|
243
163
|
const settings = typeof transition === 'string' ? springs[transition] : transition
|
|
244
164
|
if (!settings) throw new TypeError('Unknown rig transition.')
|
|
245
165
|
const prepared = [...bound].map(([name, binding]) => {
|
|
246
166
|
const part = parts.get(name)!
|
|
247
167
|
const plans = [...used.get(name)!].map((property): Plan => {
|
|
248
168
|
const rest = binding.rest[property]!
|
|
249
|
-
let total = 0,
|
|
250
|
-
offset = 0
|
|
251
|
-
for (const layer of selected) {
|
|
252
|
-
const value = layer.pose[name]?.[property]
|
|
253
|
-
if (value === undefined) continue
|
|
254
|
-
total += layer.weight
|
|
255
|
-
offset +=
|
|
256
|
-
(property === 'rotate' && !part.rotation
|
|
257
|
-
? shortest(value - rest)
|
|
258
|
-
: value - rest) * layer.weight
|
|
259
|
-
}
|
|
260
|
-
let destination = rest + offset / Math.max(1, total)
|
|
261
169
|
const current = currentValue(binding.view, property)
|
|
262
|
-
|
|
263
|
-
|
|
170
|
+
const destination = rigDestination(
|
|
171
|
+
part,
|
|
172
|
+
name,
|
|
173
|
+
property,
|
|
174
|
+
rest,
|
|
175
|
+
current,
|
|
176
|
+
selected,
|
|
177
|
+
)
|
|
264
178
|
const resolved = resolveTransition(settings, property, 2)
|
|
265
179
|
const original = createTrack(property, [current, destination], resolved, 0)
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
const [min, max] = part.rotation,
|
|
269
|
-
clamp = (value: number) => Math.max(min, Math.min(max, value))
|
|
270
|
-
track = {
|
|
271
|
-
...original,
|
|
272
|
-
values: original.values.map(clamp),
|
|
273
|
-
to: clamp(original.to),
|
|
274
|
-
valueAt: (time) => clamp(original.valueAt(time)),
|
|
275
|
-
velocityAt: (time) =>
|
|
276
|
-
original.valueAt(time) <= min || original.valueAt(time) >= max
|
|
277
|
-
? 0
|
|
278
|
-
: original.velocityAt(time),
|
|
279
|
-
} satisfies Track
|
|
280
|
-
}
|
|
180
|
+
const track =
|
|
181
|
+
property === 'rotate' ? limitRigTrack(original, part.rotation) : original
|
|
281
182
|
return {
|
|
282
183
|
property,
|
|
283
184
|
keyframes: [current, destination],
|
package/src/testing/sensors.ts
CHANGED
|
@@ -1,41 +1,9 @@
|
|
|
1
1
|
import type { Sensor } from '../sensors/index.js'
|
|
2
2
|
import { sensorDriver } from '../sensors/native.js'
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
/** Relative milliseconds from the start of playback. */
|
|
6
|
-
time: number
|
|
7
|
-
/** Device-coordinate quaternion xyzw for rotation; downward m/s² xyz for gravity. */
|
|
8
|
-
values: readonly number[]
|
|
9
|
-
/** Interface clockwise basis angle from device coordinates, in degrees. */
|
|
10
|
-
orientation?: 0 | 90 | 180 | 270
|
|
11
|
-
}
|
|
3
|
+
import { validateSensorSamples, type SensorSample } from '../core/sensor-replay.js'
|
|
4
|
+
export type { SensorSample } from '../core/sensor-replay.js'
|
|
12
5
|
/** Play finite test inputs through the native sensor pipeline; hardware is detached during playback. */
|
|
13
6
|
export function replaySensor(source: Sensor, samples: readonly SensorSample[]): void {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
let previous = -1
|
|
17
|
-
for (const sample of samples) {
|
|
18
|
-
if (
|
|
19
|
-
!Number.isFinite(sample.time) ||
|
|
20
|
-
sample.time <= previous ||
|
|
21
|
-
sample.time > 60000 ||
|
|
22
|
-
sample.time < 0
|
|
23
|
-
)
|
|
24
|
-
throw new RangeError('Sensor times must increase within 0–60000 milliseconds.')
|
|
25
|
-
previous = sample.time
|
|
26
|
-
if (
|
|
27
|
-
!Array.isArray(sample.values) ||
|
|
28
|
-
sample.values.length !== (source.kind === 'rotation' ? 4 : 3) ||
|
|
29
|
-
sample.values.some((value: number) => !Number.isFinite(value))
|
|
30
|
-
)
|
|
31
|
-
throw new TypeError('Invalid canonical sensor sample.')
|
|
32
|
-
if (source.kind === 'rotation' && Math.hypot(...sample.values) < 1e-8)
|
|
33
|
-
throw new RangeError('A quaternion cannot be zero.')
|
|
34
|
-
if (
|
|
35
|
-
sample.orientation !== undefined &&
|
|
36
|
-
![0, 90, 180, 270].includes(sample.orientation)
|
|
37
|
-
)
|
|
38
|
-
throw new RangeError('Invalid interface orientation.')
|
|
39
|
-
}
|
|
40
|
-
sensorDriver(source).replay(JSON.stringify(samples))
|
|
7
|
+
const checked = validateSensorSamples(source.kind, samples)
|
|
8
|
+
sensorDriver(source).replay(JSON.stringify(checked))
|
|
41
9
|
}
|