@hamur/adapter-solid 0.0.0-stage → 0.0.2
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 +22 -0
- package/README.md +35 -2
- package/dist/chunks/browser-index-8rhv2s58.js +14 -0
- package/dist/chunks/browser-index-b05qbzsh.js +165 -0
- package/dist/chunks/browser-index-n80jt19y.js +114 -0
- package/dist/chunks/browser-index-ncnkshd9.js +39 -0
- package/dist/chunks/browser-index-r4jwfgh7.js +407 -0
- package/dist/src/adapter.js +556 -0
- package/dist/src/error-page.js +466 -0
- package/dist/src/index.js +1535 -0
- package/dist/src/keepalive.js +6 -0
- package/dist/src/runtime/client.js +174 -0
- package/dist/src/runtime/island.js +58 -0
- package/dist/src/runtime/server.js +138 -0
- package/dist/types/src/activation.d.ts +15 -0
- package/dist/types/src/adapter.d.ts +4 -0
- package/dist/types/src/async-data.d.ts +21 -0
- package/dist/types/src/client-component.d.ts +10 -0
- package/dist/types/src/client-fallback.d.ts +2 -0
- package/dist/types/src/context.d.ts +89 -0
- package/dist/types/src/error-page.d.ts +1 -0
- package/dist/types/src/index.d.ts +392 -0
- package/dist/types/src/island-html.d.ts +13 -0
- package/dist/types/src/island.d.ts +20 -0
- package/dist/types/src/jsx-locator.d.ts +6 -0
- package/dist/types/src/keepalive.d.ts +30 -0
- package/dist/types/src/lazy-hydration.d.ts +13 -0
- package/dist/types/src/markdown-page.d.ts +9 -0
- package/dist/types/src/markdown.d.ts +13 -0
- package/dist/types/src/plugin.d.ts +44 -0
- package/dist/types/src/runtime/client.d.ts +13 -0
- package/dist/types/src/runtime/hmr-solid.d.ts +5 -0
- package/dist/types/src/runtime/hmr.d.ts +27 -0
- package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
- package/dist/types/src/runtime/hydration.d.ts +10 -0
- package/dist/types/src/runtime/island.d.ts +8 -0
- package/dist/types/src/runtime/portal.d.ts +13 -0
- package/dist/types/src/runtime/server.d.ts +14 -0
- package/dist/types/src/runtime/trusted.d.ts +2 -0
- package/dist/types/src/server-island.d.ts +10 -0
- package/dist/types/src/svg.d.ts +10 -0
- package/package.json +74 -4
- package/src/activation.ts +51 -0
- package/src/adapter.ts +42 -0
- package/src/async-data.ts +105 -0
- package/src/client-component.ts +40 -0
- package/src/client-fallback.ts +2 -0
- package/src/context.ts +495 -0
- package/src/env.d.ts +3 -0
- package/src/error-page.ts +1 -0
- package/src/index.ts +1900 -0
- package/src/island-html.ts +58 -0
- package/src/island.ts +55 -0
- package/src/jsx-locator.ts +199 -0
- package/src/keepalive.ts +147 -0
- package/src/lazy-hydration.ts +146 -0
- package/src/markdown-page.ts +18 -0
- package/src/markdown.ts +70 -0
- package/src/plugin.ts +310 -0
- package/src/runtime/client.ts +175 -0
- package/src/runtime/hmr-solid.ts +19 -0
- package/src/runtime/hmr.ts +231 -0
- package/src/runtime/hydration-dev.ts +41 -0
- package/src/runtime/hydration.ts +9 -0
- package/src/runtime/island.ts +66 -0
- package/src/runtime/portal.ts +64 -0
- package/src/runtime/server.ts +160 -0
- package/src/runtime/trusted.ts +16 -0
- package/src/server-island.ts +47 -0
- package/src/solid.d.ts +4 -0
- package/src/svg.ts +53 -0
- package/src/webmcp.d.ts +20 -0
package/src/context.ts
ADDED
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Errored,
|
|
3
|
+
createComponent,
|
|
4
|
+
createContext,
|
|
5
|
+
createEffect,
|
|
6
|
+
createMemo,
|
|
7
|
+
mapArray,
|
|
8
|
+
merge,
|
|
9
|
+
onCleanup,
|
|
10
|
+
untrack,
|
|
11
|
+
useContext,
|
|
12
|
+
} from "solid-js"
|
|
13
|
+
import type { Accessor, Component } from "solid-js"
|
|
14
|
+
import { Dynamic, dynamic, isServer, type JSX } from "@solidjs/web"
|
|
15
|
+
import type { AppContext as HamurApp, ClearErrorOptions, ErrorPageProps, Head, HamurRedirect } from "hamur/app"
|
|
16
|
+
import type {
|
|
17
|
+
ClientContext,
|
|
18
|
+
FormStatusStore,
|
|
19
|
+
KeptUnit,
|
|
20
|
+
NavigationState,
|
|
21
|
+
RenderContext,
|
|
22
|
+
RenderEntry,
|
|
23
|
+
RenderTree,
|
|
24
|
+
ResolvedRoute,
|
|
25
|
+
RouteValue,
|
|
26
|
+
RouterApi,
|
|
27
|
+
} from "hamur/adapter"
|
|
28
|
+
import { createError, isRedirect, isHamurError, withBase } from "hamur/app"
|
|
29
|
+
import { createRouterApi, noteOutlet, publicError, routeBoundary, type RouterSource } from "hamur/adapter"
|
|
30
|
+
import { routeStateOf, setServerHTML, SLOT_ELEMENT } from "hamur/adapter"
|
|
31
|
+
import type { RouteLocation } from "hamur/app"
|
|
32
|
+
import { activation, detach, setHidden, type Activation } from "./activation.ts"
|
|
33
|
+
import type { TeleportSink } from "./runtime/portal.ts"
|
|
34
|
+
|
|
35
|
+
const SlotElement = dynamic(() => SLOT_ELEMENT, { static: true }) as Component<Record<string, unknown>>
|
|
36
|
+
|
|
37
|
+
const PUBLIC_FIELDS = new Set(["name", "message", "status", "data", "fatal"])
|
|
38
|
+
|
|
39
|
+
/** A thrown value that is not an `Error` may reach a boundary as the `cause` of one, so a redirect is read back from it. */
|
|
40
|
+
export function redirectOf(error: unknown): HamurRedirect | undefined {
|
|
41
|
+
if (isRedirect(error)) return error
|
|
42
|
+
const cause = error instanceof Error ? error.cause : undefined
|
|
43
|
+
return isRedirect(cause) ? cause : undefined
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* What the server render's `onError` returns for a failure Solid handles: the masked error, as a copy with
|
|
48
|
+
* only its public fields, because Solid writes every own property into the hydration script, and Bun adds `stack`,
|
|
49
|
+
* `sourceURL`, `line` and `column`. A boundary's fallback receives this copy, never the error.
|
|
50
|
+
*/
|
|
51
|
+
export function serializableError(app: HamurApp, error: unknown): unknown {
|
|
52
|
+
const redirect = redirectOf(error)
|
|
53
|
+
if (redirect) return redirect
|
|
54
|
+
const masked = publicError(app, error)
|
|
55
|
+
const own = isRethrown(error)
|
|
56
|
+
const original = originals.get(error as object) ?? error
|
|
57
|
+
if (!isHamurError(masked)) return own ? markRethrown(masked) : masked
|
|
58
|
+
const copy = createError({ status: masked.status, message: masked.message, data: masked.data, fatal: masked.fatal })
|
|
59
|
+
for (const key of Object.getOwnPropertyNames(copy))
|
|
60
|
+
if (!PUBLIC_FIELDS.has(key)) delete (copy as unknown as Record<string, unknown>)[key]
|
|
61
|
+
if (typeof original === "object" && original !== null) originals.set(copy, original)
|
|
62
|
+
return own ? markRethrown(copy) : copy
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The error a serializable copy stands for, which the `error` hook and the request log see. */
|
|
66
|
+
const originals = new WeakMap<object, unknown>()
|
|
67
|
+
|
|
68
|
+
export function originalOf(error: unknown): unknown {
|
|
69
|
+
return typeof error === "object" && error !== null ? (originals.get(error) ?? error) : error
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const rethrown = new WeakSet<object>()
|
|
73
|
+
|
|
74
|
+
/** Marks an error a route boundary's own render threw: no route boundary above may catch it. */
|
|
75
|
+
function markRethrown<T>(error: T): T {
|
|
76
|
+
if (typeof error === "object" && error !== null) rethrown.add(error)
|
|
77
|
+
return error
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function isRethrown(error: unknown): boolean {
|
|
81
|
+
return typeof error === "object" && error !== null && rethrown.has(error)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface NavigationSource {
|
|
85
|
+
get(): NavigationState
|
|
86
|
+
subscribe(listener: () => void): () => void
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface AppState {
|
|
90
|
+
route: RouteValue
|
|
91
|
+
router: RouterApi
|
|
92
|
+
baseURL: string
|
|
93
|
+
href: RenderContext["href"]
|
|
94
|
+
components: RenderTree["components"]
|
|
95
|
+
data: Record<string, unknown>
|
|
96
|
+
handlerState?: unknown
|
|
97
|
+
tree: RenderTree
|
|
98
|
+
head: Head
|
|
99
|
+
navigation: NavigationSource
|
|
100
|
+
app: HamurApp
|
|
101
|
+
context: RenderContext | ClientContext
|
|
102
|
+
/** Server: where `Portal` writes; absent in an island's root. */
|
|
103
|
+
teleports?: TeleportSink
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface Level {
|
|
107
|
+
state: Accessor<AppState>
|
|
108
|
+
index: number
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export const AppContext = createContext<Accessor<AppState> | null>(null)
|
|
112
|
+
/** The `href` of the `Link` whose children render, read by `useLinkStatus()`. */
|
|
113
|
+
export const LinkHrefContext = createContext<Accessor<string> | null>(null)
|
|
114
|
+
export const FormStatusContext = createContext<FormStatusStore | null>(null)
|
|
115
|
+
|
|
116
|
+
export function detachedHref(to: string | RouteLocation): string {
|
|
117
|
+
if (typeof to !== "string") throw new Error(`Link to the route "${to.name}" must be rendered inside a Hamur app`)
|
|
118
|
+
return withBase(to, "/")
|
|
119
|
+
}
|
|
120
|
+
export const LevelContext = createContext<Level | null>(null)
|
|
121
|
+
export const OutletValueContext = createContext<Accessor<unknown> | null>(null)
|
|
122
|
+
/** `true` below a `"never"` lazy component on the server, so its async data stays out of the payload. */
|
|
123
|
+
export const NeverHydratedContext = createContext(false)
|
|
124
|
+
export const ActivationContext = createContext<Activation | null>(null)
|
|
125
|
+
|
|
126
|
+
/** The error of the route boundary the component renders in, read by `useError()`. */
|
|
127
|
+
export const ErrorScope = createContext<ErrorPageProps | null>(null)
|
|
128
|
+
|
|
129
|
+
export function routerOf(
|
|
130
|
+
navigator: unknown,
|
|
131
|
+
baseURL = "/",
|
|
132
|
+
page?: Omit<RouterSource, "navigator" | "baseURL">,
|
|
133
|
+
): RouterApi {
|
|
134
|
+
return createRouterApi({ ...page, navigator, baseURL })
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const idle: NavigationState = { state: "idle", pending: false }
|
|
138
|
+
|
|
139
|
+
export function navigationOf(navigator: unknown): NavigationSource {
|
|
140
|
+
const source = navigator as Partial<NavigationSource> | undefined
|
|
141
|
+
return {
|
|
142
|
+
get: () => source?.get?.() ?? idle,
|
|
143
|
+
subscribe: (listener) => source?.subscribe?.(listener) ?? (() => {}),
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function routeValue(
|
|
148
|
+
ctx: Pick<RenderContext, "route"> & Partial<Pick<RenderContext, "app" | "payload" | "tree">>,
|
|
149
|
+
): RouteValue {
|
|
150
|
+
const { route } = ctx
|
|
151
|
+
return {
|
|
152
|
+
path: route.path,
|
|
153
|
+
fullPath: route.fullPath,
|
|
154
|
+
params: route.params,
|
|
155
|
+
query: route.query,
|
|
156
|
+
search: route.search,
|
|
157
|
+
hash: route.hash,
|
|
158
|
+
name: route.name,
|
|
159
|
+
meta: route.meta,
|
|
160
|
+
matched: route.matched,
|
|
161
|
+
groups: route.groups ?? [],
|
|
162
|
+
...routeStateOf(ctx),
|
|
163
|
+
} as RouteValue
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function appState(
|
|
167
|
+
ctx: Pick<RenderContext | ClientContext, "route" | "tree" | "payload" | "app" | "head" | "href"> & {
|
|
168
|
+
navigator?: unknown
|
|
169
|
+
baseURL?: string
|
|
170
|
+
},
|
|
171
|
+
): AppState {
|
|
172
|
+
const { data = {} } = ctx.payload as { data?: Record<string, unknown> }
|
|
173
|
+
return {
|
|
174
|
+
route: routeValue(ctx),
|
|
175
|
+
router: routerOf(ctx.navigator, ctx.baseURL, ctx),
|
|
176
|
+
baseURL: ctx.baseURL ?? "/",
|
|
177
|
+
href: ctx.href,
|
|
178
|
+
components: ctx.tree.components,
|
|
179
|
+
data,
|
|
180
|
+
handlerState: (ctx.payload as { handlerState?: unknown }).handlerState,
|
|
181
|
+
tree: ctx.tree,
|
|
182
|
+
head: ctx.head,
|
|
183
|
+
navigation: navigationOf(ctx.navigator),
|
|
184
|
+
app: ctx.app,
|
|
185
|
+
context: ctx as RenderContext | ClientContext,
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function errorOf(state: AppState): ErrorPageProps | undefined {
|
|
190
|
+
const stored = state.app.error?.get()
|
|
191
|
+
if (stored) return stored
|
|
192
|
+
return state.tree.error ? state.route.error : undefined
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function useAppState(name: string): Accessor<AppState> {
|
|
196
|
+
const state = useContext(AppContext)
|
|
197
|
+
if (!state) throw new Error(`${name}() must be called inside a Hamur app`)
|
|
198
|
+
return state
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function Level(props: { state: Accessor<AppState>; index: number }) {
|
|
202
|
+
const entry = () => props.state().components[props.index]
|
|
203
|
+
const last = createMemo(() => props.index >= props.state().components.length - 1)
|
|
204
|
+
return createComponent(LevelContext, {
|
|
205
|
+
value: {
|
|
206
|
+
state: props.state,
|
|
207
|
+
get index() {
|
|
208
|
+
return props.index
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
get children() {
|
|
212
|
+
return createComponent(
|
|
213
|
+
Dynamic as Component<Record<string, unknown>>,
|
|
214
|
+
merge(() => entry()?.props ?? {}, {
|
|
215
|
+
get component() {
|
|
216
|
+
return entry()?.component
|
|
217
|
+
},
|
|
218
|
+
get children() {
|
|
219
|
+
return last()
|
|
220
|
+
? entry()?.props?.children
|
|
221
|
+
: createComponent(Nest, {
|
|
222
|
+
state: props.state,
|
|
223
|
+
get index() {
|
|
224
|
+
return props.index + 1
|
|
225
|
+
},
|
|
226
|
+
})
|
|
227
|
+
},
|
|
228
|
+
}),
|
|
229
|
+
)
|
|
230
|
+
},
|
|
231
|
+
})
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export type Props = { state: Accessor<AppState>; index: number }
|
|
235
|
+
type EntryOf = () => RenderEntry | undefined
|
|
236
|
+
|
|
237
|
+
/** What a route boundary renders, in the same nesting whether it came from a catch or from the tree. */
|
|
238
|
+
function Body(
|
|
239
|
+
props: Props,
|
|
240
|
+
component: Component<Record<string, unknown>>,
|
|
241
|
+
error: ErrorPageProps,
|
|
242
|
+
componentProps: Record<string, unknown>,
|
|
243
|
+
) {
|
|
244
|
+
return createComponent(ErrorScope, {
|
|
245
|
+
value: error,
|
|
246
|
+
get children() {
|
|
247
|
+
return createComponent(LevelContext, {
|
|
248
|
+
value: {
|
|
249
|
+
state: props.state,
|
|
250
|
+
get index() {
|
|
251
|
+
return props.index
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
get children() {
|
|
255
|
+
return createComponent(Errored, {
|
|
256
|
+
fallback: (thrown: Accessor<unknown>) => {
|
|
257
|
+
throw markRethrown(untrack(thrown))
|
|
258
|
+
},
|
|
259
|
+
get children() {
|
|
260
|
+
return createComponent(component, componentProps)
|
|
261
|
+
},
|
|
262
|
+
})
|
|
263
|
+
},
|
|
264
|
+
})
|
|
265
|
+
},
|
|
266
|
+
})
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function Caught(props: Props, entry: EntryOf, error: unknown) {
|
|
270
|
+
if (isRethrown(error)) throw error
|
|
271
|
+
const redirect = redirectOf(error)
|
|
272
|
+
if (redirect) throw redirect
|
|
273
|
+
const state = untrack(props.state)
|
|
274
|
+
const caught = routeBoundary(state.context, entry()!, originalOf(error))
|
|
275
|
+
if (!caught) throw error
|
|
276
|
+
const clearError = (options?: ClearErrorOptions) => state.app.error?.clear(options) ?? Promise.resolve()
|
|
277
|
+
return Body(props, caught.component as Component<Record<string, unknown>>, caught.error, {
|
|
278
|
+
error: caught.error,
|
|
279
|
+
clearError,
|
|
280
|
+
})
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** A route boundary: a render error below this level shows the boundary component in its place until the entry changes. */
|
|
284
|
+
function Guarded(props: Props, entry: EntryOf) {
|
|
285
|
+
let shown: (() => void) | undefined
|
|
286
|
+
// Beside the boundary, not in its fallback, the watch outlives each reset; transparent, it takes no hydration key.
|
|
287
|
+
if (!isServer)
|
|
288
|
+
createEffect(
|
|
289
|
+
entry,
|
|
290
|
+
(current, previous) => {
|
|
291
|
+
if (current === previous || !shown) return
|
|
292
|
+
queueMicrotask(shown)
|
|
293
|
+
shown = undefined
|
|
294
|
+
},
|
|
295
|
+
{ defer: true, transparent: true },
|
|
296
|
+
)
|
|
297
|
+
return createComponent(Errored, {
|
|
298
|
+
fallback: (error: Accessor<unknown>, reset: () => void) => {
|
|
299
|
+
shown = reset
|
|
300
|
+
return Caught(props, entry, untrack(error))
|
|
301
|
+
},
|
|
302
|
+
get children() {
|
|
303
|
+
return Level(props)
|
|
304
|
+
},
|
|
305
|
+
})
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* The boundary entry of a tree whose load failed. It sits in the markup that the server writes for a render error, so
|
|
310
|
+
* hydration finds the same boundary: its child throws, and its fallback is the entry, in the nesting `Caught` renders.
|
|
311
|
+
*/
|
|
312
|
+
function Shown(props: Props, entry: EntryOf) {
|
|
313
|
+
const shown = untrack(entry)!
|
|
314
|
+
const error = shown.props?.error as ErrorPageProps
|
|
315
|
+
return createComponent(Errored, {
|
|
316
|
+
fallback: () => Body(props, shown.component as Component<Record<string, unknown>>, error, shown.props ?? {}),
|
|
317
|
+
get children(): never {
|
|
318
|
+
throw createError({ status: error.status, message: error.message })
|
|
319
|
+
},
|
|
320
|
+
})
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const ISLAND_ENTRY = "island"
|
|
324
|
+
|
|
325
|
+
/** The app state of an island's own root: one entry; a `Link` takes a path, as no route table ships. */
|
|
326
|
+
/**
|
|
327
|
+
* The island's children: the server writes their HTML; in the browser the slot element has no children, which keeps
|
|
328
|
+
* the nodes the server wrote while hydrating, and the same HTML is written only into one Solid creates anew.
|
|
329
|
+
*/
|
|
330
|
+
function slotElement(uid: string, html: string, kept?: IslandSlot): JSX.Element {
|
|
331
|
+
if (isServer) return createComponent(SlotElement, { uid, innerHTML: html })
|
|
332
|
+
const fill = (node: Element) => {
|
|
333
|
+
if (kept) kept.node = node
|
|
334
|
+
if (html && !node.hasChildNodes()) setServerHTML(node, html)
|
|
335
|
+
}
|
|
336
|
+
return createComponent(SlotElement, { uid, ref: fill })
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** The slot element an island root last made and the node it rendered, which an update reuses only while that node is still in the document. */
|
|
340
|
+
export interface IslandSlot {
|
|
341
|
+
element?: JSX.Element
|
|
342
|
+
node?: Element
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export function islandState(
|
|
346
|
+
component: unknown,
|
|
347
|
+
props: Record<string, unknown>,
|
|
348
|
+
ctx: { app: HamurApp; route: ResolvedRoute; baseURL: string; idPrefix: string; slots?: { default?: string } },
|
|
349
|
+
kept: IslandSlot = {},
|
|
350
|
+
update = false,
|
|
351
|
+
): AppState {
|
|
352
|
+
const href = (to: string | RouteLocation) => {
|
|
353
|
+
if (typeof to !== "string")
|
|
354
|
+
throw new Error(`Link to the route "${to.name}" cannot render in an island; give it a path`)
|
|
355
|
+
return withBase(to, ctx.baseURL)
|
|
356
|
+
}
|
|
357
|
+
const slot = ctx.slots?.default
|
|
358
|
+
const entryProps =
|
|
359
|
+
slot === undefined
|
|
360
|
+
? props
|
|
361
|
+
: Object.defineProperty({ ...props }, "children", {
|
|
362
|
+
enumerable: true,
|
|
363
|
+
get: () =>
|
|
364
|
+
update && kept.element !== undefined && kept.node?.isConnected
|
|
365
|
+
? kept.element
|
|
366
|
+
: (kept.element = slotElement(`s${ctx.idPrefix}`, slot, kept)),
|
|
367
|
+
})
|
|
368
|
+
return appState({
|
|
369
|
+
app: ctx.app,
|
|
370
|
+
head: ctx.app.head,
|
|
371
|
+
route: ctx.route,
|
|
372
|
+
baseURL: ctx.baseURL,
|
|
373
|
+
href,
|
|
374
|
+
tree: { components: [{ id: ISLAND_ENTRY, component, props: entryProps }] },
|
|
375
|
+
payload: {},
|
|
376
|
+
})
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** What holds the kept units beside the entry at `index`: the entry above it, or the tree for its first entry. */
|
|
380
|
+
function holderOf(state: AppState, index: number): { kept?: KeptUnit[] } | undefined {
|
|
381
|
+
if (index > 0) return state.components[index - 1]
|
|
382
|
+
return state.tree && state.components === state.tree.components ? state.tree : undefined
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
let keptOutlet: ((props: Props) => JSX.Element) | undefined
|
|
386
|
+
|
|
387
|
+
/** The core's keep-alive feature loads the outlet (`Adapter.keepAliveOutlet`) and hands it to `installKeepAlive` before the first tree that holds kept units; it takes what it needs from the object it is built with, so its chunk imports nothing from the base. */
|
|
388
|
+
export function installKeepAlive(module: typeof import("./keepalive.ts")): void {
|
|
389
|
+
keptOutlet ??= module.createKeptOutlet({
|
|
390
|
+
createComponent,
|
|
391
|
+
createEffect,
|
|
392
|
+
createMemo,
|
|
393
|
+
mapArray,
|
|
394
|
+
onCleanup,
|
|
395
|
+
untrack,
|
|
396
|
+
useContext,
|
|
397
|
+
dynamic,
|
|
398
|
+
ActivationContext,
|
|
399
|
+
AppContext,
|
|
400
|
+
activation,
|
|
401
|
+
detach,
|
|
402
|
+
setHidden,
|
|
403
|
+
routeValue,
|
|
404
|
+
holderOf,
|
|
405
|
+
Nest,
|
|
406
|
+
})
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function KeptOutlet(props: Props): JSX.Element {
|
|
410
|
+
if (!keptOutlet)
|
|
411
|
+
throw new Error("A tree with kept pages was rendered before the keep-alive part of the Solid adapter was installed")
|
|
412
|
+
return keptOutlet(props)
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function Nest(props: Props & { bare?: boolean }): JSX.Element {
|
|
416
|
+
if (!props.bare) {
|
|
417
|
+
const keeping = createMemo(() => holderOf(props.state(), props.index)?.kept !== undefined)
|
|
418
|
+
return createMemo(() =>
|
|
419
|
+
keeping() ? untrack(() => KeptOutlet(props)) : untrack(() => Plain(props)),
|
|
420
|
+
) as unknown as JSX.Element
|
|
421
|
+
}
|
|
422
|
+
return Plain(props)
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function Plain(props: Props): JSX.Element {
|
|
426
|
+
const entry: EntryOf = () => props.state().components[props.index]
|
|
427
|
+
const mode = createMemo(() => (entry()?.boundary ? "catch" : entry()?.caught ? "show" : "plain"))
|
|
428
|
+
const key = createMemo(() => entry()?.key)
|
|
429
|
+
return createMemo(() => {
|
|
430
|
+
const current = mode()
|
|
431
|
+
key()
|
|
432
|
+
return untrack(() =>
|
|
433
|
+
current === "catch" ? Guarded(props, entry) : current === "show" ? Shown(props, entry) : Level(props),
|
|
434
|
+
)
|
|
435
|
+
}) as unknown as JSX.Element
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const isDev = (import.meta as { dev?: boolean }).dev === true
|
|
439
|
+
const warnedSlots = new Set<string>()
|
|
440
|
+
|
|
441
|
+
export function Outlet(props: { context?: unknown; name?: string } = {}): unknown {
|
|
442
|
+
const level = useContext(LevelContext)
|
|
443
|
+
if (!level) throw new Error("Outlet must be rendered inside a Hamur app")
|
|
444
|
+
const above = useContext(OutletValueContext)
|
|
445
|
+
const owner = () => level.state().components[level.index]
|
|
446
|
+
if (isDev && props.name === undefined) noteOutlet(owner()?.id)
|
|
447
|
+
const last = createMemo(() => level.index >= level.state().components.length - 1)
|
|
448
|
+
const slot = createMemo(() => {
|
|
449
|
+
const name = props.name
|
|
450
|
+
if (name === undefined) return undefined
|
|
451
|
+
const tree = owner()?.outlets?.[name]
|
|
452
|
+
if (isDev && !tree && !warnedSlots.has(`${owner()?.id}:${name}`)) {
|
|
453
|
+
warnedSlots.add(`${owner()?.id}:${name}`)
|
|
454
|
+
console.warn(`Outlet name "${name}" has no slot in ${owner()?.id}`)
|
|
455
|
+
}
|
|
456
|
+
return tree
|
|
457
|
+
})
|
|
458
|
+
const slotState = createMemo((): AppState => {
|
|
459
|
+
const page = level.state()
|
|
460
|
+
const own = slot()
|
|
461
|
+
if (!own?.route) return { ...page, components: own?.components ?? [] }
|
|
462
|
+
const { state, key, navigationType } = page.route
|
|
463
|
+
return {
|
|
464
|
+
...page,
|
|
465
|
+
components: own.components,
|
|
466
|
+
route: routeValue({ ...page.context, route: { ...own.route, state, key, navigationType }, tree: own }),
|
|
467
|
+
}
|
|
468
|
+
})
|
|
469
|
+
return createComponent(OutletValueContext, {
|
|
470
|
+
value: () => (props.context !== undefined ? props.context : above?.()),
|
|
471
|
+
get children() {
|
|
472
|
+
return createMemo(() => {
|
|
473
|
+
if (props.name !== undefined)
|
|
474
|
+
return slot()?.components.length ? createComponent(Nest, { state: slotState, index: 0 }) : undefined
|
|
475
|
+
return last()
|
|
476
|
+
? undefined
|
|
477
|
+
: createComponent(Nest, {
|
|
478
|
+
state: level.state,
|
|
479
|
+
get index() {
|
|
480
|
+
return level.index + 1
|
|
481
|
+
},
|
|
482
|
+
})
|
|
483
|
+
}) as unknown as JSX.Element
|
|
484
|
+
},
|
|
485
|
+
})
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
export function Root(props: { state: Accessor<AppState> }) {
|
|
489
|
+
return createComponent(AppContext, {
|
|
490
|
+
value: props.state,
|
|
491
|
+
get children() {
|
|
492
|
+
return createComponent(Nest, { state: props.state, index: 0 })
|
|
493
|
+
},
|
|
494
|
+
})
|
|
495
|
+
}
|
package/src/env.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DefaultErrorPage as default } from "./index.ts"
|