@atom.io/spa 0.0.4 → 0.0.6
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 +224 -105
- package/README.mdx +87 -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-request-DRKvDjsO.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-B9wvGIkk.d.ts} +2 -2
- package/dist/{astro-shared-CYSRrRr8.d.ts.map → astro-shared-B9wvGIkk.d.ts.map} +1 -1
- package/dist/astro.d.ts +1 -1
- package/dist/astro.js +6 -8
- package/dist/astro.js.map +1 -1
- package/dist/{capabilities-D3HObAIy.d.ts → capabilities-_zOwNJ-2.d.ts} +12 -7
- package/dist/capabilities-_zOwNJ-2.d.ts.map +1 -0
- package/dist/{capabilities-BkQ2i0vV.js → capabilities-nOIitQ-_.js} +6 -4
- package/dist/capabilities-nOIitQ-_.js.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-request-DRKvDjsO.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 +6 -8
- package/dist/development/astro.js.map +1 -1
- package/dist/development/{capabilities-BkQ2i0vV.js → capabilities-nOIitQ-_.js} +6 -4
- package/dist/development/capabilities-nOIitQ-_.js.map +1 -0
- 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-BWy4dDss.js} +53 -20
- package/dist/development/dom-BWy4dDss.js.map +1 -0
- package/dist/development/floating.js +148 -0
- package/dist/development/floating.js.map +1 -0
- package/dist/development/index.js +3 -3
- package/dist/development/jsx-runtime.js +2 -2
- package/dist/development/jsx-runtime.js.map +1 -1
- package/dist/development/konva.js +2 -2
- package/dist/development/konva.js.map +1 -1
- package/dist/development/motion.js +7 -2
- package/dist/development/motion.js.map +1 -1
- package/dist/development/renderer.js +2 -2
- package/dist/development/renderer.js.map +1 -1
- package/dist/development/server-renderer-C7lGVhpo.js +90 -0
- package/dist/development/server-renderer-C7lGVhpo.js.map +1 -0
- package/dist/development/server.js +2 -83
- package/dist/development/server.js.map +1 -1
- package/dist/development/{src-BUtG4BgX.js → src-WSHJxR9b.js} +2 -2
- package/dist/development/{src-BUtG4BgX.js.map → src-WSHJxR9b.js.map} +1 -1
- package/dist/development/three.js +12 -9
- package/dist/development/three.js.map +1 -1
- package/dist/{vite-CSHoxzXU.js → development/vite-Bmyb6P5w.js} +32 -7
- package/dist/development/vite-Bmyb6P5w.js.map +1 -0
- package/dist/development/vite.js +1 -1
- package/dist/{dom-Cfj3GtQK.js → dom-BrPrxO97.js} +53 -20
- package/dist/dom-BrPrxO97.js.map +1 -0
- package/dist/floating.d.ts +16 -0
- package/dist/floating.d.ts.map +1 -0
- package/dist/floating.js +148 -0
- package/dist/floating.js.map +1 -0
- package/dist/{index-YRZ79lN5.d.ts → index-BFIjI7gF.d.ts} +5 -5
- package/dist/{index-YRZ79lN5.d.ts.map → index-BFIjI7gF.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-BhiCZOYZ.d.ts} +7 -7
- package/dist/{jsx-runtime-Q-UBV97Y.d.ts.map → jsx-runtime-BhiCZOYZ.d.ts.map} +1 -1
- package/dist/jsx-runtime.d.ts +1 -1
- package/dist/jsx-runtime.js +2 -2
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/konva.d.ts +16 -16
- package/dist/konva.js +2 -2
- package/dist/konva.js.map +1 -1
- package/dist/motion.d.ts +1 -1
- package/dist/motion.d.ts.map +1 -1
- package/dist/motion.js +7 -2
- package/dist/motion.js.map +1 -1
- package/dist/renderer.d.ts +4 -4
- package/dist/renderer.d.ts.map +1 -1
- package/dist/renderer.js +2 -2
- package/dist/renderer.js.map +1 -1
- package/dist/server-renderer-D-uRg04V.js +90 -0
- package/dist/server-renderer-D-uRg04V.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/{src-DvkUMuNW.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 +12 -9
- package/dist/three.js.map +1 -1
- package/dist/{development/vite-CSHoxzXU.js → vite-Bmyb6P5w.js} +32 -7
- package/dist/vite-Bmyb6P5w.js.map +1 -0
- package/dist/vite.js +1 -1
- package/package.json +39 -11
- package/src/astro-client.ts +17 -4
- package/src/astro-request.ts +1 -1
- 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/capabilities.ts +9 -4
- package/src/compiler.ts +28 -9
- package/src/core.ts +15 -10
- package/src/dom.ts +85 -50
- package/src/floating.ts +234 -0
- package/src/index.ts +2 -2
- package/src/jsx-runtime.ts +11 -7
- package/src/jsx-types.d.ts +2 -2
- package/src/konva.ts +3 -3
- package/src/motion.ts +11 -1
- package/src/presence.ts +1 -1
- package/src/renderer.ts +24 -18
- package/src/server-renderer.ts +162 -0
- package/src/server.ts +4 -153
- package/src/three-events.ts +14 -10
- package/src/three.ts +4 -4
- package/dist/astro-shared-BT45auwR.js.map +0 -1
- package/dist/capabilities-BkQ2i0vV.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/capabilities-BkQ2i0vV.js.map +0 -1
- package/dist/development/core-DZFaOyTO.js.map +0 -1
- package/dist/development/dom-Dwa2SFTn.js.map +0 -1
- package/dist/development/vite-CSHoxzXU.js.map +0 -1
- package/dist/dom-Cfj3GtQK.js.map +0 -1
- package/dist/vite-CSHoxzXU.js.map +0 -1
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/capabilities.ts
CHANGED
|
@@ -13,14 +13,16 @@ export interface CapabilityTarget {
|
|
|
13
13
|
export interface CapabilityBinding {
|
|
14
14
|
exit?(props: Record<string, unknown>): ExitHandle | undefined
|
|
15
15
|
resume?(): void
|
|
16
|
-
|
|
16
|
+
/** Inline CSS before native style replacement, for restoring capability-owned values. */
|
|
17
|
+
update(props: Record<string, unknown>, previousStyle?: string): void
|
|
17
18
|
dispose(): void
|
|
18
19
|
}
|
|
19
20
|
export interface Capability {
|
|
20
21
|
namespace: string
|
|
21
22
|
presence?(root: Root): PresenceCoordinator
|
|
22
23
|
scheduleFrame?(callback: (time: number) => void): Cleanup
|
|
23
|
-
|
|
24
|
+
/** SPA supplies ownership for integrations that measure after mount or report async errors. */
|
|
25
|
+
attach(target: CapabilityTarget, scope?: Scope): CapabilityBinding
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
export function capabilityNamespace(key: string): string | undefined {
|
|
@@ -47,6 +49,7 @@ export function capabilityProps(
|
|
|
47
49
|
scope: Scope,
|
|
48
50
|
target: CapabilityTarget,
|
|
49
51
|
read?: () => Record<string, any>,
|
|
52
|
+
applyNative?: (props: Record<string, any>) => string | undefined,
|
|
50
53
|
): (props: Record<string, any>) => Record<string, any> {
|
|
51
54
|
const bindings = new Map<string, CapabilityBinding>()
|
|
52
55
|
|
|
@@ -82,10 +85,11 @@ export function capabilityProps(
|
|
|
82
85
|
group[key.slice(namespace.length + 1)] = props[key]
|
|
83
86
|
groups.set(namespace, group)
|
|
84
87
|
}
|
|
88
|
+
const previousStyle = applyNative?.(native)
|
|
85
89
|
for (const [namespace, values] of groups) {
|
|
86
90
|
let binding = bindings.get(namespace)
|
|
87
91
|
if (!binding) {
|
|
88
|
-
binding = scope._root._capabilities.get(namespace)!.attach(target)
|
|
92
|
+
binding = scope._root._capabilities.get(namespace)!.attach(target, scope)
|
|
89
93
|
bindings.set(namespace, binding)
|
|
90
94
|
scope._root._presence?.bind(
|
|
91
95
|
scope,
|
|
@@ -94,7 +98,8 @@ export function capabilityProps(
|
|
|
94
98
|
namespace,
|
|
95
99
|
)
|
|
96
100
|
}
|
|
97
|
-
binding.update(values)
|
|
101
|
+
if (previousStyle !== undefined) binding.update(values, previousStyle)
|
|
102
|
+
else binding.update(values)
|
|
98
103
|
}
|
|
99
104
|
for (const [namespace, binding] of bindings)
|
|
100
105
|
if (!groups.has(namespace)) {
|
package/src/compiler.ts
CHANGED
|
@@ -161,7 +161,11 @@ export function compile(
|
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
|
+
const component =
|
|
165
|
+
node.type === `JSXElement` &&
|
|
166
|
+
!tag(node.openingElement.name).startsWith(`"`)
|
|
164
167
|
const children: Part[][] = []
|
|
168
|
+
let lazyChildren = false
|
|
165
169
|
for (const child of node.children) {
|
|
166
170
|
if (child.type === `JSXText`) {
|
|
167
171
|
const value = text(decodeJsx(child.value))
|
|
@@ -169,18 +173,33 @@ export function compile(
|
|
|
169
173
|
} else if (child.type === `JSXElement` || child.type === `JSXFragment`)
|
|
170
174
|
children.push([child])
|
|
171
175
|
else if (child.type === `JSXExpressionContainer`) {
|
|
172
|
-
if (child.expression.type !== `JSXEmptyExpression`)
|
|
173
|
-
|
|
176
|
+
if (child.expression.type !== `JSXEmptyExpression`) {
|
|
177
|
+
if (component) {
|
|
178
|
+
// Component props expose their declared values, including callbacks.
|
|
179
|
+
children.push([`(`, child.expression, `)`])
|
|
180
|
+
if (!eager(child.expression, true)) lazyChildren = true
|
|
181
|
+
} else children.push(expression(child.expression))
|
|
182
|
+
}
|
|
174
183
|
} else
|
|
175
184
|
throw new SyntaxError(`${filename}: JSX spread children are unsupported`)
|
|
176
185
|
}
|
|
177
|
-
if (children.length)
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
186
|
+
if (children.length) {
|
|
187
|
+
const value =
|
|
188
|
+
children.length === 1 ? children[0] : [`[`, ...join(children, `, `), `]`]
|
|
189
|
+
if (lazyChildren) {
|
|
190
|
+
// Capture the computation outside the getter's method scope.
|
|
191
|
+
const read = uid(`children`)
|
|
192
|
+
properties.push([`get children(){ return ${read}(); }`])
|
|
193
|
+
chunks.push([
|
|
194
|
+
`((${read}) => ({`,
|
|
195
|
+
...join(properties, `, `),
|
|
196
|
+
`}))(() => (`,
|
|
197
|
+
...value,
|
|
198
|
+
`))`,
|
|
199
|
+
])
|
|
200
|
+
properties = []
|
|
201
|
+
} else properties.push([`children: `, ...value])
|
|
202
|
+
}
|
|
184
203
|
flush()
|
|
185
204
|
const spreads =
|
|
186
205
|
node.type === `JSXElement` &&
|
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,26 +339,18 @@ 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
|
}
|
|
344
|
+
let previous: Record<string, any> = {}
|
|
321
345
|
const nativeProps = capabilityProps(
|
|
322
346
|
owner,
|
|
323
347
|
{ kind: `dom`, node: element },
|
|
324
348
|
() => ({ ...props }),
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
})
|
|
331
|
-
observe(
|
|
332
|
-
owner,
|
|
333
|
-
() => ({ ...props }),
|
|
334
|
-
(all) => {
|
|
335
|
-
const next = nativeProps(all)
|
|
349
|
+
(next) => {
|
|
350
|
+
const previousStyle =
|
|
351
|
+
previous[`style`] !== next[`style`]
|
|
352
|
+
? (element.getAttribute(`style`) ?? ``)
|
|
353
|
+
: undefined
|
|
336
354
|
for (const key in { ...previous, ...next })
|
|
337
355
|
if (
|
|
338
356
|
!Object.is(previous[key], next[key]) ||
|
|
@@ -342,8 +360,14 @@ function mount(
|
|
|
342
360
|
previous[key] = next[key]
|
|
343
361
|
}
|
|
344
362
|
previous = next
|
|
363
|
+
return previousStyle
|
|
345
364
|
},
|
|
346
365
|
)
|
|
366
|
+
owner._cleanups.add(() => {
|
|
367
|
+
for (const key in previous)
|
|
368
|
+
if (key.startsWith(`on`)) property(element, key, null, previous[key])
|
|
369
|
+
})
|
|
370
|
+
observe(owner, () => ({ ...props }), nativeProps)
|
|
347
371
|
const ref = untrack(() => props[`ref`])
|
|
348
372
|
if (ref) {
|
|
349
373
|
owner._root._mounts!.unshift(() => {
|
|
@@ -360,21 +384,35 @@ export interface SpaRoot {
|
|
|
360
384
|
flush(): void
|
|
361
385
|
}
|
|
362
386
|
function start(
|
|
363
|
-
setup: () =>
|
|
387
|
+
setup: () => Children,
|
|
364
388
|
host: Element,
|
|
365
389
|
options: RootOptions,
|
|
366
390
|
hydrating: boolean,
|
|
367
391
|
): SpaRoot {
|
|
368
|
-
|
|
369
|
-
const
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
392
|
+
let reported = false
|
|
393
|
+
const cursor: Cursor | undefined = hydrating
|
|
394
|
+
? {
|
|
395
|
+
node: host.firstChild,
|
|
396
|
+
report(expected, found) {
|
|
397
|
+
if (!reported) {
|
|
398
|
+
reported = true
|
|
399
|
+
// eslint-disable-next-line no-console -- Recoverable degradation must remain human-readable in production.
|
|
400
|
+
console.warn(
|
|
401
|
+
`spa: hydration mismatch. Repairing server HTML to match the browser view; replaced nodes may lose focus or unsaved input.`,
|
|
402
|
+
{ host, expected, found },
|
|
403
|
+
)
|
|
404
|
+
}
|
|
405
|
+
},
|
|
406
|
+
}
|
|
407
|
+
: undefined
|
|
408
|
+
const marker = cursor?.node
|
|
409
|
+
const prefix =
|
|
410
|
+
marker?.nodeType === 8 && marker.nodeValue?.startsWith(`w:`)
|
|
411
|
+
? marker.nodeValue.slice(2)
|
|
412
|
+
: undefined
|
|
376
413
|
const root = createRoot(options)
|
|
377
|
-
if (prefix !== undefined
|
|
414
|
+
if (prefix !== undefined && options.idPrefix === undefined)
|
|
415
|
+
root._prefix = prefix
|
|
378
416
|
try {
|
|
379
417
|
comment(`w:${root._prefix}`, host, null, root, cursor)
|
|
380
418
|
within(root, () => {
|
|
@@ -388,10 +426,7 @@ function start(
|
|
|
388
426
|
)
|
|
389
427
|
})
|
|
390
428
|
comment(`/w`, host, null, root, cursor)
|
|
391
|
-
|
|
392
|
-
throw new Error(
|
|
393
|
-
import.meta.env.DEV ? `spa: hydration mismatch` : undefined,
|
|
394
|
-
)
|
|
429
|
+
finish(cursor)
|
|
395
430
|
root.flush()
|
|
396
431
|
} catch (error) {
|
|
397
432
|
fail(root, error)
|
|
@@ -404,14 +439,14 @@ function start(
|
|
|
404
439
|
}
|
|
405
440
|
}
|
|
406
441
|
export function render(
|
|
407
|
-
setup: () =>
|
|
442
|
+
setup: () => Children,
|
|
408
443
|
host: Element,
|
|
409
444
|
options: RootOptions = {},
|
|
410
445
|
): SpaRoot {
|
|
411
446
|
return start(setup, host, options, false)
|
|
412
447
|
}
|
|
413
448
|
export function hydrate(
|
|
414
|
-
setup: () =>
|
|
449
|
+
setup: () => Children,
|
|
415
450
|
host: Element,
|
|
416
451
|
options: RootOptions = {},
|
|
417
452
|
): SpaRoot {
|