@hamur/adapter-svelte 0.0.0-stage → 0.0.1
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 +31 -2
- package/dist/chunks/browser-index-324qgh1c.js +190 -0
- package/dist/chunks/browser-index-evqp5a71.js +64 -0
- package/dist/chunks/browser-index-xc6081e3.js +9 -0
- package/dist/src/adapter.js +480 -0
- package/dist/src/index.js +1161 -0
- package/dist/src/runtime/client.js +65 -0
- package/dist/src/runtime/island.js +72 -0
- package/dist/src/runtime/server.js +142 -0
- package/dist/types/src/activation.d.ts +15 -0
- package/dist/types/src/adapter.d.ts +2 -0
- package/dist/types/src/async-data.d.ts +21 -0
- package/dist/types/src/boundary-error.d.ts +9 -0
- package/dist/types/src/client-only.d.ts +6 -0
- package/dist/types/src/context.d.ts +87 -0
- package/dist/types/src/deferred.svelte.d.ts +6 -0
- package/dist/types/src/effect.svelte.d.ts +2 -0
- package/dist/types/src/hydrating.d.ts +4 -0
- package/dist/types/src/index.d.ts +270 -0
- package/dist/types/src/island-html.d.ts +13 -0
- package/dist/types/src/keepalive.d.ts +11 -0
- package/dist/types/src/lazy-hydration.d.ts +25 -0
- package/dist/types/src/markdown-page.d.ts +9 -0
- package/dist/types/src/plugin.d.ts +52 -0
- package/dist/types/src/runtime/client.d.ts +12 -0
- package/dist/types/src/runtime/hmr.d.ts +28 -0
- package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
- package/dist/types/src/runtime/hydration.d.ts +8 -0
- package/dist/types/src/runtime/island.d.ts +8 -0
- package/dist/types/src/runtime/server.d.ts +12 -0
- package/dist/types/src/server-component.d.ts +3 -0
- package/dist/types/src/store.d.ts +7 -0
- package/dist/types/src/svg.d.ts +10 -0
- package/package.json +43 -4
- package/src/Announcer.svelte +26 -0
- package/src/Await.svelte +43 -0
- package/src/AwaitAsync.svelte +42 -0
- package/src/BodyAttrs.svelte +7 -0
- package/src/Caught.svelte +41 -0
- package/src/ClientComponent.svelte +35 -0
- package/src/ClientFallback.svelte +46 -0
- package/src/ClientOnly.svelte +22 -0
- package/src/DefaultErrorPage.svelte +15 -0
- package/src/DevOnly.svelte +12 -0
- package/src/ErrorBoundary.svelte +14 -0
- package/src/FallbackEffect.svelte +9 -0
- package/src/Form.svelte +96 -0
- package/src/HeadLink.svelte +7 -0
- package/src/HeadMeta.svelte +7 -0
- package/src/HeadScript.svelte +7 -0
- package/src/HeadStyle.svelte +7 -0
- package/src/HeadTitle.svelte +6 -0
- package/src/HtmlAttrs.svelte +7 -0
- package/src/HttpHeader.svelte +7 -0
- package/src/HttpStatusCode.svelte +7 -0
- package/src/Island.svelte +59 -0
- package/src/IslandChildren.svelte +7 -0
- package/src/KeptOutlet.svelte +46 -0
- package/src/KeptPage.svelte +55 -0
- package/src/LazyHydration.svelte +54 -0
- package/src/Link.svelte +209 -0
- package/src/LoadingIndicator.svelte +18 -0
- package/src/MarkdownNodes.svelte +36 -0
- package/src/MarkdownRenderer.svelte +20 -0
- package/src/MarkdownSlots.svelte +33 -0
- package/src/Navigate.svelte +10 -0
- package/src/Nest.svelte +83 -0
- package/src/Outlet.svelte +31 -0
- package/src/Rethrow.svelte +7 -0
- package/src/Root.svelte +29 -0
- package/src/RouteAnnouncer.svelte +47 -0
- package/src/Scoped.svelte +11 -0
- package/src/ServerComponent.svelte +19 -0
- package/src/ServerComponentClient.svelte +64 -0
- package/src/ServerIsland.svelte +26 -0
- package/src/Slotted.svelte +11 -0
- package/src/Thrown.svelte +9 -0
- package/src/Time.svelte +23 -0
- package/src/activation.ts +51 -0
- package/src/adapter.ts +24 -0
- package/src/async-data.ts +105 -0
- package/src/boundary-error.ts +77 -0
- package/src/client-only.ts +8 -0
- package/src/context.ts +227 -0
- package/src/deferred.svelte.ts +33 -0
- package/src/effect.svelte.ts +7 -0
- package/src/env.d.ts +3 -0
- package/src/hydrating.ts +18 -0
- package/src/index.ts +1337 -0
- package/src/island-html.ts +58 -0
- package/src/keepalive.ts +12 -0
- package/src/lazy-hydration.ts +89 -0
- package/src/markdown-page.ts +17 -0
- package/src/plugin.ts +558 -0
- package/src/runtime/client.ts +61 -0
- package/src/runtime/hmr.ts +177 -0
- package/src/runtime/hydration-dev.ts +22 -0
- package/src/runtime/hydration.ts +7 -0
- package/src/runtime/island.ts +94 -0
- package/src/runtime/server.ts +87 -0
- package/src/server-component.ts +13 -0
- package/src/store.ts +31 -0
- package/src/svelte.d.ts +5 -0
- package/src/svg.ts +30 -0
- package/src/webmcp.d.ts +18 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
<script lang="ts" generics="T">
|
|
2
|
+
import { settledOf } from "hamur/adapter"
|
|
3
|
+
import type { Snippet } from "svelte"
|
|
4
|
+
import Rethrow from "./Rethrow.svelte"
|
|
5
|
+
|
|
6
|
+
let {
|
|
7
|
+
resolve,
|
|
8
|
+
children,
|
|
9
|
+
error,
|
|
10
|
+
}: { resolve: T; children?: Snippet<[Awaited<T>]>; fallback?: Snippet; error?: Snippet<[Error]> } = $props()
|
|
11
|
+
|
|
12
|
+
const isThenable = (value: unknown): value is PromiseLike<unknown> =>
|
|
13
|
+
typeof (value as { then?: unknown } | null)?.then === "function"
|
|
14
|
+
const settled = $derived(
|
|
15
|
+
isThenable(resolve) ? settledOf(resolve) : ({ status: "fulfilled", value: resolve } as const),
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
if ((import.meta as { dev?: boolean }).dev === true && !children)
|
|
19
|
+
console.warn("Await has nothing to render for its value")
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
{#if settled?.status === "fulfilled"}
|
|
23
|
+
{@render children?.(settled.value as Awaited<T>)}
|
|
24
|
+
{:else if settled?.status === "rejected"}
|
|
25
|
+
{#if error}
|
|
26
|
+
{@render error(settled.reason as Error)}
|
|
27
|
+
{:else}
|
|
28
|
+
<Rethrow error={settled.reason} />
|
|
29
|
+
{/if}
|
|
30
|
+
{:else}
|
|
31
|
+
<!-- a `pending` snippet would be rendered instead of the content on the server, so the boundary has none and the fallback is not shown -->
|
|
32
|
+
<svelte:boundary>
|
|
33
|
+
{@render children?.((await resolve) as Awaited<T>)}
|
|
34
|
+
{#snippet failed(reason)}
|
|
35
|
+
{#if error}
|
|
36
|
+
{@render error(reason as Error)}
|
|
37
|
+
{:else}
|
|
38
|
+
<Rethrow error={reason} />
|
|
39
|
+
{/if}
|
|
40
|
+
{/snippet}
|
|
41
|
+
</svelte:boundary>
|
|
42
|
+
{/if}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { untrack } from "svelte"
|
|
3
|
+
import type { Component as SvelteComponent } from "svelte"
|
|
4
|
+
import { routeBoundary } from "hamur/adapter"
|
|
5
|
+
import type { RenderEntry } from "hamur/adapter"
|
|
6
|
+
import { isRedirect, type ClearErrorOptions, type ErrorPageProps } from "hamur/app"
|
|
7
|
+
import { isFailure, originalOf, reviveError } from "./boundary-error.ts"
|
|
8
|
+
import { useAppState } from "./context.ts"
|
|
9
|
+
import Rethrow from "./Rethrow.svelte"
|
|
10
|
+
import Scoped from "./Scoped.svelte"
|
|
11
|
+
|
|
12
|
+
let { entry, error, reset, thrown }: { entry: RenderEntry; error: unknown; reset: () => void; thrown: boolean } =
|
|
13
|
+
$props()
|
|
14
|
+
|
|
15
|
+
const read = useAppState("Boundary")
|
|
16
|
+
const first = untrack(() => entry)
|
|
17
|
+
const clearError = (options?: ClearErrorOptions) => read().app.error?.clear(options) ?? Promise.resolve()
|
|
18
|
+
const caught = untrack(() => {
|
|
19
|
+
if (thrown)
|
|
20
|
+
return { component: entry.component, error: entry.props?.error as ErrorPageProps, props: entry.props ?? {} }
|
|
21
|
+
const revived = reviveError(error)
|
|
22
|
+
if (isFailure(revived) || isRedirect(revived)) throw revived
|
|
23
|
+
const result = routeBoundary(read().context, entry, originalOf(error) ?? revived)
|
|
24
|
+
if (!result) throw revived
|
|
25
|
+
return { component: result.component, error: result.error, props: { error: result.error, clearError } }
|
|
26
|
+
})
|
|
27
|
+
const Boundary = caught.component as SvelteComponent<any>
|
|
28
|
+
|
|
29
|
+
$effect(() => {
|
|
30
|
+
if (entry !== first) untrack(reset)
|
|
31
|
+
})
|
|
32
|
+
</script>
|
|
33
|
+
|
|
34
|
+
<svelte:boundary>
|
|
35
|
+
<Scoped error={caught.error}>
|
|
36
|
+
<Boundary {...caught.props} />
|
|
37
|
+
</Scoped>
|
|
38
|
+
{#snippet failed(thrown)}
|
|
39
|
+
<Rethrow error={thrown} />
|
|
40
|
+
{/snippet}
|
|
41
|
+
</svelte:boundary>
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext, onMount } from "svelte"
|
|
3
|
+
import type { Component } from "svelte"
|
|
4
|
+
import { CLIENT_ONLY } from "./client-only.ts"
|
|
5
|
+
import type { ClientOnlyScope } from "./client-only.ts"
|
|
6
|
+
|
|
7
|
+
let {
|
|
8
|
+
server,
|
|
9
|
+
...rest
|
|
10
|
+
}: { server: { load?: () => Promise<{ default: Component<Record<string, unknown>> }> } } & Record<string, unknown> =
|
|
11
|
+
$props()
|
|
12
|
+
const scope = getContext<ClientOnlyScope | undefined>(CLIENT_ONLY)
|
|
13
|
+
let Loaded = $state.raw<Component<Record<string, unknown>>>()
|
|
14
|
+
let failure = $state.raw<{ error: unknown }>()
|
|
15
|
+
onMount(() => {
|
|
16
|
+
server.load?.().then(
|
|
17
|
+
(module) => {
|
|
18
|
+
Loaded = module.default
|
|
19
|
+
},
|
|
20
|
+
(error: unknown) => {
|
|
21
|
+
failure = { error }
|
|
22
|
+
},
|
|
23
|
+
)
|
|
24
|
+
})
|
|
25
|
+
// An effect's throw reaches the nearest boundary; a promise callback's does not.
|
|
26
|
+
$effect(() => {
|
|
27
|
+
if (failure) throw failure.error
|
|
28
|
+
})
|
|
29
|
+
</script>
|
|
30
|
+
|
|
31
|
+
{#if Loaded}
|
|
32
|
+
<Loaded {...rest} />
|
|
33
|
+
{:else if scope?.fallback}
|
|
34
|
+
{@render scope.fallback()}
|
|
35
|
+
{/if}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { untrack } from "svelte"
|
|
3
|
+
import type { Snippet } from "svelte"
|
|
4
|
+
import { clientFallback } from "hamur/adapter"
|
|
5
|
+
import { originalOf, reviveError } from "./boundary-error.ts"
|
|
6
|
+
import { useAppState } from "./context.ts"
|
|
7
|
+
import FallbackEffect from "./FallbackEffect.svelte"
|
|
8
|
+
|
|
9
|
+
let {
|
|
10
|
+
children,
|
|
11
|
+
fallback,
|
|
12
|
+
onSsrError,
|
|
13
|
+
}: { children?: Snippet; fallback?: Snippet; keepFallback?: boolean; onSsrError?: (error: unknown) => unknown } =
|
|
14
|
+
$props()
|
|
15
|
+
|
|
16
|
+
const id = $props.id()
|
|
17
|
+
const read = useAppState("ClientFallback")
|
|
18
|
+
const server = typeof window === "undefined"
|
|
19
|
+
const ids = server
|
|
20
|
+
? undefined
|
|
21
|
+
: untrack(() => (read().context.payload as { clientFallback?: unknown }).clientFallback)
|
|
22
|
+
let retry = Array.isArray(ids) && ids.includes(id)
|
|
23
|
+
|
|
24
|
+
const report = (error: unknown) => {
|
|
25
|
+
if (server)
|
|
26
|
+
clientFallback(
|
|
27
|
+
untrack(() => read().app),
|
|
28
|
+
id,
|
|
29
|
+
originalOf(error) ?? reviveError(error),
|
|
30
|
+
onSsrError,
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
const render = (reset: () => void) => () => {
|
|
34
|
+
if (!retry) return
|
|
35
|
+
retry = false
|
|
36
|
+
reset()
|
|
37
|
+
}
|
|
38
|
+
</script>
|
|
39
|
+
|
|
40
|
+
<svelte:boundary>
|
|
41
|
+
{@render children?.()}
|
|
42
|
+
{#snippet failed(error, reset)}
|
|
43
|
+
<FallbackEffect run={() => report(error)} after={render(reset)} />
|
|
44
|
+
{@render fallback?.()}
|
|
45
|
+
{/snippet}
|
|
46
|
+
</svelte:boundary>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { onMount, setContext } from "svelte"
|
|
3
|
+
import type { Snippet } from "svelte"
|
|
4
|
+
import { CLIENT_ONLY } from "./client-only.ts"
|
|
5
|
+
|
|
6
|
+
let { children, fallback }: { children?: Snippet; fallback?: Snippet } = $props()
|
|
7
|
+
let hydrated = $state(false)
|
|
8
|
+
setContext(CLIENT_ONLY, {
|
|
9
|
+
get fallback() {
|
|
10
|
+
return fallback
|
|
11
|
+
},
|
|
12
|
+
})
|
|
13
|
+
onMount(() => {
|
|
14
|
+
hydrated = true
|
|
15
|
+
})
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
{#if hydrated}
|
|
19
|
+
{@render children?.()}
|
|
20
|
+
{:else}
|
|
21
|
+
{@render fallback?.()}
|
|
22
|
+
{/if}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { ErrorPageProps } from "hamur/app"
|
|
3
|
+
import { diagnosticURL, type DiagnosticCode } from "hamur/adapter"
|
|
4
|
+
import Link from "./Link.svelte"
|
|
5
|
+
|
|
6
|
+
let { error }: { error: ErrorPageProps } = $props()
|
|
7
|
+
const isDev = (import.meta as { dev?: boolean }).dev === true
|
|
8
|
+
const codeURL = isDev ? (code: string) => diagnosticURL(code as DiagnosticCode) : () => ""
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
<main style="font-family:system-ui,sans-serif;text-align:center;padding:4rem 1rem"
|
|
12
|
+
><h1 style="font-size:4rem;margin:0">{error.status}</h1><p>{error.message}</p>{#if isDev && error.code}<p
|
|
13
|
+
><a href={codeURL(error.code)} target="_blank" rel="noopener">{error.code}</a></p
|
|
14
|
+
>{/if}{#if error.stack}<pre>{error.stack}</pre>{/if}<Link to="/">Back home</Link></main
|
|
15
|
+
>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from "svelte"
|
|
3
|
+
|
|
4
|
+
let { children, fallback }: { children?: Snippet; fallback?: Snippet } = $props()
|
|
5
|
+
const isDev = (import.meta as { dev?: boolean }).dev === true
|
|
6
|
+
</script>
|
|
7
|
+
|
|
8
|
+
{#if isDev}
|
|
9
|
+
{@render children?.()}
|
|
10
|
+
{:else}
|
|
11
|
+
{@render fallback?.()}
|
|
12
|
+
{/if}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from "svelte"
|
|
3
|
+
import type { ErrorBoundaryFallbackProps } from "hamur/adapter"
|
|
4
|
+
import { reviveError } from "./boundary-error.ts"
|
|
5
|
+
|
|
6
|
+
let { children, fallback }: { children?: Snippet; fallback?: Snippet<[ErrorBoundaryFallbackProps]> } = $props()
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
<svelte:boundary>
|
|
10
|
+
{@render children?.()}
|
|
11
|
+
{#snippet failed(error, reset)}
|
|
12
|
+
{@render fallback?.({ error: reviveError(error), reset })}
|
|
13
|
+
{/snippet}
|
|
14
|
+
</svelte:boundary>
|
package/src/Form.svelte
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext, onMount, setContext, type Snippet } from "svelte"
|
|
3
|
+
import type { HTMLFormAttributes } from "svelte/elements"
|
|
4
|
+
import { enhance, type FormSubmitArgs, type RouteLocation } from "hamur/app"
|
|
5
|
+
import { assertFormMethod, createFormStatus, functionFormAction, runFunctionForm } from "hamur/adapter"
|
|
6
|
+
import { APP_KEY, FORM_STATUS_KEY, detachedHref, type AppState } from "./context.ts"
|
|
7
|
+
import { track, type Source } from "./store.ts"
|
|
8
|
+
|
|
9
|
+
interface Props extends Omit<HTMLFormAttributes, "action" | "method" | "enctype" | "children"> {
|
|
10
|
+
action?: string | RouteLocation | (((...args: never[]) => unknown) & { readonly url: string })
|
|
11
|
+
method?: "get" | "post"
|
|
12
|
+
encType?: "application/x-www-form-urlencoded" | "multipart/form-data"
|
|
13
|
+
replace?: boolean
|
|
14
|
+
reset?: boolean
|
|
15
|
+
reloadDocument?: boolean
|
|
16
|
+
refresh?: boolean
|
|
17
|
+
instance?: string | number
|
|
18
|
+
restore?: boolean
|
|
19
|
+
onSubmit?: (args: FormSubmitArgs) => void
|
|
20
|
+
children?: Snippet
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let {
|
|
24
|
+
action,
|
|
25
|
+
method,
|
|
26
|
+
encType,
|
|
27
|
+
replace = false,
|
|
28
|
+
reset = false,
|
|
29
|
+
reloadDocument = false,
|
|
30
|
+
refresh,
|
|
31
|
+
instance,
|
|
32
|
+
restore = true,
|
|
33
|
+
onSubmit,
|
|
34
|
+
children,
|
|
35
|
+
...rest
|
|
36
|
+
}: Props = $props()
|
|
37
|
+
const source = getContext<Source<AppState> | undefined>(APP_KEY)
|
|
38
|
+
const read = source && track(source)
|
|
39
|
+
let element: HTMLFormElement | undefined = $state()
|
|
40
|
+
assertFormMethod(action, method)
|
|
41
|
+
const target = $derived(
|
|
42
|
+
typeof action === "function"
|
|
43
|
+
? functionFormAction(
|
|
44
|
+
action as never,
|
|
45
|
+
read ? read().href(read().route.fullPath.replace(/#.*$/, "")) : "/",
|
|
46
|
+
instance,
|
|
47
|
+
)
|
|
48
|
+
: action !== undefined
|
|
49
|
+
? read
|
|
50
|
+
? read().href(action)
|
|
51
|
+
: detachedHref(action)
|
|
52
|
+
: read
|
|
53
|
+
? read().href(read().route.fullPath.replace(/#.*$/, ""))
|
|
54
|
+
: undefined,
|
|
55
|
+
)
|
|
56
|
+
const status = createFormStatus()
|
|
57
|
+
setContext(FORM_STATUS_KEY, status)
|
|
58
|
+
onMount(() =>
|
|
59
|
+
enhance(element!, {
|
|
60
|
+
onSubmit: (args) => {
|
|
61
|
+
status.submitted(args.formData)
|
|
62
|
+
onSubmit?.(args)
|
|
63
|
+
},
|
|
64
|
+
...(typeof action === "function" && source
|
|
65
|
+
? {
|
|
66
|
+
call: (input: Record<string, unknown>, formData: FormData) =>
|
|
67
|
+
runFunctionForm(
|
|
68
|
+
source.get().app,
|
|
69
|
+
action as never,
|
|
70
|
+
input,
|
|
71
|
+
formData,
|
|
72
|
+
() => source.get().router.refresh(),
|
|
73
|
+
status,
|
|
74
|
+
{ refresh, instance },
|
|
75
|
+
),
|
|
76
|
+
}
|
|
77
|
+
: {}),
|
|
78
|
+
}),
|
|
79
|
+
)
|
|
80
|
+
</script>
|
|
81
|
+
|
|
82
|
+
<form
|
|
83
|
+
{...rest}
|
|
84
|
+
bind:this={element}
|
|
85
|
+
method={typeof action === "function" ? "post" : "get"}
|
|
86
|
+
action={target}
|
|
87
|
+
enctype={encType}
|
|
88
|
+
data-hamur-form=""
|
|
89
|
+
data-hamur-replace={replace ? "" : undefined}
|
|
90
|
+
data-hamur-reset={reset ? "" : undefined}
|
|
91
|
+
data-hamur-reload={reloadDocument ? "" : undefined}
|
|
92
|
+
data-hamur-refresh={refresh === false ? "false" : undefined}
|
|
93
|
+
data-hamur-restore={restore === false ? "false" : undefined}
|
|
94
|
+
>
|
|
95
|
+
{@render children?.()}
|
|
96
|
+
</form>
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext } from "svelte"
|
|
3
|
+
import type { Component, Snippet } from "svelte"
|
|
4
|
+
import { ISLAND_ELEMENT, renderIsland } from "hamur/adapter"
|
|
5
|
+
import type { IslandOn } from "hamur/adapter"
|
|
6
|
+
import { APP_KEY, PENDING_ISLANDS_KEY } from "./context.ts"
|
|
7
|
+
import type { AppState } from "./context.ts"
|
|
8
|
+
import type { Source } from "./store.ts"
|
|
9
|
+
import { keptServerHTML } from "./hydrating.ts"
|
|
10
|
+
import { settledIsland } from "./island-html.ts"
|
|
11
|
+
import IslandChildren from "./IslandChildren.svelte"
|
|
12
|
+
|
|
13
|
+
let {
|
|
14
|
+
component,
|
|
15
|
+
props = {},
|
|
16
|
+
on,
|
|
17
|
+
fallback,
|
|
18
|
+
children,
|
|
19
|
+
persist,
|
|
20
|
+
persistProps,
|
|
21
|
+
}: {
|
|
22
|
+
component: Component<any>
|
|
23
|
+
props?: Record<string, unknown>
|
|
24
|
+
on?: IslandOn
|
|
25
|
+
fallback?: Snippet
|
|
26
|
+
children?: Snippet
|
|
27
|
+
persist?: string
|
|
28
|
+
persistProps?: boolean
|
|
29
|
+
} = $props()
|
|
30
|
+
const id = $props.id()
|
|
31
|
+
const app = getContext<Source<AppState> | undefined>(APP_KEY)?.get().app
|
|
32
|
+
if (!app) throw new Error("Island must be rendered inside a Hamur app")
|
|
33
|
+
// In the browser an Island only runs inside an island that hydrates and keeps the inner element's server nodes; Svelte reads an `on` attribute there as an event handler, so the spread is empty.
|
|
34
|
+
const island =
|
|
35
|
+
typeof document === "undefined"
|
|
36
|
+
? renderIsland(
|
|
37
|
+
app,
|
|
38
|
+
id,
|
|
39
|
+
component,
|
|
40
|
+
props,
|
|
41
|
+
on,
|
|
42
|
+
children ? { component: IslandChildren, props: { slot: children } } : undefined,
|
|
43
|
+
{ persist, persistProps },
|
|
44
|
+
)
|
|
45
|
+
: undefined
|
|
46
|
+
const attributes = island?.attributes ?? {}
|
|
47
|
+
const html = island?.html
|
|
48
|
+
? (settledIsland(island.html, getContext<Promise<void>[] | undefined>(PENDING_ISLANDS_KEY)) ?? "")
|
|
49
|
+
: keptServerHTML()
|
|
50
|
+
</script>
|
|
51
|
+
|
|
52
|
+
{#if on === "only"}
|
|
53
|
+
<svelte:element this={ISLAND_ELEMENT} {...attributes}>{@render fallback?.()}</svelte:element>
|
|
54
|
+
{:else}
|
|
55
|
+
<svelte:element this={ISLAND_ELEMENT} {...attributes}>
|
|
56
|
+
<!-- svelte-ignore hydration_html_changed -->
|
|
57
|
+
{@html html}
|
|
58
|
+
</svelte:element>
|
|
59
|
+
{/if}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Component } from "svelte"
|
|
3
|
+
import type { KeptUnit, RenderEntry, RenderTree, RouteValue } from "hamur/adapter"
|
|
4
|
+
import type { KeepAliveBase } from "./keepalive.ts"
|
|
5
|
+
import KeptPage from "./KeptPage.svelte"
|
|
6
|
+
|
|
7
|
+
const SHOWN = "\0shown"
|
|
8
|
+
|
|
9
|
+
let {
|
|
10
|
+
kept,
|
|
11
|
+
entry,
|
|
12
|
+
components,
|
|
13
|
+
index,
|
|
14
|
+
route,
|
|
15
|
+
Nest,
|
|
16
|
+
base,
|
|
17
|
+
}: {
|
|
18
|
+
kept: KeptUnit[]
|
|
19
|
+
entry?: RenderEntry
|
|
20
|
+
components: RenderTree["components"]
|
|
21
|
+
index: number
|
|
22
|
+
route?: RouteValue
|
|
23
|
+
Nest: Component<any>
|
|
24
|
+
base: KeepAliveBase
|
|
25
|
+
} = $props()
|
|
26
|
+
const places = $derived.by(() => {
|
|
27
|
+
const list: Array<{ key: string; unit?: KeptUnit }> = kept.map((unit) => ({ key: unit.key, unit }))
|
|
28
|
+
if (entry) list.push({ key: entry.keepalive ?? SHOWN })
|
|
29
|
+
return list
|
|
30
|
+
})
|
|
31
|
+
</script>
|
|
32
|
+
|
|
33
|
+
{#each places as place (place.key)}
|
|
34
|
+
{#if place.key === SHOWN}
|
|
35
|
+
<Nest {components} {index} {route} bare />
|
|
36
|
+
{:else}
|
|
37
|
+
<KeptPage name={place.key} unit={place.unit} {base}>
|
|
38
|
+
<Nest
|
|
39
|
+
components={place.unit ? place.unit.tree.components : components}
|
|
40
|
+
index={place.unit ? 0 : index}
|
|
41
|
+
route={place.unit ? undefined : route}
|
|
42
|
+
bare
|
|
43
|
+
/>
|
|
44
|
+
</KeptPage>
|
|
45
|
+
{/if}
|
|
46
|
+
{/each}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import type { KeptUnit } from "hamur/adapter"
|
|
3
|
+
import type { AppState, routeValue } from "./context.ts"
|
|
4
|
+
|
|
5
|
+
const keptStates = new WeakMap<KeptUnit, AppState>()
|
|
6
|
+
|
|
7
|
+
/** The app state a kept unit renders with while hidden: the route, payload and tree it was last shown with. */
|
|
8
|
+
function keptState(route: typeof routeValue, state: AppState, unit: KeptUnit): AppState {
|
|
9
|
+
let own = keptStates.get(unit)
|
|
10
|
+
if (!own) {
|
|
11
|
+
const { data = {}, handlerState } = unit.payload as { data?: Record<string, unknown>; handlerState?: unknown }
|
|
12
|
+
keptStates.set(
|
|
13
|
+
unit,
|
|
14
|
+
(own = {
|
|
15
|
+
...state,
|
|
16
|
+
route: route({ route: unit.route, payload: unit.payload, tree: unit.tree, app: state.app }),
|
|
17
|
+
components: unit.tree.components,
|
|
18
|
+
data,
|
|
19
|
+
handlerState,
|
|
20
|
+
tree: unit.tree,
|
|
21
|
+
}),
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
return own
|
|
25
|
+
}
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<script lang="ts">
|
|
29
|
+
import { getContext, onDestroy, setContext, untrack } from "svelte"
|
|
30
|
+
import type { Snippet } from "svelte"
|
|
31
|
+
import type { Source } from "./store.ts"
|
|
32
|
+
import type { Activation } from "./activation.ts"
|
|
33
|
+
import type { KeepAliveBase } from "./keepalive.ts"
|
|
34
|
+
|
|
35
|
+
let { name, unit, base, children }: { name: string; unit?: KeptUnit; base: KeepAliveBase; children: Snippet } =
|
|
36
|
+
$props()
|
|
37
|
+
const { APP_KEY, ACTIVATION_KEY, routeValue, activation, detach, setHidden } = base
|
|
38
|
+
const parent = getContext<Source<AppState>>(APP_KEY)
|
|
39
|
+
setContext<Source<AppState>>(APP_KEY, {
|
|
40
|
+
get: () => (unit ? keptState(routeValue, parent.get(), unit) : parent.get()),
|
|
41
|
+
set: parent.set,
|
|
42
|
+
subscribe: parent.subscribe,
|
|
43
|
+
})
|
|
44
|
+
const own = activation(
|
|
45
|
+
getContext<Activation | undefined>(ACTIVATION_KEY),
|
|
46
|
+
untrack(() => unit !== undefined),
|
|
47
|
+
)
|
|
48
|
+
setContext(ACTIVATION_KEY, own)
|
|
49
|
+
$effect(() => setHidden(own, unit !== undefined))
|
|
50
|
+
onDestroy(() => detach(own))
|
|
51
|
+
</script>
|
|
52
|
+
|
|
53
|
+
<div data-hamur-keepalive={name} hidden={unit !== undefined} style={unit ? undefined : "display:contents"}
|
|
54
|
+
>{@render children()}</div
|
|
55
|
+
>
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import {
|
|
3
|
+
flushSync,
|
|
4
|
+
getAllContexts,
|
|
5
|
+
getContext,
|
|
6
|
+
hydrate,
|
|
7
|
+
mount,
|
|
8
|
+
onDestroy,
|
|
9
|
+
onMount,
|
|
10
|
+
setContext,
|
|
11
|
+
unmount,
|
|
12
|
+
} from "svelte"
|
|
13
|
+
import { replayInteraction, whenTriggered } from "hamur/adapter"
|
|
14
|
+
import { APP_KEY, IDS_KEY, nextId } from "./context.ts"
|
|
15
|
+
import type { AppState, Ids } from "./context.ts"
|
|
16
|
+
import type { Source } from "./store.ts"
|
|
17
|
+
import type { Lazy } from "./lazy-hydration.ts"
|
|
18
|
+
|
|
19
|
+
let { lazy, onHydrated, ...rest }: { lazy: Lazy; onHydrated?: () => void } & Record<string, unknown> = $props()
|
|
20
|
+
|
|
21
|
+
const parentIds = getContext<Ids | undefined>(IDS_KEY)
|
|
22
|
+
if (parentIds) setContext(IDS_KEY, { base: `${nextId(parentIds)}-`, next: 0 })
|
|
23
|
+
const contexts = getAllContexts()
|
|
24
|
+
const app = getContext<Source<AppState> | undefined>(APP_KEY)?.get().app
|
|
25
|
+
const html = lazy.html(rest, contexts, app)
|
|
26
|
+
const controller = new AbortController()
|
|
27
|
+
let wrapper: HTMLElement
|
|
28
|
+
let instance: Record<string, unknown> | undefined
|
|
29
|
+
|
|
30
|
+
onMount(() => {
|
|
31
|
+
const hydrating = wrapper.firstElementChild !== null || (wrapper.textContent ?? "") !== ""
|
|
32
|
+
const ready = hydrating
|
|
33
|
+
? whenTriggered(wrapper, lazy.on, { ...lazy.options, signal: controller.signal }).then(lazy.load)
|
|
34
|
+
: lazy.load()
|
|
35
|
+
void ready.then((component) => {
|
|
36
|
+
if (controller.signal.aborted) return
|
|
37
|
+
const mounting = { target: wrapper, props: rest, context: contexts }
|
|
38
|
+
instance = hydrating ? hydrate(component, mounting) : mount(component, mounting)
|
|
39
|
+
flushSync()
|
|
40
|
+
onHydrated?.()
|
|
41
|
+
replayInteraction(wrapper)
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
onDestroy(() => {
|
|
46
|
+
controller.abort()
|
|
47
|
+
if (instance) void unmount(instance)
|
|
48
|
+
})
|
|
49
|
+
</script>
|
|
50
|
+
|
|
51
|
+
<hamur-hydrate {...lazy.attributes} bind:this={wrapper}>
|
|
52
|
+
<!-- svelte-ignore hydration_html_changed -->
|
|
53
|
+
{@html html}
|
|
54
|
+
</hamur-hydrate>
|