@atom.io/spa 0.0.1 → 0.0.3
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 +254 -6
- package/README.mdx +83 -1
- 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 +6 -3
- package/dist/astro-client.js.map +1 -1
- package/dist/astro-server.js +2 -1
- package/dist/astro-server.js.map +1 -1
- package/dist/astro-shared-BT45auwR.js.map +1 -1
- package/dist/{astro-shared-DkZaqX7L.d.ts → astro-shared-BkWvVZWZ.d.ts} +4 -2
- package/dist/astro-shared-BkWvVZWZ.d.ts.map +1 -0
- package/dist/astro.d.ts +1 -1
- package/dist/astro.js +1 -1
- package/dist/capabilities-BkQ2i0vV.js +63 -0
- package/dist/capabilities-BkQ2i0vV.js.map +1 -0
- package/dist/capabilities-CFP-HIuY.d.ts +115 -0
- package/dist/capabilities-CFP-HIuY.d.ts.map +1 -0
- package/dist/client.min.js +1 -1
- package/dist/{core-Dvqm7w2y.js → core-DCmqLZQP.js} +23 -4
- package/dist/core-DCmqLZQP.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 +6 -3
- package/dist/development/astro-client.js.map +1 -1
- package/dist/development/astro-server.js +2 -1
- package/dist/development/astro-server.js.map +1 -1
- package/dist/development/astro-shared-BT45auwR.js.map +1 -1
- package/dist/development/astro.js +1 -1
- package/dist/development/capabilities-BkQ2i0vV.js +63 -0
- package/dist/development/capabilities-BkQ2i0vV.js.map +1 -0
- package/dist/development/{core-DB3sQz4h.js → core-p9vciX9C.js} +23 -4
- package/dist/development/core-p9vciX9C.js.map +1 -0
- package/dist/development/{dom-D9cY6l1R.js → dom-Ci1XvbVJ.js} +21 -7
- package/dist/development/dom-Ci1XvbVJ.js.map +1 -0
- package/dist/development/index.js +3 -38
- package/dist/development/jsx-runtime.js.map +1 -1
- package/dist/development/konva.js +143 -0
- package/dist/development/konva.js.map +1 -0
- package/dist/development/motion.js +601 -0
- package/dist/development/motion.js.map +1 -0
- package/dist/development/renderer.js +190 -0
- package/dist/development/renderer.js.map +1 -0
- package/dist/development/server.js +4 -2
- package/dist/development/server.js.map +1 -1
- package/dist/development/src-DG0xjuS5.js +38 -0
- package/dist/development/src-DG0xjuS5.js.map +1 -0
- package/dist/development/three.js +452 -0
- package/dist/development/three.js.map +1 -0
- package/dist/development/vite-CSHoxzXU.js +240 -0
- package/dist/development/vite-CSHoxzXU.js.map +1 -0
- package/dist/development/vite.js +1 -1
- package/dist/{dom-BR9o9wZW.js → dom-dHQM0O7Q.js} +21 -7
- package/dist/dom-dHQM0O7Q.js.map +1 -0
- package/dist/index-CJJXcKaw.d.ts +24 -0
- package/dist/index-CJJXcKaw.d.ts.map +1 -0
- package/dist/index.d.ts +4 -24
- package/dist/index.js +3 -38
- package/dist/{jsx-runtime-BzGV18VO.d.ts → jsx-runtime-CREZYKDC.d.ts} +6 -4
- package/dist/jsx-runtime-CREZYKDC.d.ts.map +1 -0
- package/dist/jsx-runtime.d.ts +2 -2
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/konva.d.ts +32 -0
- package/dist/konva.d.ts.map +1 -0
- package/dist/konva.js +143 -0
- package/dist/konva.js.map +1 -0
- package/dist/motion.d.ts +45 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +601 -0
- package/dist/motion.js.map +1 -0
- package/dist/renderer.d.ts +24 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +190 -0
- package/dist/renderer.js.map +1 -0
- package/dist/server.d.ts +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +4 -2
- package/dist/server.js.map +1 -1
- package/dist/size.json +16 -16
- package/dist/src-DCQa6qxs.js +38 -0
- package/dist/src-DCQa6qxs.js.map +1 -0
- package/dist/three.d.ts +110 -0
- package/dist/three.d.ts.map +1 -0
- package/dist/three.js +452 -0
- package/dist/three.js.map +1 -0
- package/dist/vite-CSHoxzXU.js +240 -0
- package/dist/vite-CSHoxzXU.js.map +1 -0
- package/dist/vite.js +1 -1
- package/package.json +62 -15
- package/src/astro-application.ts +2 -2
- package/src/astro-client.ts +1 -0
- package/src/astro-server.ts +1 -0
- package/src/astro-shared.ts +3 -0
- package/src/capabilities.ts +107 -0
- package/src/compiler.ts +251 -178
- package/src/core.ts +38 -2
- package/src/dom.ts +38 -4
- package/src/index.ts +5 -0
- package/src/jsx-runtime.ts +4 -0
- package/src/jsx-types.d.ts +3 -1
- package/src/konva.ts +186 -0
- package/src/motion-tween.ts +106 -0
- package/src/motion.ts +519 -0
- package/src/presence.ts +324 -0
- package/src/renderer.ts +278 -0
- package/src/server.ts +3 -0
- package/src/three-events.ts +342 -0
- package/src/three.ts +373 -0
- package/dist/astro-shared-DkZaqX7L.d.ts.map +0 -1
- package/dist/core-BhiA9fFi.d.ts +0 -44
- package/dist/core-BhiA9fFi.d.ts.map +0 -1
- package/dist/core-Dvqm7w2y.js.map +0 -1
- package/dist/development/core-DB3sQz4h.js.map +0 -1
- package/dist/development/dom-D9cY6l1R.js.map +0 -1
- package/dist/development/index.js.map +0 -1
- package/dist/development/vite-CQchIQ_-.js +0 -126
- package/dist/development/vite-CQchIQ_-.js.map +0 -1
- package/dist/dom-BR9o9wZW.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/jsx-runtime-BzGV18VO.d.ts.map +0 -1
- package/dist/vite-CQchIQ_-.js +0 -126
- package/dist/vite-CQchIQ_-.js.map +0 -1
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
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import type { CapabilityTarget } from "./capabilities.ts"
|
|
2
|
+
import { capabilityProps } from "./capabilities.ts"
|
|
3
|
+
import type { Child, Component, RootOptions, Scope, VNode } from "./core.ts"
|
|
4
|
+
import {
|
|
5
|
+
createRoot,
|
|
6
|
+
createScope,
|
|
7
|
+
dispose,
|
|
8
|
+
fail,
|
|
9
|
+
invalidate,
|
|
10
|
+
isPresent,
|
|
11
|
+
observe,
|
|
12
|
+
owner,
|
|
13
|
+
untrack,
|
|
14
|
+
within,
|
|
15
|
+
} from "./core.ts"
|
|
16
|
+
import { Fragment, merge } from "./jsx-runtime.ts"
|
|
17
|
+
|
|
18
|
+
export interface SceneHost<N> {
|
|
19
|
+
target?(node: N): CapabilityTarget
|
|
20
|
+
create(type: string, props: Record<string, any>): N
|
|
21
|
+
update(
|
|
22
|
+
node: N,
|
|
23
|
+
props: Record<string, any>,
|
|
24
|
+
previous: Record<string, any>,
|
|
25
|
+
): void
|
|
26
|
+
children(parent: N, children: N[], activeChildren: N[]): void
|
|
27
|
+
destroy(node: N): void
|
|
28
|
+
}
|
|
29
|
+
type SceneNodes<N> = (activeOnly?: boolean) => N[]
|
|
30
|
+
|
|
31
|
+
interface Entry<N> {
|
|
32
|
+
scope: Scope
|
|
33
|
+
value: Child
|
|
34
|
+
nodes: SceneNodes<N>
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface SceneRenderer<N> {
|
|
38
|
+
mount(setup: () => Child, parent: N): void
|
|
39
|
+
render(
|
|
40
|
+
setup: () => Child,
|
|
41
|
+
parent: N,
|
|
42
|
+
options?: RootOptions,
|
|
43
|
+
): { flush(): void; dispose(): void }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A scene renderer shares SPA's owner, store, scheduler, and component lifecycle. */
|
|
47
|
+
export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
48
|
+
function mount(
|
|
49
|
+
source: () => Child,
|
|
50
|
+
scope: Scope,
|
|
51
|
+
changed: () => void,
|
|
52
|
+
): SceneNodes<N> {
|
|
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
|
+
}
|
|
70
|
+
observe(scope, source, (next) => {
|
|
71
|
+
const values = (
|
|
72
|
+
Array.isArray(next) ? next.flat(Infinity) : [next]
|
|
73
|
+
) as Child[]
|
|
74
|
+
const previous = entries
|
|
75
|
+
const old = new Map(
|
|
76
|
+
entries.map((entry, i) => [key(entry.value, i), entry]),
|
|
77
|
+
)
|
|
78
|
+
const seen = new Set<string>()
|
|
79
|
+
entries = values.map((value, i) => {
|
|
80
|
+
const id = key(value, i)
|
|
81
|
+
if (seen.has(id)) throw new Error(`spa: duplicate scene key`)
|
|
82
|
+
seen.add(id)
|
|
83
|
+
let entry = old.get(id)
|
|
84
|
+
old.delete(id)
|
|
85
|
+
if (entry && !same(entry.value, value)) {
|
|
86
|
+
retire(
|
|
87
|
+
entry,
|
|
88
|
+
id,
|
|
89
|
+
value == null || typeof value === `boolean`,
|
|
90
|
+
previous.indexOf(entry),
|
|
91
|
+
)
|
|
92
|
+
entry = undefined
|
|
93
|
+
}
|
|
94
|
+
entry ??= presence?.take(id, value)
|
|
95
|
+
if (entry) {
|
|
96
|
+
entry.value = value
|
|
97
|
+
scope._root._presence?.resume(entry.scope)
|
|
98
|
+
invalidate(entry.scope)
|
|
99
|
+
return entry
|
|
100
|
+
}
|
|
101
|
+
const childScope = createScope(scope)
|
|
102
|
+
const created: Entry<N> = { scope: childScope, value, nodes: null! }
|
|
103
|
+
created.nodes = build(() => created.value, childScope, changed)
|
|
104
|
+
return created
|
|
105
|
+
})
|
|
106
|
+
for (const [id, entry] of old)
|
|
107
|
+
retire(entry, id, true, previous.indexOf(entry))
|
|
108
|
+
changed()
|
|
109
|
+
})
|
|
110
|
+
return (activeOnly = false) => {
|
|
111
|
+
const rendered = activeOnly
|
|
112
|
+
? entries
|
|
113
|
+
: (presence?.merge(entries) ?? entries)
|
|
114
|
+
return rendered.flatMap((entry) => entry.nodes(activeOnly))
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function key(value: Child, i: number): string {
|
|
118
|
+
const explicit = (value as VNode | null)?.props?.[`key`]
|
|
119
|
+
return explicit == null ? `index:${i}` : `key:${explicit}`
|
|
120
|
+
}
|
|
121
|
+
function same(a: Child, b: Child): boolean {
|
|
122
|
+
return (
|
|
123
|
+
typeof a === typeof b &&
|
|
124
|
+
(a as VNode | null)?.type === (b as VNode | null)?.type
|
|
125
|
+
)
|
|
126
|
+
}
|
|
127
|
+
function build(
|
|
128
|
+
source: () => Child,
|
|
129
|
+
scope: Scope,
|
|
130
|
+
changed: () => void,
|
|
131
|
+
): SceneNodes<N> {
|
|
132
|
+
const value = untrack(source)
|
|
133
|
+
if (typeof value === `function`)
|
|
134
|
+
return mount(() => (source() as () => Child)(), scope, changed)
|
|
135
|
+
if (value == null || typeof value === `boolean`) return () => []
|
|
136
|
+
if (typeof value !== `object`)
|
|
137
|
+
throw new Error(`spa: scene children must be nodes`)
|
|
138
|
+
if (!(`type` in value) || !(`props` in value))
|
|
139
|
+
throw new Error(`spa: invalid scene child`)
|
|
140
|
+
const vnode = value
|
|
141
|
+
const props = merge(() => (source() as VNode).props)
|
|
142
|
+
if (!vnode.type || vnode.type === Fragment)
|
|
143
|
+
return mount(() => props[`children`], scope, changed)
|
|
144
|
+
if (typeof vnode.type === `function`) {
|
|
145
|
+
const output = within(scope, () =>
|
|
146
|
+
untrack(() => (vnode.type as Component)(props)),
|
|
147
|
+
)
|
|
148
|
+
return mount(() => output, scope, changed)
|
|
149
|
+
}
|
|
150
|
+
const node = host.create(
|
|
151
|
+
vnode.type,
|
|
152
|
+
untrack(() => props),
|
|
153
|
+
)
|
|
154
|
+
scope._cleanups.add(() => {
|
|
155
|
+
host.destroy(node)
|
|
156
|
+
})
|
|
157
|
+
const nativeProps = capabilityProps(
|
|
158
|
+
scope,
|
|
159
|
+
host.target?.(node) ?? { kind: `scene`, node },
|
|
160
|
+
() => ({ ...props }),
|
|
161
|
+
)
|
|
162
|
+
let previous: Record<string, any> = {}
|
|
163
|
+
observe(
|
|
164
|
+
scope,
|
|
165
|
+
() => ({ ...props }),
|
|
166
|
+
(all) => {
|
|
167
|
+
const next = nativeProps(all)
|
|
168
|
+
host.update(node, next, previous)
|
|
169
|
+
previous = next
|
|
170
|
+
},
|
|
171
|
+
)
|
|
172
|
+
let children: SceneNodes<N> = () => []
|
|
173
|
+
const sync = () => {
|
|
174
|
+
host.children(node, children(), children(true))
|
|
175
|
+
}
|
|
176
|
+
children = mount(() => props[`children`], scope, sync)
|
|
177
|
+
sync()
|
|
178
|
+
const ref = untrack(() => props[`ref`])
|
|
179
|
+
if (ref)
|
|
180
|
+
scope._root._mounts!.unshift(() => {
|
|
181
|
+
if (!scope._dead) {
|
|
182
|
+
scope._cleanups.add(() => ref(null))
|
|
183
|
+
ref(node)
|
|
184
|
+
}
|
|
185
|
+
})
|
|
186
|
+
return () => [node]
|
|
187
|
+
}
|
|
188
|
+
function attach(setup: () => Child, parent: N, scope: Scope) {
|
|
189
|
+
let nodes: SceneNodes<N> = () => []
|
|
190
|
+
const sync = () => {
|
|
191
|
+
host.children(parent, nodes(), nodes(true))
|
|
192
|
+
}
|
|
193
|
+
nodes = mount(() => untrack(setup), scope, sync)
|
|
194
|
+
sync()
|
|
195
|
+
scope._cleanups.add(() => {
|
|
196
|
+
host.children(parent, [], [])
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
/** Attach beneath the current component; disposal follows its lifetime. */
|
|
201
|
+
mount(setup: () => Child, parent: N): void {
|
|
202
|
+
if (!owner) throw new Error(`spa: scene mount requires an owner`)
|
|
203
|
+
const scope = createScope(owner)
|
|
204
|
+
try {
|
|
205
|
+
attach(setup, parent, scope)
|
|
206
|
+
} catch (error) {
|
|
207
|
+
fail(scope, error)
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
render(setup: () => Child, parent: N, options: RootOptions = {}) {
|
|
211
|
+
const root = createRoot(options)
|
|
212
|
+
try {
|
|
213
|
+
within(root, () => {
|
|
214
|
+
attach(setup, parent, root)
|
|
215
|
+
})
|
|
216
|
+
root.flush()
|
|
217
|
+
} catch (error) {
|
|
218
|
+
fail(root, error)
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
flush: root.flush,
|
|
222
|
+
dispose: () => {
|
|
223
|
+
dispose(root)
|
|
224
|
+
},
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Define a typed scene component without introducing another JSX runtime. */
|
|
231
|
+
export function sceneComponent<P>(type: string): (props: P) => VNode {
|
|
232
|
+
return (props) => ({ type, props: props as Record<string, any> })
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Bind imperative scene work to the current owner and its reactive dependencies. */
|
|
236
|
+
export function bindScene<T>(read: () => T, apply: (value: T) => void): void {
|
|
237
|
+
observe(owner!, read, apply)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Scene drawing follows an installed capability clock, or the browser clock. */
|
|
241
|
+
export function onSceneFrame(
|
|
242
|
+
callback: (time: number) => void,
|
|
243
|
+
infrastructure = false,
|
|
244
|
+
): void {
|
|
245
|
+
const scope = owner!
|
|
246
|
+
const clocks = [...scope._root._capabilities.values()].filter(
|
|
247
|
+
(capability) => capability.scheduleFrame,
|
|
248
|
+
)
|
|
249
|
+
if (clocks.length > 1)
|
|
250
|
+
throw new Error(`spa: multiple frame clock capabilities`)
|
|
251
|
+
const draw = (time: number) => {
|
|
252
|
+
if (scope._dead || (!infrastructure && !isPresent(scope))) return
|
|
253
|
+
try {
|
|
254
|
+
callback(time)
|
|
255
|
+
} catch (error) {
|
|
256
|
+
try {
|
|
257
|
+
fail(scope._root, error)
|
|
258
|
+
} catch (failure) {
|
|
259
|
+
scope._root.error(failure)
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
let stop: () => void
|
|
264
|
+
if (clocks[0]) stop = clocks[0].scheduleFrame!(draw)
|
|
265
|
+
else {
|
|
266
|
+
let frame = 0
|
|
267
|
+
const tick = (time: number) => {
|
|
268
|
+
if (scope._dead) return
|
|
269
|
+
draw(time)
|
|
270
|
+
if (!scope._dead) frame = requestAnimationFrame(tick)
|
|
271
|
+
}
|
|
272
|
+
frame = requestAnimationFrame(tick)
|
|
273
|
+
stop = () => {
|
|
274
|
+
cancelAnimationFrame(frame)
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
scope._cleanups.add(stop)
|
|
278
|
+
}
|
package/src/server.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { capabilityNamespace, validateCapabilities } from "./capabilities.ts"
|
|
1
2
|
import type { Child, RootOptions, Scope, VNode } from "./core.ts"
|
|
2
3
|
import {
|
|
3
4
|
createRoot,
|
|
@@ -73,10 +74,12 @@ function stringify(
|
|
|
73
74
|
if (type === `astro-slot`)
|
|
74
75
|
return `<astro-slot${props[`name`] ? ` name="${escape(props[`name`])}"` : ``}>${props[`innerHTML`] ?? ``}</astro-slot>`
|
|
75
76
|
const ns = type === `svg` ? `svg` : namespace
|
|
77
|
+
validateCapabilities(owner, props)
|
|
76
78
|
let attrs = ``
|
|
77
79
|
let textValue: string | number | undefined
|
|
78
80
|
for (let key of Object.keys(props)) {
|
|
79
81
|
if (
|
|
82
|
+
capabilityNamespace(key) !== undefined ||
|
|
80
83
|
key === `children` ||
|
|
81
84
|
key === `ref` ||
|
|
82
85
|
key === `key` ||
|