@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.
Files changed (88) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +42 -2
  3. package/dist/chunks/browser-index-7h7x2xz2.js +131 -0
  4. package/dist/chunks/browser-index-nann848q.js +205 -0
  5. package/dist/chunks/browser-index-qn8q5fa2.js +580 -0
  6. package/dist/src/adapter.js +1075 -0
  7. package/dist/src/error-page.js +34 -0
  8. package/dist/src/index.js +1729 -0
  9. package/dist/src/keepalive.js +0 -0
  10. package/dist/src/runtime/client.js +21 -0
  11. package/dist/src/runtime/island.js +92 -0
  12. package/dist/src/runtime/server.js +175 -0
  13. package/dist/types/src/activation.d.ts +20 -0
  14. package/dist/types/src/adapter.d.ts +7 -0
  15. package/dist/types/src/app.d.ts +6 -0
  16. package/dist/types/src/async-data.d.ts +21 -0
  17. package/dist/types/src/client-parts.d.ts +35 -0
  18. package/dist/types/src/compile.d.ts +99 -0
  19. package/dist/types/src/context.d.ts +74 -0
  20. package/dist/types/src/data.d.ts +65 -0
  21. package/dist/types/src/definition.d.ts +13 -0
  22. package/dist/types/src/error-page.d.ts +8 -0
  23. package/dist/types/src/error.d.ts +4 -0
  24. package/dist/types/src/form.d.ts +68 -0
  25. package/dist/types/src/head.d.ts +12 -0
  26. package/dist/types/src/hmr.d.ts +14 -0
  27. package/dist/types/src/hydration.d.ts +16 -0
  28. package/dist/types/src/index.d.ts +24 -0
  29. package/dist/types/src/indicators.d.ts +76 -0
  30. package/dist/types/src/island.d.ts +42 -0
  31. package/dist/types/src/keepalive.d.ts +1 -0
  32. package/dist/types/src/link.d.ts +68 -0
  33. package/dist/types/src/markdown-page.d.ts +10 -0
  34. package/dist/types/src/markdown.d.ts +40 -0
  35. package/dist/types/src/mask.d.ts +12 -0
  36. package/dist/types/src/navigate.d.ts +17 -0
  37. package/dist/types/src/outlet.d.ts +76 -0
  38. package/dist/types/src/request.d.ts +31 -0
  39. package/dist/types/src/routing.d.ts +47 -0
  40. package/dist/types/src/runtime/client.d.ts +29 -0
  41. package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
  42. package/dist/types/src/runtime/hydration.d.ts +8 -0
  43. package/dist/types/src/runtime/island.d.ts +8 -0
  44. package/dist/types/src/runtime/server.d.ts +15 -0
  45. package/dist/types/src/server-island.d.ts +24 -0
  46. package/dist/types/src/state.d.ts +14 -0
  47. package/dist/types/src/svg.d.ts +15 -0
  48. package/dist/types/src/time.d.ts +40 -0
  49. package/dist/types/src/wrapper.d.ts +21 -0
  50. package/package.json +77 -4
  51. package/src/activation.ts +83 -0
  52. package/src/adapter.ts +107 -0
  53. package/src/app.ts +12 -0
  54. package/src/async-data.ts +105 -0
  55. package/src/client-parts.ts +107 -0
  56. package/src/compile.ts +963 -0
  57. package/src/context.ts +189 -0
  58. package/src/data.ts +389 -0
  59. package/src/definition.ts +144 -0
  60. package/src/error-page.ts +30 -0
  61. package/src/error.ts +15 -0
  62. package/src/form.ts +182 -0
  63. package/src/head.ts +63 -0
  64. package/src/hmr.ts +61 -0
  65. package/src/hydration.ts +120 -0
  66. package/src/index.ts +98 -0
  67. package/src/indicators.ts +220 -0
  68. package/src/island.ts +89 -0
  69. package/src/keepalive.ts +1 -0
  70. package/src/link.ts +214 -0
  71. package/src/markdown-page.ts +31 -0
  72. package/src/markdown.ts +140 -0
  73. package/src/mask.ts +36 -0
  74. package/src/navigate.ts +22 -0
  75. package/src/outlet.ts +316 -0
  76. package/src/request.ts +170 -0
  77. package/src/routing.ts +218 -0
  78. package/src/runtime/client.ts +140 -0
  79. package/src/runtime/contract.d.ts +4 -0
  80. package/src/runtime/hydration-dev.ts +18 -0
  81. package/src/runtime/hydration.ts +7 -0
  82. package/src/runtime/island.ts +109 -0
  83. package/src/runtime/server.ts +228 -0
  84. package/src/server-island.ts +67 -0
  85. package/src/state.ts +70 -0
  86. package/src/svg.ts +65 -0
  87. package/src/time.ts +115 -0
  88. 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 }
