@atom.io/spa 0.0.0-stage → 0.0.0
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/LICENSE +373 -0
- package/README.md +401 -2
- package/README.mdx +150 -0
- package/dist/astro-application.d.ts +15 -0
- package/dist/astro-application.d.ts.map +1 -0
- package/dist/astro-application.js +62 -0
- package/dist/astro-application.js.map +1 -0
- package/dist/astro-client.d.ts +9 -0
- package/dist/astro-client.d.ts.map +1 -0
- package/dist/astro-client.js +98 -0
- package/dist/astro-client.js.map +1 -0
- package/dist/astro-context-OdlexHR4.js +79 -0
- package/dist/astro-context-OdlexHR4.js.map +1 -0
- package/dist/astro-middleware.d.ts +4 -0
- package/dist/astro-middleware.d.ts.map +1 -0
- package/dist/astro-middleware.js +49 -0
- package/dist/astro-middleware.js.map +1 -0
- package/dist/astro-request-DRKvDjsO.js +70 -0
- package/dist/astro-request-DRKvDjsO.js.map +1 -0
- package/dist/astro-server.d.ts +4 -0
- package/dist/astro-server.d.ts.map +1 -0
- package/dist/astro-server.js +30 -0
- package/dist/astro-server.js.map +1 -0
- package/dist/astro-shared-BT45auwR.js +29 -0
- package/dist/astro-shared-BT45auwR.js.map +1 -0
- package/dist/astro-shared-DkZaqX7L.d.ts +31 -0
- package/dist/astro-shared-DkZaqX7L.d.ts.map +1 -0
- package/dist/astro.d.ts +6 -0
- package/dist/astro.d.ts.map +1 -0
- package/dist/astro.js +26 -0
- package/dist/astro.js.map +1 -0
- package/dist/client.min.d.ts +1 -0
- package/dist/client.min.js +1 -0
- package/dist/core-BhiA9fFi.d.ts +44 -0
- package/dist/core-BhiA9fFi.d.ts.map +1 -0
- package/dist/core-Dvqm7w2y.js +231 -0
- package/dist/core-Dvqm7w2y.js.map +1 -0
- package/dist/development/astro-application.js +62 -0
- package/dist/development/astro-application.js.map +1 -0
- package/dist/development/astro-client.js +98 -0
- package/dist/development/astro-client.js.map +1 -0
- package/dist/development/astro-context-OdlexHR4.js +79 -0
- package/dist/development/astro-context-OdlexHR4.js.map +1 -0
- package/dist/development/astro-middleware.js +49 -0
- package/dist/development/astro-middleware.js.map +1 -0
- package/dist/development/astro-request-DRKvDjsO.js +70 -0
- package/dist/development/astro-request-DRKvDjsO.js.map +1 -0
- package/dist/development/astro-server.js +30 -0
- package/dist/development/astro-server.js.map +1 -0
- package/dist/development/astro-shared-BT45auwR.js +29 -0
- package/dist/development/astro-shared-BT45auwR.js.map +1 -0
- package/dist/development/astro.js +26 -0
- package/dist/development/astro.js.map +1 -0
- package/dist/development/core-DB3sQz4h.js +234 -0
- package/dist/development/core-DB3sQz4h.js.map +1 -0
- package/dist/development/dom-D9cY6l1R.js +194 -0
- package/dist/development/dom-D9cY6l1R.js.map +1 -0
- package/dist/development/index.js +40 -0
- package/dist/development/index.js.map +1 -0
- package/dist/development/jsx-runtime.js +25 -0
- package/dist/development/jsx-runtime.js.map +1 -0
- package/dist/development/server.js +78 -0
- package/dist/development/server.js.map +1 -0
- package/dist/development/vite-BadrFSIM.js +126 -0
- package/dist/development/vite-BadrFSIM.js.map +1 -0
- package/dist/development/vite.js +1 -0
- package/dist/dom-BR9o9wZW.js +188 -0
- package/dist/dom-BR9o9wZW.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-runtime-BzGV18VO.d.ts +965 -0
- package/dist/jsx-runtime-BzGV18VO.d.ts.map +1 -0
- package/dist/jsx-runtime.d.ts +2 -0
- package/dist/jsx-runtime.js +25 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +78 -0
- package/dist/server.js.map +1 -0
- package/dist/size.json +30 -0
- package/dist/vite-BadrFSIM.js +126 -0
- package/dist/vite-BadrFSIM.js.map +1 -0
- package/dist/vite.d.ts +6 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +1 -0
- package/package.json +131 -5
- package/src/astro-application.ts +71 -0
- package/src/astro-client.ts +136 -0
- package/src/astro-context.ts +102 -0
- package/src/astro-middleware.ts +48 -0
- package/src/astro-request.ts +79 -0
- package/src/astro-server.ts +43 -0
- package/src/astro-shared.ts +78 -0
- package/src/astro.ts +47 -0
- package/src/compiler.ts +189 -0
- package/src/core.ts +385 -0
- package/src/dom.ts +385 -0
- package/src/env.d.ts +1 -0
- package/src/index.ts +24 -0
- package/src/jsx-runtime.ts +63 -0
- package/src/jsx-types.d.ts +909 -0
- package/src/server.ts +150 -0
- package/src/use-loadable.ts +67 -0
- package/src/vite.ts +48 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import type { RegularAtomFamilyToken } from "atom.io"
|
|
2
|
+
import type { RootStore } from "atom.io/internal"
|
|
3
|
+
import {
|
|
4
|
+
createRegularAtomFamily,
|
|
5
|
+
disposeFromStore,
|
|
6
|
+
findInStore,
|
|
7
|
+
IMPLICIT,
|
|
8
|
+
setIntoStore,
|
|
9
|
+
} from "atom.io/internal"
|
|
10
|
+
|
|
11
|
+
import { browserApplication } from "./astro-application.ts"
|
|
12
|
+
import { failPreparation, storeContext } from "./astro-context.ts"
|
|
13
|
+
import type { Island } from "./astro-shared.ts"
|
|
14
|
+
import { island, islandStore } from "./astro-shared.ts"
|
|
15
|
+
import { onCleanup, useValue } from "./core.ts"
|
|
16
|
+
import { hydrate, render } from "./dom.ts"
|
|
17
|
+
|
|
18
|
+
type Props = Record<string, any>
|
|
19
|
+
type Slots = Record<string, string>
|
|
20
|
+
type Hydrator = (
|
|
21
|
+
component: Island,
|
|
22
|
+
props: Props,
|
|
23
|
+
slots: Slots,
|
|
24
|
+
metadata: { client: string },
|
|
25
|
+
) => Promise<void>
|
|
26
|
+
const roots = new WeakMap<
|
|
27
|
+
Element,
|
|
28
|
+
{
|
|
29
|
+
component: Island
|
|
30
|
+
ready: Promise<void>
|
|
31
|
+
update(props: Props, slots: Slots): void
|
|
32
|
+
dispose(): void
|
|
33
|
+
}
|
|
34
|
+
>()
|
|
35
|
+
type State = { props: Props; slots: Slots }
|
|
36
|
+
const states = new WeakMap<RootStore, RegularAtomFamilyToken<State, number>>()
|
|
37
|
+
let serial = 0
|
|
38
|
+
|
|
39
|
+
export default (element: HTMLElement): Hydrator =>
|
|
40
|
+
async (component, props, slots, { client }) => {
|
|
41
|
+
const previous = roots.get(element)
|
|
42
|
+
if (previous?.component === component) {
|
|
43
|
+
previous.update(props, slots)
|
|
44
|
+
return previous.ready
|
|
45
|
+
}
|
|
46
|
+
previous?.dispose()
|
|
47
|
+
const shared = !component.createStore && !component.isolate
|
|
48
|
+
const application = shared
|
|
49
|
+
? browserApplication(element.ownerDocument)
|
|
50
|
+
: undefined
|
|
51
|
+
if (application && client === `only`) application.canResume = false
|
|
52
|
+
const lifetime = storeContext(element, application)
|
|
53
|
+
const initial = { props, slots }
|
|
54
|
+
let current = initial
|
|
55
|
+
let update: ((value: State) => void) | undefined
|
|
56
|
+
let disposed = false
|
|
57
|
+
const dispose = () => {
|
|
58
|
+
if (disposed) return
|
|
59
|
+
disposed = true
|
|
60
|
+
element.removeEventListener(`astro:unmount`, dispose)
|
|
61
|
+
roots.delete(element)
|
|
62
|
+
lifetime.dispose()
|
|
63
|
+
}
|
|
64
|
+
// Publish the pending owner before invoking user code. Repeated hydration
|
|
65
|
+
// calls update its props and share its preparation rather than racing stores.
|
|
66
|
+
const ready = Promise.resolve().then(async () => {
|
|
67
|
+
if (lifetime.context.signal.aborted) return
|
|
68
|
+
let mounting = false
|
|
69
|
+
try {
|
|
70
|
+
const store = await Promise.race([
|
|
71
|
+
islandStore(
|
|
72
|
+
component,
|
|
73
|
+
props,
|
|
74
|
+
lifetime.context,
|
|
75
|
+
shared ? IMPLICIT.STORE : undefined,
|
|
76
|
+
),
|
|
77
|
+
lifetime.cancelled,
|
|
78
|
+
])
|
|
79
|
+
if (lifetime.context.signal.aborted) return
|
|
80
|
+
mounting = true
|
|
81
|
+
const replace =
|
|
82
|
+
client === `only` ||
|
|
83
|
+
previous !== undefined ||
|
|
84
|
+
application?.canResume === false
|
|
85
|
+
// Astro leaves this sentinel on islands inserted by ClientRouter.
|
|
86
|
+
const sentinel = element.lastChild
|
|
87
|
+
if (sentinel?.nodeType === 8 && sentinel.nodeValue === `astro:end`)
|
|
88
|
+
sentinel.remove()
|
|
89
|
+
let family = states.get(store!)
|
|
90
|
+
if (!family) {
|
|
91
|
+
family = createRegularAtomFamily<State, number, never>(store!, {
|
|
92
|
+
key: `🔍 spa islands`,
|
|
93
|
+
default: { props: {}, slots: {} },
|
|
94
|
+
})
|
|
95
|
+
states.set(store!, family)
|
|
96
|
+
}
|
|
97
|
+
const state = findInStore(store!, family, serial++)
|
|
98
|
+
setIntoStore(store!, state, replace ? current : initial)
|
|
99
|
+
lifetime.context.onCleanup(() => {
|
|
100
|
+
disposeFromStore(store!, state)
|
|
101
|
+
})
|
|
102
|
+
const mount = () => {
|
|
103
|
+
onCleanup(dispose)
|
|
104
|
+
const value = useValue(state)
|
|
105
|
+
return () => island(component, value().props, value().slots)
|
|
106
|
+
}
|
|
107
|
+
if (replace) element.replaceChildren()
|
|
108
|
+
const root = (replace ? render : hydrate)(mount, element, {
|
|
109
|
+
store: store!,
|
|
110
|
+
})
|
|
111
|
+
lifetime.context.onCleanup(root.dispose)
|
|
112
|
+
update = (next) => {
|
|
113
|
+
setIntoStore(store!, state, next)
|
|
114
|
+
root.flush()
|
|
115
|
+
}
|
|
116
|
+
// Match the existing SSR tree before applying a structural prop update
|
|
117
|
+
// that arrived while its store was still being prepared.
|
|
118
|
+
if (!replace && current !== initial) update(current)
|
|
119
|
+
} catch (error) {
|
|
120
|
+
if (lifetime.context.signal.aborted && !mounting) return
|
|
121
|
+
failPreparation(error, dispose)
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
roots.set(element, {
|
|
125
|
+
component,
|
|
126
|
+
ready,
|
|
127
|
+
update(nextProps, nextSlots) {
|
|
128
|
+
current = { props: nextProps, slots: nextSlots }
|
|
129
|
+
update?.(current)
|
|
130
|
+
},
|
|
131
|
+
dispose,
|
|
132
|
+
})
|
|
133
|
+
element.addEventListener(`astro:unmount`, dispose, { once: true })
|
|
134
|
+
lifetime.context.onCleanup(dispose)
|
|
135
|
+
return ready
|
|
136
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AstroApplicationContext,
|
|
3
|
+
AstroStoreContext,
|
|
4
|
+
} from "./astro-shared.ts"
|
|
5
|
+
|
|
6
|
+
type Lifetime = {
|
|
7
|
+
signal: AbortSignal
|
|
8
|
+
onCleanup(cleanup: () => void): () => void
|
|
9
|
+
dispose(): void
|
|
10
|
+
cancelled: Promise<undefined>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function lifetime(): Lifetime {
|
|
14
|
+
const controller = new AbortController()
|
|
15
|
+
const cleanups = new Set<() => void>()
|
|
16
|
+
let cancel!: () => void
|
|
17
|
+
const cancelled = new Promise<undefined>((resolve) => {
|
|
18
|
+
cancel = () => {
|
|
19
|
+
resolve(undefined)
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
return {
|
|
23
|
+
signal: controller.signal,
|
|
24
|
+
onCleanup(cleanup) {
|
|
25
|
+
if (controller.signal.aborted) cleanup()
|
|
26
|
+
else cleanups.add(cleanup)
|
|
27
|
+
return () => {
|
|
28
|
+
cleanups.delete(cleanup)
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
cancelled,
|
|
32
|
+
dispose() {
|
|
33
|
+
if (controller.signal.aborted) return
|
|
34
|
+
controller.abort()
|
|
35
|
+
cancel()
|
|
36
|
+
const errors: unknown[] = []
|
|
37
|
+
for (const cleanup of [...cleanups].reverse()) {
|
|
38
|
+
try {
|
|
39
|
+
cleanup()
|
|
40
|
+
} catch (error) {
|
|
41
|
+
errors.push(error)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
cleanups.clear()
|
|
45
|
+
if (errors.length)
|
|
46
|
+
throw new AggregateError(errors, `spa: Astro store cleanup failed`)
|
|
47
|
+
},
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type Application = Lifetime &
|
|
52
|
+
AstroApplicationContext & { canResume: boolean }
|
|
53
|
+
export function applicationContext(): Application {
|
|
54
|
+
const life = lifetime()
|
|
55
|
+
const resources = new Map<unknown, unknown>()
|
|
56
|
+
life.onCleanup(() => {
|
|
57
|
+
resources.clear()
|
|
58
|
+
})
|
|
59
|
+
return {
|
|
60
|
+
...life,
|
|
61
|
+
canResume: true,
|
|
62
|
+
get<T>(key: unknown, initialize: () => T): T {
|
|
63
|
+
life.signal.throwIfAborted()
|
|
64
|
+
if (!resources.has(key)) resources.set(key, initialize())
|
|
65
|
+
return resources.get(key) as T
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Island resources and shared application resources have distinct owners. */
|
|
71
|
+
export function storeContext(
|
|
72
|
+
element?: HTMLElement,
|
|
73
|
+
shared?: ReturnType<typeof applicationContext>,
|
|
74
|
+
): {
|
|
75
|
+
context: AstroStoreContext
|
|
76
|
+
dispose(): void
|
|
77
|
+
cancelled: Promise<undefined>
|
|
78
|
+
} {
|
|
79
|
+
const life = lifetime()
|
|
80
|
+
const application = shared ?? applicationContext()
|
|
81
|
+
if (shared) life.onCleanup(shared.onCleanup(life.dispose))
|
|
82
|
+
else life.onCleanup(application.dispose)
|
|
83
|
+
return {
|
|
84
|
+
context: {
|
|
85
|
+
element,
|
|
86
|
+
signal: life.signal,
|
|
87
|
+
onCleanup: life.onCleanup,
|
|
88
|
+
application,
|
|
89
|
+
},
|
|
90
|
+
dispose: life.dispose,
|
|
91
|
+
cancelled: life.cancelled,
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function failPreparation(error: unknown, dispose: () => void): never {
|
|
96
|
+
try {
|
|
97
|
+
dispose()
|
|
98
|
+
} catch (cleanup) {
|
|
99
|
+
throw new AggregateError([error, cleanup])
|
|
100
|
+
}
|
|
101
|
+
throw error
|
|
102
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { MiddlewareHandler } from "astro"
|
|
2
|
+
|
|
3
|
+
import { failPreparation } from "./astro-context.ts"
|
|
4
|
+
import { requestApplication } from "./astro-request.ts"
|
|
5
|
+
|
|
6
|
+
/** Keep request resources alive until the streamed response is finished. */
|
|
7
|
+
export const onRequest: MiddlewareHandler = async ({ request }, next) => {
|
|
8
|
+
const { application } = requestApplication(request)
|
|
9
|
+
try {
|
|
10
|
+
const response = await next()
|
|
11
|
+
if (!response.body) {
|
|
12
|
+
application.dispose()
|
|
13
|
+
return response
|
|
14
|
+
}
|
|
15
|
+
const reader = response.body.getReader()
|
|
16
|
+
const body = new ReadableStream<Uint8Array>({
|
|
17
|
+
async pull(controller) {
|
|
18
|
+
try {
|
|
19
|
+
const result = await reader.read()
|
|
20
|
+
if (result.done) {
|
|
21
|
+
application.dispose()
|
|
22
|
+
controller.close()
|
|
23
|
+
} else controller.enqueue(result.value)
|
|
24
|
+
} catch (error) {
|
|
25
|
+
try {
|
|
26
|
+
failPreparation(error, application.dispose)
|
|
27
|
+
} catch (failure) {
|
|
28
|
+
controller.error(failure)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
async cancel(reason) {
|
|
33
|
+
try {
|
|
34
|
+
await reader.cancel(reason)
|
|
35
|
+
} finally {
|
|
36
|
+
application.dispose()
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
return new Response(body, {
|
|
41
|
+
status: response.status,
|
|
42
|
+
statusText: response.statusText,
|
|
43
|
+
headers: response.headers,
|
|
44
|
+
})
|
|
45
|
+
} catch (error) {
|
|
46
|
+
return failPreparation(error, application.dispose)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { RootStore } from "atom.io/internal"
|
|
2
|
+
import { IMPLICIT, Store } from "atom.io/internal"
|
|
3
|
+
|
|
4
|
+
import { applicationContext } from "./astro-context.ts"
|
|
5
|
+
|
|
6
|
+
const requests = new WeakMap<Request, ReturnType<typeof createRequest>>()
|
|
7
|
+
function createRequest() {
|
|
8
|
+
const application = applicationContext()
|
|
9
|
+
let store: RootStore | undefined
|
|
10
|
+
return {
|
|
11
|
+
application,
|
|
12
|
+
get store(): RootStore {
|
|
13
|
+
application.signal.throwIfAborted()
|
|
14
|
+
if (!store) {
|
|
15
|
+
store = new Store(
|
|
16
|
+
{ name: `spa-request`, lifespan: `ephemeral`, isProduction: true },
|
|
17
|
+
IMPLICIT.STORE,
|
|
18
|
+
) as RootStore
|
|
19
|
+
const selected = store
|
|
20
|
+
application.onCleanup(() => {
|
|
21
|
+
const errors: unknown[] = []
|
|
22
|
+
for (const state of selected.atoms.values()) {
|
|
23
|
+
try {
|
|
24
|
+
state.cleanup?.()
|
|
25
|
+
} catch (error) {
|
|
26
|
+
errors.push(error)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
for (const resource of selected.miscResources.values()) {
|
|
30
|
+
try {
|
|
31
|
+
resource[Symbol.dispose]()
|
|
32
|
+
} catch (error) {
|
|
33
|
+
errors.push(error)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (errors.length)
|
|
37
|
+
throw new AggregateError(errors, `spa: request cleanup failed`)
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
syncDefinitions(store)
|
|
41
|
+
return store
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export function requestApplication(
|
|
46
|
+
request: Request,
|
|
47
|
+
): ReturnType<typeof createRequest> {
|
|
48
|
+
let app = requests.get(request)
|
|
49
|
+
if (!app) {
|
|
50
|
+
app = createRequest()
|
|
51
|
+
requests.set(request, app)
|
|
52
|
+
const dispose = () => {
|
|
53
|
+
app!.application.dispose()
|
|
54
|
+
}
|
|
55
|
+
request.signal.addEventListener(`abort`, dispose, { once: true })
|
|
56
|
+
app.application.onCleanup(() => {
|
|
57
|
+
request.signal.removeEventListener(`abort`, dispose)
|
|
58
|
+
})
|
|
59
|
+
if (request.signal.aborted) dispose()
|
|
60
|
+
}
|
|
61
|
+
return app
|
|
62
|
+
}
|
|
63
|
+
/** Install definitions imported after an earlier island started this request. */
|
|
64
|
+
export function syncDefinitions(store: RootStore): void {
|
|
65
|
+
const source = IMPLICIT.STORE
|
|
66
|
+
for (const field of [
|
|
67
|
+
`families`,
|
|
68
|
+
`atoms`,
|
|
69
|
+
`readonlySelectors`,
|
|
70
|
+
`writableSelectors`,
|
|
71
|
+
`transactions`,
|
|
72
|
+
`timelineFamilies`,
|
|
73
|
+
`timelines`,
|
|
74
|
+
] as const) {
|
|
75
|
+
for (const [key, resource] of source[field]) {
|
|
76
|
+
if (!store[field].has(key)) resource.install(store)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/* oxlint-disable typescript/require-await -- Astro requires async renderer callbacks, even for synchronous rendering. */
|
|
2
|
+
import type { NamedSSRLoadedRendererValue } from "astro"
|
|
3
|
+
|
|
4
|
+
import { failPreparation, storeContext } from "./astro-context.ts"
|
|
5
|
+
import { requestApplication } from "./astro-request.ts"
|
|
6
|
+
import type { Island } from "./astro-shared.ts"
|
|
7
|
+
import { island, islandStore } from "./astro-shared.ts"
|
|
8
|
+
import { renderToString } from "./server.ts"
|
|
9
|
+
|
|
10
|
+
const renderer: NamedSSRLoadedRendererValue = {
|
|
11
|
+
name: `spa`,
|
|
12
|
+
async check(component: Island) {
|
|
13
|
+
return component?.spa === true
|
|
14
|
+
},
|
|
15
|
+
async renderToStaticMarkup(component: Island, props, slots) {
|
|
16
|
+
const request = (this as unknown as { result?: { request: Request } }).result
|
|
17
|
+
?.request
|
|
18
|
+
const shared =
|
|
19
|
+
!component.createStore && !component.isolate && request
|
|
20
|
+
? requestApplication(request)
|
|
21
|
+
: undefined
|
|
22
|
+
const lifetime = storeContext(undefined, shared?.application)
|
|
23
|
+
try {
|
|
24
|
+
const store = await islandStore(
|
|
25
|
+
component,
|
|
26
|
+
props,
|
|
27
|
+
lifetime.context,
|
|
28
|
+
shared?.store,
|
|
29
|
+
)
|
|
30
|
+
return {
|
|
31
|
+
html: renderToString(() => () => island(component, props, slots), {
|
|
32
|
+
store,
|
|
33
|
+
idPrefix: crypto.randomUUID(),
|
|
34
|
+
}),
|
|
35
|
+
}
|
|
36
|
+
} catch (error) {
|
|
37
|
+
failPreparation(error, lifetime.dispose)
|
|
38
|
+
} finally {
|
|
39
|
+
lifetime.dispose()
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
export default renderer
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { RootStore } from "atom.io/internal"
|
|
2
|
+
import { IMPLICIT, Store } from "atom.io/internal"
|
|
3
|
+
|
|
4
|
+
import type { Component, VNode } from "./core.ts"
|
|
5
|
+
import { jsx } from "./jsx-runtime.ts"
|
|
6
|
+
|
|
7
|
+
export interface AstroApplicationContext {
|
|
8
|
+
/** Whether the application still represents its initial server snapshot. */
|
|
9
|
+
readonly canResume: boolean
|
|
10
|
+
readonly signal: AbortSignal
|
|
11
|
+
onCleanup(cleanup: () => void): void
|
|
12
|
+
/** Initialize a resource once for this application lifetime. */
|
|
13
|
+
get<T>(key: unknown, initialize: () => T): T
|
|
14
|
+
}
|
|
15
|
+
export interface AstroStoreContext {
|
|
16
|
+
/** Shared across default islands; private for explicitly isolated islands. */
|
|
17
|
+
application: AstroApplicationContext
|
|
18
|
+
/** The browser island host; undefined during server rendering. */
|
|
19
|
+
element: HTMLElement | undefined
|
|
20
|
+
/** Aborted when preparation is cancelled or the island is released. */
|
|
21
|
+
signal: AbortSignal
|
|
22
|
+
/** Release resources even if preparation or rendering fails. */
|
|
23
|
+
onCleanup(cleanup: () => void): void
|
|
24
|
+
}
|
|
25
|
+
export type AstroStoreFactory<P = Record<string, any>> = (
|
|
26
|
+
props: P,
|
|
27
|
+
context: AstroStoreContext,
|
|
28
|
+
) => RootStore | Promise<RootStore>
|
|
29
|
+
export type AstroStorePreparation<P = Record<string, any>> = (
|
|
30
|
+
store: RootStore,
|
|
31
|
+
props: P,
|
|
32
|
+
context: AstroStoreContext,
|
|
33
|
+
) => void | Promise<void>
|
|
34
|
+
export type Island<P = any> = Component<P> & {
|
|
35
|
+
spa?: boolean
|
|
36
|
+
/** Opt into a private island store instead of the shared application store. */
|
|
37
|
+
isolate?: boolean
|
|
38
|
+
createStore?: AstroStoreFactory<P>
|
|
39
|
+
prepareStore?: AstroStorePreparation<P>
|
|
40
|
+
}
|
|
41
|
+
export async function islandStore(
|
|
42
|
+
component: Island,
|
|
43
|
+
props: Record<string, any>,
|
|
44
|
+
context: AstroStoreContext,
|
|
45
|
+
shared?: RootStore,
|
|
46
|
+
): Promise<RootStore> {
|
|
47
|
+
const store = await (component.createStore?.(props, context) ??
|
|
48
|
+
shared ??
|
|
49
|
+
(new Store(
|
|
50
|
+
{ name: `spa-island`, lifespan: `ephemeral`, isProduction: true },
|
|
51
|
+
IMPLICIT.STORE,
|
|
52
|
+
) as RootStore))
|
|
53
|
+
context.signal.throwIfAborted()
|
|
54
|
+
await component.prepareStore?.(store, props, context)
|
|
55
|
+
context.signal.throwIfAborted()
|
|
56
|
+
return store
|
|
57
|
+
}
|
|
58
|
+
export function island(
|
|
59
|
+
component: Island,
|
|
60
|
+
props: Record<string, any>,
|
|
61
|
+
slots: Record<string, string>,
|
|
62
|
+
): VNode {
|
|
63
|
+
const children = Object.fromEntries(
|
|
64
|
+
Object.entries(slots).map(([name, html]) => [
|
|
65
|
+
name,
|
|
66
|
+
jsx(`astro-slot`, {
|
|
67
|
+
key: html,
|
|
68
|
+
name: name === `default` ? undefined : name,
|
|
69
|
+
innerHTML: html,
|
|
70
|
+
}),
|
|
71
|
+
]),
|
|
72
|
+
)
|
|
73
|
+
return jsx(component, {
|
|
74
|
+
...props,
|
|
75
|
+
slots: children,
|
|
76
|
+
children: children[`default`],
|
|
77
|
+
})
|
|
78
|
+
}
|
package/src/astro.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { AstroIntegration } from "astro"
|
|
2
|
+
|
|
3
|
+
import type { SpaPluginOptions } from "./vite.ts"
|
|
4
|
+
import spa from "./vite.ts"
|
|
5
|
+
|
|
6
|
+
export type {
|
|
7
|
+
AstroApplicationContext,
|
|
8
|
+
AstroStoreContext,
|
|
9
|
+
AstroStoreFactory,
|
|
10
|
+
AstroStorePreparation,
|
|
11
|
+
Island,
|
|
12
|
+
} from "./astro-shared.ts"
|
|
13
|
+
|
|
14
|
+
export default function integration(
|
|
15
|
+
options: SpaPluginOptions = {},
|
|
16
|
+
): AstroIntegration {
|
|
17
|
+
return {
|
|
18
|
+
name: `spa`,
|
|
19
|
+
hooks: {
|
|
20
|
+
"astro:config:setup"({
|
|
21
|
+
addRenderer,
|
|
22
|
+
addMiddleware,
|
|
23
|
+
injectScript,
|
|
24
|
+
updateConfig,
|
|
25
|
+
}) {
|
|
26
|
+
addMiddleware({
|
|
27
|
+
entrypoint: `@atom.io/spa/astro-middleware`,
|
|
28
|
+
order: `pre`,
|
|
29
|
+
})
|
|
30
|
+
injectScript(
|
|
31
|
+
`page`,
|
|
32
|
+
`import { browserApplication } from '@atom.io/spa/astro-application'; browserApplication(document);`,
|
|
33
|
+
)
|
|
34
|
+
addRenderer({
|
|
35
|
+
name: `spa`,
|
|
36
|
+
clientEntrypoint: `@atom.io/spa/astro-client`,
|
|
37
|
+
serverEntrypoint: `@atom.io/spa/astro-server`,
|
|
38
|
+
})
|
|
39
|
+
updateConfig({
|
|
40
|
+
vite: {
|
|
41
|
+
plugins: [spa({ include: /\.spa\.[jt]sx$/, ...options })],
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
}
|