@hamur/adapter-angular 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 +42 -2
- package/dist/chunks/browser-index-7h7x2xz2.js +131 -0
- package/dist/chunks/browser-index-nann848q.js +205 -0
- package/dist/chunks/browser-index-qn8q5fa2.js +580 -0
- package/dist/src/adapter.js +1075 -0
- package/dist/src/error-page.js +34 -0
- package/dist/src/index.js +1729 -0
- package/dist/src/keepalive.js +0 -0
- package/dist/src/runtime/client.js +21 -0
- package/dist/src/runtime/island.js +92 -0
- package/dist/src/runtime/server.js +175 -0
- package/dist/types/src/activation.d.ts +20 -0
- package/dist/types/src/adapter.d.ts +7 -0
- package/dist/types/src/app.d.ts +6 -0
- package/dist/types/src/async-data.d.ts +21 -0
- package/dist/types/src/client-parts.d.ts +35 -0
- package/dist/types/src/compile.d.ts +99 -0
- package/dist/types/src/context.d.ts +74 -0
- package/dist/types/src/data.d.ts +65 -0
- package/dist/types/src/definition.d.ts +13 -0
- package/dist/types/src/error-page.d.ts +8 -0
- package/dist/types/src/error.d.ts +4 -0
- package/dist/types/src/form.d.ts +68 -0
- package/dist/types/src/head.d.ts +12 -0
- package/dist/types/src/hmr.d.ts +14 -0
- package/dist/types/src/hydration.d.ts +16 -0
- package/dist/types/src/index.d.ts +24 -0
- package/dist/types/src/indicators.d.ts +76 -0
- package/dist/types/src/island.d.ts +42 -0
- package/dist/types/src/keepalive.d.ts +1 -0
- package/dist/types/src/link.d.ts +68 -0
- package/dist/types/src/markdown-page.d.ts +10 -0
- package/dist/types/src/markdown.d.ts +40 -0
- package/dist/types/src/mask.d.ts +12 -0
- package/dist/types/src/navigate.d.ts +17 -0
- package/dist/types/src/outlet.d.ts +76 -0
- package/dist/types/src/request.d.ts +31 -0
- package/dist/types/src/routing.d.ts +47 -0
- package/dist/types/src/runtime/client.d.ts +29 -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 +15 -0
- package/dist/types/src/server-island.d.ts +24 -0
- package/dist/types/src/state.d.ts +14 -0
- package/dist/types/src/svg.d.ts +15 -0
- package/dist/types/src/time.d.ts +40 -0
- package/dist/types/src/wrapper.d.ts +21 -0
- package/package.json +77 -4
- package/src/activation.ts +83 -0
- package/src/adapter.ts +107 -0
- package/src/app.ts +12 -0
- package/src/async-data.ts +105 -0
- package/src/client-parts.ts +107 -0
- package/src/compile.ts +963 -0
- package/src/context.ts +189 -0
- package/src/data.ts +389 -0
- package/src/definition.ts +144 -0
- package/src/error-page.ts +30 -0
- package/src/error.ts +15 -0
- package/src/form.ts +182 -0
- package/src/head.ts +63 -0
- package/src/hmr.ts +61 -0
- package/src/hydration.ts +120 -0
- package/src/index.ts +98 -0
- package/src/indicators.ts +220 -0
- package/src/island.ts +89 -0
- package/src/keepalive.ts +1 -0
- package/src/link.ts +214 -0
- package/src/markdown-page.ts +31 -0
- package/src/markdown.ts +140 -0
- package/src/mask.ts +36 -0
- package/src/navigate.ts +22 -0
- package/src/outlet.ts +316 -0
- package/src/request.ts +170 -0
- package/src/routing.ts +218 -0
- package/src/runtime/client.ts +140 -0
- package/src/runtime/contract.d.ts +4 -0
- package/src/runtime/hydration-dev.ts +18 -0
- package/src/runtime/hydration.ts +7 -0
- package/src/runtime/island.ts +109 -0
- package/src/runtime/server.ts +228 -0
- package/src/server-island.ts +67 -0
- package/src/state.ts +70 -0
- package/src/svg.ts +65 -0
- package/src/time.ts +115 -0
- package/src/wrapper.ts +187 -0
package/src/context.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { InjectionToken, PLATFORM_ID, inject, signal } from "@angular/core"
|
|
2
|
+
import type { EnvironmentInjector, EnvironmentProviders, Provider, Signal, WritableSignal } from "@angular/core"
|
|
3
|
+
import { createRouterApi, routeStateOf } from "hamur/adapter"
|
|
4
|
+
import type { ClientContext, NavigationState, RenderContext, RenderTree, RouteValue, RouterApi } from "hamur/adapter"
|
|
5
|
+
import { withBase } from "hamur/app"
|
|
6
|
+
import type { AppContext, ErrorPageProps, RouteLocation } from "hamur/app"
|
|
7
|
+
|
|
8
|
+
/** `app.adapter`, handed to `app:adapter` before the application is created; a plugin pushes Angular providers. */
|
|
9
|
+
export interface AngularSetup {
|
|
10
|
+
providers: Array<Provider | EnvironmentProviders>
|
|
11
|
+
/** The application's injector, set once it is bootstrapped. */
|
|
12
|
+
readonly injector: EnvironmentInjector | undefined
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type Source = Pick<RenderContext | ClientContext, "route" | "tree" | "payload" | "app"> &
|
|
16
|
+
Partial<Pick<RenderContext, "baseURL" | "href" | "url">> & { navigator?: unknown }
|
|
17
|
+
type Payload = RenderContext["payload"]
|
|
18
|
+
|
|
19
|
+
/** Dev-only diagnostics read it; a test sets it, as the build defines `import.meta.dev`. */
|
|
20
|
+
export const mode = { dev: (import.meta as { dev?: boolean }).dev === true }
|
|
21
|
+
|
|
22
|
+
export const HAMUR_APP = new InjectionToken<AppContext>("hamur app")
|
|
23
|
+
export const HAMUR_TREE = new InjectionToken<Signal<RenderTree>>("hamur tree")
|
|
24
|
+
/** The tree of the page; `HAMUR_TREE` is the chain of a slot below an `Outlet name`. */
|
|
25
|
+
export const HAMUR_ROOT_TREE = new InjectionToken<Signal<RenderTree>>("hamur root tree")
|
|
26
|
+
export const HAMUR_ROUTER = new InjectionToken<RouterApi>("hamur router")
|
|
27
|
+
export const HAMUR_NAVIGATION = new InjectionToken<Signal<NavigationState>>("hamur navigation")
|
|
28
|
+
export const HAMUR_BASE_URL = new InjectionToken<string>("hamur base url")
|
|
29
|
+
export const HAMUR_HREF = new InjectionToken<RenderContext["href"]>("hamur href")
|
|
30
|
+
/** The value an `Outlet` above hands to the entries it renders. */
|
|
31
|
+
export const HAMUR_OUTLET_CONTEXT = new InjectionToken<Signal<unknown>>("hamur outlet context")
|
|
32
|
+
export const HAMUR_ROUTE: InjectionToken<Signal<RouteValue>> = new InjectionToken("hamur route")
|
|
33
|
+
export const HAMUR_PAYLOAD: InjectionToken<Signal<Payload>> = new InjectionToken("hamur payload")
|
|
34
|
+
/** What a level does with a throw of the entry it creates: recorded for `render` on the server, the error page in the browser. */
|
|
35
|
+
export const HAMUR_FAIL = new InjectionToken<(error: unknown) => void>("hamur fail")
|
|
36
|
+
/** The page's render or client context as `routeBoundary` reads it, built from the latest tree the state was given. */
|
|
37
|
+
export const HAMUR_CONTEXT = new InjectionToken<() => RenderContext | ClientContext>("hamur context")
|
|
38
|
+
/** The error a route boundary or not-found component shows: set on the injector of an entry the core marks `caught`. */
|
|
39
|
+
export const HAMUR_CAUGHT = new InjectionToken<ErrorPageProps>("hamur caught error")
|
|
40
|
+
/** The index in `tree.components` of the entry whose injector this is; the `Outlet` below it renders the next one. */
|
|
41
|
+
export const HAMUR_LEVEL = new InjectionToken<number>("hamur level")
|
|
42
|
+
/**
|
|
43
|
+
* Registers a cleanup run when the level's entry is dropped. A view that throws while `@boundary` creates it is never destroyed, so its
|
|
44
|
+
* `DestroyRef` never fires; what it registered goes with the level (K32).
|
|
45
|
+
*/
|
|
46
|
+
export const HAMUR_LEVEL_DISPOSE = new InjectionToken<(dispose: () => void) => void>("hamur level dispose")
|
|
47
|
+
|
|
48
|
+
/** Whether the component renders on the server; a bare injector with no platform has no `document` there, as on the server. */
|
|
49
|
+
export function onServer(): boolean {
|
|
50
|
+
const platform = inject(PLATFORM_ID, { optional: true })
|
|
51
|
+
return platform === null ? typeof document === "undefined" : platform === "server"
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function required<T>(token: InjectionToken<T>, name: string): T {
|
|
55
|
+
const value = inject(token, { optional: true })
|
|
56
|
+
if (value === null) throw new Error(`${name}() must be called inside a Hamur app`)
|
|
57
|
+
return value
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The id of the tree entry whose component calls the inject function, read when asked: the entry's data and its guards are its own. */
|
|
61
|
+
export function entryOf(name: string): () => string | undefined {
|
|
62
|
+
const tree = required(HAMUR_TREE, name)
|
|
63
|
+
const level = inject(HAMUR_LEVEL, { optional: true })
|
|
64
|
+
if (level === null) throw new Error(`${name}() must be called inside a Hamur app`)
|
|
65
|
+
return () => tree().components[level]?.id
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const dataOf = (payload: Signal<{ data?: unknown }>) =>
|
|
69
|
+
(payload().data as Record<string, unknown> | undefined) ?? {}
|
|
70
|
+
|
|
71
|
+
export function routeValue({ route, app, payload, tree }: Source): RouteValue {
|
|
72
|
+
return {
|
|
73
|
+
path: route.path,
|
|
74
|
+
fullPath: route.fullPath,
|
|
75
|
+
params: route.params,
|
|
76
|
+
query: route.query,
|
|
77
|
+
search: route.search,
|
|
78
|
+
hash: route.hash,
|
|
79
|
+
name: route.name,
|
|
80
|
+
meta: route.meta,
|
|
81
|
+
matched: route.matched,
|
|
82
|
+
groups: route.groups ?? [],
|
|
83
|
+
...routeStateOf({ route, app, payload, tree }),
|
|
84
|
+
} as RouteValue
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
interface NavigationSource {
|
|
88
|
+
get(): NavigationState
|
|
89
|
+
subscribe(listener: () => void): () => void
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function navigationOf(navigator: unknown): WritableSignal<NavigationState> {
|
|
93
|
+
const source = navigator as Partial<NavigationSource> | undefined
|
|
94
|
+
if (typeof source?.get !== "function" || typeof source.subscribe !== "function")
|
|
95
|
+
return signal<NavigationState>({ state: "idle", pending: false })
|
|
96
|
+
const navigation = signal(source.get())
|
|
97
|
+
source.subscribe(() => navigation.set((source as NavigationSource).get()))
|
|
98
|
+
return navigation
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A `Link` in an island takes a path, as no route table ships there. */
|
|
102
|
+
export function islandHref(baseURL: string): RenderContext["href"] {
|
|
103
|
+
return (to: string | RouteLocation) => {
|
|
104
|
+
if (typeof to !== "string")
|
|
105
|
+
throw new Error(`Link to the route "${to.name}" cannot render in an island; give it a path`)
|
|
106
|
+
return withBase(to, baseURL)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function detachedHref(to: string | RouteLocation): string {
|
|
111
|
+
if (typeof to !== "string") throw new Error(`Link to the route "${to.name}" must be rendered inside a Hamur app`)
|
|
112
|
+
return withBase(to, "/")
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface HamurState {
|
|
116
|
+
tree: WritableSignal<RenderTree>
|
|
117
|
+
route: WritableSignal<RouteValue>
|
|
118
|
+
payload: WritableSignal<Payload>
|
|
119
|
+
navigation: Signal<NavigationState>
|
|
120
|
+
router: RouterApi
|
|
121
|
+
baseURL: string
|
|
122
|
+
href: RenderContext["href"]
|
|
123
|
+
context(): RenderContext | ClientContext
|
|
124
|
+
write(source: Source): void
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function createState(source: Source): HamurState {
|
|
128
|
+
const baseURL = source.baseURL ?? "/"
|
|
129
|
+
const href = source.href ?? detachedHref
|
|
130
|
+
let current = source
|
|
131
|
+
const state: HamurState = {
|
|
132
|
+
tree: signal(source.tree),
|
|
133
|
+
route: signal(routeValue(source)),
|
|
134
|
+
payload: signal(source.payload),
|
|
135
|
+
navigation: navigationOf(source.navigator).asReadonly(),
|
|
136
|
+
router: createRouterApi({ navigator: source.navigator, baseURL, href, route: source.route, url: source.url }),
|
|
137
|
+
baseURL,
|
|
138
|
+
href,
|
|
139
|
+
context: () =>
|
|
140
|
+
({
|
|
141
|
+
...current,
|
|
142
|
+
url: current.url ?? new URL(current.route.fullPath, "http://localhost"),
|
|
143
|
+
baseURL,
|
|
144
|
+
}) as RenderContext | ClientContext,
|
|
145
|
+
write(next) {
|
|
146
|
+
current = next
|
|
147
|
+
state.route.set(routeValue(next))
|
|
148
|
+
state.payload.set(next.payload)
|
|
149
|
+
state.tree.set(next.tree)
|
|
150
|
+
},
|
|
151
|
+
}
|
|
152
|
+
return state
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function hamurProviders(app: AppContext, state: HamurState, fail: (error: unknown) => void): Provider[] {
|
|
156
|
+
return [
|
|
157
|
+
{ provide: HAMUR_APP, useValue: app },
|
|
158
|
+
{ provide: HAMUR_TREE, useValue: state.tree.asReadonly() },
|
|
159
|
+
{ provide: HAMUR_ROOT_TREE, useValue: state.tree.asReadonly() },
|
|
160
|
+
{ provide: HAMUR_ROUTE, useValue: state.route.asReadonly() },
|
|
161
|
+
{ provide: HAMUR_PAYLOAD, useValue: state.payload.asReadonly() },
|
|
162
|
+
{ provide: HAMUR_ROUTER, useValue: state.router },
|
|
163
|
+
{ provide: HAMUR_NAVIGATION, useValue: state.navigation },
|
|
164
|
+
{ provide: HAMUR_BASE_URL, useValue: state.baseURL },
|
|
165
|
+
{ provide: HAMUR_HREF, useValue: state.href },
|
|
166
|
+
{ provide: HAMUR_FAIL, useValue: fail },
|
|
167
|
+
{ provide: HAMUR_CONTEXT, useValue: state.context },
|
|
168
|
+
]
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const setups = new WeakMap<object, { setup: AngularSetup; injector?: EnvironmentInjector }>()
|
|
172
|
+
|
|
173
|
+
/** One `AngularSetup` per app context: a request on the server, a page load in the browser. */
|
|
174
|
+
export function setupOf(app: AppContext): { setup: AngularSetup; bootstrapped(injector: EnvironmentInjector): void } {
|
|
175
|
+
let held = setups.get(app)
|
|
176
|
+
if (!held) {
|
|
177
|
+
const own: { setup: AngularSetup; injector?: EnvironmentInjector } = {
|
|
178
|
+
setup: {
|
|
179
|
+
providers: [],
|
|
180
|
+
get injector() {
|
|
181
|
+
return own.injector
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
}
|
|
185
|
+
setups.set(app, (held = own))
|
|
186
|
+
}
|
|
187
|
+
const current = held
|
|
188
|
+
return { setup: current.setup, bootstrapped: (injector) => void (current.injector = injector) }
|
|
189
|
+
}
|
package/src/data.ts
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DestroyRef,
|
|
3
|
+
PendingTasks,
|
|
4
|
+
assertInInjectionContext,
|
|
5
|
+
computed,
|
|
6
|
+
effect,
|
|
7
|
+
inject,
|
|
8
|
+
signal,
|
|
9
|
+
untracked,
|
|
10
|
+
type Resource,
|
|
11
|
+
type ResourceSnapshot,
|
|
12
|
+
type Signal,
|
|
13
|
+
} from "@angular/core"
|
|
14
|
+
import { asyncDataArguments, asyncDataKey, isHandlerReference, isPending } from "hamur/adapter"
|
|
15
|
+
import type {
|
|
16
|
+
AsyncDataHandler,
|
|
17
|
+
FetchRequest,
|
|
18
|
+
GetCachedData,
|
|
19
|
+
GetCachedDataCause,
|
|
20
|
+
Handler,
|
|
21
|
+
HandlerReference,
|
|
22
|
+
RouteLoaderData,
|
|
23
|
+
UseDataArgs,
|
|
24
|
+
} from "hamur/adapter"
|
|
25
|
+
import { callOnce as callOnceOf, clearAsyncData, clearState as clearStateOf } from "hamur/app"
|
|
26
|
+
import type {
|
|
27
|
+
$Fetch,
|
|
28
|
+
AppContext,
|
|
29
|
+
AsyncDataEntry,
|
|
30
|
+
AsyncDataOptions,
|
|
31
|
+
FetchCheckedInput,
|
|
32
|
+
FetchData,
|
|
33
|
+
FetchInputField,
|
|
34
|
+
PickFrom,
|
|
35
|
+
} from "hamur/app"
|
|
36
|
+
import { HAMUR_APP, HAMUR_PAYLOAD, dataOf, entryOf, onServer, required } from "./context.ts"
|
|
37
|
+
import { defaultGetCachedData, fetchSlots, handlerSlots, keepCached, refreshCached, valueOf } from "./async-data.ts"
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Angular's `Resource<T>` over an entry of the core's store, with `refresh()` and `clear()`. The core's `idle`,
|
|
41
|
+
* `pending`, `success` and `error` are `idle`, `loading` (`reloading` while there is a value), `resolved` and `error`; `value`
|
|
42
|
+
* throws the error of a failed entry, as Angular's own resources do.
|
|
43
|
+
*/
|
|
44
|
+
export interface AsyncDataResource<T, D = undefined> extends Resource<T | D> {
|
|
45
|
+
/** Runs the handler again (through `getCachedData`) and resolves when the run ends. */
|
|
46
|
+
refresh(): Promise<void>
|
|
47
|
+
/** Resets the entry of the current key to `idle` (`clearAsyncData`). */
|
|
48
|
+
clear(): void
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface InjectAsyncDataOptions<T = unknown, R = T, K extends keyof T & string = never, D = T> extends Omit<
|
|
52
|
+
AsyncDataOptions<T, R, D>,
|
|
53
|
+
"pick"
|
|
54
|
+
> {
|
|
55
|
+
/** Signals (or any getters) whose change runs the handler again. `false` is accepted and does nothing: nothing watches implicitly. */
|
|
56
|
+
watch?: ReadonlyArray<() => unknown> | false
|
|
57
|
+
getCachedData?: GetCachedData<T>
|
|
58
|
+
/** Top-level keys of an object result to keep. */
|
|
59
|
+
pick?: readonly K[]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface InjectFetchOptions<T = unknown, R = T, K extends keyof T & string = never, D = T>
|
|
63
|
+
extends FetchRequest, InjectAsyncDataOptions<T, R, K, D> {
|
|
64
|
+
/** The store key, instead of the one derived from the url and the request. */
|
|
65
|
+
key?: string
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type Status = AsyncDataEntry["status"]
|
|
69
|
+
|
|
70
|
+
function resourceOf<V>(
|
|
71
|
+
app: AppContext,
|
|
72
|
+
current: () => Pick<AsyncDataEntry, "status" | "data" | "error"> | undefined,
|
|
73
|
+
refresh: () => Promise<void>,
|
|
74
|
+
clear: () => void,
|
|
75
|
+
): AsyncDataResource<V, never> {
|
|
76
|
+
const snapshot = computed((): ResourceSnapshot<V> => {
|
|
77
|
+
const entry = current()
|
|
78
|
+
if (entry?.status === "error") return { status: "error", error: entry.error as Error }
|
|
79
|
+
const value = entry?.data as V
|
|
80
|
+
const status = (entry?.status ?? "idle") as Status
|
|
81
|
+
if (status === "pending") return { status: value === undefined ? "loading" : "reloading", value }
|
|
82
|
+
return { status: status === "success" ? "resolved" : "idle", value }
|
|
83
|
+
})
|
|
84
|
+
const value = computed(() => {
|
|
85
|
+
const read = snapshot()
|
|
86
|
+
if (read.status === "error") throw read.error
|
|
87
|
+
return read.value
|
|
88
|
+
})
|
|
89
|
+
return {
|
|
90
|
+
value,
|
|
91
|
+
status: computed(() => snapshot().status),
|
|
92
|
+
error: computed(() => {
|
|
93
|
+
const read = snapshot()
|
|
94
|
+
return read.status === "error" ? read.error : undefined
|
|
95
|
+
}),
|
|
96
|
+
isLoading: computed(() => {
|
|
97
|
+
const entry = current()
|
|
98
|
+
return entry !== undefined && isPending(app, entry)
|
|
99
|
+
}),
|
|
100
|
+
snapshot,
|
|
101
|
+
hasValue: () => {
|
|
102
|
+
const read = snapshot()
|
|
103
|
+
return read.status !== "error" && read.value !== undefined
|
|
104
|
+
},
|
|
105
|
+
refresh,
|
|
106
|
+
clear,
|
|
107
|
+
} as unknown as AsyncDataResource<V, never>
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function keyedAsyncData<T, R, K extends keyof T & string = never, D = T>(
|
|
111
|
+
key: string | (() => string),
|
|
112
|
+
handlerFor: (key: string) => AsyncDataHandler<R>,
|
|
113
|
+
options: InjectAsyncDataOptions<T, R, K, D>,
|
|
114
|
+
name: string,
|
|
115
|
+
): AsyncDataResource<PickFrom<T, K>, D> {
|
|
116
|
+
const app = required(HAMUR_APP, name)
|
|
117
|
+
const store = app.asyncData
|
|
118
|
+
const tasks = inject(PendingTasks)
|
|
119
|
+
const destroy = inject(DestroyRef)
|
|
120
|
+
const server = onServer()
|
|
121
|
+
const {
|
|
122
|
+
server: runsOnServer,
|
|
123
|
+
lazy,
|
|
124
|
+
default: init,
|
|
125
|
+
transform,
|
|
126
|
+
immediate,
|
|
127
|
+
dedupe,
|
|
128
|
+
pick,
|
|
129
|
+
watch,
|
|
130
|
+
getCachedData = defaultGetCachedData,
|
|
131
|
+
} = options
|
|
132
|
+
const settings = {
|
|
133
|
+
server: runsOnServer,
|
|
134
|
+
lazy,
|
|
135
|
+
default: init as AsyncDataOptions<T, R>["default"],
|
|
136
|
+
transform,
|
|
137
|
+
immediate,
|
|
138
|
+
dedupe,
|
|
139
|
+
pick,
|
|
140
|
+
}
|
|
141
|
+
// A page's non-lazy data holds the render and the navigation; a lazy entry is waited for on the server only (K38).
|
|
142
|
+
const hold = (entry: AsyncDataEntry<T>) => {
|
|
143
|
+
if (entry.status !== "pending" || !entry.promise || (lazy === true && !server)) return
|
|
144
|
+
const release = tasks.add()
|
|
145
|
+
// The store tells its subscribers in a microtask after the run ends; the task ends after that, so the signals have changed by the time the app is stable.
|
|
146
|
+
void entry.promise.then(() => void setTimeout(release))
|
|
147
|
+
}
|
|
148
|
+
const first = (active: string) => {
|
|
149
|
+
keepCached(app, active, getCachedData)
|
|
150
|
+
const entry = store.fetch<T, R>(active, handlerFor(active), {
|
|
151
|
+
...settings,
|
|
152
|
+
cached: getCachedData(active, app, { cause: "initial" }),
|
|
153
|
+
})
|
|
154
|
+
hold(entry)
|
|
155
|
+
return entry
|
|
156
|
+
}
|
|
157
|
+
const keyNow = computed(() => valueOf(key))
|
|
158
|
+
const initial = untracked(keyNow)
|
|
159
|
+
let entry = first(initial)
|
|
160
|
+
const active = signal(initial)
|
|
161
|
+
const version = signal(0)
|
|
162
|
+
const bump = () => version.update((count) => count + 1)
|
|
163
|
+
let unsubscribe = store.subscribe(initial, bump)
|
|
164
|
+
let unhook = store.onRefresh(initial, () => again("refresh:hook"))
|
|
165
|
+
destroy.onDestroy(() => (unsubscribe(), unhook()))
|
|
166
|
+
|
|
167
|
+
const again = (cause: GetCachedDataCause) => {
|
|
168
|
+
const current = untracked(active)
|
|
169
|
+
return refreshCached(app, current, handlerFor(current), settings, getCachedData, cause)
|
|
170
|
+
}
|
|
171
|
+
if (typeof key === "function")
|
|
172
|
+
effect(() => {
|
|
173
|
+
const next = keyNow()
|
|
174
|
+
untracked(() => {
|
|
175
|
+
if (next === active()) return
|
|
176
|
+
unsubscribe()
|
|
177
|
+
unhook()
|
|
178
|
+
entry = first(next)
|
|
179
|
+
unsubscribe = store.subscribe(next, bump)
|
|
180
|
+
unhook = store.onRefresh(next, () => again("refresh:hook"))
|
|
181
|
+
active.set(next)
|
|
182
|
+
bump()
|
|
183
|
+
})
|
|
184
|
+
})
|
|
185
|
+
if (Array.isArray(watch) && watch.length) {
|
|
186
|
+
let started = false
|
|
187
|
+
effect(() => {
|
|
188
|
+
for (const source of watch) source()
|
|
189
|
+
if (!started) {
|
|
190
|
+
started = true
|
|
191
|
+
return
|
|
192
|
+
}
|
|
193
|
+
untracked(() => void again("watch"))
|
|
194
|
+
})
|
|
195
|
+
}
|
|
196
|
+
const current = () => {
|
|
197
|
+
void version()
|
|
198
|
+
return store.get<T>(active()) ?? entry
|
|
199
|
+
}
|
|
200
|
+
return resourceOf<PickFrom<T, K> | D>(
|
|
201
|
+
app,
|
|
202
|
+
current,
|
|
203
|
+
() => again("refresh:manual"),
|
|
204
|
+
() => clearAsyncData(app, untracked(active)),
|
|
205
|
+
) as unknown as AsyncDataResource<PickFrom<T, K>, D>
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function injectAsyncData<I, R, T = R, K extends keyof T & string = never, D = T>(
|
|
209
|
+
fn: Handler<I, R>,
|
|
210
|
+
input: NoInfer<I> | (() => NoInfer<I>) | undefined,
|
|
211
|
+
options: InjectAsyncDataOptions<T, R, K, D> & { default: () => D },
|
|
212
|
+
): AsyncDataResource<PickFrom<T, K>, D>
|
|
213
|
+
export function injectAsyncData<I, R, T = R, K extends keyof T & string = never>(
|
|
214
|
+
fn: Handler<I, R>,
|
|
215
|
+
input?: NoInfer<I> | (() => NoInfer<I>),
|
|
216
|
+
options?: InjectAsyncDataOptions<T, R, K, undefined>,
|
|
217
|
+
): AsyncDataResource<PickFrom<T, K>>
|
|
218
|
+
export function injectAsyncData<T = unknown, R = T, K extends keyof T & string = never, D = T>(
|
|
219
|
+
key: string | (() => string),
|
|
220
|
+
handler: AsyncDataHandler<R>,
|
|
221
|
+
options: InjectAsyncDataOptions<T, R, K, D> & { default: () => D },
|
|
222
|
+
): AsyncDataResource<PickFrom<T, K>, D>
|
|
223
|
+
export function injectAsyncData<T = unknown, R = T, K extends keyof T & string = never>(
|
|
224
|
+
key: string | (() => string),
|
|
225
|
+
handler: AsyncDataHandler<R>,
|
|
226
|
+
options?: InjectAsyncDataOptions<T, R, K, undefined>,
|
|
227
|
+
): AsyncDataResource<PickFrom<T, K>>
|
|
228
|
+
export function injectAsyncData(...args: unknown[]): AsyncDataResource<any, any> {
|
|
229
|
+
assertInInjectionContext(injectAsyncData)
|
|
230
|
+
return asyncData("injectAsyncData", args)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function asyncData(name: string, args: unknown[]): AsyncDataResource<any, any> {
|
|
234
|
+
const [first, second, given] = asyncDataArguments(name, args) as [
|
|
235
|
+
string | (() => string) | HandlerReference,
|
|
236
|
+
unknown,
|
|
237
|
+
InjectAsyncDataOptions<any, any, any, any> | undefined,
|
|
238
|
+
]
|
|
239
|
+
const options = given ?? {}
|
|
240
|
+
if (!isHandlerReference(first))
|
|
241
|
+
return keyedAsyncData(first as string | (() => string), () => second as AsyncDataHandler<unknown>, options, name)
|
|
242
|
+
const { keyOf, handlerFor } = handlerSlots<unknown>(required(HAMUR_APP, name), first as HandlerReference, second)
|
|
243
|
+
return keyedAsyncData(keyOf, handlerFor, options, name)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** What a literal `injectFetch` call is checked against; any other call stays loose. */
|
|
247
|
+
type FetchInput<X> = X extends { key: infer K extends string; input: infer I }
|
|
248
|
+
? FetchInputField<I, K, I | (() => I)>
|
|
249
|
+
: unknown
|
|
250
|
+
type Unkeyed<O, X> = X extends { key: infer K extends string } ? Omit<O, K> : O
|
|
251
|
+
type FetchCall<U extends string, M extends string, B extends string | undefined> = {
|
|
252
|
+
method?: M | string
|
|
253
|
+
baseURL?: B | string
|
|
254
|
+
} & FetchInput<FetchCheckedInput<U, M, B>>
|
|
255
|
+
|
|
256
|
+
export function injectFetch<
|
|
257
|
+
U extends string,
|
|
258
|
+
M extends string = "GET",
|
|
259
|
+
K extends keyof FetchData<U, M> & string = never,
|
|
260
|
+
D = undefined,
|
|
261
|
+
B extends string | undefined = undefined,
|
|
262
|
+
>(
|
|
263
|
+
url: U | (() => U),
|
|
264
|
+
options?: Unkeyed<InjectFetchOptions<FetchData<U, M>, FetchData<U, M>, K, D>, FetchCheckedInput<U, M, B>> &
|
|
265
|
+
FetchCall<U, M, B>,
|
|
266
|
+
): AsyncDataResource<PickFrom<FetchData<U, M>, K>, D>
|
|
267
|
+
export function injectFetch<
|
|
268
|
+
T = unknown,
|
|
269
|
+
R = T,
|
|
270
|
+
K extends keyof T & string = never,
|
|
271
|
+
D = T,
|
|
272
|
+
U extends string = string,
|
|
273
|
+
M extends string = "GET",
|
|
274
|
+
B extends string | undefined = undefined,
|
|
275
|
+
>(
|
|
276
|
+
url: U | (() => U),
|
|
277
|
+
options: Unkeyed<InjectFetchOptions<T, R, K, D>, FetchCheckedInput<U, M, B>> & { default: () => D } & FetchCall<
|
|
278
|
+
U,
|
|
279
|
+
M,
|
|
280
|
+
B
|
|
281
|
+
>,
|
|
282
|
+
): AsyncDataResource<PickFrom<T, K>, D>
|
|
283
|
+
export function injectFetch<
|
|
284
|
+
T = unknown,
|
|
285
|
+
R = T,
|
|
286
|
+
K extends keyof T & string = never,
|
|
287
|
+
U extends string = string,
|
|
288
|
+
M extends string = "GET",
|
|
289
|
+
B extends string | undefined = undefined,
|
|
290
|
+
>(
|
|
291
|
+
url: U | (() => U),
|
|
292
|
+
options?: Unkeyed<InjectFetchOptions<T, R, K, undefined>, FetchCheckedInput<U, M, B>> & FetchCall<U, M, B>,
|
|
293
|
+
): AsyncDataResource<PickFrom<T, K>>
|
|
294
|
+
export function injectFetch<T = unknown, R = T, K extends keyof T & string = never, D = T>(
|
|
295
|
+
url: string | (() => string),
|
|
296
|
+
options: InjectFetchOptions<T, R, K, D> = {},
|
|
297
|
+
): AsyncDataResource<PickFrom<T, K>, D> {
|
|
298
|
+
assertInInjectionContext(injectFetch)
|
|
299
|
+
return fetchWith<T, R, K, D>(url, options, "injectFetch")
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function fetchWith<T, R, K extends keyof T & string, D>(
|
|
303
|
+
url: string | (() => string),
|
|
304
|
+
options: InjectFetchOptions<T, R, K, D>,
|
|
305
|
+
name: string,
|
|
306
|
+
): AsyncDataResource<PickFrom<T, K>, D> {
|
|
307
|
+
const {
|
|
308
|
+
key,
|
|
309
|
+
server,
|
|
310
|
+
lazy,
|
|
311
|
+
default: init,
|
|
312
|
+
transform,
|
|
313
|
+
immediate,
|
|
314
|
+
dedupe,
|
|
315
|
+
pick,
|
|
316
|
+
watch,
|
|
317
|
+
getCachedData,
|
|
318
|
+
...rest
|
|
319
|
+
} = options
|
|
320
|
+
const app = required(HAMUR_APP, name)
|
|
321
|
+
const { keyOf, handlerFor } = fetchSlots<R>(url, rest, app.fetch as $Fetch | undefined, key)
|
|
322
|
+
return keyedAsyncData<T, R, K, D>(
|
|
323
|
+
keyOf,
|
|
324
|
+
handlerFor,
|
|
325
|
+
{ server, lazy, default: init, transform, immediate, dedupe, pick, watch, getCachedData },
|
|
326
|
+
name,
|
|
327
|
+
)
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
type AnyFetchOptions = InjectFetchOptions<any, any, any, any>
|
|
331
|
+
|
|
332
|
+
export const injectLazyFetch: typeof injectFetch = ((url: string | (() => string), options?: AnyFetchOptions) => {
|
|
333
|
+
assertInInjectionContext(injectLazyFetch)
|
|
334
|
+
return fetchWith(url, { ...options, lazy: true }, "injectLazyFetch")
|
|
335
|
+
}) as typeof injectFetch
|
|
336
|
+
|
|
337
|
+
export const injectLazyAsyncData: typeof injectAsyncData = ((...args: unknown[]) => {
|
|
338
|
+
assertInInjectionContext(injectLazyAsyncData)
|
|
339
|
+
const [first, second, options] = asyncDataArguments("injectLazyAsyncData", args)
|
|
340
|
+
return asyncData("injectLazyAsyncData", [
|
|
341
|
+
first,
|
|
342
|
+
second,
|
|
343
|
+
{ ...(options as InjectAsyncDataOptions<any, any, any, any>), lazy: true },
|
|
344
|
+
])
|
|
345
|
+
}) as typeof injectAsyncData
|
|
346
|
+
|
|
347
|
+
/** The entry another component made under `key`, or under the key of `fn` and `input`, read as a resource; it creates none. */
|
|
348
|
+
export function injectNuxtData<T = unknown>(key: string): AsyncDataResource<T>
|
|
349
|
+
export function injectNuxtData<I, O>(fn: Handler<I, O>, input?: I | (() => I)): AsyncDataResource<O>
|
|
350
|
+
export function injectNuxtData<T = unknown>(target: string | HandlerReference, input?: unknown): AsyncDataResource<T> {
|
|
351
|
+
assertInInjectionContext(injectNuxtData)
|
|
352
|
+
const app = required(HAMUR_APP, "injectNuxtData")
|
|
353
|
+
const store = app.asyncData
|
|
354
|
+
const key = typeof target === "string" ? target : asyncDataKey(target, valueOf(input))
|
|
355
|
+
const version = signal(0)
|
|
356
|
+
const off = store.subscribe(key, () => version.update((count) => count + 1))
|
|
357
|
+
inject(DestroyRef).onDestroy(off)
|
|
358
|
+
return resourceOf<T | undefined>(
|
|
359
|
+
app,
|
|
360
|
+
() => {
|
|
361
|
+
void version()
|
|
362
|
+
return store.get<T>(key)
|
|
363
|
+
},
|
|
364
|
+
() => (store.has(key) ? store.refresh(key) : Promise.resolve()),
|
|
365
|
+
() => clearAsyncData(app, key),
|
|
366
|
+
) as unknown as AsyncDataResource<T>
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** The loader data of the page or layout the component belongs to, `null` for a route with no loader. */
|
|
370
|
+
export function injectData<T = unknown>(..._route: UseDataArgs<T>): Signal<RouteLoaderData<T>> {
|
|
371
|
+
assertInInjectionContext(injectData)
|
|
372
|
+
const payload = required(HAMUR_PAYLOAD, "injectData")
|
|
373
|
+
const entry = entryOf("injectData")
|
|
374
|
+
return computed(() => (dataOf(payload)[entry() as string] ?? null) as RouteLoaderData<T>)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export function clearState(keys?: Parameters<typeof clearStateOf>[1], options?: { reset?: boolean }): void {
|
|
378
|
+
assertInInjectionContext(clearState)
|
|
379
|
+
clearStateOf(required(HAMUR_APP, "clearState"), keys, options)
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export function callOnce(
|
|
383
|
+
key: string,
|
|
384
|
+
fn: () => unknown | Promise<unknown>,
|
|
385
|
+
options?: { mode?: "render" | "navigation" },
|
|
386
|
+
): Promise<void> {
|
|
387
|
+
assertInInjectionContext(callOnce)
|
|
388
|
+
return callOnceOf(required(HAMUR_APP, "callOnce"), key, fn, options)
|
|
389
|
+
}
|