@@ -0,0 +1,140 @@
1
+ import { ApplicationRef, Component, ElementRef, EnvironmentInjector, Injector, Renderer2, createComponent, effect, inject, input, reflectComponentType, untracked, } from "@angular/core";
2
+ import { linkAttributes, linkDefaultsOf, linkPrefetchProps, markdownLink, markdownProse, markdownView, } from "hamur/adapter";
3
+ import { HAMUR_APP, HAMUR_HREF, detachedHref } from "./context.ts";
4
+ import * as i0 from "@angular/core";
5
+ const camelize = (name) => name.replace(/-(\w)/g, (_, char) => char.toUpperCase());
6
+ const SLOT = /^\[(?:slot=)?["']?([\w-]+)["']?\]$|^([\w-]+)$/;
7
+ /**
8
+ * Renders the Markdown AST as elements: an internal link as the anchor `Link` writes, a component node as its component
9
+ * in `components` (its props as inputs, the others as attributes; its default slot and each named slot projected into the `ng-content`
10
+ * whose selector is the slot's name, as `[name]` or `[slot=name]`). The elements are made with the renderer, so Angular skips
11
+ * the host in hydration and the browser builds them again from the same AST.
12
+ */
13
+ export class MarkdownRenderer {
14
+ value = input.required(/* @ts-ignore */
15
+ ...(ngDevMode ? [{ debugName: "value" }] : /* istanbul ignore next */ []));
16
+ components = input(/* @ts-ignore */
17
+ ...(ngDevMode ? [undefined, { debugName: "components" }] : /* istanbul ignore next */ []));
18
+ raw = input(/* @ts-ignore */
19
+ ...(ngDevMode ? [undefined, { debugName: "raw" }] : /* istanbul ignore next */ []));
20
+ host = inject(ElementRef).nativeElement;
21
+ renderer = inject(Renderer2);
22
+ app = inject(HAMUR_APP);
23
+ resolve = inject(HAMUR_HREF, { optional: true }) ?? detachedHref;
24
+ injector = inject(Injector);
25
+ environment = inject(EnvironmentInjector);
26
+ appRef = inject(ApplicationRef);
27
+ constructor() {
28
+ effect((onCleanup) => {
29
+ const raw = this.raw();
30
+ const prose = markdownProse(this.value(), this.components() ?? {});
31
+ const views = markdownView(prose.value, { html: raw !== undefined, components: prose.components });
32
+ const refs = [];
33
+ untracked(() => {
34
+ for (const view of views)
35
+ this.append(this.host, view, raw, prose.components, refs);
36
+ });
37
+ onCleanup(() => {
38
+ for (const ref of refs)
39
+ ref.destroy();
40
+ while (this.host.firstChild)
41
+ this.renderer.removeChild(this.host, this.host.firstChild);
42
+ });
43
+ });
44
+ }
45
+ append(parent, view, raw, components, refs) {
46
+ const renderer = this.renderer;
47
+ if (typeof view === "string")
48
+ return renderer.appendChild(parent, renderer.createText(view));
49
+ if ("html" in view) {
50
+ const element = renderer.createElement("hamur-html");
51
+ renderer.setStyle(element, "display", "contents");
52
+ renderer.setProperty(element, "innerHTML", raw(view.html));
53
+ return renderer.appendChild(parent, element);
54
+ }
55
+ if ("component" in view)
56
+ return this.appendComponent(parent, view, raw, components, refs);
57
+ const link = markdownLink(view);
58
+ const element = renderer.createElement(view.tag);
59
+ if (link) {
60
+ const defaults = linkDefaultsOf(this.app);
61
+ const attributes = linkAttributes({
62
+ href: this.resolve(link.to),
63
+ external: false,
64
+ ...linkPrefetchProps({}, defaults),
65
+ replace: false,
66
+ pendingClass: defaults.pendingClass,
67
+ trim: false,
68
+ prefetchedClass: defaults.prefetchedClass,
69
+ prefetchDelay: defaults.prefetchDelay,
70
+ externalRelAttribute: defaults.externalRelAttribute,
71
+ });
72
+ for (const [name, value] of Object.entries({ ...link.attrs, ...attributes }))
73
+ renderer.setAttribute(element, name, value === true ? "" : value);
74
+ }
75
+ else
76
+ for (const [name, value] of Object.entries(view.attrs))
77
+ renderer.setAttribute(element, name, value === true ? "" : value);
78
+ for (const child of view.children)
79
+ this.append(element, child, raw, components, refs);
80
+ renderer.appendChild(parent, element);
81
+ }
82
+ appendComponent(parent, view, raw, components, refs) {
83
+ const renderer = this.renderer;
84
+ const component = Object.hasOwn(components, view.component) ? components[view.component] : undefined;
85
+ const nodes = (views) => {
86
+ const holder = renderer.createElement("div");
87
+ for (const child of views)
88
+ this.append(holder, child, raw, components, refs);
89
+ return Array.from(holder.childNodes);
90
+ };
91
+ if (!component) {
92
+ if (!view.attrs) {
93
+ for (const child of view.children)
94
+ this.append(parent, child, raw, components, refs);
95
+ return;
96
+ }
97
+ const element = renderer.createElement(view.component);
98
+ for (const [name, value] of Object.entries(view.attrs))
99
+ renderer.setAttribute(element, name, value === true ? "" : value);
100
+ for (const child of view.children)
101
+ this.append(element, child, raw, components, refs);
102
+ return renderer.appendChild(parent, element);
103
+ }
104
+ const mirror = reflectComponentType(component);
105
+ const declared = new Set(mirror?.inputs.map((input) => input.templateName));
106
+ const entries = Object.entries(view.props);
107
+ const [element] = markdownView([
108
+ "span",
109
+ Object.fromEntries(entries.filter(([name]) => !declared.has(camelize(name)))),
110
+ ]);
111
+ const slots = new Map();
112
+ for (const [name, views] of Object.entries(view.slots))
113
+ slots.set(name, nodes(views));
114
+ const content = nodes(view.children);
115
+ const ref = createComponent(component, {
116
+ environmentInjector: this.environment,
117
+ elementInjector: this.injector,
118
+ projectableNodes: (mirror?.ngContentSelectors ?? []).map((selector) => {
119
+ const named = SLOT.exec(selector);
120
+ const name = named?.[1] ?? named?.[2];
121
+ return selector === "*" ? content : name !== undefined ? (slots.get(name) ?? []) : [];
122
+ }),
123
+ });
124
+ for (const [name, value] of entries)
125
+ if (declared.has(camelize(name)))
126
+ ref.setInput(camelize(name), value);
127
+ const host = ref.location.nativeElement;
128
+ for (const [name, value] of Object.entries(element?.attrs ?? {}))
129
+ renderer.setAttribute(host, name, value === true ? "" : value);
130
+ this.appRef.attachView(ref.hostView);
131
+ refs.push(ref);
132
+ renderer.appendChild(parent, host);
133
+ }
134
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: MarkdownRenderer, deps: [], target: i0.ɵɵFactoryTarget.Component });
135
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "22.3.2", type: MarkdownRenderer, isStandalone: true, selector: "hamur-markdown", inputs: { value: { classPropertyName: "value", publicName: "value", isSignal: true, isRequired: true, transformFunction: null }, components: { classPropertyName: "components", publicName: "components", isSignal: true, isRequired: false, transformFunction: null }, raw: { classPropertyName: "raw", publicName: "raw", isSignal: true, isRequired: false, transformFunction: null } }, host: { attributes: { "ngSkipHydration": "true" } }, ngImport: i0, template: "", isInline: true });
136
+ }
137
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: MarkdownRenderer, decorators: [{
138
+ type: Component,
139
+ args: [{ selector: "hamur-markdown", template: "", host: { ngSkipHydration: "true" } }]
140
+ }], ctorParameters: () => [], propDecorators: { value: [{ type: i0.Input, args: [{ isSignal: true, alias: "value", required: true }] }], components: [{ type: i0.Input, args: [{ isSignal: true, alias: "components", required: false }] }], raw: [{ type: i0.Input, args: [{ isSignal: true, alias: "raw", required: false }] }] } });
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
+ }
@@ -0,0 +1,22 @@
1
+ import { Component, afterNextRender, booleanAttribute, input } from "@angular/core";
2
+ import { navigateTo, redirect } from "hamur/app";
3
+ import * as i0 from "@angular/core";
4
+ /** Navigates to `to` once after mount; during SSR it throws the redirect `navigateTo` throws there. */
5
+ export class Navigate {
6
+ to = input.required(/* @ts-ignore */
7
+ ...(ngDevMode ? [{ debugName: "to" }] : /* istanbul ignore next */ []));
8
+ replace = input(false, { ...(ngDevMode ? { debugName: "replace" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
9
+ constructor() {
10
+ afterNextRender(() => void navigateTo(this.to(), { replace: this.replace() }));
11
+ }
12
+ ngOnInit() {
13
+ if (typeof window === "undefined")
14
+ throw redirect(this.to());
15
+ }
16
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Navigate, deps: [], target: i0.ɵɵFactoryTarget.Component });
17
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "22.3.2", type: Navigate, isStandalone: true, selector: "hamur-navigate", inputs: { to: { classPropertyName: "to", publicName: "to", isSignal: true, isRequired: true, transformFunction: null }, replace: { classPropertyName: "replace", publicName: "replace", isSignal: true, isRequired: false, transformFunction: null } }, ngImport: i0, template: "", isInline: true });
18
+ }
19
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Navigate, decorators: [{
20
+ type: Component,
21
+ args: [{ selector: "hamur-navigate", template: "" }]
22
+ }], ctorParameters: () => [], propDecorators: { to: [{ type: i0.Input, args: [{ isSignal: true, alias: "to", required: true }] }], replace: [{ type: i0.Input, args: [{ isSignal: true, alias: "replace", required: false }] }] } });
package/src/outlet.ts ADDED
@@ -0,0 +1,316 @@
1
+ import { Component, DestroyRef, InjectionToken, Injector, PendingTasks, ViewContainerRef, computed, effect, inject, input, reflectComponentType, untracked, } from "@angular/core";
2
+ import { noteOutlet, routeBoundary } from "hamur/adapter";
3
+ import { HAMUR_ACTIVATION, activation, detach, setHidden } from "./activation.ts";
4
+ import { HAMUR_APP, HAMUR_CAUGHT, HAMUR_CONTEXT, HAMUR_FAIL, HAMUR_LEVEL, HAMUR_LEVEL_DISPOSE, HAMUR_OUTLET_CONTEXT, HAMUR_PAYLOAD, HAMUR_ROUTE, HAMUR_TREE, mode, routeValue, } from "./context.ts";
5
+ import * as i0 from "@angular/core";
6
+ const EMPTY = { components: [] };
7
+ const same = (a, b) => a.id === b.id && a.key === b.key && a.component === b.component;
8
+ /** `onError` gets a thrown value that is not an `Error` as the `cause` of one. */
9
+ const unwrap = (error) => error instanceof Error && error.name === "ErrorBoundaryWrappedError" ? error.cause : error;
10
+ /**
11
+ * Places the next level of the tree as its next sibling; the `hamur-outlet` element stays empty. With `name` it places
12
+ * the chain of that slot of the entry it sits in; `context` reaches the entries below through `injectOutletContext()`.
13
+ */
14
+ export class Outlet {
15
+ name = input(/* @ts-ignore */
16
+ ...(ngDevMode ? [undefined, { debugName: "name" }] : /* istanbul ignore next */ []));
17
+ context = input(/* @ts-ignore */
18
+ ...(ngDevMode ? [undefined, { debugName: "context" }] : /* istanbul ignore next */ []));
19
+ tree = inject(HAMUR_TREE);
20
+ fail = inject(HAMUR_FAIL);
21
+ app = inject(HAMUR_APP);
22
+ payload = inject(HAMUR_PAYLOAD);
23
+ route = inject(HAMUR_ROUTE);
24
+ above = inject(HAMUR_OUTLET_CONTEXT, { optional: true });
25
+ owner = inject(HAMUR_LEVEL, { optional: true }) ?? -1;
26
+ injector = inject(Injector);
27
+ container = inject(ViewContainerRef);
28
+ pageContext = inject(HAMUR_CONTEXT);
29
+ tasks = inject(PendingTasks);
30
+ warned = new Set();
31
+ shown;
32
+ /** The keep-alive wrappers this outlet holds, by keep-alive key. */
33
+ pages = new Map();
34
+ started = false;
35
+ /** Counts what the level shows, so an answer that waits for a microtask finds out the level moved on. */
36
+ shownCount = 0;
37
+ /** The tree this outlet reads and the index of its entry in it. */
38
+ view = computed(() => {
39
+ const name = this.name();
40
+ const tree = this.tree();
41
+ if (name === undefined)
42
+ return { tree, index: this.owner + 1, slot: false };
43
+ return { tree: tree.components[this.owner]?.outlets?.[name], index: 0, slot: true };
44
+ }, /* @ts-ignore */
45
+ ...(ngDevMode ? [{ debugName: "view" }] : /* istanbul ignore next */ []));
46
+ value = computed(() => {
47
+ const own = this.context();
48
+ return own !== undefined ? own : this.above?.();
49
+ }, /* @ts-ignore */
50
+ ...(ngDevMode ? [{ debugName: "value" }] : /* istanbul ignore next */ []));
51
+ slotTree = computed(() => this.view().tree ?? EMPTY, /* @ts-ignore */
52
+ ...(ngDevMode ? [{ debugName: "slotTree" }] : /* istanbul ignore next */ []));
53
+ slotRoute = computed(() => {
54
+ const slot = this.view().tree;
55
+ if (!slot?.route)
56
+ return undefined;
57
+ const { state, key, navigationType } = this.route();
58
+ return routeValue({
59
+ route: { ...slot.route, state, key, navigationType },
60
+ tree: slot,
61
+ app: this.app,
62
+ payload: this.payload(),
63
+ });
64
+ }, /* @ts-ignore */
65
+ ...(ngDevMode ? [{ debugName: "slotRoute" }] : /* istanbul ignore next */ []));
66
+ constructor() {
67
+ inject(DestroyRef).onDestroy(() => {
68
+ this.shownCount++;
69
+ for (const dispose of this.shown?.disposers ?? [])
70
+ dispose();
71
+ });
72
+ effect(() => {
73
+ const entry = this.entry();
74
+ const units = this.units();
75
+ if (this.started)
76
+ untracked(() => this.apply(entry, units));
77
+ });
78
+ }
79
+ ngOnInit() {
80
+ this.started = true;
81
+ this.apply(this.entry(), this.units());
82
+ }
83
+ /** The kept units of this outlet; `undefined` while the app has keep-alive off, so the outlet keeps one shape. */
84
+ units() {
85
+ if (this.name() !== undefined)
86
+ return undefined;
87
+ const tree = this.tree();
88
+ return (this.owner >= 0 ? tree.components[this.owner] : tree)?.kept;
89
+ }
90
+ /** Each kept unit hidden in its wrapper, then the current child, in the wrapper of its key when its route keeps its pages. */
91
+ apply(entry, units) {
92
+ if (units === undefined) {
93
+ this.dropPages();
94
+ this.show(entry);
95
+ return;
96
+ }
97
+ const wanted = new Set();
98
+ let index = 0;
99
+ for (const unit of units) {
100
+ this.page(unit.key, unit, index++);
101
+ wanted.add(unit.key);
102
+ }
103
+ if (entry?.keepalive !== undefined) {
104
+ this.show(undefined);
105
+ this.page(entry.keepalive, undefined, index);
106
+ wanted.add(entry.keepalive);
107
+ }
108
+ else
109
+ this.show(entry);
110
+ for (const [key, ref] of this.pages)
111
+ if (!wanted.has(key)) {
112
+ ref.destroy();
113
+ this.pages.delete(key);
114
+ }
115
+ }
116
+ page(key, unit, index) {
117
+ let ref = this.pages.get(key);
118
+ if (!ref) {
119
+ const injector = Injector.create({
120
+ providers: [
121
+ {
122
+ provide: KEPT_SOURCE,
123
+ useValue: { tree: this.tree, level: this.owner, route: this.route, payload: this.payload },
124
+ },
125
+ ],
126
+ parent: this.injector,
127
+ });
128
+ ref = this.container.createComponent(KeptPage, { injector, index });
129
+ this.pages.set(key, ref);
130
+ }
131
+ ref.setInput("name", key);
132
+ ref.setInput("unit", unit);
133
+ ref.setInput("hidden", unit !== undefined);
134
+ if (this.container.indexOf(ref.hostView) !== index)
135
+ this.container.move(ref.hostView, index);
136
+ }
137
+ dropPages() {
138
+ for (const ref of this.pages.values())
139
+ ref.destroy();
140
+ this.pages.clear();
141
+ }
142
+ dropShown() {
143
+ const shown = this.shown;
144
+ this.shown = undefined;
145
+ if (!shown)
146
+ return;
147
+ shown.ref.destroy();
148
+ for (const dispose of shown.disposers)
149
+ dispose();
150
+ }
151
+ entry() {
152
+ const { tree, index, slot } = this.view();
153
+ const name = this.name();
154
+ if (mode.dev) {
155
+ if (!slot)
156
+ noteOutlet(this.tree().components[this.owner]?.id);
157
+ else if (!tree && name !== undefined && !this.warned.has(name)) {
158
+ this.warned.add(name);
159
+ console.warn(`Outlet name "${name}" has no slot in ${this.tree().components[this.owner]?.id}`);
160
+ }
161
+ }
162
+ return tree?.components[index];
163
+ }
164
+ show(entry) {
165
+ const shown = this.shown;
166
+ if (shown && entry && same(shown.entry, entry)) {
167
+ shown.entry = entry;
168
+ this.setInputs(shown, entry);
169
+ return;
170
+ }
171
+ this.shownCount++;
172
+ this.dropShown();
173
+ if (entry)
174
+ this.place(entry);
175
+ }
176
+ place(entry) {
177
+ const component = entry.component;
178
+ const disposers = new Set();
179
+ const providers = [
180
+ { provide: HAMUR_LEVEL, useValue: this.view().index },
181
+ { provide: HAMUR_LEVEL_DISPOSE, useValue: (dispose) => disposers.add(dispose) },
182
+ { provide: HAMUR_OUTLET_CONTEXT, useValue: this.value },
183
+ ];
184
+ if (entry.caught)
185
+ providers.push({ provide: HAMUR_CAUGHT, useValue: entry.props.error });
186
+ if (this.view().slot) {
187
+ providers.push({ provide: HAMUR_TREE, useValue: this.slotTree });
188
+ providers.push({ provide: HAMUR_ROUTE, useFactory: () => computed(() => this.slotRoute() ?? this.route()) });
189
+ }
190
+ const injector = Injector.create({ providers, parent: this.injector });
191
+ const failed = (error) => this.failed(entry, unwrap(error));
192
+ try {
193
+ const ref = this.container.createComponent(component, { injector, onError: failed });
194
+ const inputs = new Set(reflectComponentType(component)?.inputs.map((input) => input.templateName) ?? []);
195
+ this.shown = { entry, ref, inputs, disposers };
196
+ this.setInputs(this.shown, entry);
197
+ }
198
+ catch (error) {
199
+ failed(error);
200
+ }
201
+ }
202
+ /**
203
+ * A throw of the entry this level creates, in its constructor or its checks. The route boundary covering the entry
204
+ * 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.
205
+ * A route boundary's own throw skips every boundary above it.
206
+ */
207
+ failed(entry, error) {
208
+ if (entry.caught)
209
+ return this.fail(error);
210
+ const caught = entry.boundary ? routeBoundary(this.pageContext(), entry, error) : undefined;
211
+ if (caught)
212
+ return this.answer(caught);
213
+ if (this.owner >= 0)
214
+ throw error;
215
+ this.fail(error);
216
+ }
217
+ /** The boundary component takes the entry's place once the check that threw is over: Angular is still walking the view. */
218
+ answer(caught) {
219
+ const count = this.shownCount;
220
+ const done = this.tasks.add();
221
+ queueMicrotask(() => {
222
+ try {
223
+ if (count !== this.shownCount)
224
+ return;
225
+ this.shownCount++;
226
+ this.dropShown();
227
+ this.place({
228
+ id: caught.id,
229
+ component: caught.component,
230
+ props: { error: caught.error, clearError: (options) => this.clear(options) },
231
+ caught: caught.caught,
232
+ });
233
+ }
234
+ finally {
235
+ done();
236
+ }
237
+ });
238
+ }
239
+ clear(options) {
240
+ return this.app.error?.clear(options) ?? Promise.resolve();
241
+ }
242
+ setInputs({ ref, inputs }, entry) {
243
+ for (const [name, value] of Object.entries(entry.props ?? {}))
244
+ if (inputs.has(name))
245
+ ref.setInput(name, value);
246
+ }
247
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Outlet, deps: [], target: i0.ɵɵFactoryTarget.Component });
248
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "22.3.2", type: Outlet, isStandalone: true, selector: "hamur-outlet", inputs: { name: { classPropertyName: "name", publicName: "name", isSignal: true, isRequired: false, transformFunction: null }, context: { classPropertyName: "context", publicName: "context", isSignal: true, isRequired: false, transformFunction: null } }, ngImport: i0, template: "", isInline: true });
249
+ }
250
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Outlet, decorators: [{
251
+ type: Component,
252
+ args: [{ selector: "hamur-outlet", template: "" }]
253
+ }], ctorParameters: () => [], propDecorators: { name: [{ type: i0.Input, args: [{ isSignal: true, alias: "name", required: false }] }], context: [{ type: i0.Input, args: [{ isSignal: true, alias: "context", required: false }] }] } });
254
+ const KEPT_SOURCE = new InjectionToken("hamur kept source");
255
+ const keptRoutes = new WeakMap();
256
+ /**
257
+ * The `<div data-hamur-keepalive="<key>">` of a page whose route keeps its pages, `display:contents` while it is shown
258
+ * 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.
259
+ */
260
+ export class KeptPage {
261
+ name = input.required(/* @ts-ignore */
262
+ ...(ngDevMode ? [{ debugName: "name" }] : /* istanbul ignore next */ []));
263
+ unit = input(/* @ts-ignore */
264
+ ...(ngDevMode ? [undefined, { debugName: "unit" }] : /* istanbul ignore next */ []));
265
+ hidden = input(false, /* @ts-ignore */
266
+ ...(ngDevMode ? [{ debugName: "hidden" }] : /* istanbul ignore next */ []));
267
+ source = inject(KEPT_SOURCE);
268
+ app = inject(HAMUR_APP);
269
+ activation = activation(inject(HAMUR_ACTIVATION, { skipSelf: true, optional: true }) ?? undefined, false);
270
+ tree = computed(() => this.unit()?.tree ?? { components: this.source.tree().components.slice(this.source.level + 1) }, /* @ts-ignore */
271
+ ...(ngDevMode ? [{ debugName: "tree" }] : /* istanbul ignore next */ []));
272
+ route = computed(() => {
273
+ const unit = this.unit();
274
+ if (!unit)
275
+ return this.source.route();
276
+ let value = keptRoutes.get(unit);
277
+ if (!value)
278
+ keptRoutes.set(unit, (value = routeValue({ route: unit.route, app: this.app, payload: unit.payload, tree: unit.tree })));
279
+ return value;
280
+ }, /* @ts-ignore */
281
+ ...(ngDevMode ? [{ debugName: "route" }] : /* istanbul ignore next */ []));
282
+ payload = computed(() => this.unit()?.payload ?? this.source.payload(), /* @ts-ignore */
283
+ ...(ngDevMode ? [{ debugName: "payload" }] : /* istanbul ignore next */ []));
284
+ constructor() {
285
+ effect(() => setHidden(this.activation, this.hidden()));
286
+ inject(DestroyRef).onDestroy(() => detach(this.activation));
287
+ }
288
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: KeptPage, deps: [], target: i0.ɵɵFactoryTarget.Component });
289
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "22.3.2", type: KeptPage, isStandalone: true, selector: "div[data-hamur-keepalive]", inputs: { name: { classPropertyName: "name", publicName: "name", isSignal: true, isRequired: true, transformFunction: null }, unit: { classPropertyName: "unit", publicName: "unit", isSignal: true, isRequired: false, transformFunction: null }, hidden: { classPropertyName: "hidden", publicName: "hidden", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "attr.data-hamur-keepalive": "name()", "attr.hidden": "hidden() ? '' : null", "style.display": "hidden() ? null : 'contents'" } }, providers: [
290
+ { provide: HAMUR_LEVEL, useValue: -1 },
291
+ { provide: HAMUR_TREE, useFactory: () => inject(KeptPage).tree },
292
+ { provide: HAMUR_ROUTE, useFactory: () => inject(KeptPage).route },
293
+ { provide: HAMUR_PAYLOAD, useFactory: () => inject(KeptPage).payload },
294
+ { provide: HAMUR_ACTIVATION, useFactory: () => inject(KeptPage).activation },
295
+ ], ngImport: i0, template: "<hamur-outlet />", isInline: true, dependencies: [{ kind: "component", type: Outlet, selector: "hamur-outlet", inputs: ["name", "context"] }] });
296
+ }
297
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: KeptPage, decorators: [{
298
+ type: Component,
299
+ args: [{
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
+ }], ctorParameters: () => [], propDecorators: { name: [{ type: i0.Input, args: [{ isSignal: true, alias: "name", required: true }] }], unit: [{ type: i0.Input, args: [{ isSignal: true, alias: "unit", required: false }] }], hidden: [{ type: i0.Input, args: [{ isSignal: true, alias: "hidden", required: false }] }] } });