@atom.io/spa 0.0.4 → 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 +125 -105
- package/README.mdx +51 -20
- 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 +9 -8
- 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-CYSRrRr8.d.ts → astro-shared-DHIpjNIG.d.ts} +2 -2
- package/dist/{astro-shared-CYSRrRr8.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-D3HObAIy.d.ts → capabilities-BCtBG-ni.d.ts} +8 -5
- package/dist/capabilities-BCtBG-ni.d.ts.map +1 -0
- package/dist/client.min.js +1 -1
- package/dist/{core-Bl1Ec1hY.js → core-DjFJ0aIJ.js} +4 -2
- package/dist/core-DjFJ0aIJ.js.map +1 -0
- package/dist/development/astro-client.js +9 -8
- 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-DZFaOyTO.js → core-DxPiKryn.js} +4 -2
- package/dist/development/core-DxPiKryn.js.map +1 -0
- package/dist/development/{dom-Dwa2SFTn.js → dom-BytVV6Rp.js} +44 -12
- package/dist/development/dom-BytVV6Rp.js.map +1 -0
- package/dist/development/index.js +3 -3
- 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-DvkUMuNW.js → development/src-WSHJxR9b.js} +2 -2
- package/dist/development/{src-BUtG4BgX.js.map → src-WSHJxR9b.js.map} +1 -1
- package/dist/development/three.js +1 -1
- package/dist/development/three.js.map +1 -1
- package/dist/{dom-Cfj3GtQK.js → dom-B5OxgHkr.js} +44 -12
- package/dist/dom-B5OxgHkr.js.map +1 -0
- package/dist/{index-YRZ79lN5.d.ts → index-DufwkB6W.d.ts} +5 -5
- package/dist/{index-YRZ79lN5.d.ts.map → index-DufwkB6W.d.ts.map} +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +3 -3
- package/dist/{jsx-runtime-Q-UBV97Y.d.ts → jsx-runtime-DMDVr7A5.d.ts} +7 -7
- package/dist/{jsx-runtime-Q-UBV97Y.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-BUtG4BgX.js → src-DWIqXCiz.js} +2 -2
- package/dist/{src-DvkUMuNW.js.map → src-DWIqXCiz.js.map} +1 -1
- 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 +17 -4
- 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 +15 -10
- package/src/dom.ts +73 -39
- package/src/index.ts +2 -2
- 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/dist/astro-shared-BT45auwR.js.map +0 -1
- package/dist/capabilities-D3HObAIy.d.ts.map +0 -1
- package/dist/core-Bl1Ec1hY.js.map +0 -1
- package/dist/development/astro-shared-BT45auwR.js.map +0 -1
- package/dist/development/core-DZFaOyTO.js.map +0 -1
- package/dist/development/dom-Dwa2SFTn.js.map +0 -1
- package/dist/dom-Cfj3GtQK.js.map +0 -1
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {
|
|
2
|
+
browserResumption,
|
|
3
|
+
captureElement,
|
|
4
|
+
createResumption,
|
|
5
|
+
} from "resumability/astro-client"
|
|
6
|
+
|
|
7
|
+
import { browserApplication } from "./astro-application.ts"
|
|
8
|
+
import { createHydrator } from "./astro-client.ts"
|
|
9
|
+
|
|
10
|
+
export default createHydrator((store, _props, { element, application }) => {
|
|
11
|
+
const scopes = application.get(
|
|
12
|
+
createResumption,
|
|
13
|
+
() => new WeakMap<object, ReturnType<typeof createResumption>>(),
|
|
14
|
+
)
|
|
15
|
+
let scope = scopes.get(store)
|
|
16
|
+
if (!scope) {
|
|
17
|
+
const shared = application === browserApplication(element!.ownerDocument)
|
|
18
|
+
if (shared) {
|
|
19
|
+
const host = browserResumption(element!.ownerDocument)
|
|
20
|
+
application.onCleanup(host.resumeWhen(() => application.canResume))
|
|
21
|
+
scope = host
|
|
22
|
+
} else {
|
|
23
|
+
// Lazy defaults can run after component setup. Bind their private owner so
|
|
24
|
+
// refreshes and pending work end with this island instead of the page.
|
|
25
|
+
const original = store.runDefault
|
|
26
|
+
const local = createResumption()
|
|
27
|
+
store.runDefault = <T>(run: () => T): T =>
|
|
28
|
+
local.run(() => original.call(store, run) as T, {
|
|
29
|
+
resume: application.canResume,
|
|
30
|
+
})
|
|
31
|
+
application.onCleanup(() => {
|
|
32
|
+
local.dispose()
|
|
33
|
+
store.runDefault = original
|
|
34
|
+
})
|
|
35
|
+
scope = local
|
|
36
|
+
}
|
|
37
|
+
scopes.set(store, scope)
|
|
38
|
+
}
|
|
39
|
+
captureElement(scope, element!)
|
|
40
|
+
})
|
package/src/astro-server.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
/* oxlint-disable typescript/require-await -- Astro requires async renderer callbacks, even for synchronous rendering. */
|
|
2
2
|
import type { NamedSSRLoadedRendererValue } from "astro"
|
|
3
|
+
import type { ReadableToken } from "atom.io"
|
|
3
4
|
|
|
4
5
|
import { failPreparation, storeContext } from "./astro-context.ts"
|
|
5
6
|
import { requestApplication } from "./astro-request.ts"
|
|
6
7
|
import type { Island } from "./astro-shared.ts"
|
|
7
8
|
import { island, islandStore } from "./astro-shared.ts"
|
|
8
|
-
import {
|
|
9
|
+
import { renderToStringWithReads } from "./server-renderer.ts"
|
|
9
10
|
|
|
10
|
-
const
|
|
11
|
+
export const createRenderer = (
|
|
12
|
+
awaitState = false,
|
|
13
|
+
): NamedSSRLoadedRendererValue => ({
|
|
11
14
|
name: `spa`,
|
|
12
15
|
async check(component: Island) {
|
|
13
16
|
return component?.spa === true
|
|
@@ -15,24 +18,60 @@ const renderer: NamedSSRLoadedRendererValue = {
|
|
|
15
18
|
async renderToStaticMarkup(component: Island, props, slots) {
|
|
16
19
|
const request = (this as unknown as { result?: { request: Request } }).result
|
|
17
20
|
?.request
|
|
21
|
+
const requestOwner = request ? requestApplication(request) : undefined
|
|
18
22
|
const shared =
|
|
19
|
-
!component.createStore && !component.isolate
|
|
20
|
-
? requestApplication(request)
|
|
21
|
-
: undefined
|
|
23
|
+
!component.createStore && !component.isolate ? requestOwner : undefined
|
|
22
24
|
const lifetime = storeContext(undefined, shared?.application)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
props,
|
|
27
|
-
lifetime.context,
|
|
28
|
-
shared?.store,
|
|
25
|
+
if (requestOwner && !shared) {
|
|
26
|
+
lifetime.context.onCleanup(
|
|
27
|
+
requestOwner.application.onCleanup(lifetime.dispose),
|
|
29
28
|
)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
lifetime.context.signal.throwIfAborted()
|
|
32
|
+
const store = await Promise.race([
|
|
33
|
+
islandStore(component, props, lifetime.context, shared?.store),
|
|
34
|
+
lifetime.cancelled,
|
|
35
|
+
])
|
|
36
|
+
lifetime.context.signal.throwIfAborted()
|
|
37
|
+
const options = {
|
|
38
|
+
store: store!,
|
|
39
|
+
idPrefix: crypto.randomUUID(),
|
|
40
|
+
capabilities: component.capabilities ?? [],
|
|
41
|
+
}
|
|
42
|
+
// Retry only after reads made by this view settle. Each discarded render
|
|
43
|
+
// releases its scopes; conditional branches can discover more async state.
|
|
44
|
+
for (;;) {
|
|
45
|
+
lifetime.context.signal.throwIfAborted()
|
|
46
|
+
const reads = new Map<string, ReadableToken<unknown>>()
|
|
47
|
+
const pending = new Set<Promise<unknown>>()
|
|
48
|
+
const html = renderToStringWithReads(
|
|
49
|
+
() => () => island(component, props, slots),
|
|
50
|
+
options,
|
|
51
|
+
awaitState
|
|
52
|
+
? (value, token) => {
|
|
53
|
+
reads.set(token.key, token)
|
|
54
|
+
if (value instanceof Promise) {
|
|
55
|
+
pending.add(value)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
: undefined,
|
|
59
|
+
)
|
|
60
|
+
if (!pending.size)
|
|
61
|
+
return {
|
|
62
|
+
html,
|
|
63
|
+
// Even a cached synchronous server read can be async in the browser.
|
|
64
|
+
...(reads.size
|
|
65
|
+
? {
|
|
66
|
+
attrs: {
|
|
67
|
+
"data-spa-reads": JSON.stringify([...reads.values()]),
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
: {}),
|
|
71
|
+
}
|
|
72
|
+
// Caught producer errors resolve as state values. An uncaught failure
|
|
73
|
+
// must reject this render with its original error.
|
|
74
|
+
await Promise.race([Promise.all(pending), lifetime.cancelled])
|
|
36
75
|
}
|
|
37
76
|
} catch (error) {
|
|
38
77
|
failPreparation(error, lifetime.dispose)
|
|
@@ -40,5 +79,5 @@ const renderer: NamedSSRLoadedRendererValue = {
|
|
|
40
79
|
lifetime.dispose()
|
|
41
80
|
}
|
|
42
81
|
},
|
|
43
|
-
}
|
|
44
|
-
export default
|
|
82
|
+
})
|
|
83
|
+
export default createRenderer()
|
package/src/astro-shared.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { RootStore } from "atom.io/internal"
|
|
|
2
2
|
import { IMPLICIT, Store } from "atom.io/internal"
|
|
3
3
|
|
|
4
4
|
import type { Capability } from "./capabilities.ts"
|
|
5
|
-
import type { Component,
|
|
5
|
+
import type { Component, SpaElement } from "./core.ts"
|
|
6
6
|
import { jsx } from "./jsx-runtime.ts"
|
|
7
7
|
|
|
8
8
|
export interface AstroApplicationContext {
|
|
@@ -46,6 +46,7 @@ export async function islandStore(
|
|
|
46
46
|
props: Record<string, any>,
|
|
47
47
|
context: AstroStoreContext,
|
|
48
48
|
shared?: RootStore,
|
|
49
|
+
prepare?: AstroStorePreparation,
|
|
49
50
|
): Promise<RootStore> {
|
|
50
51
|
const store = await (component.createStore?.(props, context) ??
|
|
51
52
|
shared ??
|
|
@@ -54,6 +55,8 @@ export async function islandStore(
|
|
|
54
55
|
IMPLICIT.STORE,
|
|
55
56
|
) as RootStore))
|
|
56
57
|
context.signal.throwIfAborted()
|
|
58
|
+
await prepare?.(store, props, context)
|
|
59
|
+
context.signal.throwIfAborted()
|
|
57
60
|
await component.prepareStore?.(store, props, context)
|
|
58
61
|
context.signal.throwIfAborted()
|
|
59
62
|
return store
|
|
@@ -62,7 +65,7 @@ export function island(
|
|
|
62
65
|
component: Island,
|
|
63
66
|
props: Record<string, any>,
|
|
64
67
|
slots: Record<string, string>,
|
|
65
|
-
):
|
|
68
|
+
): SpaElement {
|
|
66
69
|
const children = Object.fromEntries(
|
|
67
70
|
Object.entries(slots).map(([name, html]) => [
|
|
68
71
|
name,
|
package/src/astro.ts
CHANGED
|
@@ -18,6 +18,7 @@ export default function integration(
|
|
|
18
18
|
name: `spa`,
|
|
19
19
|
hooks: {
|
|
20
20
|
"astro:config:setup"({
|
|
21
|
+
config,
|
|
21
22
|
addRenderer,
|
|
22
23
|
addMiddleware,
|
|
23
24
|
injectScript,
|
|
@@ -31,14 +32,21 @@ export default function integration(
|
|
|
31
32
|
`page`,
|
|
32
33
|
`import { browserApplication } from '@atom.io/spa/astro-application'; browserApplication(document);`,
|
|
33
34
|
)
|
|
35
|
+
const resumable = config.integrations.some(
|
|
36
|
+
({ name }) => name === `resumability`,
|
|
37
|
+
)
|
|
34
38
|
addRenderer({
|
|
35
39
|
name: `spa`,
|
|
36
|
-
clientEntrypoint:
|
|
37
|
-
|
|
40
|
+
clientEntrypoint: resumable
|
|
41
|
+
? `@atom.io/spa/astro-resumability-client`
|
|
42
|
+
: `@atom.io/spa/astro-client`,
|
|
43
|
+
serverEntrypoint: resumable
|
|
44
|
+
? `@atom.io/spa/astro-resumability-server`
|
|
45
|
+
: `@atom.io/spa/astro-server`,
|
|
38
46
|
})
|
|
39
47
|
updateConfig({
|
|
40
48
|
vite: {
|
|
41
|
-
plugins: [spa(
|
|
49
|
+
plugins: [spa(options)],
|
|
42
50
|
},
|
|
43
51
|
})
|
|
44
52
|
},
|
package/src/core.ts
CHANGED
|
@@ -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
|
|
@@ -290,11 +293,13 @@ export function useGetter<T, K extends Canonical, E = never>(
|
|
|
290
293
|
): Accessor<ViewOf<T | E>>
|
|
291
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
|
}
|
|
@@ -404,7 +409,7 @@ export function useTimeline(token: any, key?: Canonical): TimelineControls {
|
|
|
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
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.
|