@hamur/adapter-angular 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 +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 +1073 -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 +3 -0
- package/dist/types/src/async-data.d.ts +21 -0
- package/dist/types/src/client-parts.d.ts +32 -0
- package/dist/types/src/compile.d.ts +97 -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 +5 -0
- package/dist/types/src/error.d.ts +4 -0
- package/dist/types/src/form.d.ts +65 -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 +69 -0
- package/dist/types/src/island.d.ts +39 -0
- package/dist/types/src/keepalive.d.ts +1 -0
- package/dist/types/src/link.d.ts +65 -0
- package/dist/types/src/markdown-page.d.ts +10 -0
- package/dist/types/src/markdown.d.ts +37 -0
- package/dist/types/src/mask.d.ts +12 -0
- package/dist/types/src/navigate.d.ts +14 -0
- package/dist/types/src/outlet.d.ts +71 -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 +21 -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 +37 -0
- package/dist/types/src/wrapper.d.ts +21 -0
- package/package.json +56 -4
- package/src/activation.ts +83 -0
- package/src/adapter.ts +107 -0
- package/src/app.ts +6 -0
- package/src/async-data.ts +105 -0
- package/src/client-parts.ts +127 -0
- package/src/compile.ts +959 -0
- package/src/context.ts +189 -0
- package/src/data.ts +389 -0
- package/src/definition.ts +144 -0
- package/src/error-page.ts +17 -0
- package/src/error.ts +15 -0
- package/src/form.ts +253 -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 +251 -0
- package/src/island.ts +95 -0
- package/src/keepalive.ts +1 -0
- package/src/link.ts +251 -0
- package/src/markdown-page.ts +31 -0
- package/src/markdown.ts +170 -0
- package/src/mask.ts +36 -0
- package/src/navigate.ts +19 -0
- package/src/outlet.ts +344 -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 +54 -0
- package/src/state.ts +70 -0
- package/src/svg.ts +65 -0
- package/src/time.ts +108 -0
- package/src/wrapper.ts +187 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { markdownPageCode } from "hamur/adapter/build"
|
|
2
|
+
import type { MarkdownSource } from "hamur/adapter"
|
|
3
|
+
|
|
4
|
+
/** The decorator is written through a constant so the compile step does not take this file for a component of the package. */
|
|
5
|
+
const COMPONENT = "@Component"
|
|
6
|
+
|
|
7
|
+
/** The module of a Markdown page, a component rendering `source.document.ast` with `MarkdownRenderer`; it is compiled with the app. */
|
|
8
|
+
function module(source: MarkdownSource): { contents: string; extension: string } {
|
|
9
|
+
const { imports, value, props, head } = markdownPageCode(source)
|
|
10
|
+
const names = Object.keys(source.components)
|
|
11
|
+
const template = `<hamur-markdown [value]="value"${source.html ? ` [raw]="raw"` : ""}${names.length ? ` [components]="components"` : ""} />`
|
|
12
|
+
const contents = `import { Component } from "@angular/core";
|
|
13
|
+
import { MarkdownRenderer, injectHead } from "@hamur/adapter-angular";
|
|
14
|
+
${imports}
|
|
15
|
+
const value = ${value};
|
|
16
|
+
const props: Record<string, unknown> = ${props};
|
|
17
|
+
|
|
18
|
+
${COMPONENT}({ imports: [MarkdownRenderer], template: ${JSON.stringify(template)} })
|
|
19
|
+
export default class MarkdownPage {
|
|
20
|
+
protected readonly value = value as never;
|
|
21
|
+
protected readonly raw = props["raw"] as never;
|
|
22
|
+
protected readonly components = props["components"] as never;
|
|
23
|
+
|
|
24
|
+
constructor() {
|
|
25
|
+
${head ? ` injectHead(${head});\n` : ""} }
|
|
26
|
+
}
|
|
27
|
+
`
|
|
28
|
+
return { contents, extension: ".ts" }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const markdownComponent = { module }
|
package/src/markdown.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ApplicationRef,
|
|
3
|
+
Component,
|
|
4
|
+
ElementRef,
|
|
5
|
+
EnvironmentInjector,
|
|
6
|
+
Injector,
|
|
7
|
+
Renderer2,
|
|
8
|
+
createComponent,
|
|
9
|
+
effect,
|
|
10
|
+
inject,
|
|
11
|
+
input,
|
|
12
|
+
reflectComponentType,
|
|
13
|
+
untracked,
|
|
14
|
+
type ComponentRef,
|
|
15
|
+
type Type,
|
|
16
|
+
} from "@angular/core"
|
|
17
|
+
import {
|
|
18
|
+
linkAttributes,
|
|
19
|
+
linkDefaultsOf,
|
|
20
|
+
linkPrefetchProps,
|
|
21
|
+
markdownLink,
|
|
22
|
+
markdownProse,
|
|
23
|
+
markdownView,
|
|
24
|
+
} from "hamur/adapter"
|
|
25
|
+
import type { MdView, MdViewComponent } from "hamur/adapter"
|
|
26
|
+
import type { MarkdownDocument, MdNode, MdProps } from "hamur/markdown"
|
|
27
|
+
import { HAMUR_APP, HAMUR_HREF, detachedHref } from "./context.ts"
|
|
28
|
+
|
|
29
|
+
export interface MarkdownRendererProps {
|
|
30
|
+
/** A Markdown AST node, a list of nodes, or a document, which renders its `ast`. */
|
|
31
|
+
value: MdNode | MdNode[] | MarkdownDocument
|
|
32
|
+
/** Components by Markdown component name (kebab case); each gets the node's props, its default slot and a slot per named slot. */
|
|
33
|
+
components?: Record<string, Type<unknown>>
|
|
34
|
+
/** Renders `html` nodes, writing what it returns as their inner HTML; without it they are left out. A Markdown page under config `markdown.html` passes the core's Trusted Types writer. */
|
|
35
|
+
raw?: (html: string) => string | { readonly __brand: "TrustedHTML" }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type Components = Record<string, Type<unknown>>
|
|
39
|
+
|
|
40
|
+
const camelize = (name: string) => name.replace(/-(\w)/g, (_, char: string) => char.toUpperCase())
|
|
41
|
+
const SLOT = /^\[(?:slot=)?["']?([\w-]+)["']?\]$|^([\w-]+)$/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Renders the Markdown AST as elements: an internal link as the anchor `Link` writes, a component node as its component
|
|
45
|
+
* in `components` (its props as inputs, the others as attributes; its default slot and each named slot projected into the `ng-content`
|
|
46
|
+
* whose selector is the slot's name, as `[name]` or `[slot=name]`). The elements are made with the renderer, so Angular skips
|
|
47
|
+
* the host in hydration and the browser builds them again from the same AST.
|
|
48
|
+
*/
|
|
49
|
+
@Component({ selector: "hamur-markdown", template: "", host: { ngSkipHydration: "true" } })
|
|
50
|
+
export class MarkdownRenderer {
|
|
51
|
+
readonly value = input.required<MarkdownRendererProps["value"]>()
|
|
52
|
+
readonly components = input<Components>()
|
|
53
|
+
readonly raw = input<MarkdownRendererProps["raw"]>()
|
|
54
|
+
private readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement
|
|
55
|
+
private readonly renderer = inject(Renderer2)
|
|
56
|
+
private readonly app = inject(HAMUR_APP)
|
|
57
|
+
private readonly resolve = inject(HAMUR_HREF, { optional: true }) ?? detachedHref
|
|
58
|
+
private readonly injector = inject(Injector)
|
|
59
|
+
private readonly environment = inject(EnvironmentInjector)
|
|
60
|
+
private readonly appRef = inject(ApplicationRef)
|
|
61
|
+
|
|
62
|
+
constructor() {
|
|
63
|
+
effect((onCleanup) => {
|
|
64
|
+
const raw = this.raw()
|
|
65
|
+
const prose = markdownProse(this.value(), this.components() ?? {})
|
|
66
|
+
const views = markdownView(prose.value, { html: raw !== undefined, components: prose.components })
|
|
67
|
+
const refs: Array<ComponentRef<unknown>> = []
|
|
68
|
+
untracked(() => {
|
|
69
|
+
for (const view of views) this.append(this.host, view, raw, prose.components, refs)
|
|
70
|
+
})
|
|
71
|
+
onCleanup(() => {
|
|
72
|
+
for (const ref of refs) ref.destroy()
|
|
73
|
+
while (this.host.firstChild) this.renderer.removeChild(this.host, this.host.firstChild)
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private append(
|
|
79
|
+
parent: Node,
|
|
80
|
+
view: MdView,
|
|
81
|
+
raw: MarkdownRendererProps["raw"],
|
|
82
|
+
components: Components,
|
|
83
|
+
refs: Array<ComponentRef<unknown>>,
|
|
84
|
+
): void {
|
|
85
|
+
const renderer = this.renderer
|
|
86
|
+
if (typeof view === "string") return renderer.appendChild(parent, renderer.createText(view))
|
|
87
|
+
if ("html" in view) {
|
|
88
|
+
const element = renderer.createElement("hamur-html")
|
|
89
|
+
renderer.setStyle(element, "display", "contents")
|
|
90
|
+
renderer.setProperty(element, "innerHTML", raw!(view.html))
|
|
91
|
+
return renderer.appendChild(parent, element)
|
|
92
|
+
}
|
|
93
|
+
if ("component" in view) return this.appendComponent(parent, view, raw, components, refs)
|
|
94
|
+
const link = markdownLink(view)
|
|
95
|
+
const element = renderer.createElement(view.tag)
|
|
96
|
+
if (link) {
|
|
97
|
+
const defaults = linkDefaultsOf(this.app)
|
|
98
|
+
const attributes = linkAttributes({
|
|
99
|
+
href: this.resolve(link.to),
|
|
100
|
+
external: false,
|
|
101
|
+
...linkPrefetchProps({}, defaults),
|
|
102
|
+
replace: false,
|
|
103
|
+
pendingClass: defaults.pendingClass,
|
|
104
|
+
trim: false,
|
|
105
|
+
prefetchedClass: defaults.prefetchedClass,
|
|
106
|
+
prefetchDelay: defaults.prefetchDelay,
|
|
107
|
+
externalRelAttribute: defaults.externalRelAttribute,
|
|
108
|
+
})
|
|
109
|
+
for (const [name, value] of Object.entries({ ...link.attrs, ...attributes }))
|
|
110
|
+
renderer.setAttribute(element, name, value === true ? "" : value)
|
|
111
|
+
} else
|
|
112
|
+
for (const [name, value] of Object.entries(view.attrs))
|
|
113
|
+
renderer.setAttribute(element, name, value === true ? "" : value)
|
|
114
|
+
for (const child of view.children) this.append(element, child, raw, components, refs)
|
|
115
|
+
renderer.appendChild(parent, element)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private appendComponent(
|
|
119
|
+
parent: Node,
|
|
120
|
+
view: MdViewComponent,
|
|
121
|
+
raw: MarkdownRendererProps["raw"],
|
|
122
|
+
components: Components,
|
|
123
|
+
refs: Array<ComponentRef<unknown>>,
|
|
124
|
+
): void {
|
|
125
|
+
const renderer = this.renderer
|
|
126
|
+
const component = Object.hasOwn(components, view.component) ? components[view.component] : undefined
|
|
127
|
+
const nodes = (views: MdView[]): Node[] => {
|
|
128
|
+
const holder = renderer.createElement("div")
|
|
129
|
+
for (const child of views) this.append(holder, child, raw, components, refs)
|
|
130
|
+
return Array.from(holder.childNodes)
|
|
131
|
+
}
|
|
132
|
+
if (!component) {
|
|
133
|
+
if (!view.attrs) {
|
|
134
|
+
for (const child of view.children) this.append(parent, child, raw, components, refs)
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
const element = renderer.createElement(view.component)
|
|
138
|
+
for (const [name, value] of Object.entries(view.attrs))
|
|
139
|
+
renderer.setAttribute(element, name, value === true ? "" : value)
|
|
140
|
+
for (const child of view.children) this.append(element, child, raw, components, refs)
|
|
141
|
+
return renderer.appendChild(parent, element)
|
|
142
|
+
}
|
|
143
|
+
const mirror = reflectComponentType(component)
|
|
144
|
+
const declared = new Set(mirror?.inputs.map((input) => input.templateName))
|
|
145
|
+
const entries = Object.entries(view.props as MdProps)
|
|
146
|
+
const [element] = markdownView([
|
|
147
|
+
"span",
|
|
148
|
+
Object.fromEntries(entries.filter(([name]) => !declared.has(camelize(name)))),
|
|
149
|
+
]) as Array<{ attrs: Record<string, string | true> }>
|
|
150
|
+
const slots = new Map<string, Node[]>()
|
|
151
|
+
for (const [name, views] of Object.entries(view.slots)) slots.set(name, nodes(views))
|
|
152
|
+
const content = nodes(view.children)
|
|
153
|
+
const ref = createComponent(component, {
|
|
154
|
+
environmentInjector: this.environment,
|
|
155
|
+
elementInjector: this.injector,
|
|
156
|
+
projectableNodes: (mirror?.ngContentSelectors ?? []).map((selector) => {
|
|
157
|
+
const named = SLOT.exec(selector)
|
|
158
|
+
const name = named?.[1] ?? named?.[2]
|
|
159
|
+
return selector === "*" ? content : name !== undefined ? (slots.get(name) ?? []) : []
|
|
160
|
+
}),
|
|
161
|
+
})
|
|
162
|
+
for (const [name, value] of entries) if (declared.has(camelize(name))) ref.setInput(camelize(name), value)
|
|
163
|
+
const host = ref.location.nativeElement as HTMLElement
|
|
164
|
+
for (const [name, value] of Object.entries(element?.attrs ?? {}))
|
|
165
|
+
renderer.setAttribute(host, name, value === true ? "" : value)
|
|
166
|
+
this.appRef.attachView(ref.hostView)
|
|
167
|
+
refs.push(ref)
|
|
168
|
+
renderer.appendChild(parent, host)
|
|
169
|
+
}
|
|
170
|
+
}
|
package/src/mask.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { HamurError } from "hamur/app"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Angular binds the object it hands to `onViewError` to `$error` (`boundary.error`, set before the call), so a render overwrites it with what `publicError` made of it: its own properties go, its message, status, statusText and data are the masked ones.
|
|
5
|
+
*/
|
|
6
|
+
export function maskInPlace(error: object, shown: HamurError): void {
|
|
7
|
+
for (const key of Reflect.ownKeys(error)) Reflect.deleteProperty(error, key)
|
|
8
|
+
Object.defineProperties(error, {
|
|
9
|
+
name: { value: shown.name, configurable: true, writable: true },
|
|
10
|
+
message: { value: shown.message, configurable: true, writable: true },
|
|
11
|
+
stack: { value: `${shown.name}: ${shown.message}`, configurable: true, writable: true },
|
|
12
|
+
status: { value: shown.status, configurable: true, writable: true, enumerable: true },
|
|
13
|
+
statusText: { value: shown.statusText, configurable: true, writable: true, enumerable: true },
|
|
14
|
+
...(shown.data === undefined
|
|
15
|
+
? {}
|
|
16
|
+
: { data: { value: shown.data, configurable: true, writable: true, enumerable: true } }),
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The `@error` blocks the server rendered, in the order they caught, under `$boundary:<n>` in the app state (as vue's
|
|
22
|
+
* `$boundary:<useId()>`): the browser creates each block again while it hydrates (A6) and shows the same text.
|
|
23
|
+
*/
|
|
24
|
+
export const boundaryKey = (index: number): string => `$boundary:${index}`
|
|
25
|
+
|
|
26
|
+
/** What the server's `@error` block showed, as plain data for the payload. */
|
|
27
|
+
export function shownError(error: Error): HamurError {
|
|
28
|
+
const { status, statusText, data } = error as Partial<HamurError>
|
|
29
|
+
return {
|
|
30
|
+
name: error.name,
|
|
31
|
+
message: error.message,
|
|
32
|
+
...(status === undefined ? {} : { status }),
|
|
33
|
+
...(statusText === undefined ? {} : { statusText }),
|
|
34
|
+
...(data === undefined ? {} : { data }),
|
|
35
|
+
} as HamurError
|
|
36
|
+
}
|
package/src/navigate.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Component, afterNextRender, booleanAttribute, input, type OnInit } from "@angular/core"
|
|
2
|
+
import { navigateTo, redirect } from "hamur/app"
|
|
3
|
+
import type { RouteLocation } from "hamur/app"
|
|
4
|
+
import type { CheckedTarget } from "hamur/adapter"
|
|
5
|
+
|
|
6
|
+
/** Navigates to `to` once after mount; during SSR it throws the redirect `navigateTo` throws there. */
|
|
7
|
+
@Component({ selector: "hamur-navigate", template: "" })
|
|
8
|
+
export class Navigate<T extends string = string> implements OnInit {
|
|
9
|
+
readonly to = input.required<CheckedTarget<T, false> | RouteLocation>()
|
|
10
|
+
readonly replace = input(false, { transform: booleanAttribute })
|
|
11
|
+
|
|
12
|
+
constructor() {
|
|
13
|
+
afterNextRender(() => void navigateTo<string>(this.to() as string | RouteLocation, { replace: this.replace() }))
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
ngOnInit(): void {
|
|
17
|
+
if (typeof window === "undefined") throw redirect<string>(this.to() as string | RouteLocation)
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/outlet.ts
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Component,
|
|
3
|
+
DestroyRef,
|
|
4
|
+
InjectionToken,
|
|
5
|
+
Injector,
|
|
6
|
+
PendingTasks,
|
|
7
|
+
ViewContainerRef,
|
|
8
|
+
computed,
|
|
9
|
+
effect,
|
|
10
|
+
inject,
|
|
11
|
+
input,
|
|
12
|
+
reflectComponentType,
|
|
13
|
+
untracked,
|
|
14
|
+
type ComponentRef,
|
|
15
|
+
type OnInit,
|
|
16
|
+
type Provider,
|
|
17
|
+
type Signal,
|
|
18
|
+
type Type,
|
|
19
|
+
} from "@angular/core"
|
|
20
|
+
import { noteOutlet, routeBoundary } from "hamur/adapter"
|
|
21
|
+
import type { KeptUnit, RenderContext, RenderEntry, RenderTree, RouteValue } from "hamur/adapter"
|
|
22
|
+
import type { ClearErrorOptions } from "hamur/app"
|
|
23
|
+
import { HAMUR_ACTIVATION, activation, detach, setHidden } from "./activation.ts"
|
|
24
|
+
import {
|
|
25
|
+
HAMUR_APP,
|
|
26
|
+
HAMUR_CAUGHT,
|
|
27
|
+
HAMUR_CONTEXT,
|
|
28
|
+
HAMUR_FAIL,
|
|
29
|
+
HAMUR_LEVEL,
|
|
30
|
+
HAMUR_LEVEL_DISPOSE,
|
|
31
|
+
HAMUR_OUTLET_CONTEXT,
|
|
32
|
+
HAMUR_PAYLOAD,
|
|
33
|
+
HAMUR_ROUTE,
|
|
34
|
+
HAMUR_TREE,
|
|
35
|
+
mode,
|
|
36
|
+
routeValue,
|
|
37
|
+
} from "./context.ts"
|
|
38
|
+
|
|
39
|
+
interface Shown {
|
|
40
|
+
entry: RenderEntry
|
|
41
|
+
ref: ComponentRef<unknown>
|
|
42
|
+
inputs: Set<string>
|
|
43
|
+
disposers: Set<() => void>
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const EMPTY: RenderTree = { components: [] }
|
|
47
|
+
const same = (a: RenderEntry, b: RenderEntry) => a.id === b.id && a.key === b.key && a.component === b.component
|
|
48
|
+
|
|
49
|
+
/** `onError` gets a thrown value that is not an `Error` as the `cause` of one. */
|
|
50
|
+
const unwrap = (error: unknown): unknown =>
|
|
51
|
+
error instanceof Error && error.name === "ErrorBoundaryWrappedError" ? error.cause : error
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Places the next level of the tree as its next sibling; the `hamur-outlet` element stays empty. With `name` it places
|
|
55
|
+
* the chain of that slot of the entry it sits in; `context` reaches the entries below through `injectOutletContext()`.
|
|
56
|
+
*/
|
|
57
|
+
@Component({ selector: "hamur-outlet", template: "" })
|
|
58
|
+
export class Outlet implements OnInit {
|
|
59
|
+
readonly name = input<string>()
|
|
60
|
+
readonly context = input<unknown>()
|
|
61
|
+
private readonly tree = inject(HAMUR_TREE)
|
|
62
|
+
private readonly fail = inject(HAMUR_FAIL)
|
|
63
|
+
private readonly app = inject(HAMUR_APP)
|
|
64
|
+
private readonly payload = inject(HAMUR_PAYLOAD)
|
|
65
|
+
private readonly route = inject(HAMUR_ROUTE)
|
|
66
|
+
private readonly above = inject(HAMUR_OUTLET_CONTEXT, { optional: true })
|
|
67
|
+
private readonly owner = inject(HAMUR_LEVEL, { optional: true }) ?? -1
|
|
68
|
+
private readonly injector = inject(Injector)
|
|
69
|
+
private readonly container = inject(ViewContainerRef)
|
|
70
|
+
private readonly pageContext = inject(HAMUR_CONTEXT)
|
|
71
|
+
private readonly tasks = inject(PendingTasks)
|
|
72
|
+
private readonly warned = new Set<string>()
|
|
73
|
+
private shown: Shown | undefined
|
|
74
|
+
/** The keep-alive wrappers this outlet holds, by keep-alive key. */
|
|
75
|
+
private readonly pages = new Map<string, ComponentRef<KeptPage>>()
|
|
76
|
+
private started = false
|
|
77
|
+
/** Counts what the level shows, so an answer that waits for a microtask finds out the level moved on. */
|
|
78
|
+
private shownCount = 0
|
|
79
|
+
|
|
80
|
+
/** The tree this outlet reads and the index of its entry in it. */
|
|
81
|
+
private readonly view = computed(() => {
|
|
82
|
+
const name = this.name()
|
|
83
|
+
const tree = this.tree()
|
|
84
|
+
if (name === undefined) return { tree, index: this.owner + 1, slot: false }
|
|
85
|
+
return { tree: tree.components[this.owner]?.outlets?.[name], index: 0, slot: true }
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
private readonly value = computed(() => {
|
|
89
|
+
const own = this.context()
|
|
90
|
+
return own !== undefined ? own : this.above?.()
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
private readonly slotTree = computed(() => this.view().tree ?? EMPTY)
|
|
94
|
+
|
|
95
|
+
private readonly slotRoute = computed(() => {
|
|
96
|
+
const slot = this.view().tree
|
|
97
|
+
if (!slot?.route) return undefined
|
|
98
|
+
const { state, key, navigationType } = this.route()
|
|
99
|
+
return routeValue({
|
|
100
|
+
route: { ...slot.route, state, key, navigationType },
|
|
101
|
+
tree: slot,
|
|
102
|
+
app: this.app,
|
|
103
|
+
payload: this.payload(),
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
constructor() {
|
|
108
|
+
inject(DestroyRef).onDestroy(() => {
|
|
109
|
+
this.shownCount++
|
|
110
|
+
for (const dispose of this.shown?.disposers ?? []) dispose()
|
|
111
|
+
})
|
|
112
|
+
effect(() => {
|
|
113
|
+
const entry = this.entry()
|
|
114
|
+
const units = this.units()
|
|
115
|
+
if (this.started) untracked(() => this.apply(entry, units))
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
ngOnInit(): void {
|
|
120
|
+
this.started = true
|
|
121
|
+
this.apply(this.entry(), this.units())
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The kept units of this outlet; `undefined` while the app has keep-alive off, so the outlet keeps one shape. */
|
|
125
|
+
private units(): KeptUnit[] | undefined {
|
|
126
|
+
if (this.name() !== undefined) return undefined
|
|
127
|
+
const tree = this.tree()
|
|
128
|
+
return (this.owner >= 0 ? tree.components[this.owner] : tree)?.kept
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Each kept unit hidden in its wrapper, then the current child, in the wrapper of its key when its route keeps its pages. */
|
|
132
|
+
private apply(entry: RenderEntry | undefined, units: KeptUnit[] | undefined): void {
|
|
133
|
+
if (units === undefined) {
|
|
134
|
+
this.dropPages()
|
|
135
|
+
this.show(entry)
|
|
136
|
+
return
|
|
137
|
+
}
|
|
138
|
+
const wanted = new Set<string>()
|
|
139
|
+
let index = 0
|
|
140
|
+
for (const unit of units) {
|
|
141
|
+
this.page(unit.key, unit, index++)
|
|
142
|
+
wanted.add(unit.key)
|
|
143
|
+
}
|
|
144
|
+
if (entry?.keepalive !== undefined) {
|
|
145
|
+
this.show(undefined)
|
|
146
|
+
this.page(entry.keepalive, undefined, index)
|
|
147
|
+
wanted.add(entry.keepalive)
|
|
148
|
+
} else this.show(entry)
|
|
149
|
+
for (const [key, ref] of this.pages)
|
|
150
|
+
if (!wanted.has(key)) {
|
|
151
|
+
ref.destroy()
|
|
152
|
+
this.pages.delete(key)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private page(key: string, unit: KeptUnit | undefined, index: number): void {
|
|
157
|
+
let ref = this.pages.get(key)
|
|
158
|
+
if (!ref) {
|
|
159
|
+
const injector = Injector.create({
|
|
160
|
+
providers: [
|
|
161
|
+
{
|
|
162
|
+
provide: KEPT_SOURCE,
|
|
163
|
+
useValue: { tree: this.tree, level: this.owner, route: this.route, payload: this.payload },
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
parent: this.injector,
|
|
167
|
+
})
|
|
168
|
+
ref = this.container.createComponent(KeptPage, { injector, index })
|
|
169
|
+
this.pages.set(key, ref)
|
|
170
|
+
}
|
|
171
|
+
ref.setInput("name", key)
|
|
172
|
+
ref.setInput("unit", unit)
|
|
173
|
+
ref.setInput("hidden", unit !== undefined)
|
|
174
|
+
if (this.container.indexOf(ref.hostView) !== index) this.container.move(ref.hostView, index)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private dropPages(): void {
|
|
178
|
+
for (const ref of this.pages.values()) ref.destroy()
|
|
179
|
+
this.pages.clear()
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private dropShown(): void {
|
|
183
|
+
const shown = this.shown
|
|
184
|
+
this.shown = undefined
|
|
185
|
+
if (!shown) return
|
|
186
|
+
shown.ref.destroy()
|
|
187
|
+
for (const dispose of shown.disposers) dispose()
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private entry(): RenderEntry | undefined {
|
|
191
|
+
const { tree, index, slot } = this.view()
|
|
192
|
+
const name = this.name()
|
|
193
|
+
if (mode.dev) {
|
|
194
|
+
if (!slot) noteOutlet(this.tree().components[this.owner]?.id)
|
|
195
|
+
else if (!tree && name !== undefined && !this.warned.has(name)) {
|
|
196
|
+
this.warned.add(name)
|
|
197
|
+
console.warn(`Outlet name "${name}" has no slot in ${this.tree().components[this.owner]?.id}`)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return tree?.components[index]
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private show(entry: RenderEntry | undefined): void {
|
|
204
|
+
const shown = this.shown
|
|
205
|
+
if (shown && entry && same(shown.entry, entry)) {
|
|
206
|
+
shown.entry = entry
|
|
207
|
+
this.setInputs(shown, entry)
|
|
208
|
+
return
|
|
209
|
+
}
|
|
210
|
+
this.shownCount++
|
|
211
|
+
this.dropShown()
|
|
212
|
+
if (entry) this.place(entry)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private place(entry: RenderEntry): void {
|
|
216
|
+
const component = entry.component as Type<unknown>
|
|
217
|
+
const disposers = new Set<() => void>()
|
|
218
|
+
const providers: Provider[] = [
|
|
219
|
+
{ provide: HAMUR_LEVEL, useValue: this.view().index },
|
|
220
|
+
{ provide: HAMUR_LEVEL_DISPOSE, useValue: (dispose: () => void) => disposers.add(dispose) },
|
|
221
|
+
{ provide: HAMUR_OUTLET_CONTEXT, useValue: this.value },
|
|
222
|
+
]
|
|
223
|
+
if (entry.caught) providers.push({ provide: HAMUR_CAUGHT, useValue: (entry.props as { error: unknown }).error })
|
|
224
|
+
if (this.view().slot) {
|
|
225
|
+
providers.push({ provide: HAMUR_TREE, useValue: this.slotTree })
|
|
226
|
+
providers.push({ provide: HAMUR_ROUTE, useFactory: () => computed(() => this.slotRoute() ?? this.route()) })
|
|
227
|
+
}
|
|
228
|
+
const injector = Injector.create({ providers, parent: this.injector })
|
|
229
|
+
const failed = (error: unknown) => this.failed(entry, unwrap(error))
|
|
230
|
+
try {
|
|
231
|
+
const ref = this.container.createComponent(component, { injector, onError: failed })
|
|
232
|
+
const inputs = new Set(reflectComponentType(component)?.inputs.map((input) => input.templateName) ?? [])
|
|
233
|
+
this.shown = { entry, ref, inputs, disposers }
|
|
234
|
+
this.setInputs(this.shown, entry)
|
|
235
|
+
} catch (error) {
|
|
236
|
+
failed(error)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* A throw of the entry this level creates, in its constructor or its checks. The route boundary covering the entry
|
|
242
|
+
* answers in its place; with none the error goes up to the next `@boundary` or level, and from the first level to the app's error page.
|
|
243
|
+
* A route boundary's own throw skips every boundary above it.
|
|
244
|
+
*/
|
|
245
|
+
private failed(entry: RenderEntry, error: unknown): void {
|
|
246
|
+
if (entry.caught) return this.fail(error)
|
|
247
|
+
const caught = entry.boundary ? routeBoundary(this.pageContext(), entry, error) : undefined
|
|
248
|
+
if (caught) return this.answer(caught)
|
|
249
|
+
if (this.owner >= 0) throw error
|
|
250
|
+
this.fail(error)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** The boundary component takes the entry's place once the check that threw is over: Angular is still walking the view. */
|
|
254
|
+
private answer(caught: NonNullable<ReturnType<typeof routeBoundary>>): void {
|
|
255
|
+
const count = this.shownCount
|
|
256
|
+
const done = this.tasks.add()
|
|
257
|
+
queueMicrotask(() => {
|
|
258
|
+
try {
|
|
259
|
+
if (count !== this.shownCount) return
|
|
260
|
+
this.shownCount++
|
|
261
|
+
this.dropShown()
|
|
262
|
+
this.place({
|
|
263
|
+
id: caught.id,
|
|
264
|
+
component: caught.component,
|
|
265
|
+
props: { error: caught.error, clearError: (options?: ClearErrorOptions) => this.clear(options) },
|
|
266
|
+
caught: caught.caught,
|
|
267
|
+
})
|
|
268
|
+
} finally {
|
|
269
|
+
done()
|
|
270
|
+
}
|
|
271
|
+
})
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private clear(options?: ClearErrorOptions): Promise<void> {
|
|
275
|
+
return this.app.error?.clear(options) ?? Promise.resolve()
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
private setInputs({ ref, inputs }: Shown, entry: RenderEntry): void {
|
|
279
|
+
for (const [name, value] of Object.entries(entry.props ?? {})) if (inputs.has(name)) ref.setInput(name, value)
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** What a keep-alive wrapper reads from the outlet that creates it. */
|
|
284
|
+
interface KeptSource {
|
|
285
|
+
tree: Signal<RenderTree>
|
|
286
|
+
/** The index in `tree.components` of the entry whose outlet holds the wrapper; `-1` for the top level. */
|
|
287
|
+
level: number
|
|
288
|
+
route: Signal<RouteValue>
|
|
289
|
+
payload: Signal<RenderContext["payload"]>
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const KEPT_SOURCE = new InjectionToken<KeptSource>("hamur kept source")
|
|
293
|
+
const keptRoutes = new WeakMap<KeptUnit, RouteValue>()
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* The `<div data-hamur-keepalive="<key>">` of a page whose route keeps its pages, `display:contents` while it is shown
|
|
297
|
+
* and `hidden` while kept. A kept unit reads its own tree, route and payload; a shown one reads the page's, from its own level down.
|
|
298
|
+
*/
|
|
299
|
+
@Component({
|
|
300
|
+
selector: "div[data-hamur-keepalive]",
|
|
301
|
+
imports: [Outlet],
|
|
302
|
+
template: "<hamur-outlet />",
|
|
303
|
+
providers: [
|
|
304
|
+
{ provide: HAMUR_LEVEL, useValue: -1 },
|
|
305
|
+
{ provide: HAMUR_TREE, useFactory: () => inject(KeptPage).tree },
|
|
306
|
+
{ provide: HAMUR_ROUTE, useFactory: () => inject(KeptPage).route },
|
|
307
|
+
{ provide: HAMUR_PAYLOAD, useFactory: () => inject(KeptPage).payload },
|
|
308
|
+
{ provide: HAMUR_ACTIVATION, useFactory: () => inject(KeptPage).activation },
|
|
309
|
+
],
|
|
310
|
+
host: {
|
|
311
|
+
"[attr.data-hamur-keepalive]": "name()",
|
|
312
|
+
"[attr.hidden]": "hidden() ? '' : null",
|
|
313
|
+
"[style.display]": "hidden() ? null : 'contents'",
|
|
314
|
+
},
|
|
315
|
+
})
|
|
316
|
+
export class KeptPage {
|
|
317
|
+
readonly name = input.required<string>()
|
|
318
|
+
readonly unit = input<KeptUnit>()
|
|
319
|
+
readonly hidden = input(false)
|
|
320
|
+
private readonly source = inject(KEPT_SOURCE)
|
|
321
|
+
private readonly app = inject(HAMUR_APP)
|
|
322
|
+
readonly activation = activation(inject(HAMUR_ACTIVATION, { skipSelf: true, optional: true }) ?? undefined, false)
|
|
323
|
+
|
|
324
|
+
readonly tree: Signal<RenderTree> = computed(
|
|
325
|
+
() => this.unit()?.tree ?? { components: this.source.tree().components.slice(this.source.level + 1) },
|
|
326
|
+
)
|
|
327
|
+
readonly route: Signal<RouteValue> = computed(() => {
|
|
328
|
+
const unit = this.unit()
|
|
329
|
+
if (!unit) return this.source.route()
|
|
330
|
+
let value = keptRoutes.get(unit)
|
|
331
|
+
if (!value)
|
|
332
|
+
keptRoutes.set(
|
|
333
|
+
unit,
|
|
334
|
+
(value = routeValue({ route: unit.route, app: this.app, payload: unit.payload, tree: unit.tree })),
|
|
335
|
+
)
|
|
336
|
+
return value
|
|
337
|
+
})
|
|
338
|
+
readonly payload: Signal<RenderContext["payload"]> = computed(() => this.unit()?.payload ?? this.source.payload())
|
|
339
|
+
|
|
340
|
+
constructor() {
|
|
341
|
+
effect(() => setHidden(this.activation, this.hidden()))
|
|
342
|
+
inject(DestroyRef).onDestroy(() => detach(this.activation))
|
|
343
|
+
}
|
|
344
|
+
}
|