@atom.io/spa 0.0.2 → 0.0.4
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 +144 -47
- package/README.mdx +37 -11
- package/dist/astro-application.d.ts +1 -1
- package/dist/astro-application.js +2 -2
- package/dist/astro-application.js.map +1 -1
- package/dist/astro-client.d.ts +1 -1
- package/dist/astro-client.js +4 -4
- package/dist/astro-client.js.map +1 -1
- package/dist/{astro-shared-DyzbUi6c.d.ts → astro-shared-CYSRrRr8.d.ts} +2 -2
- package/dist/{astro-shared-DyzbUi6c.d.ts.map → astro-shared-CYSRrRr8.d.ts.map} +1 -1
- package/dist/astro.d.ts +1 -1
- package/dist/capabilities-BkQ2i0vV.js +63 -0
- package/dist/capabilities-BkQ2i0vV.js.map +1 -0
- package/dist/{capabilities-yC9TsHDI.d.ts → capabilities-D3HObAIy.d.ts} +66 -11
- package/dist/capabilities-D3HObAIy.d.ts.map +1 -0
- package/dist/client.min.js +1 -1
- package/dist/{capabilities-BuZUQpiD.js → core-Bl1Ec1hY.js} +42 -79
- package/dist/core-Bl1Ec1hY.js.map +1 -0
- package/dist/development/astro-application.js +2 -2
- package/dist/development/astro-application.js.map +1 -1
- package/dist/development/astro-client.js +4 -4
- package/dist/development/astro-client.js.map +1 -1
- package/dist/development/capabilities-BkQ2i0vV.js +63 -0
- package/dist/development/capabilities-BkQ2i0vV.js.map +1 -0
- package/dist/development/{capabilities-DhOJmeoJ.js → core-DZFaOyTO.js} +42 -79
- package/dist/development/core-DZFaOyTO.js.map +1 -0
- package/dist/development/{dom-BKc6V_Kz.js → dom-Dwa2SFTn.js} +16 -7
- package/dist/development/dom-Dwa2SFTn.js.map +1 -0
- package/dist/development/index.js +4 -4
- package/dist/development/konva.js +25 -3
- package/dist/development/konva.js.map +1 -1
- package/dist/development/motion.js +392 -8
- package/dist/development/motion.js.map +1 -1
- package/dist/development/renderer.js +23 -10
- package/dist/development/renderer.js.map +1 -1
- package/dist/development/server.js +2 -1
- package/dist/development/server.js.map +1 -1
- package/dist/development/{src-Dh-d6w4y.js → src-BUtG4BgX.js} +4 -4
- package/dist/development/src-BUtG4BgX.js.map +1 -0
- package/dist/development/three.js +24 -10
- package/dist/development/three.js.map +1 -1
- package/dist/{dom-B2rm8o2L.js → dom-Cfj3GtQK.js} +16 -7
- package/dist/dom-Cfj3GtQK.js.map +1 -0
- package/dist/{index-BdwxmSKg.d.ts → index-YRZ79lN5.d.ts} +3 -3
- package/dist/{index-BdwxmSKg.d.ts.map → index-YRZ79lN5.d.ts.map} +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/{jsx-runtime-CZU7F8Qz.d.ts → jsx-runtime-Q-UBV97Y.d.ts} +2 -2
- package/dist/{jsx-runtime-CZU7F8Qz.d.ts.map → jsx-runtime-Q-UBV97Y.d.ts.map} +1 -1
- package/dist/jsx-runtime.d.ts +1 -1
- package/dist/konva.d.ts +3 -3
- package/dist/konva.d.ts.map +1 -1
- package/dist/konva.js +25 -3
- package/dist/konva.js.map +1 -1
- package/dist/motion.d.ts +2 -1
- package/dist/motion.d.ts.map +1 -1
- package/dist/motion.js +392 -8
- package/dist/motion.js.map +1 -1
- package/dist/renderer.d.ts +3 -3
- package/dist/renderer.d.ts.map +1 -1
- package/dist/renderer.js +23 -10
- package/dist/renderer.js.map +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +2 -1
- package/dist/server.js.map +1 -1
- package/dist/size.json +12 -12
- package/dist/{src-BZxX4xbU.js → src-DvkUMuNW.js} +4 -4
- package/dist/src-DvkUMuNW.js.map +1 -0
- package/dist/three.d.ts +3 -3
- package/dist/three.d.ts.map +1 -1
- package/dist/three.js +24 -10
- package/dist/three.js.map +1 -1
- package/package.json +9 -7
- package/src/astro-application.ts +2 -2
- package/src/astro-client.ts +3 -3
- package/src/capabilities.ts +21 -2
- package/src/core.ts +66 -41
- package/src/dom.ts +35 -4
- package/src/index.ts +2 -2
- package/src/konva.ts +41 -14
- package/src/motion-tween.ts +106 -0
- package/src/motion.ts +122 -9
- package/src/presence.ts +324 -0
- package/src/renderer.ts +51 -14
- package/src/three-events.ts +13 -1
- package/src/three.ts +38 -15
- package/src/use-loadable.ts +3 -3
- package/dist/capabilities-BuZUQpiD.js.map +0 -1
- package/dist/capabilities-yC9TsHDI.d.ts.map +0 -1
- package/dist/development/capabilities-DhOJmeoJ.js.map +0 -1
- package/dist/development/dom-BKc6V_Kz.js.map +0 -1
- package/dist/development/src-Dh-d6w4y.js.map +0 -1
- package/dist/dom-B2rm8o2L.js.map +0 -1
- package/dist/src-BZxX4xbU.js.map +0 -1
package/src/motion.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { MotionValue, ValueAnimationTransition } from "motion"
|
|
1
|
+
import type { animate, MotionValue, ValueAnimationTransition } from "motion"
|
|
2
2
|
import {
|
|
3
|
-
animate,
|
|
4
3
|
cancelFrame,
|
|
5
4
|
frame,
|
|
6
5
|
hover,
|
|
@@ -15,6 +14,9 @@ import type {
|
|
|
15
14
|
CapabilityBinding,
|
|
16
15
|
CapabilityTarget,
|
|
17
16
|
} from "./capabilities.ts"
|
|
17
|
+
import { animateTween } from "./motion-tween.ts"
|
|
18
|
+
import type { ExitHandle } from "./presence.ts"
|
|
19
|
+
import { createPresence } from "./presence.ts"
|
|
18
20
|
|
|
19
21
|
export type MotionScalar = number | string
|
|
20
22
|
export type MotionDOMTarget = Partial<
|
|
@@ -78,6 +80,7 @@ export interface MotionTransition {
|
|
|
78
80
|
export interface MotionAttributes<Kind extends string = `dom`> {
|
|
79
81
|
[`motion:initial`]?: MotionTarget<Kind> | false
|
|
80
82
|
[`motion:animate`]?: MotionTarget<Kind>
|
|
83
|
+
[`motion:exit`]?: MotionTarget<Kind>
|
|
81
84
|
[`motion:transition`]?: MotionTransition
|
|
82
85
|
[`motion:whileHover`]?: MotionTarget<Kind>
|
|
83
86
|
[`motion:whileTap`]?: MotionTarget<Kind>
|
|
@@ -108,6 +111,7 @@ const transform =
|
|
|
108
111
|
const allowed = new Set([
|
|
109
112
|
`initial`,
|
|
110
113
|
`animate`,
|
|
114
|
+
`exit`,
|
|
111
115
|
`transition`,
|
|
112
116
|
`whileHover`,
|
|
113
117
|
`whileTap`,
|
|
@@ -140,6 +144,8 @@ function attach(
|
|
|
140
144
|
let tapped = false
|
|
141
145
|
let started = false
|
|
142
146
|
let dead = false
|
|
147
|
+
let exiting = false
|
|
148
|
+
let exitHandle: ExitHandle | undefined
|
|
143
149
|
let gestures: (() => void)[] = []
|
|
144
150
|
const preference =
|
|
145
151
|
options.reducedMotion !== `never` && typeof matchMedia === `function`
|
|
@@ -230,8 +236,8 @@ function attach(
|
|
|
230
236
|
properties.set(key, property)
|
|
231
237
|
return property
|
|
232
238
|
}
|
|
233
|
-
const refresh = () => {
|
|
234
|
-
if (dead) return
|
|
239
|
+
const refresh = (force = false) => {
|
|
240
|
+
if (dead || exiting) return
|
|
235
241
|
const initial = (props[`initial`] ?? {}) as TargetValues | false
|
|
236
242
|
const animateTo = (props[`animate`] ?? {}) as TargetValues
|
|
237
243
|
const hoverTo = (props[`whileHover`] ?? {}) as TargetValues
|
|
@@ -268,14 +274,20 @@ function attach(
|
|
|
268
274
|
goals[key] ??
|
|
269
275
|
(initial === false ? undefined : initial[key]) ??
|
|
270
276
|
property.rest
|
|
271
|
-
if (equal(property.target, goal)) continue
|
|
277
|
+
if (!force && equal(property.target, goal)) continue
|
|
272
278
|
property.target = Array.isArray(goal) ? [...goal] : goal
|
|
273
279
|
const next = Array.isArray(goal) ? goal : [goal]
|
|
274
280
|
if (next.length !== property.values.length)
|
|
275
281
|
throw new Error(`spa/motion: cannot change the shape of ${key}`)
|
|
276
282
|
property.values.forEach((value, index) => {
|
|
277
283
|
if (reduced() || (!started && initial === false)) value.jump(next[index])
|
|
278
|
-
else
|
|
284
|
+
else
|
|
285
|
+
animateTween(
|
|
286
|
+
value,
|
|
287
|
+
next[index],
|
|
288
|
+
transition,
|
|
289
|
+
force || value.isAnimating(),
|
|
290
|
+
)
|
|
279
291
|
})
|
|
280
292
|
}
|
|
281
293
|
started = true
|
|
@@ -351,8 +363,107 @@ function attach(
|
|
|
351
363
|
}
|
|
352
364
|
}
|
|
353
365
|
}
|
|
354
|
-
|
|
366
|
+
const finishExit = () => {
|
|
367
|
+
exitHandle?.cancel()
|
|
368
|
+
}
|
|
369
|
+
const mediaChanged = () => {
|
|
370
|
+
preferenceChanged()
|
|
371
|
+
if (reduced() && exiting) finishExit()
|
|
372
|
+
}
|
|
373
|
+
preference?.addEventListener(`change`, mediaChanged)
|
|
355
374
|
return {
|
|
375
|
+
exit(next) {
|
|
376
|
+
const goals = next[`exit`] as TargetValues | undefined
|
|
377
|
+
exiting = true
|
|
378
|
+
hovered = tapped = false
|
|
379
|
+
clearGestures()
|
|
380
|
+
const moving = new Set<MotionValue<MotionScalar>>()
|
|
381
|
+
for (const property of properties.values())
|
|
382
|
+
for (const value of property.values) {
|
|
383
|
+
if (value.isAnimating()) moving.add(value)
|
|
384
|
+
value.stop()
|
|
385
|
+
}
|
|
386
|
+
if (!goals || !Object.keys(goals).length || reduced()) return undefined
|
|
387
|
+
const transition = (next[`transition`] ?? {
|
|
388
|
+
type: `spring`,
|
|
389
|
+
}) as MotionTransition
|
|
390
|
+
for (const key of [
|
|
391
|
+
`duration`,
|
|
392
|
+
`delay`,
|
|
393
|
+
`visualDuration`,
|
|
394
|
+
`velocity`,
|
|
395
|
+
`bounce`,
|
|
396
|
+
`stiffness`,
|
|
397
|
+
`damping`,
|
|
398
|
+
`mass`,
|
|
399
|
+
`restDelta`,
|
|
400
|
+
`restSpeed`,
|
|
401
|
+
] as const) {
|
|
402
|
+
const value = transition[key]
|
|
403
|
+
if (
|
|
404
|
+
value !== undefined &&
|
|
405
|
+
(!Number.isFinite(value) ||
|
|
406
|
+
(key !== `velocity` && value < 0) ||
|
|
407
|
+
(key === `bounce` && value >= 1) ||
|
|
408
|
+
([`stiffness`, `damping`, `mass`, `restDelta`, `restSpeed`].includes(
|
|
409
|
+
key,
|
|
410
|
+
) &&
|
|
411
|
+
value === 0))
|
|
412
|
+
)
|
|
413
|
+
throw new Error(
|
|
414
|
+
`spa/motion: exit requires a finite settling transition`,
|
|
415
|
+
)
|
|
416
|
+
}
|
|
417
|
+
let added = false
|
|
418
|
+
for (const [key, goal] of Object.entries(goals)) {
|
|
419
|
+
const had = properties.has(key)
|
|
420
|
+
ensure(key, goal, {})
|
|
421
|
+
if (!had) added = true
|
|
422
|
+
}
|
|
423
|
+
if (added) bindStyles()
|
|
424
|
+
let settle!: () => void
|
|
425
|
+
let reject!: (error: unknown) => void
|
|
426
|
+
const finished = new Promise<void>((resolve, fail) => {
|
|
427
|
+
settle = resolve
|
|
428
|
+
reject = fail
|
|
429
|
+
})
|
|
430
|
+
const controls: ReturnType<typeof animate>[] = []
|
|
431
|
+
for (const [key, goal] of Object.entries(goals)) {
|
|
432
|
+
const property = properties.get(key)!
|
|
433
|
+
property.target = Array.isArray(goal) ? [...goal] : goal
|
|
434
|
+
const values = Array.isArray(goal) ? goal : [goal]
|
|
435
|
+
property.values.forEach((value, index) =>
|
|
436
|
+
controls.push(
|
|
437
|
+
animateTween(
|
|
438
|
+
value,
|
|
439
|
+
values[index],
|
|
440
|
+
transition as ValueAnimationTransition,
|
|
441
|
+
moving.has(value),
|
|
442
|
+
),
|
|
443
|
+
),
|
|
444
|
+
)
|
|
445
|
+
}
|
|
446
|
+
void Promise.all(controls.map((control) => Promise.resolve(control))).then(
|
|
447
|
+
settle,
|
|
448
|
+
reject,
|
|
449
|
+
)
|
|
450
|
+
exitHandle = {
|
|
451
|
+
finished,
|
|
452
|
+
cancel() {
|
|
453
|
+
for (const control of controls) control.stop()
|
|
454
|
+
settle()
|
|
455
|
+
},
|
|
456
|
+
}
|
|
457
|
+
return exitHandle
|
|
458
|
+
},
|
|
459
|
+
resume() {
|
|
460
|
+
if (dead) return
|
|
461
|
+
exitHandle?.cancel()
|
|
462
|
+
exitHandle = undefined
|
|
463
|
+
exiting = false
|
|
464
|
+
installGestures()
|
|
465
|
+
refresh(true)
|
|
466
|
+
},
|
|
356
467
|
update(next) {
|
|
357
468
|
if (dead) return
|
|
358
469
|
for (const key of Object.keys(next))
|
|
@@ -363,14 +474,15 @@ function attach(
|
|
|
363
474
|
Boolean(next[`whileHover`]) !== Boolean(props[`whileHover`]) ||
|
|
364
475
|
Boolean(next[`whileTap`]) !== Boolean(props[`whileTap`])
|
|
365
476
|
props = next
|
|
366
|
-
if (gestureChange) installGestures()
|
|
477
|
+
if (gestureChange && !exiting) installGestures()
|
|
367
478
|
refresh()
|
|
368
479
|
},
|
|
369
480
|
dispose() {
|
|
370
481
|
if (dead) return
|
|
371
482
|
dead = true
|
|
372
483
|
clearGestures()
|
|
373
|
-
|
|
484
|
+
exitHandle?.cancel()
|
|
485
|
+
preference?.removeEventListener(`change`, mediaChanged)
|
|
374
486
|
styles?.()
|
|
375
487
|
for (const [key, property] of properties) {
|
|
376
488
|
property.release()
|
|
@@ -392,6 +504,7 @@ export function motionCapability(
|
|
|
392
504
|
): Capability {
|
|
393
505
|
return {
|
|
394
506
|
namespace: `motion`,
|
|
507
|
+
presence: createPresence,
|
|
395
508
|
attach: (target) => attach(target, options),
|
|
396
509
|
scheduleFrame(callback) {
|
|
397
510
|
const run = ({ timestamp }: { timestamp: number }) => {
|
package/src/presence.ts
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import type { CapabilityBinding } from "./capabilities.ts"
|
|
2
|
+
import type { Cleanup, Root, Scope } from "./core.ts"
|
|
3
|
+
import { dispose, fail, invalidate, isPresent, untrack } from "./core.ts"
|
|
4
|
+
|
|
5
|
+
export interface ExitHandle {
|
|
6
|
+
finished: Promise<void>
|
|
7
|
+
cancel(): void
|
|
8
|
+
}
|
|
9
|
+
export interface ExitParticipant {
|
|
10
|
+
exit(): ExitHandle | undefined
|
|
11
|
+
resume(): void
|
|
12
|
+
}
|
|
13
|
+
export interface PresenceTarget {
|
|
14
|
+
kind: string
|
|
15
|
+
node: unknown
|
|
16
|
+
suspend?(): Cleanup
|
|
17
|
+
}
|
|
18
|
+
export interface PresenceCollection<T, V> {
|
|
19
|
+
take(key: unknown, value: V): T | undefined
|
|
20
|
+
remove(
|
|
21
|
+
record: T,
|
|
22
|
+
key: unknown,
|
|
23
|
+
eligible: boolean,
|
|
24
|
+
index: number,
|
|
25
|
+
done: Cleanup,
|
|
26
|
+
): void
|
|
27
|
+
merge(records: T[]): T[]
|
|
28
|
+
}
|
|
29
|
+
export interface PresenceCoordinator {
|
|
30
|
+
collection<T, V>(
|
|
31
|
+
scope: (record: T) => Scope,
|
|
32
|
+
compatible: (record: T, value: V) => boolean,
|
|
33
|
+
): PresenceCollection<T, V>
|
|
34
|
+
target(scope: Scope, target: PresenceTarget): void
|
|
35
|
+
bind(
|
|
36
|
+
scope: Scope,
|
|
37
|
+
binding: CapabilityBinding,
|
|
38
|
+
read: () => Record<string, unknown>,
|
|
39
|
+
namespace: string,
|
|
40
|
+
): void
|
|
41
|
+
unbind(binding: CapabilityBinding): void
|
|
42
|
+
event(scope: Scope, handler: Function): Function
|
|
43
|
+
remove(scope: Scope, done: Cleanup): boolean
|
|
44
|
+
resume(scope: Scope): void
|
|
45
|
+
forget(scope: Scope): void
|
|
46
|
+
}
|
|
47
|
+
interface Session {
|
|
48
|
+
scope: Scope
|
|
49
|
+
participant: ExitParticipant
|
|
50
|
+
handle: ExitHandle
|
|
51
|
+
done: boolean
|
|
52
|
+
cancelled: boolean
|
|
53
|
+
}
|
|
54
|
+
interface Group {
|
|
55
|
+
scope: Scope
|
|
56
|
+
sessions: Session[]
|
|
57
|
+
done: Cleanup
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Optional coordinator: only imported by integrations that support retention. */
|
|
61
|
+
export function createPresence(root: Root): PresenceCoordinator {
|
|
62
|
+
const releases = new Map<CapabilityBinding, Cleanup>()
|
|
63
|
+
const handlers = new WeakMap<Scope, WeakMap<Function, Function>>()
|
|
64
|
+
// Groups own absent ranges; sessions can be shared when a parent adopts a child exit.
|
|
65
|
+
const groups = new Map<Scope, Group>()
|
|
66
|
+
const sessions = new Set<Session>()
|
|
67
|
+
const active = new Map<ExitParticipant, Session>()
|
|
68
|
+
const suspended = new Map<PresenceTarget, { scope: Scope; restore: Cleanup }>()
|
|
69
|
+
const suspend = (target: PresenceTarget): Cleanup => {
|
|
70
|
+
if (target.kind !== `dom`) return target.suspend!()
|
|
71
|
+
const node = target.node as HTMLElement
|
|
72
|
+
const inert = node.getAttribute(`inert`),
|
|
73
|
+
hidden = node.getAttribute(`aria-hidden`)
|
|
74
|
+
const pointer = node.style.getPropertyValue(`pointer-events`),
|
|
75
|
+
priority = node.style.getPropertyPriority(`pointer-events`)
|
|
76
|
+
const focused = node.ownerDocument.activeElement as HTMLElement | null
|
|
77
|
+
if (focused && node.contains(focused)) focused.blur()
|
|
78
|
+
node.setAttribute(`inert`, ``)
|
|
79
|
+
node.setAttribute(`aria-hidden`, `true`)
|
|
80
|
+
node.style.setProperty(`pointer-events`, `none`, `important`)
|
|
81
|
+
return () => {
|
|
82
|
+
for (const [key, value] of [
|
|
83
|
+
[`inert`, inert],
|
|
84
|
+
[`aria-hidden`, hidden],
|
|
85
|
+
] as const) {
|
|
86
|
+
if (value === null) node.removeAttribute(key)
|
|
87
|
+
else node.setAttribute(key, value)
|
|
88
|
+
}
|
|
89
|
+
if (pointer) node.style.setProperty(`pointer-events`, pointer, priority)
|
|
90
|
+
else node.style.removeProperty(`pointer-events`)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const walk = (scope: Scope, visit: (scope: Scope) => void) => {
|
|
94
|
+
const descend = (at: Scope) => {
|
|
95
|
+
visit(at)
|
|
96
|
+
for (const child of at._cleanups)
|
|
97
|
+
if (typeof child !== `function`) descend(child)
|
|
98
|
+
}
|
|
99
|
+
descend(scope)
|
|
100
|
+
}
|
|
101
|
+
const ancestor = (scope: Scope, parent: Scope) => {
|
|
102
|
+
for (let at: Scope | undefined = scope; at; at = at._parent)
|
|
103
|
+
if (at === parent) return true
|
|
104
|
+
return false
|
|
105
|
+
}
|
|
106
|
+
const report = (error: unknown) => {
|
|
107
|
+
if (root._dead) return
|
|
108
|
+
try {
|
|
109
|
+
fail(root, error)
|
|
110
|
+
} catch (failure) {
|
|
111
|
+
root.error(failure)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const finish = () => {
|
|
115
|
+
for (const group of [...groups.values()]) {
|
|
116
|
+
if (group.scope._dead || group.sessions.some((session) => !session.done))
|
|
117
|
+
continue
|
|
118
|
+
if (
|
|
119
|
+
[...groups.keys()].some(
|
|
120
|
+
(scope) => scope !== group.scope && ancestor(group.scope, scope),
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
continue
|
|
124
|
+
groups.delete(group.scope)
|
|
125
|
+
try {
|
|
126
|
+
group.done()
|
|
127
|
+
} catch (error) {
|
|
128
|
+
report(error)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const cancel = (session: Session) => {
|
|
133
|
+
session.cancelled = true
|
|
134
|
+
sessions.delete(session)
|
|
135
|
+
active.delete(session.participant)
|
|
136
|
+
session.handle.cancel()
|
|
137
|
+
}
|
|
138
|
+
const coordinator: PresenceCoordinator = {
|
|
139
|
+
target(scope, target) {
|
|
140
|
+
;(scope._targets ??= new Set()).add(target)
|
|
141
|
+
scope._cleanups.add(() => {
|
|
142
|
+
scope._targets!.delete(target)
|
|
143
|
+
})
|
|
144
|
+
},
|
|
145
|
+
bind(scope, binding, props, namespace) {
|
|
146
|
+
const read = () =>
|
|
147
|
+
Object.fromEntries(
|
|
148
|
+
Object.entries(props())
|
|
149
|
+
.filter(([key]) => key.startsWith(`${namespace}:`))
|
|
150
|
+
.map(([key, value]) => [key.slice(namespace.length + 1), value]),
|
|
151
|
+
)
|
|
152
|
+
if (!binding.exit) return
|
|
153
|
+
const participant = {
|
|
154
|
+
exit: () => binding.exit!(untrack(read)),
|
|
155
|
+
resume() {
|
|
156
|
+
binding.update(untrack(read))
|
|
157
|
+
binding.resume?.()
|
|
158
|
+
},
|
|
159
|
+
}
|
|
160
|
+
;(scope._exits ??= new Set()).add(participant)
|
|
161
|
+
const release = () => {
|
|
162
|
+
scope._exits!.delete(participant)
|
|
163
|
+
scope._cleanups.delete(release)
|
|
164
|
+
releases.delete(binding)
|
|
165
|
+
}
|
|
166
|
+
releases.set(binding, release)
|
|
167
|
+
scope._cleanups.add(release)
|
|
168
|
+
},
|
|
169
|
+
unbind(binding) {
|
|
170
|
+
releases.get(binding)?.()
|
|
171
|
+
},
|
|
172
|
+
event(scope, handler) {
|
|
173
|
+
const cached = handlers.get(scope) ?? new WeakMap<Function, Function>()
|
|
174
|
+
handlers.set(scope, cached)
|
|
175
|
+
if (!cached.has(handler))
|
|
176
|
+
cached.set(handler, function (this: unknown, ...args: unknown[]) {
|
|
177
|
+
if (isPresent(scope)) return handler.apply(this, args)
|
|
178
|
+
})
|
|
179
|
+
return cached.get(handler)!
|
|
180
|
+
},
|
|
181
|
+
collection<T, V>(
|
|
182
|
+
scopeOf: (record: T) => Scope,
|
|
183
|
+
compatible: (record: T, value: V) => boolean,
|
|
184
|
+
): PresenceCollection<T, V> {
|
|
185
|
+
let order: T[] = []
|
|
186
|
+
const exiting = new Map<
|
|
187
|
+
T,
|
|
188
|
+
{ key: unknown; eligible: boolean; index: number }
|
|
189
|
+
>()
|
|
190
|
+
return {
|
|
191
|
+
take(key, value) {
|
|
192
|
+
// Empty conditional children do not replace an exiting generation.
|
|
193
|
+
if (value == null || typeof value === `boolean`) return undefined
|
|
194
|
+
for (const [record, state] of exiting)
|
|
195
|
+
if (state.eligible && state.key === key) {
|
|
196
|
+
if (!compatible(record, value)) {
|
|
197
|
+
state.eligible = false
|
|
198
|
+
return undefined
|
|
199
|
+
}
|
|
200
|
+
exiting.delete(record)
|
|
201
|
+
return record
|
|
202
|
+
}
|
|
203
|
+
return undefined
|
|
204
|
+
},
|
|
205
|
+
remove(record, key, eligible, index, done) {
|
|
206
|
+
const position = order.indexOf(record)
|
|
207
|
+
const state = { key, eligible, index: position < 0 ? index : position }
|
|
208
|
+
exiting.set(record, state)
|
|
209
|
+
let pending = false
|
|
210
|
+
const release = () => {
|
|
211
|
+
dispose(scopeOf(record))
|
|
212
|
+
exiting.delete(record)
|
|
213
|
+
order = order.filter((item) => item !== record)
|
|
214
|
+
for (const other of exiting.values())
|
|
215
|
+
if (other.index > state.index) other.index--
|
|
216
|
+
if (pending) done()
|
|
217
|
+
}
|
|
218
|
+
pending = coordinator.remove(scopeOf(record), release)
|
|
219
|
+
if (!pending) release()
|
|
220
|
+
},
|
|
221
|
+
merge(records) {
|
|
222
|
+
const rendered = [...records]
|
|
223
|
+
for (const [record, state] of [...exiting].sort(
|
|
224
|
+
(a, b) => a[1].index - b[1].index,
|
|
225
|
+
)) {
|
|
226
|
+
rendered.splice(Math.min(state.index, rendered.length), 0, record)
|
|
227
|
+
state.index = rendered.indexOf(record)
|
|
228
|
+
}
|
|
229
|
+
// DOM reconciliation reverses its result while moving ranges. Keep a snapshot.
|
|
230
|
+
order = [...rendered]
|
|
231
|
+
return rendered
|
|
232
|
+
},
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
remove(scope, done) {
|
|
236
|
+
const collected: Session[] = []
|
|
237
|
+
walk(scope, (at) => {
|
|
238
|
+
for (const participant of at._exits ?? []) {
|
|
239
|
+
let session = active.get(participant)
|
|
240
|
+
if (!session) {
|
|
241
|
+
const handle = untrack(() => participant.exit())
|
|
242
|
+
if (!handle) continue
|
|
243
|
+
session = {
|
|
244
|
+
scope: at,
|
|
245
|
+
participant,
|
|
246
|
+
handle,
|
|
247
|
+
done: false,
|
|
248
|
+
cancelled: false,
|
|
249
|
+
}
|
|
250
|
+
sessions.add(session)
|
|
251
|
+
active.set(participant, session)
|
|
252
|
+
const held = session
|
|
253
|
+
void handle.finished.then(
|
|
254
|
+
() => {
|
|
255
|
+
if (held.cancelled || root._dead) return
|
|
256
|
+
held.done = true
|
|
257
|
+
finish()
|
|
258
|
+
},
|
|
259
|
+
(error) => {
|
|
260
|
+
if (!held.cancelled) report(error)
|
|
261
|
+
},
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
collected.push(session)
|
|
265
|
+
}
|
|
266
|
+
})
|
|
267
|
+
if (!collected.some((session) => !session.done)) return false
|
|
268
|
+
scope._exiting = true
|
|
269
|
+
groups.set(scope, { scope, sessions: collected, done })
|
|
270
|
+
walk(scope, (at) => {
|
|
271
|
+
for (const target of at._targets ?? [])
|
|
272
|
+
if (
|
|
273
|
+
!suspended.has(target) &&
|
|
274
|
+
(target.kind === `dom` || target.suspend)
|
|
275
|
+
)
|
|
276
|
+
suspended.set(target, { scope: at, restore: suspend(target) })
|
|
277
|
+
})
|
|
278
|
+
return true
|
|
279
|
+
},
|
|
280
|
+
resume(scope) {
|
|
281
|
+
if (!groups.delete(scope)) return
|
|
282
|
+
scope._exiting = false
|
|
283
|
+
for (const session of [...sessions])
|
|
284
|
+
if (ancestor(session.scope, scope) && isPresent(session.scope)) {
|
|
285
|
+
cancel(session)
|
|
286
|
+
}
|
|
287
|
+
walk(scope, (at) => {
|
|
288
|
+
if (isPresent(at))
|
|
289
|
+
for (const participant of at._exits ?? []) participant.resume()
|
|
290
|
+
})
|
|
291
|
+
for (const [target, state] of suspended)
|
|
292
|
+
if (isPresent(state.scope)) {
|
|
293
|
+
suspended.delete(target)
|
|
294
|
+
state.restore()
|
|
295
|
+
}
|
|
296
|
+
invalidate(scope)
|
|
297
|
+
// Reconcile returning descendants before releasing their adopted, completed exits.
|
|
298
|
+
queueMicrotask(finish)
|
|
299
|
+
},
|
|
300
|
+
forget(scope) {
|
|
301
|
+
const errors: unknown[] = []
|
|
302
|
+
for (const session of [...sessions])
|
|
303
|
+
if (ancestor(session.scope, scope)) {
|
|
304
|
+
try {
|
|
305
|
+
cancel(session)
|
|
306
|
+
} catch (error) {
|
|
307
|
+
errors.push(error)
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
for (const at of groups.keys()) if (ancestor(at, scope)) groups.delete(at)
|
|
311
|
+
for (const [target, state] of suspended)
|
|
312
|
+
if (ancestor(state.scope, scope)) {
|
|
313
|
+
suspended.delete(target)
|
|
314
|
+
try {
|
|
315
|
+
state.restore()
|
|
316
|
+
} catch (error) {
|
|
317
|
+
errors.push(error)
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (errors.length) throw new AggregateError(errors)
|
|
321
|
+
},
|
|
322
|
+
}
|
|
323
|
+
return coordinator
|
|
324
|
+
}
|
package/src/renderer.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
dispose,
|
|
8
8
|
fail,
|
|
9
9
|
invalidate,
|
|
10
|
+
isPresent,
|
|
10
11
|
observe,
|
|
11
12
|
owner,
|
|
12
13
|
untrack,
|
|
@@ -22,13 +23,15 @@ export interface SceneHost<N> {
|
|
|
22
23
|
props: Record<string, any>,
|
|
23
24
|
previous: Record<string, any>,
|
|
24
25
|
): void
|
|
25
|
-
children(parent: N, children: N[]): void
|
|
26
|
+
children(parent: N, children: N[], activeChildren: N[]): void
|
|
26
27
|
destroy(node: N): void
|
|
27
28
|
}
|
|
29
|
+
type SceneNodes<N> = (activeOnly?: boolean) => N[]
|
|
30
|
+
|
|
28
31
|
interface Entry<N> {
|
|
29
32
|
scope: Scope
|
|
30
33
|
value: Child
|
|
31
|
-
nodes:
|
|
34
|
+
nodes: SceneNodes<N>
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
export interface SceneRenderer<N> {
|
|
@@ -46,12 +49,29 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
|
46
49
|
source: () => Child,
|
|
47
50
|
scope: Scope,
|
|
48
51
|
changed: () => void,
|
|
49
|
-
):
|
|
52
|
+
): SceneNodes<N> {
|
|
50
53
|
let entries: Entry<N>[] = []
|
|
54
|
+
const presence = scope._root._presence?.collection<Entry<N>, Child>(
|
|
55
|
+
(entry) => entry.scope,
|
|
56
|
+
(entry, value) => same(entry.value, value),
|
|
57
|
+
)
|
|
58
|
+
const retire = (
|
|
59
|
+
entry: Entry<N>,
|
|
60
|
+
id: string,
|
|
61
|
+
eligible: boolean,
|
|
62
|
+
index: number,
|
|
63
|
+
) => {
|
|
64
|
+
if (presence)
|
|
65
|
+
presence.remove(entry, id, eligible, index, () => {
|
|
66
|
+
if (!scope._dead) changed()
|
|
67
|
+
})
|
|
68
|
+
else dispose(entry.scope)
|
|
69
|
+
}
|
|
51
70
|
observe(scope, source, (next) => {
|
|
52
71
|
const values = (
|
|
53
72
|
Array.isArray(next) ? next.flat(Infinity) : [next]
|
|
54
73
|
) as Child[]
|
|
74
|
+
const previous = entries
|
|
55
75
|
const old = new Map(
|
|
56
76
|
entries.map((entry, i) => [key(entry.value, i), entry]),
|
|
57
77
|
)
|
|
@@ -63,11 +83,18 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
|
63
83
|
let entry = old.get(id)
|
|
64
84
|
old.delete(id)
|
|
65
85
|
if (entry && !same(entry.value, value)) {
|
|
66
|
-
|
|
86
|
+
retire(
|
|
87
|
+
entry,
|
|
88
|
+
id,
|
|
89
|
+
value == null || typeof value === `boolean`,
|
|
90
|
+
previous.indexOf(entry),
|
|
91
|
+
)
|
|
67
92
|
entry = undefined
|
|
68
93
|
}
|
|
94
|
+
entry ??= presence?.take(id, value)
|
|
69
95
|
if (entry) {
|
|
70
96
|
entry.value = value
|
|
97
|
+
scope._root._presence?.resume(entry.scope)
|
|
71
98
|
invalidate(entry.scope)
|
|
72
99
|
return entry
|
|
73
100
|
}
|
|
@@ -76,10 +103,16 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
|
76
103
|
created.nodes = build(() => created.value, childScope, changed)
|
|
77
104
|
return created
|
|
78
105
|
})
|
|
79
|
-
for (const entry of old
|
|
106
|
+
for (const [id, entry] of old)
|
|
107
|
+
retire(entry, id, true, previous.indexOf(entry))
|
|
80
108
|
changed()
|
|
81
109
|
})
|
|
82
|
-
return (
|
|
110
|
+
return (activeOnly = false) => {
|
|
111
|
+
const rendered = activeOnly
|
|
112
|
+
? entries
|
|
113
|
+
: (presence?.merge(entries) ?? entries)
|
|
114
|
+
return rendered.flatMap((entry) => entry.nodes(activeOnly))
|
|
115
|
+
}
|
|
83
116
|
}
|
|
84
117
|
function key(value: Child, i: number): string {
|
|
85
118
|
const explicit = (value as VNode | null)?.props?.[`key`]
|
|
@@ -95,7 +128,7 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
|
95
128
|
source: () => Child,
|
|
96
129
|
scope: Scope,
|
|
97
130
|
changed: () => void,
|
|
98
|
-
):
|
|
131
|
+
): SceneNodes<N> {
|
|
99
132
|
const value = untrack(source)
|
|
100
133
|
if (typeof value === `function`)
|
|
101
134
|
return mount(() => (source() as () => Child)(), scope, changed)
|
|
@@ -124,6 +157,7 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
|
124
157
|
const nativeProps = capabilityProps(
|
|
125
158
|
scope,
|
|
126
159
|
host.target?.(node) ?? { kind: `scene`, node },
|
|
160
|
+
() => ({ ...props }),
|
|
127
161
|
)
|
|
128
162
|
let previous: Record<string, any> = {}
|
|
129
163
|
observe(
|
|
@@ -135,9 +169,9 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
|
135
169
|
previous = next
|
|
136
170
|
},
|
|
137
171
|
)
|
|
138
|
-
let children:
|
|
172
|
+
let children: SceneNodes<N> = () => []
|
|
139
173
|
const sync = () => {
|
|
140
|
-
host.children(node, children())
|
|
174
|
+
host.children(node, children(), children(true))
|
|
141
175
|
}
|
|
142
176
|
children = mount(() => props[`children`], scope, sync)
|
|
143
177
|
sync()
|
|
@@ -152,14 +186,14 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
|
152
186
|
return () => [node]
|
|
153
187
|
}
|
|
154
188
|
function attach(setup: () => Child, parent: N, scope: Scope) {
|
|
155
|
-
let nodes:
|
|
189
|
+
let nodes: SceneNodes<N> = () => []
|
|
156
190
|
const sync = () => {
|
|
157
|
-
host.children(parent, nodes())
|
|
191
|
+
host.children(parent, nodes(), nodes(true))
|
|
158
192
|
}
|
|
159
193
|
nodes = mount(() => untrack(setup), scope, sync)
|
|
160
194
|
sync()
|
|
161
195
|
scope._cleanups.add(() => {
|
|
162
|
-
host.children(parent, [])
|
|
196
|
+
host.children(parent, [], [])
|
|
163
197
|
})
|
|
164
198
|
}
|
|
165
199
|
return {
|
|
@@ -204,7 +238,10 @@ export function bindScene<T>(read: () => T, apply: (value: T) => void): void {
|
|
|
204
238
|
}
|
|
205
239
|
|
|
206
240
|
/** Scene drawing follows an installed capability clock, or the browser clock. */
|
|
207
|
-
export function onSceneFrame(
|
|
241
|
+
export function onSceneFrame(
|
|
242
|
+
callback: (time: number) => void,
|
|
243
|
+
infrastructure = false,
|
|
244
|
+
): void {
|
|
208
245
|
const scope = owner!
|
|
209
246
|
const clocks = [...scope._root._capabilities.values()].filter(
|
|
210
247
|
(capability) => capability.scheduleFrame,
|
|
@@ -212,7 +249,7 @@ export function onSceneFrame(callback: (time: number) => void): void {
|
|
|
212
249
|
if (clocks.length > 1)
|
|
213
250
|
throw new Error(`spa: multiple frame clock capabilities`)
|
|
214
251
|
const draw = (time: number) => {
|
|
215
|
-
if (scope._dead) return
|
|
252
|
+
if (scope._dead || (!infrastructure && !isPresent(scope))) return
|
|
216
253
|
try {
|
|
217
254
|
callback(time)
|
|
218
255
|
} catch (error) {
|