@atom.io/spa 0.0.3 → 0.0.5
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 +202 -138
- package/README.mdx +62 -26
- package/dist/astro-application.d.ts +1 -1
- package/dist/astro-client.d.ts +3 -2
- package/dist/astro-client.d.ts.map +1 -1
- package/dist/astro-client.js +11 -10
- package/dist/astro-client.js.map +1 -1
- package/dist/astro-resumability-client.d.ts +10 -0
- package/dist/astro-resumability-client.d.ts.map +1 -0
- package/dist/astro-resumability-client.js +29 -0
- package/dist/astro-resumability-client.js.map +1 -0
- package/dist/astro-resumability-server.d.ts +3 -0
- package/dist/astro-resumability-server.d.ts.map +1 -0
- package/dist/astro-resumability-server.js +7 -0
- package/dist/astro-resumability-server.js.map +1 -0
- package/dist/astro-server.d.ts +3 -2
- package/dist/astro-server.d.ts.map +1 -1
- package/dist/astro-server.js +28 -9
- package/dist/astro-server.js.map +1 -1
- package/dist/{astro-shared-BT45auwR.js → astro-shared-83hxw7-M.js} +4 -2
- package/dist/astro-shared-83hxw7-M.js.map +1 -0
- package/dist/{astro-shared-BkWvVZWZ.d.ts → astro-shared-DHIpjNIG.d.ts} +2 -2
- package/dist/{astro-shared-BkWvVZWZ.d.ts.map → astro-shared-DHIpjNIG.d.ts.map} +1 -1
- package/dist/astro.d.ts +1 -1
- package/dist/astro.js +5 -7
- package/dist/astro.js.map +1 -1
- package/dist/{capabilities-CFP-HIuY.d.ts → capabilities-BCtBG-ni.d.ts} +19 -14
- package/dist/capabilities-BCtBG-ni.d.ts.map +1 -0
- package/dist/client.min.js +1 -1
- package/dist/{core-DCmqLZQP.js → core-DjFJ0aIJ.js} +31 -23
- package/dist/core-DjFJ0aIJ.js.map +1 -0
- package/dist/development/astro-client.js +11 -10
- package/dist/development/astro-client.js.map +1 -1
- package/dist/development/astro-resumability-client.js +29 -0
- package/dist/development/astro-resumability-client.js.map +1 -0
- package/dist/development/astro-resumability-server.js +7 -0
- package/dist/development/astro-resumability-server.js.map +1 -0
- package/dist/development/astro-server.js +28 -9
- package/dist/development/astro-server.js.map +1 -1
- package/dist/development/{astro-shared-BT45auwR.js → astro-shared-83hxw7-M.js} +4 -2
- package/dist/development/astro-shared-83hxw7-M.js.map +1 -0
- package/dist/development/astro.js +5 -7
- package/dist/development/astro.js.map +1 -1
- package/dist/development/{core-p9vciX9C.js → core-DxPiKryn.js} +31 -23
- package/dist/development/core-DxPiKryn.js.map +1 -0
- package/dist/development/{dom-Ci1XvbVJ.js → dom-BytVV6Rp.js} +44 -12
- package/dist/development/dom-BytVV6Rp.js.map +1 -0
- package/dist/development/index.js +4 -4
- package/dist/development/jsx-runtime.js.map +1 -1
- package/dist/development/konva.js +1 -1
- package/dist/development/konva.js.map +1 -1
- package/dist/development/motion.js +1 -1
- package/dist/development/renderer.js +1 -1
- package/dist/development/renderer.js.map +1 -1
- package/dist/development/server-renderer-CUQXh9oF.js +90 -0
- package/dist/development/server-renderer-CUQXh9oF.js.map +1 -0
- package/dist/development/server.js +2 -83
- package/dist/development/server.js.map +1 -1
- package/dist/{src-DCQa6qxs.js → development/src-WSHJxR9b.js} +4 -4
- package/dist/development/src-WSHJxR9b.js.map +1 -0
- package/dist/development/three.js +1 -1
- package/dist/development/three.js.map +1 -1
- package/dist/{dom-dHQM0O7Q.js → dom-B5OxgHkr.js} +44 -12
- package/dist/dom-B5OxgHkr.js.map +1 -0
- package/dist/{index-CJJXcKaw.d.ts → index-DufwkB6W.d.ts} +5 -5
- package/dist/{index-CJJXcKaw.d.ts.map → index-DufwkB6W.d.ts.map} +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/{jsx-runtime-CREZYKDC.d.ts → jsx-runtime-DMDVr7A5.d.ts} +7 -7
- package/dist/{jsx-runtime-CREZYKDC.d.ts.map → jsx-runtime-DMDVr7A5.d.ts.map} +1 -1
- package/dist/jsx-runtime.d.ts +1 -1
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/konva.d.ts +16 -16
- package/dist/konva.js +1 -1
- package/dist/konva.js.map +1 -1
- package/dist/motion.d.ts +1 -1
- package/dist/motion.js +1 -1
- package/dist/renderer.d.ts +4 -4
- package/dist/renderer.d.ts.map +1 -1
- package/dist/renderer.js +1 -1
- package/dist/renderer.js.map +1 -1
- package/dist/server-renderer-DcU55e6s.js +90 -0
- package/dist/server-renderer-DcU55e6s.js.map +1 -0
- package/dist/server.d.ts +2 -2
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +2 -83
- package/dist/server.js.map +1 -1
- package/dist/size.json +16 -16
- package/dist/{development/src-DG0xjuS5.js → src-DWIqXCiz.js} +4 -4
- package/dist/src-DWIqXCiz.js.map +1 -0
- package/dist/three.d.ts +17 -17
- package/dist/three.js +1 -1
- package/dist/three.js.map +1 -1
- package/package.json +27 -11
- package/src/astro-client.ts +20 -7
- package/src/astro-resumability-client.ts +40 -0
- package/src/astro-resumability-server.ts +7 -0
- package/src/astro-server.ts +58 -19
- package/src/astro-shared.ts +5 -2
- package/src/astro.ts +11 -3
- package/src/core.ts +54 -49
- package/src/dom.ts +73 -39
- package/src/index.ts +4 -4
- package/src/jsx-runtime.ts +7 -6
- package/src/jsx-types.d.ts +2 -2
- package/src/konva.ts +3 -3
- package/src/renderer.ts +24 -18
- package/src/server-renderer.ts +162 -0
- package/src/server.ts +4 -153
- package/src/three.ts +4 -4
- package/src/use-loadable.ts +3 -3
- package/dist/astro-shared-BT45auwR.js.map +0 -1
- package/dist/capabilities-CFP-HIuY.d.ts.map +0 -1
- package/dist/core-DCmqLZQP.js.map +0 -1
- package/dist/development/astro-shared-BT45auwR.js.map +0 -1
- package/dist/development/core-p9vciX9C.js.map +0 -1
- package/dist/development/dom-Ci1XvbVJ.js.map +0 -1
- package/dist/development/src-DG0xjuS5.js.map +0 -1
- package/dist/dom-dHQM0O7Q.js.map +0 -1
- package/dist/src-DCQa6qxs.js.map +0 -1
package/src/core.ts
CHANGED
|
@@ -22,12 +22,12 @@ import {
|
|
|
22
22
|
getFromStore,
|
|
23
23
|
getTimelineTransactionAtHead,
|
|
24
24
|
IMPLICIT,
|
|
25
|
-
inspectTimelineInStore,
|
|
26
25
|
setIntoStore,
|
|
27
26
|
subscribeToState,
|
|
28
27
|
subscribeToTimeline,
|
|
29
28
|
timeTravel,
|
|
30
29
|
timeTravelTransactionGroupInStore,
|
|
30
|
+
withdraw,
|
|
31
31
|
} from "atom.io/internal"
|
|
32
32
|
|
|
33
33
|
import type { Capability } from "./capabilities.ts"
|
|
@@ -38,27 +38,29 @@ import type {
|
|
|
38
38
|
} from "./presence.ts"
|
|
39
39
|
|
|
40
40
|
export type Cleanup = () => void
|
|
41
|
-
// A callable interface permits the recursive
|
|
42
|
-
/* oxlint-disable typescript/prefer-function-type -- A callable interface is required for recursive
|
|
41
|
+
// A callable interface permits the recursive Children union. A type alias does not.
|
|
42
|
+
/* oxlint-disable typescript/prefer-function-type -- A callable interface is required for recursive Children. */
|
|
43
43
|
export interface Accessor<T> {
|
|
44
44
|
(): T
|
|
45
45
|
}
|
|
46
46
|
/* oxlint-enable typescript/prefer-function-type */
|
|
47
|
-
export type Component<P = any> = (props: P) =>
|
|
48
|
-
|
|
47
|
+
export type Component<P = any> = (props: P) => Children
|
|
48
|
+
/** A JSX descriptor for an intrinsic tag, component, or fragment. */
|
|
49
|
+
export interface SpaElement {
|
|
49
50
|
type: string | Component | null
|
|
50
51
|
props: Record<string, any>
|
|
51
52
|
}
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
/** Renderable content, including nested groups and reactive accessors. */
|
|
54
|
+
export type Children =
|
|
55
|
+
| SpaElement
|
|
54
56
|
| string
|
|
55
57
|
| number
|
|
56
58
|
| bigint
|
|
57
59
|
| boolean
|
|
58
60
|
| null
|
|
59
61
|
| undefined
|
|
60
|
-
| readonly
|
|
61
|
-
| Accessor<
|
|
62
|
+
| readonly Children[]
|
|
63
|
+
| Accessor<Children>
|
|
62
64
|
export interface RootOptions {
|
|
63
65
|
capabilities?: readonly Capability[]
|
|
64
66
|
store?: RootStore
|
|
@@ -76,6 +78,7 @@ export interface Scope {
|
|
|
76
78
|
_dead: boolean
|
|
77
79
|
}
|
|
78
80
|
export interface Root extends Scope {
|
|
81
|
+
_onRead?: ((value: unknown, token: ReadableToken<unknown>) => void) | undefined
|
|
79
82
|
_presence?: PresenceCoordinator
|
|
80
83
|
_capabilities: Map<string, Capability>
|
|
81
84
|
_store: RootStore
|
|
@@ -281,20 +284,22 @@ export function resolveStateToken(token: any, key?: Canonical): any {
|
|
|
281
284
|
}
|
|
282
285
|
|
|
283
286
|
/** Capture a token for this instance. Change the component key to change its token. */
|
|
284
|
-
export function
|
|
287
|
+
export function useGetter<T, E = never>(
|
|
285
288
|
token: ReadableToken<T, any, E>,
|
|
286
289
|
): Accessor<ViewOf<T | E>>
|
|
287
|
-
export function
|
|
290
|
+
export function useGetter<T, K extends Canonical, E = never>(
|
|
288
291
|
family: ReadableFamilyToken<T, K, E>,
|
|
289
292
|
key: NoInfer<K>,
|
|
290
293
|
): Accessor<ViewOf<T | E>>
|
|
291
|
-
export function
|
|
294
|
+
export function useGetter(token: any, key?: Canonical): Accessor<any> {
|
|
292
295
|
token = resolveStateToken(token, key)
|
|
293
|
-
const
|
|
296
|
+
const root = owner!._root
|
|
297
|
+
const store = root._store
|
|
294
298
|
const subscribe: Subscribe = (notify) =>
|
|
295
299
|
subscribeToState(store, token, `spa:${serial++}`, notify)
|
|
296
300
|
return () => {
|
|
297
301
|
const value = getFromStore(store, token)
|
|
302
|
+
root._onRead?.(value, token)
|
|
298
303
|
tracking?.(subscribe)
|
|
299
304
|
return value
|
|
300
305
|
}
|
|
@@ -349,62 +354,62 @@ export function useRef<E extends Element>(
|
|
|
349
354
|
}
|
|
350
355
|
}
|
|
351
356
|
}
|
|
352
|
-
export interface
|
|
353
|
-
|
|
354
|
-
|
|
357
|
+
export interface TimelineControls {
|
|
358
|
+
getCursor(): number
|
|
359
|
+
getLength(): number
|
|
355
360
|
undo(): void
|
|
356
361
|
redo(): void
|
|
357
362
|
clear(): void
|
|
358
|
-
|
|
359
|
-
redoTransaction?: Cleanup
|
|
363
|
+
getTransaction(): { undo?: Cleanup; redo?: Cleanup }
|
|
360
364
|
}
|
|
361
365
|
export function useTimeline<M extends TimelineManageable>(
|
|
362
366
|
token: TimelineToken<M>,
|
|
363
|
-
):
|
|
367
|
+
): TimelineControls
|
|
364
368
|
export function useTimeline<K extends Canonical, M extends TimelineManageable>(
|
|
365
369
|
family: TimelineFamilyToken<K, M>,
|
|
366
370
|
key: NoInfer<K>,
|
|
367
|
-
):
|
|
368
|
-
export function useTimeline(
|
|
369
|
-
token: any,
|
|
370
|
-
key?: Canonical,
|
|
371
|
-
): Accessor<TimelineValue> {
|
|
371
|
+
): TimelineControls
|
|
372
|
+
export function useTimeline(token: any, key?: Canonical): TimelineControls {
|
|
372
373
|
const store = owner!._root._store
|
|
373
|
-
|
|
374
|
-
token
|
|
374
|
+
const selected: TimelineToken<any> =
|
|
375
|
+
token.type === `timeline_family`
|
|
376
|
+
? findTimelineInStore(store, token, key!)
|
|
377
|
+
: token
|
|
375
378
|
const subscribe: Subscribe = (notify) =>
|
|
376
|
-
subscribeToTimeline(store,
|
|
377
|
-
const
|
|
379
|
+
subscribeToTimeline(store, selected, `spa:${serial++}`, notify)
|
|
380
|
+
const read = () => {
|
|
381
|
+
const timeline = withdraw(store, selected)
|
|
382
|
+
tracking?.(subscribe)
|
|
383
|
+
return timeline
|
|
384
|
+
}
|
|
385
|
+
return {
|
|
386
|
+
getCursor: () => read().at,
|
|
387
|
+
getLength: () => read().history.length,
|
|
378
388
|
undo: () => {
|
|
379
|
-
timeTravel(store, `undo`,
|
|
389
|
+
timeTravel(store, `undo`, selected)
|
|
380
390
|
},
|
|
381
391
|
redo: () => {
|
|
382
|
-
timeTravel(store, `redo`,
|
|
392
|
+
timeTravel(store, `redo`, selected)
|
|
383
393
|
},
|
|
384
394
|
clear: () => {
|
|
385
|
-
clearTimelineInStore(store,
|
|
395
|
+
clearTimelineInStore(store, selected)
|
|
396
|
+
},
|
|
397
|
+
getTransaction: () => {
|
|
398
|
+
const timeline = read()
|
|
399
|
+
const controls: ReturnType<TimelineControls[`getTransaction`]> = {}
|
|
400
|
+
for (const direction of [`undo`, `redo`] as const) {
|
|
401
|
+
const group = getTimelineTransactionAtHead(timeline, direction)
|
|
402
|
+
if (group)
|
|
403
|
+
controls[direction] = () => {
|
|
404
|
+
timeTravelTransactionGroupInStore(store, direction, group)
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return controls
|
|
386
408
|
},
|
|
387
|
-
}
|
|
388
|
-
return () => {
|
|
389
|
-
const value: TimelineValue = {
|
|
390
|
-
...inspectTimelineInStore(store, token),
|
|
391
|
-
...controls,
|
|
392
|
-
}
|
|
393
|
-
tracking?.(subscribe)
|
|
394
|
-
for (const direction of [`undo`, `redo`] as const) {
|
|
395
|
-
const group = getTimelineTransactionAtHead(
|
|
396
|
-
store.timelines.get(token.key)!,
|
|
397
|
-
direction,
|
|
398
|
-
)
|
|
399
|
-
if (group)
|
|
400
|
-
value[`${direction}Transaction`] = () =>
|
|
401
|
-
timeTravelTransactionGroupInStore(store, direction, group)
|
|
402
|
-
}
|
|
403
|
-
return value
|
|
404
409
|
}
|
|
405
410
|
}
|
|
406
411
|
|
|
407
|
-
export function plain(value:
|
|
412
|
+
export function plain(value: Children): string {
|
|
408
413
|
if (typeof value === `function`) return plain(value())
|
|
409
414
|
if (Array.isArray(value)) return value.map(plain).join(``)
|
|
410
415
|
if (value == null || typeof value === `boolean`) return ``
|
package/src/dom.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { capabilityProps } from "./capabilities.ts"
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
Accessor,
|
|
4
|
+
Children,
|
|
5
|
+
RootOptions,
|
|
6
|
+
Scope,
|
|
7
|
+
SpaElement,
|
|
8
|
+
} from "./core.ts"
|
|
3
9
|
import {
|
|
4
10
|
createRoot,
|
|
5
11
|
createScope,
|
|
@@ -15,8 +21,22 @@ import { Fragment, merge } from "./jsx-runtime.ts"
|
|
|
15
21
|
|
|
16
22
|
const HTML = `http://www.w3.org/1999/xhtml`
|
|
17
23
|
const SVG = `http://www.w3.org/2000/svg`
|
|
18
|
-
type Cursor = {
|
|
19
|
-
|
|
24
|
+
type Cursor = {
|
|
25
|
+
node: Node | null
|
|
26
|
+
report(expected: Node | null, found: Node | null): void
|
|
27
|
+
}
|
|
28
|
+
type RecordNode = { _value: Children; _scope: Scope; _start: Node; _end: Node }
|
|
29
|
+
|
|
30
|
+
function finish(cursor?: Cursor): void {
|
|
31
|
+
if (cursor?.node) {
|
|
32
|
+
cursor.report(null, cursor.node)
|
|
33
|
+
while (cursor.node) {
|
|
34
|
+
const node: Node = cursor.node
|
|
35
|
+
cursor.node = node.nextSibling
|
|
36
|
+
;(node as ChildNode).remove()
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
20
40
|
|
|
21
41
|
function take(
|
|
22
42
|
node: Node,
|
|
@@ -36,12 +56,15 @@ function take(
|
|
|
36
56
|
found.nodeName !== node.nodeName ||
|
|
37
57
|
(found as Element).namespaceURI !== (node as Element).namespaceURI ||
|
|
38
58
|
(node.nodeType !== 1 && found.nodeValue !== node.nodeValue)
|
|
39
|
-
)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
59
|
+
) {
|
|
60
|
+
cursor.report(node, found)
|
|
61
|
+
parent.insertBefore(node, found)
|
|
62
|
+
cursor.node = found?.nextSibling ?? null
|
|
63
|
+
;(found as ChildNode | null)?.remove()
|
|
64
|
+
} else {
|
|
65
|
+
node = found
|
|
66
|
+
cursor.node = found.nextSibling
|
|
67
|
+
}
|
|
45
68
|
} else parent.insertBefore(node, cursor ? cursor.node : before)
|
|
46
69
|
owner._cleanups.add(() => {
|
|
47
70
|
;(node as ChildNode).remove()
|
|
@@ -117,7 +140,7 @@ function property(
|
|
|
117
140
|
}
|
|
118
141
|
|
|
119
142
|
function mount(
|
|
120
|
-
value:
|
|
143
|
+
value: Children,
|
|
121
144
|
parent: Node,
|
|
122
145
|
before: Node | null,
|
|
123
146
|
owner: Scope,
|
|
@@ -129,11 +152,12 @@ function mount(
|
|
|
129
152
|
comment(`{`, parent, before, owner, cursor)
|
|
130
153
|
let end: Node | null = null
|
|
131
154
|
let records = new Map<unknown, RecordNode>()
|
|
132
|
-
const presence = owner._root._presence?.collection<RecordNode,
|
|
155
|
+
const presence = owner._root._presence?.collection<RecordNode, Children>(
|
|
133
156
|
(record) => record._scope,
|
|
134
157
|
(record, child) =>
|
|
135
158
|
typeof record._value === typeof child &&
|
|
136
|
-
(record._value as
|
|
159
|
+
(record._value as SpaElement | null)?.type ===
|
|
160
|
+
(child as SpaElement | null)?.type,
|
|
137
161
|
)
|
|
138
162
|
const retire = (
|
|
139
163
|
record: RecordNode,
|
|
@@ -147,14 +171,14 @@ function mount(
|
|
|
147
171
|
let initial = cursor
|
|
148
172
|
observe(
|
|
149
173
|
owner,
|
|
150
|
-
() => (source() as () =>
|
|
174
|
+
() => (source() as () => Children)(),
|
|
151
175
|
(next) => {
|
|
152
|
-
const values = ([next] as unknown[]).flat(Infinity) as
|
|
176
|
+
const values = ([next] as unknown[]).flat(Infinity) as Children[]
|
|
153
177
|
const kept = new Map<unknown, RecordNode>()
|
|
154
178
|
const focus = parent.ownerDocument!.activeElement as HTMLElement | null
|
|
155
179
|
for (let index = 0; index < values.length; index++) {
|
|
156
180
|
const child = values[index]
|
|
157
|
-
const vnode = child as
|
|
181
|
+
const vnode = child as SpaElement | null
|
|
158
182
|
const explicit = vnode?.props?.[`key`]
|
|
159
183
|
const key = explicit == null ? index : String(explicit)
|
|
160
184
|
if (import.meta.env.DEV && kept.has(key))
|
|
@@ -163,7 +187,7 @@ function mount(
|
|
|
163
187
|
if (
|
|
164
188
|
record &&
|
|
165
189
|
(typeof record._value !== typeof child ||
|
|
166
|
-
(record._value as
|
|
190
|
+
(record._value as SpaElement | null)?.type !== vnode?.type)
|
|
167
191
|
) {
|
|
168
192
|
retire(
|
|
169
193
|
record,
|
|
@@ -260,8 +284,8 @@ function mount(
|
|
|
260
284
|
}
|
|
261
285
|
if (import.meta.env.DEV && (!(`type` in value) || !(`props` in value)))
|
|
262
286
|
throw new Error(`spa: invalid child`)
|
|
263
|
-
const vnode = value as
|
|
264
|
-
const props = merge(() => (source() as
|
|
287
|
+
const vnode = value as SpaElement
|
|
288
|
+
const props = merge(() => (source() as SpaElement).props)
|
|
265
289
|
if (!vnode.type || vnode.type === Fragment) {
|
|
266
290
|
mount(() => props[`children`], parent, before, owner, namespace, cursor)
|
|
267
291
|
return
|
|
@@ -298,7 +322,9 @@ function mount(
|
|
|
298
322
|
},
|
|
299
323
|
)
|
|
300
324
|
} else {
|
|
301
|
-
const children = cursor
|
|
325
|
+
const children = cursor
|
|
326
|
+
? { node: element.firstChild, report: cursor.report }
|
|
327
|
+
: undefined
|
|
302
328
|
if (
|
|
303
329
|
!/^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/.test(
|
|
304
330
|
vnode.type,
|
|
@@ -313,10 +339,7 @@ function mount(
|
|
|
313
339
|
vnode.type === `foreignObject` ? HTML : ns,
|
|
314
340
|
children,
|
|
315
341
|
)
|
|
316
|
-
|
|
317
|
-
throw new Error(
|
|
318
|
-
import.meta.env.DEV ? `spa: hydration mismatch` : undefined,
|
|
319
|
-
)
|
|
342
|
+
finish(children)
|
|
320
343
|
}
|
|
321
344
|
const nativeProps = capabilityProps(
|
|
322
345
|
owner,
|
|
@@ -360,21 +383,35 @@ export interface SpaRoot {
|
|
|
360
383
|
flush(): void
|
|
361
384
|
}
|
|
362
385
|
function start(
|
|
363
|
-
setup: () =>
|
|
386
|
+
setup: () => Children,
|
|
364
387
|
host: Element,
|
|
365
388
|
options: RootOptions,
|
|
366
389
|
hydrating: boolean,
|
|
367
390
|
): SpaRoot {
|
|
368
|
-
|
|
369
|
-
const
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
391
|
+
let reported = false
|
|
392
|
+
const cursor: Cursor | undefined = hydrating
|
|
393
|
+
? {
|
|
394
|
+
node: host.firstChild,
|
|
395
|
+
report(expected, found) {
|
|
396
|
+
if (!reported) {
|
|
397
|
+
reported = true
|
|
398
|
+
// eslint-disable-next-line no-console -- Recoverable degradation must remain human-readable in production.
|
|
399
|
+
console.warn(
|
|
400
|
+
`spa: hydration mismatch. Repairing server HTML to match the browser view; replaced nodes may lose focus or unsaved input.`,
|
|
401
|
+
{ host, expected, found },
|
|
402
|
+
)
|
|
403
|
+
}
|
|
404
|
+
},
|
|
405
|
+
}
|
|
406
|
+
: undefined
|
|
407
|
+
const marker = cursor?.node
|
|
408
|
+
const prefix =
|
|
409
|
+
marker?.nodeType === 8 && marker.nodeValue?.startsWith(`w:`)
|
|
410
|
+
? marker.nodeValue.slice(2)
|
|
411
|
+
: undefined
|
|
376
412
|
const root = createRoot(options)
|
|
377
|
-
if (prefix !== undefined
|
|
413
|
+
if (prefix !== undefined && options.idPrefix === undefined)
|
|
414
|
+
root._prefix = prefix
|
|
378
415
|
try {
|
|
379
416
|
comment(`w:${root._prefix}`, host, null, root, cursor)
|
|
380
417
|
within(root, () => {
|
|
@@ -388,10 +425,7 @@ function start(
|
|
|
388
425
|
)
|
|
389
426
|
})
|
|
390
427
|
comment(`/w`, host, null, root, cursor)
|
|
391
|
-
|
|
392
|
-
throw new Error(
|
|
393
|
-
import.meta.env.DEV ? `spa: hydration mismatch` : undefined,
|
|
394
|
-
)
|
|
428
|
+
finish(cursor)
|
|
395
429
|
root.flush()
|
|
396
430
|
} catch (error) {
|
|
397
431
|
fail(root, error)
|
|
@@ -404,14 +438,14 @@ function start(
|
|
|
404
438
|
}
|
|
405
439
|
}
|
|
406
440
|
export function render(
|
|
407
|
-
setup: () =>
|
|
441
|
+
setup: () => Children,
|
|
408
442
|
host: Element,
|
|
409
443
|
options: RootOptions = {},
|
|
410
444
|
): SpaRoot {
|
|
411
445
|
return start(setup, host, options, false)
|
|
412
446
|
}
|
|
413
447
|
export function hydrate(
|
|
414
|
-
setup: () =>
|
|
448
|
+
setup: () => Children,
|
|
415
449
|
host: Element,
|
|
416
450
|
options: RootOptions = {},
|
|
417
451
|
): SpaRoot {
|
package/src/index.ts
CHANGED
|
@@ -5,22 +5,22 @@ export type {
|
|
|
5
5
|
} from "./capabilities.ts"
|
|
6
6
|
export type {
|
|
7
7
|
Accessor,
|
|
8
|
-
|
|
8
|
+
Children,
|
|
9
9
|
Component,
|
|
10
10
|
RootOptions,
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
SpaElement,
|
|
12
|
+
TimelineControls,
|
|
13
13
|
} from "./core.ts"
|
|
14
14
|
export {
|
|
15
15
|
onCleanup,
|
|
16
16
|
onMount,
|
|
17
|
+
useGetter,
|
|
17
18
|
useId,
|
|
18
19
|
useRef,
|
|
19
20
|
useSetter,
|
|
20
21
|
useStore,
|
|
21
22
|
useTimeline,
|
|
22
23
|
useTransaction,
|
|
23
|
-
useValue,
|
|
24
24
|
} from "./core.ts"
|
|
25
25
|
export type { SpaRoot } from "./dom.ts"
|
|
26
26
|
export { hydrate, render } from "./dom.ts"
|
package/src/jsx-runtime.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Children, Component, SpaElement } from "./core.ts"
|
|
2
2
|
import type { HTMLAttributes, NativeElements } from "./jsx-types.d.ts"
|
|
3
3
|
|
|
4
4
|
export type {
|
|
@@ -16,12 +16,13 @@ export type {
|
|
|
16
16
|
// oxlint-disable-next-line typescript/no-empty-interface -- Optional integrations augment this declaration.
|
|
17
17
|
export interface CapabilityAttributes<Kind extends string = string> {}
|
|
18
18
|
|
|
19
|
-
export const Fragment = (props: { children?:
|
|
19
|
+
export const Fragment = (props: { children?: Children }): Children =>
|
|
20
|
+
props.children
|
|
20
21
|
export function jsx(
|
|
21
|
-
type:
|
|
22
|
-
props:
|
|
22
|
+
type: SpaElement[`type`],
|
|
23
|
+
props: SpaElement[`props`] | null,
|
|
23
24
|
key?: unknown,
|
|
24
|
-
):
|
|
25
|
+
): SpaElement {
|
|
25
26
|
return {
|
|
26
27
|
type,
|
|
27
28
|
props: key === undefined ? (props ?? {}) : merge(props ?? {}, { key }),
|
|
@@ -52,7 +53,7 @@ export function merge(
|
|
|
52
53
|
}
|
|
53
54
|
|
|
54
55
|
export namespace JSX {
|
|
55
|
-
export type Element =
|
|
56
|
+
export type Element = SpaElement
|
|
56
57
|
export type ElementType = keyof IntrinsicElements | Component
|
|
57
58
|
export interface ElementChildrenAttribute {
|
|
58
59
|
children: {}
|
package/src/jsx-types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Children } from "./core.ts"
|
|
2
2
|
import type { CapabilityAttributes } from "./jsx-runtime.ts"
|
|
3
3
|
|
|
4
4
|
/** Native events retain their target; only currentTarget is the attached element. */
|
|
@@ -200,7 +200,7 @@ export type DOMAttributes<E extends Element> = CapabilityAttributes<`dom`> &
|
|
|
200
200
|
EventAttributes<E> &
|
|
201
201
|
AriaAttributes &
|
|
202
202
|
Attributes<{
|
|
203
|
-
children:
|
|
203
|
+
children: Children
|
|
204
204
|
key: string | number
|
|
205
205
|
ref: Ref<E>
|
|
206
206
|
id: string
|
package/src/konva.ts
CHANGED
|
@@ -2,7 +2,7 @@ import Konva from "konva"
|
|
|
2
2
|
|
|
3
3
|
import { capabilityProps } from "./capabilities.ts"
|
|
4
4
|
import { owner } from "./core.ts"
|
|
5
|
-
import type {
|
|
5
|
+
import type { Children, SpaElement } from "./index.ts"
|
|
6
6
|
import { onCleanup, onMount } from "./index.ts"
|
|
7
7
|
import type { CapabilityAttributes } from "./jsx-runtime.ts"
|
|
8
8
|
import { jsx } from "./jsx-runtime.ts"
|
|
@@ -10,7 +10,7 @@ import { bindScene, createSceneRenderer, sceneComponent } from "./renderer.ts"
|
|
|
10
10
|
|
|
11
11
|
type Props<C, N> = C &
|
|
12
12
|
CapabilityAttributes<`konva`> & {
|
|
13
|
-
children?:
|
|
13
|
+
children?: Children
|
|
14
14
|
ref?: (node: N | null) => void
|
|
15
15
|
onClick?: (event: Konva.KonvaEventObject<MouseEvent>) => void
|
|
16
16
|
onPointerDown?: (event: Konva.KonvaEventObject<PointerEvent>) => void
|
|
@@ -106,7 +106,7 @@ export function Stage(
|
|
|
106
106
|
width?: number
|
|
107
107
|
height?: number
|
|
108
108
|
},
|
|
109
|
-
):
|
|
109
|
+
): SpaElement {
|
|
110
110
|
let container: HTMLDivElement | null = null
|
|
111
111
|
onMount(() => {
|
|
112
112
|
// Konva clears its container; preserve SPA's markers in the outer element.
|
package/src/renderer.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { CapabilityTarget } from "./capabilities.ts"
|
|
2
2
|
import { capabilityProps } from "./capabilities.ts"
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
Children,
|
|
5
|
+
Component,
|
|
6
|
+
RootOptions,
|
|
7
|
+
Scope,
|
|
8
|
+
SpaElement,
|
|
9
|
+
} from "./core.ts"
|
|
4
10
|
import {
|
|
5
11
|
createRoot,
|
|
6
12
|
createScope,
|
|
@@ -30,14 +36,14 @@ type SceneNodes<N> = (activeOnly?: boolean) => N[]
|
|
|
30
36
|
|
|
31
37
|
interface Entry<N> {
|
|
32
38
|
scope: Scope
|
|
33
|
-
value:
|
|
39
|
+
value: Children
|
|
34
40
|
nodes: SceneNodes<N>
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
export interface SceneRenderer<N> {
|
|
38
|
-
mount(setup: () =>
|
|
44
|
+
mount(setup: () => Children, parent: N): void
|
|
39
45
|
render(
|
|
40
|
-
setup: () =>
|
|
46
|
+
setup: () => Children,
|
|
41
47
|
parent: N,
|
|
42
48
|
options?: RootOptions,
|
|
43
49
|
): { flush(): void; dispose(): void }
|
|
@@ -46,12 +52,12 @@ export interface SceneRenderer<N> {
|
|
|
46
52
|
/** A scene renderer shares SPA's owner, store, scheduler, and component lifecycle. */
|
|
47
53
|
export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
48
54
|
function mount(
|
|
49
|
-
source: () =>
|
|
55
|
+
source: () => Children,
|
|
50
56
|
scope: Scope,
|
|
51
57
|
changed: () => void,
|
|
52
58
|
): SceneNodes<N> {
|
|
53
59
|
let entries: Entry<N>[] = []
|
|
54
|
-
const presence = scope._root._presence?.collection<Entry<N>,
|
|
60
|
+
const presence = scope._root._presence?.collection<Entry<N>, Children>(
|
|
55
61
|
(entry) => entry.scope,
|
|
56
62
|
(entry, value) => same(entry.value, value),
|
|
57
63
|
)
|
|
@@ -70,7 +76,7 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
|
70
76
|
observe(scope, source, (next) => {
|
|
71
77
|
const values = (
|
|
72
78
|
Array.isArray(next) ? next.flat(Infinity) : [next]
|
|
73
|
-
) as
|
|
79
|
+
) as Children[]
|
|
74
80
|
const previous = entries
|
|
75
81
|
const old = new Map(
|
|
76
82
|
entries.map((entry, i) => [key(entry.value, i), entry]),
|
|
@@ -114,31 +120,31 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
|
114
120
|
return rendered.flatMap((entry) => entry.nodes(activeOnly))
|
|
115
121
|
}
|
|
116
122
|
}
|
|
117
|
-
function key(value:
|
|
118
|
-
const explicit = (value as
|
|
123
|
+
function key(value: Children, i: number): string {
|
|
124
|
+
const explicit = (value as SpaElement | null)?.props?.[`key`]
|
|
119
125
|
return explicit == null ? `index:${i}` : `key:${explicit}`
|
|
120
126
|
}
|
|
121
|
-
function same(a:
|
|
127
|
+
function same(a: Children, b: Children): boolean {
|
|
122
128
|
return (
|
|
123
129
|
typeof a === typeof b &&
|
|
124
|
-
(a as
|
|
130
|
+
(a as SpaElement | null)?.type === (b as SpaElement | null)?.type
|
|
125
131
|
)
|
|
126
132
|
}
|
|
127
133
|
function build(
|
|
128
|
-
source: () =>
|
|
134
|
+
source: () => Children,
|
|
129
135
|
scope: Scope,
|
|
130
136
|
changed: () => void,
|
|
131
137
|
): SceneNodes<N> {
|
|
132
138
|
const value = untrack(source)
|
|
133
139
|
if (typeof value === `function`)
|
|
134
|
-
return mount(() => (source() as () =>
|
|
140
|
+
return mount(() => (source() as () => Children)(), scope, changed)
|
|
135
141
|
if (value == null || typeof value === `boolean`) return () => []
|
|
136
142
|
if (typeof value !== `object`)
|
|
137
143
|
throw new Error(`spa: scene children must be nodes`)
|
|
138
144
|
if (!(`type` in value) || !(`props` in value))
|
|
139
145
|
throw new Error(`spa: invalid scene child`)
|
|
140
146
|
const vnode = value
|
|
141
|
-
const props = merge(() => (source() as
|
|
147
|
+
const props = merge(() => (source() as SpaElement).props)
|
|
142
148
|
if (!vnode.type || vnode.type === Fragment)
|
|
143
149
|
return mount(() => props[`children`], scope, changed)
|
|
144
150
|
if (typeof vnode.type === `function`) {
|
|
@@ -185,7 +191,7 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
|
185
191
|
})
|
|
186
192
|
return () => [node]
|
|
187
193
|
}
|
|
188
|
-
function attach(setup: () =>
|
|
194
|
+
function attach(setup: () => Children, parent: N, scope: Scope) {
|
|
189
195
|
let nodes: SceneNodes<N> = () => []
|
|
190
196
|
const sync = () => {
|
|
191
197
|
host.children(parent, nodes(), nodes(true))
|
|
@@ -198,7 +204,7 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
|
198
204
|
}
|
|
199
205
|
return {
|
|
200
206
|
/** Attach beneath the current component; disposal follows its lifetime. */
|
|
201
|
-
mount(setup: () =>
|
|
207
|
+
mount(setup: () => Children, parent: N): void {
|
|
202
208
|
if (!owner) throw new Error(`spa: scene mount requires an owner`)
|
|
203
209
|
const scope = createScope(owner)
|
|
204
210
|
try {
|
|
@@ -207,7 +213,7 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
|
207
213
|
fail(scope, error)
|
|
208
214
|
}
|
|
209
215
|
},
|
|
210
|
-
render(setup: () =>
|
|
216
|
+
render(setup: () => Children, parent: N, options: RootOptions = {}) {
|
|
211
217
|
const root = createRoot(options)
|
|
212
218
|
try {
|
|
213
219
|
within(root, () => {
|
|
@@ -228,7 +234,7 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
|
228
234
|
}
|
|
229
235
|
|
|
230
236
|
/** Define a typed scene component without introducing another JSX runtime. */
|
|
231
|
-
export function sceneComponent<P>(type: string): (props: P) =>
|
|
237
|
+
export function sceneComponent<P>(type: string): (props: P) => SpaElement {
|
|
232
238
|
return (props) => ({ type, props: props as Record<string, any> })
|
|
233
239
|
}
|
|
234
240
|
|