@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.
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 +1073 -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 +3 -0
  16. package/dist/types/src/async-data.d.ts +21 -0
  17. package/dist/types/src/client-parts.d.ts +32 -0
  18. package/dist/types/src/compile.d.ts +97 -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 +5 -0
  23. package/dist/types/src/error.d.ts +4 -0
  24. package/dist/types/src/form.d.ts +65 -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 +69 -0
  30. package/dist/types/src/island.d.ts +39 -0
  31. package/dist/types/src/keepalive.d.ts +1 -0
  32. package/dist/types/src/link.d.ts +65 -0
  33. package/dist/types/src/markdown-page.d.ts +10 -0
  34. package/dist/types/src/markdown.d.ts +37 -0
  35. package/dist/types/src/mask.d.ts +12 -0
  36. package/dist/types/src/navigate.d.ts +14 -0
  37. package/dist/types/src/outlet.d.ts +71 -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 +21 -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 +37 -0
  49. package/dist/types/src/wrapper.d.ts +21 -0
  50. package/package.json +56 -4
  51. package/src/activation.ts +83 -0
  52. package/src/adapter.ts +107 -0
  53. package/src/app.ts +6 -0
  54. package/src/async-data.ts +105 -0
  55. package/src/client-parts.ts +127 -0
  56. package/src/compile.ts +959 -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 +17 -0
  61. package/src/error.ts +15 -0
  62. package/src/form.ts +253 -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 +251 -0
  68. package/src/island.ts +95 -0
  69. package/src/keepalive.ts +1 -0
  70. package/src/link.ts +251 -0
  71. package/src/markdown-page.ts +31 -0
  72. package/src/markdown.ts +170 -0
  73. package/src/mask.ts +36 -0
  74. package/src/navigate.ts +19 -0
  75. package/src/outlet.ts +344 -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 +54 -0
  85. package/src/state.ts +70 -0
  86. package/src/svg.ts +65 -0
  87. package/src/time.ts +108 -0
  88. package/src/wrapper.ts +187 -0
package/src/wrapper.ts ADDED
@@ -0,0 +1,187 @@
1
+ import {
2
+ ApplicationRef,
3
+ DOCUMENT,
4
+ DestroyRef,
5
+ ElementRef,
6
+ EnvironmentInjector,
7
+ Injector,
8
+ afterNextRender,
9
+ createComponent,
10
+ inject,
11
+ type ComponentRef,
12
+ type Type,
13
+ } from "@angular/core"
14
+ import {
15
+ fetchServerComponent,
16
+ serverComponentAttributes,
17
+ serverComponentProps,
18
+ setServerHTML,
19
+ takeServerHTML,
20
+ } from "hamur/adapter"
21
+ import type { ServerComponentView } from "hamur/adapter"
22
+ import { HAMUR_IDS, injectId } from "./client-parts.ts"
23
+ import { HAMUR_APP, required } from "./context.ts"
24
+
25
+ /** The state of one wrapper instance: the input values Angular wrote, and what to do when one changes. Its fields start with `$`, so no input of the real component shares a name. */
26
+ interface Instance {
27
+ readonly $values: Record<string, unknown>
28
+ readonly $host: HTMLElement
29
+ readonly $changed: Set<(name: string) => void>
30
+ }
31
+
32
+ /** Runs in the constructor, an injection context; what it returns runs once the inputs of the first binding are set (`ngOnInit`). */
33
+ type Behaviour = (instance: Instance) => (() => void) | void
34
+
35
+ /** What a generated wrapper component calls: its inputs as they change, and once the first ones are set. */
36
+ export interface Controller {
37
+ set(name: string, value: unknown): void
38
+ init(): void
39
+ }
40
+
41
+ /**
42
+ * The behaviour of a generated wrapper: a component the app's program compiled with the selector and the inputs of the
43
+ * real one and no template, so its own view has no nodes and what the behaviour adds below its host is its own.
44
+ * The wrapper creates it in a field initializer, an injection context.
45
+ */
46
+ function controller(behaviour: Behaviour): Controller {
47
+ const instance: Instance = {
48
+ $values: {},
49
+ $host: inject<ElementRef<HTMLElement>>(ElementRef).nativeElement,
50
+ $changed: new Set(),
51
+ }
52
+ const init = behaviour(instance)
53
+ return {
54
+ set(name, value) {
55
+ instance.$values[name] = value
56
+ for (const listener of [...instance.$changed]) listener(name)
57
+ },
58
+ init() {
59
+ if (typeof init === "function") init()
60
+ },
61
+ }
62
+ }
63
+
64
+ type Loaded = Type<unknown> | { default: Type<unknown> }
65
+
66
+ const nothing: Behaviour = () => {}
67
+
68
+ /** The server side of a client-only component renders nothing. */
69
+ export function clientComponentOnServer(): Controller {
70
+ return controller(nothing)
71
+ }
72
+
73
+ /**
74
+ * The client side of a client-only component: nothing in the hydration render, the file's chunk created inside the host once
75
+ * the first render is done, and at once after a client navigation. Its inputs are the real component's, set as they change.
76
+ */
77
+ export function clientComponentOnClient(load: () => Promise<Loaded>): Controller {
78
+ return controller((instance) => {
79
+ const environment = inject(EnvironmentInjector)
80
+ const injector = inject(Injector)
81
+ const appRef = inject(ApplicationRef)
82
+ let ref: ComponentRef<unknown> | undefined
83
+ let destroyed = false
84
+ instance.$changed.add((name) => ref?.setInput(name, instance.$values[name]))
85
+ inject(DestroyRef).onDestroy(() => {
86
+ destroyed = true
87
+ ref?.destroy()
88
+ })
89
+ afterNextRender(() => {
90
+ void load().then((loaded) => {
91
+ if (destroyed) return
92
+ const type = "default" in loaded ? loaded.default : loaded
93
+ ref = createComponent(type, { environmentInjector: environment, elementInjector: injector })
94
+ for (const [name, value] of Object.entries(instance.$values)) ref.setInput(name, value)
95
+ appRef.attachView(ref.hostView)
96
+ instance.$host.appendChild(ref.location.nativeElement)
97
+ })
98
+ })
99
+ })
100
+ }
101
+
102
+ /** The server side of a server-only component: the component itself inside `hamur-component`, its props checked as the client sends them. */
103
+ export function serverComponentOnServer(name: string, component: Type<unknown>): Controller {
104
+ return controller((instance) => {
105
+ const uid = injectId()
106
+ const document = inject(DOCUMENT)
107
+ const environment = inject(EnvironmentInjector)
108
+ const parent = inject(Injector)
109
+ const appRef = inject(ApplicationRef)
110
+ let ref: ComponentRef<unknown> | undefined
111
+ inject(DestroyRef).onDestroy(() => ref?.destroy())
112
+ return () => {
113
+ serverComponentProps(name, { ...instance.$values })
114
+ const element = document.createElement("hamur-component")
115
+ element.setAttribute("name", name)
116
+ element.setAttribute("uid", uid)
117
+ for (const [key, value] of Object.entries(serverComponentAttributes(name))) element.setAttribute(key, value)
118
+ const injector = Injector.create({
119
+ providers: [{ provide: HAMUR_IDS, useValue: { next: 0, prefix: `${uid}-` } }],
120
+ parent,
121
+ })
122
+ ref = createComponent(component, { environmentInjector: environment, elementInjector: injector })
123
+ for (const [key, value] of Object.entries(instance.$values)) ref.setInput(key, value)
124
+ appRef.attachView(ref.hostView)
125
+ element.appendChild(ref.location.nativeElement)
126
+ instance.$host.appendChild(element)
127
+ }
128
+ })
129
+ }
130
+
131
+ /** The client side of a server-only component: the server HTML, never hydrated; a new props text fetches it anew. */
132
+ export function serverComponentOnClient(name: string): Controller {
133
+ return controller((instance) => {
134
+ const app = required(HAMUR_APP, "serverComponent")
135
+ const uid = injectId()
136
+ const document = inject(DOCUMENT)
137
+ const textOf = () => serverComponentProps(name, { ...instance.$values })
138
+ let controller: AbortController | undefined
139
+ let head: { dispose(): void } | undefined
140
+ let requested: string | undefined
141
+ let mounted = false
142
+ const initial = takeServerHTML(app, uid, name)
143
+ let element = instance.$host.querySelector<HTMLElement>(`:scope > hamur-component[uid="${uid}"]`)
144
+ if (!element) {
145
+ element = document.createElement("hamur-component")
146
+ element.setAttribute("name", name)
147
+ element.setAttribute("uid", uid)
148
+ for (const [key, value] of Object.entries(serverComponentAttributes(name))) element.setAttribute(key, value)
149
+ if (initial !== undefined) setServerHTML(element, initial)
150
+ instance.$host.appendChild(element)
151
+ }
152
+ const shown = element
153
+ const sync = () => {
154
+ const text = textOf()
155
+ if (text === requested) return
156
+ requested = text
157
+ controller?.abort()
158
+ const current = (controller = new AbortController())
159
+ fetchServerComponent(app, name, text, current.signal)
160
+ .then((view: ServerComponentView) => {
161
+ if (current.signal.aborted) return
162
+ head?.dispose()
163
+ head = view.head ? app.head.push(view.head) : undefined
164
+ setServerHTML(shown, view.html)
165
+ app.head.apply()
166
+ })
167
+ .catch((error: unknown) => {
168
+ if (current.signal.aborted) return
169
+ queueMicrotask(() => {
170
+ throw error
171
+ })
172
+ })
173
+ }
174
+ afterNextRender(() => {
175
+ mounted = true
176
+ requested = initial === undefined ? undefined : textOf()
177
+ sync()
178
+ })
179
+ instance.$changed.add(() => {
180
+ if (mounted) queueMicrotask(sync)
181
+ })
182
+ inject(DestroyRef).onDestroy(() => {
183
+ controller?.abort()
184
+ head?.dispose()
185
+ })
186
+ })
187
+ }