brumate 0.2.0 → 0.4.0

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 (138) hide show
  1. package/README.md +2 -2
  2. package/check.d.ts +49 -0
  3. package/dist/{boot-D-IdPy7B.d.ts → boot-DLlRmD9z.d.ts} +11 -10
  4. package/dist/cli.js +275 -15
  5. package/dist/cli.js.map +1 -1
  6. package/dist/client-BvDsC6Qr.js +3463 -0
  7. package/dist/client-BvDsC6Qr.js.map +1 -0
  8. package/dist/client-DBwL2wKQ.d.ts +669 -0
  9. package/dist/{client-routes-JTbGtukf.js → client-routes-BtGpYCef.js} +60 -30
  10. package/dist/client-routes-BtGpYCef.js.map +1 -0
  11. package/dist/{cookies-Av5WFmcf.js → cookies-Cnbf4w-S.js} +15 -7
  12. package/dist/cookies-Cnbf4w-S.js.map +1 -0
  13. package/dist/{defer-D0bo1Gqx.js → defer-CKWZLv2K.js} +8 -4
  14. package/dist/defer-CKWZLv2K.js.map +1 -0
  15. package/dist/devtools-4uTPwR_v.js +597 -0
  16. package/dist/devtools-4uTPwR_v.js.map +1 -0
  17. package/dist/devtools-panel-Be9pxYgv.js +1994 -0
  18. package/dist/devtools-panel-Be9pxYgv.js.map +1 -0
  19. package/dist/events-LgLdp1R7.js +114 -0
  20. package/dist/events-LgLdp1R7.js.map +1 -0
  21. package/dist/features-CHY5Ekku.js +30 -0
  22. package/dist/features-CHY5Ekku.js.map +1 -0
  23. package/dist/files-WW4RlajL.js +506 -0
  24. package/dist/files-WW4RlajL.js.map +1 -0
  25. package/dist/forms-j1xPAR14.d.ts +281 -0
  26. package/dist/forms-nXK7-Lv_.js +440 -0
  27. package/dist/forms-nXK7-Lv_.js.map +1 -0
  28. package/dist/highlight.d.ts +17 -0
  29. package/dist/highlight.js +278 -0
  30. package/dist/highlight.js.map +1 -0
  31. package/dist/html-BtKRyi7g.js +305 -0
  32. package/dist/html-BtKRyi7g.js.map +1 -0
  33. package/dist/html-Bw9Qwu6F.d.ts +107 -0
  34. package/dist/index.d.ts +142 -11
  35. package/dist/index.js +641 -106
  36. package/dist/index.js.map +1 -1
  37. package/dist/{inspect-BJtACnQp.js → inspect-Y6y4uNZp.js} +4333 -681
  38. package/dist/inspect-Y6y4uNZp.js.map +1 -0
  39. package/dist/{loader-B18i3_-T.js → loader-BjKGJzLN.js} +21 -14
  40. package/dist/loader-BjKGJzLN.js.map +1 -0
  41. package/dist/logo-DQ_OYhBi.js +10 -0
  42. package/dist/logo-DQ_OYhBi.js.map +1 -0
  43. package/dist/{navigate-BwuZveDF.js → navigate-CfOr-TRF.js} +130 -24
  44. package/dist/navigate-CfOr-TRF.js.map +1 -0
  45. package/dist/nesting-DA5vJw9X.js +265 -0
  46. package/dist/nesting-DA5vJw9X.js.map +1 -0
  47. package/dist/produce-B7X9ori9.js +402 -0
  48. package/dist/produce-B7X9ori9.js.map +1 -0
  49. package/dist/produce-DIgHBVwL.d.ts +148 -0
  50. package/dist/{rpc-B-Zkn6Ch.js → rpc-Q-XkLwFh.js} +28 -13
  51. package/dist/rpc-Q-XkLwFh.js.map +1 -0
  52. package/dist/runtime/batch.js +9 -7
  53. package/dist/runtime/batch.js.map +1 -1
  54. package/dist/runtime/client.d.ts +4 -4
  55. package/dist/runtime/client.js +4 -4
  56. package/dist/runtime/devtools.d.ts +172 -5
  57. package/dist/runtime/devtools.js +3 -1
  58. package/dist/runtime/env.js +14 -2
  59. package/dist/runtime/env.js.map +1 -1
  60. package/dist/runtime/events.d.ts +38 -1
  61. package/dist/runtime/events.js +1 -1
  62. package/dist/runtime/forms.d.ts +2 -0
  63. package/dist/runtime/forms.js +1 -0
  64. package/dist/runtime/gather.d.ts +25 -0
  65. package/dist/runtime/gather.js +33 -0
  66. package/dist/runtime/gather.js.map +1 -0
  67. package/dist/runtime/hot.d.ts +1 -1
  68. package/dist/runtime/hot.js +5 -2
  69. package/dist/runtime/hot.js.map +1 -1
  70. package/dist/runtime/html.d.ts +2 -2
  71. package/dist/runtime/html.js +1 -1
  72. package/dist/runtime/loader.d.ts +2 -2
  73. package/dist/runtime/loader.js +2 -2
  74. package/dist/runtime/routes.js +4 -1
  75. package/dist/runtime/routes.js.map +1 -1
  76. package/dist/runtime/server.d.ts +5 -4
  77. package/dist/runtime/server.js +5 -5
  78. package/dist/runtime/signals.d.ts +75 -2
  79. package/dist/runtime/signals.js +44 -2
  80. package/dist/runtime/signals.js.map +1 -1
  81. package/dist/runtime/static-loader.d.ts +2 -2
  82. package/dist/runtime/static-loader.js +1 -1
  83. package/dist/runtime/testing.d.ts +18 -3
  84. package/dist/runtime/testing.js +150 -33
  85. package/dist/runtime/testing.js.map +1 -1
  86. package/dist/runtime/values.d.ts +2 -0
  87. package/dist/runtime/values.js +24 -0
  88. package/dist/runtime/values.js.map +1 -0
  89. package/dist/{server-BA9SJ2Vm.d.ts → server-BvPIg9pT.d.ts} +363 -32
  90. package/dist/{server-BqfM95r1.js → server-DjyOVJtJ.js} +1309 -220
  91. package/dist/server-DjyOVJtJ.js.map +1 -0
  92. package/dist/{static-loader-BzE9SRaj.d.ts → static-loader-BGgdp_Y5.d.ts} +2 -2
  93. package/dist/static-loader-BZR17hSw.js +312 -0
  94. package/dist/static-loader-BZR17hSw.js.map +1 -0
  95. package/dist/{stream-JaMHMdBD.js → stream-BP5AVmg5.js} +182 -17
  96. package/dist/stream-BP5AVmg5.js.map +1 -0
  97. package/dist/values-B8C7CLvG.d.ts +63 -0
  98. package/dist/vite.d.ts +20 -20
  99. package/dist/vite.js +2031 -539
  100. package/dist/vite.js.map +1 -1
  101. package/elements.d.ts +951 -0
  102. package/forms/forms.brm.ts +141 -0
  103. package/package.json +41 -3
  104. package/skills/brumate/SKILL.md +58 -0
  105. package/skills/brumate/references/errors.md +2078 -0
  106. package/skills/brumate/references/events.md +117 -0
  107. package/skills/brumate/references/signals.md +61 -0
  108. package/skills/brumate/references/state.md +235 -0
  109. package/skills/brumate/references/syntax.md +250 -0
  110. package/types.d.ts +4 -4
  111. package/dist/client-C0UCNL3J.js +0 -1643
  112. package/dist/client-C0UCNL3J.js.map +0 -1
  113. package/dist/client-MD0ZmcFR.d.ts +0 -354
  114. package/dist/client-routes-JTbGtukf.js.map +0 -1
  115. package/dist/cookies-Av5WFmcf.js.map +0 -1
  116. package/dist/defer-D0bo1Gqx.js.map +0 -1
  117. package/dist/devtools-C50VH8gZ.js +0 -113
  118. package/dist/devtools-C50VH8gZ.js.map +0 -1
  119. package/dist/devtools-panel-AYjfwPhj.js +0 -435
  120. package/dist/devtools-panel-AYjfwPhj.js.map +0 -1
  121. package/dist/events-BKWRqtMg.js +0 -52
  122. package/dist/events-BKWRqtMg.js.map +0 -1
  123. package/dist/files-Bnn1JqHx.js +0 -192
  124. package/dist/files-Bnn1JqHx.js.map +0 -1
  125. package/dist/html-bHZbkK-h.js +0 -170
  126. package/dist/html-bHZbkK-h.js.map +0 -1
  127. package/dist/html-y1q7-t7D.d.ts +0 -60
  128. package/dist/inspect-BJtACnQp.js.map +0 -1
  129. package/dist/loader-B18i3_-T.js.map +0 -1
  130. package/dist/navigate-BwuZveDF.js.map +0 -1
  131. package/dist/produce-CWfJ4byV.js +0 -167
  132. package/dist/produce-CWfJ4byV.js.map +0 -1
  133. package/dist/produce-D4K7qpaL.d.ts +0 -72
  134. package/dist/rpc-B-Zkn6Ch.js.map +0 -1
  135. package/dist/server-BqfM95r1.js.map +0 -1
  136. package/dist/static-loader-DOygOFj5.js +0 -216
  137. package/dist/static-loader-DOygOFj5.js.map +0 -1
  138. package/dist/stream-JaMHMdBD.js.map +0 -1
@@ -1,1643 +0,0 @@
1
- import { _ as textValue, a as attributeText, h as spread, n as actionAttributes, t as VOID_ELEMENTS, u as insertValue } from "./html-bHZbkK-h.js";
2
- import { a as parseId, i as isAsyncIterable, n as ElementReference, o as stateValues, r as FunctionReference } from "./produce-CWfJ4byV.js";
3
- import { n as ROUTE_KEY } from "./context-CTiSzQRc.js";
4
- import { n as trustedHTML, t as loadOnce } from "./skew-FRmAy9iy.js";
5
- //#region src/runtime/client.ts
6
- /** The part that holds an export: `h<n>` and `c<n>` have their own, the rest are in `update`. */
7
- function partOf(name) {
8
- return /^[hct]\d/.test(name) ? name : "update";
9
- }
10
- let options;
11
- /** The URL prefix of server function calls, from the options. */
12
- let rpcPrefix = "/_brumate";
13
- /** The ID of the build of the page, from the loader. Undefined on the dev server. */
14
- let build;
15
- /**
16
- * Tells the page that the site changed since it loaded: a call answered 409
17
- * with another build, or the code of a handler is gone. The loader then
18
- * loads the next page in full, and an app can save a draft first.
19
- */
20
- function skew(reason) {
21
- document.dispatchEvent(new CustomEvent("brumate:skew", { detail: reason }));
22
- }
23
- /** The scopes of the page, by ID. */
24
- const scopes = /* @__PURE__ */ new Map();
25
- /** What modules of optional features add to the core. See `client-routes.ts` and `hot`. */
26
- const hooks = {};
27
- /** The global key of the devtools hooks. */
28
- const DEVTOOLS = Symbol.for("brumate.devtools");
29
- /**
30
- * True on the dev server, which has the devtools. A Vite build replaces it
31
- * with `false`, so the calls of the devtools hooks are left out. Without
32
- * Vite, `import.meta.env` is undefined, and the hooks are off.
33
- */
34
- const DEV = import.meta.env?.DEV;
35
- function devtools() {
36
- return globalThis[DEVTOOLS];
37
- }
38
- const modules = /* @__PURE__ */ new Map();
39
- const loading = /* @__PURE__ */ new Map();
40
- const parts = /* @__PURE__ */ new Map();
41
- const loadedParts = /* @__PURE__ */ new Map();
42
- const comments = /* @__PURE__ */ new Map();
43
- const elements = /* @__PURE__ */ new Map();
44
- let nextId = -1;
45
- const refs = /* @__PURE__ */ new Map();
46
- /**
47
- * The records that ran before the core started, or while it stopped. Each
48
- * state script hands its record to `push`, which the core replaces while it
49
- * runs, so it reads later records at once.
50
- */
51
- function stateQueue() {
52
- const global = globalThis;
53
- return global.$brm ??= [];
54
- }
55
- /** Reads the records that later state scripts hand over. Set while the core runs. */
56
- function pushRecords(...added) {
57
- for (const record of added) readRecord(record);
58
- return 0;
59
- }
60
- /**
61
- * Reads a state record. The first record holds the page state, and later
62
- * records settle the promises in it.
63
- */
64
- function readRecord([script, code]) {
65
- if (!script?.isConnected) return;
66
- const root = script.getAttribute("b-root") ?? "0";
67
- if (!refs.has(root)) refs.set(root, []);
68
- const decoded = code(refs.get(root), stateValues);
69
- if (script.hasAttribute("b-scopes")) {
70
- const unpacked = unpackState(decoded);
71
- const added = addScopes(hooks.record ? hooks.record(script, unpacked) : unpacked, root);
72
- if (writes > 0) refresh(added);
73
- }
74
- }
75
- /**
76
- * Updates scopes that the server rendered before values changed on the
77
- * client, such as the body of a `#loading` block that streams in after an
78
- * event. Effects do not run: they run only when a value changes.
79
- */
80
- function refresh(added) {
81
- for (const scope of added) {
82
- const id = rootOf(scope).m;
83
- if (!id) continue;
84
- loadModule(id).then(async (module) => {
85
- const names = /* @__PURE__ */ new Set();
86
- for (const slots of Object.values(module.g)) for (const entries of Object.values(slots)) for (const [kind, name] of entries) if (kind === scope.k && name[0] !== "e") names.add(name);
87
- if (names.size === 0) return;
88
- const updates = await loadPart(module, "update");
89
- for (const name of [...names].toSorted((a, b) => Number(b[0] === "m") - Number(a[0] === "m"))) if (scopes.has(scope.id)) updates[name](scope);
90
- });
91
- }
92
- }
93
- /** Reads the groups of scope records back into one record per scope. See `PackedState`. */
94
- function unpackState(state) {
95
- const taken = [];
96
- for (const group of state.g ?? []) {
97
- const [k, base, flags] = group;
98
- const count = group[3];
99
- let at = 4;
100
- const m = flags & 16 ? group[at++] : void 0;
101
- const source = flags & 32 ? [] : void 0;
102
- if (source) taken.push([
103
- source,
104
- group[at++],
105
- group[at++],
106
- flags & 1
107
- ]);
108
- const prefix = flags & 64 ? group[at++] : "";
109
- const id = (n) => prefix ? prefix + n : n;
110
- const skip = source && flags & 1 ? 1 : 0;
111
- let n = flags & 72 ? 0 : base;
112
- while (at < group.length) {
113
- n += group[at++];
114
- const s = group.slice(at - skip, at += count - skip);
115
- const record = flags & 8 ? {
116
- k,
117
- o: id(n - base)
118
- } : {
119
- k,
120
- p: base
121
- };
122
- record.s = s;
123
- if (m !== void 0) record.m = m;
124
- if (source) source.push(record);
125
- else if (flags & 1) record.i = s[0];
126
- else if (flags & 2) record.i = group[at++];
127
- if (flags & 4) record.c = group[at++];
128
- state[id(n)] = record;
129
- }
130
- }
131
- delete state.g;
132
- for (const [records, source, slot, first] of taken) {
133
- const values = Array.from(state[source].s[slot]);
134
- for (let n = 0; n < records.length; n++) {
135
- const record = records[n];
136
- record.i = values[n];
137
- if (first) record.s[0] = values[n];
138
- }
139
- }
140
- return state;
141
- }
142
- function addScopes(state, root) {
143
- const added = [];
144
- for (const [key, record] of Object.entries(state)) {
145
- const id = parseId(key);
146
- const scope = {
147
- id,
148
- k: record.k ?? 0,
149
- m: record.m,
150
- s: record.s ?? [],
151
- i: record.i,
152
- c: record.c,
153
- v: record.v,
154
- x: record.x,
155
- children: /* @__PURE__ */ new Set(),
156
- r: root
157
- };
158
- scopes.set(id, scope);
159
- added.push(scope);
160
- }
161
- for (const scope of added) {
162
- const record = state[scope.id];
163
- if (record.p !== void 0) {
164
- scope.p = scopes.get(record.p);
165
- scope.p?.children.add(scope);
166
- treeChanges++;
167
- }
168
- if (record.o !== void 0) scope.o = scopes.get(record.o);
169
- if (record.t !== void 0) scope.t = scopes.get(record.t);
170
- }
171
- const seen = /* @__PURE__ */ new Set();
172
- for (const scope of added) {
173
- for (const key of Object.keys(scope.v ?? {})) scope.v[key] = callable(scope.v[key], seen);
174
- for (let i = 0; i < scope.s.length; i++) {
175
- const value = callable(scope.s[i], seen);
176
- scope.s[i] = value;
177
- if (value instanceof Promise) value.then((result) => {
178
- settled.set(value, result);
179
- if (scope.s[i] === value) set(scope, i, result);
180
- });
181
- }
182
- }
183
- hooks.added?.(added);
184
- return added;
185
- }
186
- /**
187
- * Indexes the markers of markup that others insert, such as streamed
188
- * `#loading` bodies and the pages that navigation adds, so `findComment` can
189
- * take its records at once.
190
- */
191
- let markerObserver;
192
- /** Stops reading state records and markers that arrive later. Later records wait for `init`. */
193
- function dispose() {
194
- const queue = stateQueue();
195
- if (queue.push === pushRecords) delete queue.push;
196
- markerObserver?.disconnect();
197
- markerObserver = void 0;
198
- }
199
- /**
200
- * Stops reading, before client navigation replaces only some routes. The
201
- * server renders the new routes with its current values, so only the writes
202
- * that come after count for the records that stream in later.
203
- */
204
- function leaveRoutes() {
205
- dispose();
206
- writes = 0;
207
- }
208
- /**
209
- * Removes the scopes of a render, by its `b-root`, with the scopes inside
210
- * them. Their cleanups run, and their memos no longer load. Its markup
211
- * stays. `brumate/testing` calls it when a mounted component unmounts.
212
- */
213
- function removeRoot(root) {
214
- const inside = (scope) => {
215
- for (let current = scope; current; current = current.p ?? current.o) if (current.r === root) return true;
216
- return false;
217
- };
218
- for (const scope of scopes.values()) if (inside(scope)) removeScope(scope);
219
- refs.delete(root);
220
- for (const cache of [comments, elements]) for (const [key, node] of cache) if (!node.isConnected) cache.delete(key);
221
- }
222
- /**
223
- * Forgets the code of components that changed on the dev server, so the next
224
- * event or update loads the new code. Their state stays. `brumate/hot` calls it.
225
- */
226
- function forget(ids) {
227
- for (const id of ids) {
228
- modules.delete(id);
229
- loading.delete(id);
230
- for (const key of parts.keys()) if (key.startsWith(`${id}:`)) {
231
- parts.delete(key);
232
- loadedParts.delete(key);
233
- }
234
- }
235
- }
236
- /** The values of the signals of components whose markup changed, for when the page renders again. */
237
- const remounts = /* @__PURE__ */ new Map();
238
- /**
239
- * Keeps the values of the signals of each instance of a component, by
240
- * name, before the page renders again with its new markup. `brumate/hot` calls it.
241
- */
242
- function remount(detail) {
243
- hooks.added = restoreRemounts;
244
- for (const { id, before, after, signals } of detail) {
245
- const names = new Set(signals);
246
- const instances = [...scopes.values()].filter((scope) => scope.m === id);
247
- remounts.set(id, {
248
- after,
249
- values: instances.map((root) => {
250
- const values = {};
251
- (before[root.k] ?? []).forEach((name, slot) => {
252
- if (names.has(name)) values[name] = root.s[slot];
253
- });
254
- return values;
255
- })
256
- });
257
- }
258
- }
259
- /** Writes back the values of signals that `onRemount` kept, into the instances of the new page. */
260
- function restoreRemounts(added) {
261
- for (const [id, { after, values }] of remounts) {
262
- const instances = added.filter((scope) => scope.m === id);
263
- if (instances.length === 0) continue;
264
- remounts.delete(id);
265
- instances.forEach((root, index) => {
266
- const saved = values[index];
267
- (after[root.k] ?? []).forEach((name, slot) => {
268
- if (saved && name in saved) set(root, slot, saved[name]);
269
- });
270
- });
271
- }
272
- }
273
- /**
274
- * Forgets the page, before another page replaces it. Cleanups run. Loaded
275
- * modules and parts stay, so the next page does not load them again. Only
276
- * client navigation calls it.
277
- */
278
- function leavePage() {
279
- dispose();
280
- for (const scope of scopes.values()) for (const cleanup of scope.cleanups?.values() ?? []) cleanup();
281
- scopes.clear();
282
- comments.clear();
283
- elements.clear();
284
- pendingEffects.clear();
285
- hiding = void 0;
286
- loads.clear();
287
- loadingSlots.clear();
288
- for (const memoController of memoControllers.values()) memoController.abort();
289
- memoControllers.clear();
290
- renderedLoads.clear();
291
- for (const { timer } of timers.values()) clearTimeout(timer);
292
- timers.clear();
293
- guesses?.map.clear();
294
- nextId = -1;
295
- refs.clear();
296
- writes = 0;
297
- calling = 0;
298
- callEpoch++;
299
- lastHandler = Promise.resolve();
300
- }
301
- /** Reads the page state. The loader calls it once, before the first handler. */
302
- function init(value) {
303
- options = value;
304
- rpcPrefix = value.rpc ?? "/_brumate";
305
- build = value.build;
306
- const queue = stateQueue();
307
- queue.push = pushRecords;
308
- for (const record of queue.splice(0)) readRecord(record);
309
- markerObserver = new MutationObserver(indexAdded);
310
- markerObserver.observe(document, {
311
- childList: true,
312
- subtree: true
313
- });
314
- indexMarkers(document.documentElement);
315
- watchTitles(document);
316
- if (DEV) devtools()?.attach?.({
317
- scopes,
318
- set
319
- });
320
- }
321
- /** Replaces function references with functions that call them. */
322
- function callable(value, seen) {
323
- if (value instanceof FunctionReference) return isTemplate(value.ref) ? templateReference(value.ref, scopes.get(value.scope)) : (...args) => call(value.ref, scopes.get(value.scope), args);
324
- if (typeof value === "object" && value !== null && !seen.has(value)) {
325
- seen.add(value);
326
- const prototype = Object.getPrototypeOf(value);
327
- if (Array.isArray(value) || prototype === Object.prototype || prototype === null) {
328
- const record = value;
329
- for (const key of Object.keys(record)) record[key] = callable(record[key], seen);
330
- }
331
- }
332
- return value;
333
- }
334
- function loadModule(id) {
335
- let promise = loading.get(id);
336
- if (!promise) {
337
- promise = Promise.resolve(options.load(id)).then((loaded) => {
338
- const module = loaded ?? {
339
- id,
340
- props: {},
341
- g: {}
342
- };
343
- modules.set(id, module);
344
- return module;
345
- });
346
- loading.set(id, promise);
347
- }
348
- return promise;
349
- }
350
- function loadPart(module, name) {
351
- const key = `${module.id}:${name}`;
352
- let promise = parts.get(key);
353
- if (!promise) {
354
- promise = (module.load ? module.load(name) : Promise.resolve(module)).then((part) => {
355
- loadedParts.set(key, part);
356
- if (DEV) devtools()?.part?.(module.id, name);
357
- return part;
358
- });
359
- parts.set(key, promise);
360
- }
361
- return promise;
362
- }
363
- /** Waits forever once the page loads again, so no error shows before it goes. */
364
- let reloading;
365
- async function resolve(ref) {
366
- const [id, name] = ref.split("#");
367
- try {
368
- return (await loadPart(await loadModule(id), partOf(name)))[name];
369
- } catch (error) {
370
- const next = await serverBuild();
371
- if (!reloading && next !== void 0 && loadOnce(next)) {
372
- skew("code");
373
- location.reload();
374
- reloading = new Promise(() => {});
375
- }
376
- if (reloading) return reloading;
377
- throw error;
378
- }
379
- }
380
- /**
381
- * Asks the server for its build, after the code of a handler failed to
382
- * load. Returns it when it is not the build of the page. Undefined without
383
- * a build, when the request fails, or when the builds are the same.
384
- */
385
- async function serverBuild() {
386
- if (build === void 0) return;
387
- try {
388
- const current = (await fetch(`${rpcPrefix}/`, {
389
- method: "POST",
390
- headers: { "x-brumate-build": build }
391
- })).headers.get("x-brumate-build");
392
- return current !== null && current !== build ? current : void 0;
393
- } catch {
394
- return;
395
- }
396
- }
397
- async function call(ref, scope, args) {
398
- return (await resolve(ref))(scope, ...args);
399
- }
400
- /** True for a reference to a `#template` that a component got as a prop, such as `Page#t3`. */
401
- function isTemplate(ref) {
402
- return /#t\d/.test(ref);
403
- }
404
- /** The code of templates that is still loading. Updates wait for it, since a render cannot. */
405
- const loadingTemplates = /* @__PURE__ */ new Set();
406
- /**
407
- * A `#template` of another component, which renders at once, as markup does.
408
- * Its code starts to load when the page state is read, and updates wait for it.
409
- */
410
- function templateReference(ref, scope) {
411
- let render;
412
- const code = resolve(ref).then((handler) => {
413
- render = handler;
414
- }).finally(() => loadingTemplates.delete(code));
415
- loadingTemplates.add(code);
416
- return (...args) => render ? render(scope, ...args) : "";
417
- }
418
- let lastHandler = Promise.resolve();
419
- /**
420
- * Keeps `currentTarget` on an event. The handler runs after the event has
421
- * finished dispatching, when the browser has already cleared it.
422
- */
423
- function withTarget(event, element) {
424
- return new Proxy(event, { get(target, key) {
425
- if (key === "currentTarget") return element;
426
- const value = Reflect.get(target, key);
427
- return typeof value === "function" ? value.bind(target) : value;
428
- } });
429
- }
430
- /**
431
- * Runs the handler `ref` for an element. The scope comes from the element's
432
- * `b-b` anchor, or from the nearest element around it that has one, which
433
- * the compiler checks is in the same scope.
434
- */
435
- function handle(element, entry, event) {
436
- const [ref, owner] = entry.split("@");
437
- const anchor = owner ?? element.closest("[b-b]")?.getAttribute("b-b")?.split(":")[0];
438
- const scope = anchor ? scopes.get(parseId(anchor)) : void 0;
439
- if (scope && hiding?.hidden(scope) && element.hasAttribute("b-listen")) return Promise.resolve();
440
- const args = event ? [withTarget(event, element)] : [];
441
- if (DEV) devtools()?.handler?.(ref, element, event);
442
- const loaded = resolve(ref);
443
- const started = lastHandler.then(() => loaded).then((handler) => ({ value: handler(scope, ...args) }));
444
- lastHandler = started.catch(() => {});
445
- return started.then((result) => result.value).catch((error) => {
446
- fail(scope, error);
447
- });
448
- }
449
- /**
450
- * Reports an error of client code: a handler, an effect, a `$client`
451
- * callback or a memo that loads. It renders the `#catch` body of the nearest
452
- * `#try` around the scope of the code, through the parents and the owners of
453
- * scopes. Without one, the page gets a `brumate:error` event on `window`,
454
- * with the error and the scope, and the error is logged.
455
- */
456
- function fail(scope, error) {
457
- options.onError?.(error, { scope: scope?.id });
458
- (async () => {
459
- for (let current = scope; current; current = current.p ?? current.o) {
460
- const id = rootOf(current).m;
461
- if (!id) continue;
462
- const module = await loadModule(id);
463
- const entry = module.t?.[current.k];
464
- if (!entry) continue;
465
- let owner = current.p;
466
- while (owner && owner.k !== entry[0]) owner = owner.p;
467
- if (owner) {
468
- await catchInto(owner, module, entry, error);
469
- return;
470
- }
471
- }
472
- window.dispatchEvent(new CustomEvent("brumate:error", { detail: {
473
- error,
474
- scope
475
- } }));
476
- console.error(error);
477
- })();
478
- }
479
- /** Renders the `#catch` body of a `#try` with an error, in place of what the block shows. */
480
- async function catchInto(owner, module, [, anchor, , fallback, kinds], error) {
481
- const updates = await loadPart(module, "update");
482
- const start = findComment(`b${owner.id}:${anchor}`);
483
- if (!start) return;
484
- const end = rangeEnd(start);
485
- disposeKinds(owner, kinds);
486
- clear(start, end);
487
- insertHTML(end, updates[fallback](owner, error));
488
- }
489
- /** Returns the instance of a scope by its ID. */
490
- function scopeOf(id) {
491
- return scopes.get(id);
492
- }
493
- function get(scope, slot) {
494
- const value = scope.s[slot];
495
- if (computing > 0 && value instanceof Promise) throw new NotReady(value);
496
- if (value instanceof ElementReference) {
497
- const element = findElement(value.scope, value.anchor);
498
- if (element) scope.s[slot] = element;
499
- return element ?? void 0;
500
- }
501
- return value;
502
- }
503
- let writes = 0;
504
- function set(scope, slot, value) {
505
- if (!Object.is(scope.s[slot], value)) {
506
- writes++;
507
- scope.s[slot] = value;
508
- if (DEV) devtools()?.write?.(scope, slot, value);
509
- trigger(scope, slot);
510
- }
511
- return value;
512
- }
513
- /**
514
- * The names of a call of a module function, for its shared code. Each ref
515
- * is a slot of `scope`, `[scope, slot]` for a slot of another scope, or
516
- * `[value]` for a value that does not change, such as a literal argument.
517
- */
518
- function cells(scope, links, names) {
519
- const result = {};
520
- names.forEach((name, index) => {
521
- const ref = links[index];
522
- let owner = scope;
523
- let slot;
524
- if (typeof ref === "number") slot = ref;
525
- else if (ref.length === 2) [owner, slot] = ref;
526
- else {
527
- Object.defineProperty(result, name, {
528
- get: () => ref[0],
529
- set: () => {}
530
- });
531
- return;
532
- }
533
- Object.defineProperty(result, name, {
534
- get: () => get(owner, slot),
535
- set: (value) => set(owner, slot, value)
536
- });
537
- });
538
- return result;
539
- }
540
- const loads = /* @__PURE__ */ new Map();
541
- let loadCount = 0;
542
- const loadingSlots = /* @__PURE__ */ new Set();
543
- /** Marks a memo as loading. Its readers update the first time, so `$isPending` becomes true. */
544
- function startLoading(scope, slot, key) {
545
- if (!loadingSlots.has(key)) {
546
- loadingSlots.add(key);
547
- trigger(scope, slot);
548
- }
549
- }
550
- /** The timers of debounced and throttled memos, by `scope:slot`, with when each last computed. */
551
- const timers = /* @__PURE__ */ new Map();
552
- /**
553
- * Computes a memo of `$debounced` after its value stops changing for `ms`,
554
- * or one of `$throttled` at most once every `ms`, with the last value after
555
- * the wait. The memo is pending while it waits.
556
- */
557
- function timed(scope, slot, ms, throttle, compute) {
558
- const key = `${scope.id}:${slot}`;
559
- const state = timers.get(key) ?? { last: -Infinity };
560
- timers.set(key, state);
561
- const run = () => {
562
- state.timer = void 0;
563
- state.last = Date.now();
564
- if (scopes.get(scope.id) === scope) memo(scope, slot, compute);
565
- };
566
- const since = Date.now() - state.last;
567
- if (throttle && state.timer === void 0 && since >= ms) {
568
- run();
569
- return;
570
- }
571
- if (!throttle) {
572
- clearTimeout(state.timer);
573
- state.timer = void 0;
574
- }
575
- state.timer ??= setTimeout(run, throttle ? ms - since : ms);
576
- startLoading(scope, slot, key);
577
- }
578
- /** True for a promise, or any object with a `then` method. */
579
- function isThenable(value) {
580
- return value !== null && typeof value === "object" && typeof value.then === "function";
581
- }
582
- /**
583
- * Writes the result of an async memo when it arrives. Until then the slot
584
- * keeps its value, and `pending` is true. Only the latest load is written.
585
- */
586
- function asyncMemo(scope, slot, promise) {
587
- const key = `${scope.id}:${slot}`;
588
- const load = ++loadCount;
589
- loads.set(key, load);
590
- startLoading(scope, slot, key);
591
- const settle = (value, failed = false) => {
592
- if (loads.get(key) !== load) return;
593
- loadingSlots.delete(key);
594
- if (!failed && !Object.is(scope.s[slot], value)) set(scope, slot, value);
595
- else trigger(scope, slot);
596
- };
597
- const failed = (error) => {
598
- if (loads.get(key) === load) {
599
- fail(scope, error);
600
- settle(void 0, true);
601
- }
602
- };
603
- promise.then((value) => {
604
- if (!isAsyncIterable(value)) {
605
- settle(value);
606
- return;
607
- }
608
- (async () => {
609
- try {
610
- for await (const item of value) {
611
- if (loads.get(key) !== load) break;
612
- if (loadingSlots.has(key)) settle(item);
613
- else if (!Object.is(scope.s[slot], item)) set(scope, slot, item);
614
- }
615
- } catch (error) {
616
- failed(error);
617
- }
618
- })();
619
- }, failed);
620
- }
621
- /** Thrown when a memo reads a value that is still loading. */
622
- var NotReady = class extends Error {
623
- source;
624
- constructor(source) {
625
- super("The value is still loading");
626
- this.source = source;
627
- }
628
- };
629
- let computing = 0;
630
- /** Aborts the server function calls of a memo's compute, by memo key. */
631
- const memoControllers = /* @__PURE__ */ new Map();
632
- /** The key of the memo that computes, if any. */
633
- let memoKey;
634
- /** The signal for server function calls while a memo computes. It is made on the first call. */
635
- function memoSignal() {
636
- if (memoKey === void 0) return;
637
- let controller = memoControllers.get(memoKey);
638
- if (!controller) {
639
- controller = new AbortController();
640
- memoControllers.set(memoKey, controller);
641
- }
642
- return controller.signal;
643
- }
644
- /**
645
- * Computes a memo. A promise loads, and the memo keeps its value until it
646
- * settles. A memo that reads a value that is still loading keeps its value
647
- * too, and is pending until that value arrives and the memo computes again.
648
- */
649
- function memo(scope, slot, compute) {
650
- const key = `${scope.id}:${slot}`;
651
- if (guesses?.same(key)) return;
652
- const rendered = scope.s[slot];
653
- if (renderedLoads.delete(key) && isThenable(rendered)) {
654
- asyncMemo(scope, slot, Promise.resolve(rendered));
655
- return;
656
- }
657
- let value;
658
- memoControllers.get(key)?.abort();
659
- memoControllers.delete(key);
660
- const outer = memoKey;
661
- memoKey = key;
662
- computing++;
663
- try {
664
- value = compute();
665
- } catch (error) {
666
- if (!(error instanceof NotReady)) throw error;
667
- startLoading(scope, slot, key);
668
- return;
669
- } finally {
670
- computing--;
671
- memoKey = outer;
672
- }
673
- if (isThenable(value) || isAsyncIterable(value)) {
674
- asyncMemo(scope, slot, Promise.resolve(value));
675
- return;
676
- }
677
- const waited = loadingSlots.delete(key);
678
- loads.delete(key);
679
- guesses?.map.delete(key);
680
- if (!Object.is(scope.s[slot], value)) set(scope, slot, value);
681
- else if (waited) trigger(scope, slot);
682
- }
683
- /**
684
- * The guesses of `$optimistic` values. The first `guess` sets it, so the
685
- * code that reads guesses is left out of a build that makes none.
686
- */
687
- let guesses;
688
- /** The values of promises that the server streamed, so a guess knows its source did not change. */
689
- const settled = /* @__PURE__ */ new WeakMap();
690
- /** True while the source of a guess holds the value it had when the guess was made. */
691
- function sameSource(key) {
692
- const entry = guesses.map.get(key);
693
- if (!entry?.source) return false;
694
- const current = entry.source[0].s[entry.source[1]];
695
- return Object.is(current, entry.base) || entry.base instanceof Promise && settled.get(entry.base) === current;
696
- }
697
- /** Server calls that have not finished, from forms and from `rpc`. */
698
- let calling = 0;
699
- /** Counts the pages that client navigation left, so calls of an old page are not counted down. */
700
- let callEpoch = 0;
701
- /**
702
- * Writes a guess into an optimistic value. It goes back to its source when
703
- * the source changes, which computes it again, or when every server call of
704
- * the page finishes, such as after a post failed.
705
- */
706
- function guess(scope, slot, ...rest) {
707
- const value = rest.at(-1);
708
- const source = rest.length === 3 ? [rest[0], rest[1]] : void 0;
709
- const base = source && source[0].s[source[1]];
710
- guesses ??= {
711
- map: /* @__PURE__ */ new Map(),
712
- same: sameSource,
713
- reset: resetGuesses
714
- };
715
- guesses.map.set(`${scope.id}:${slot}`, {
716
- scope,
717
- slot,
718
- source,
719
- base
720
- });
721
- return set(scope, slot, value);
722
- }
723
- /** Counts a server call, and puts guesses back when the last one finishes. */
724
- function track(request) {
725
- calling++;
726
- const epoch = callEpoch;
727
- const done = () => {
728
- if (epoch !== callEpoch) return;
729
- calling--;
730
- setTimeout(() => calling || guesses?.reset(), 0);
731
- };
732
- request.then(done, done);
733
- return request;
734
- }
735
- function resetGuesses() {
736
- for (const [key, { scope, slot, source }] of guesses.map) {
737
- if (source && pending(source[0], source[1])) continue;
738
- guesses.map.delete(key);
739
- if (source && scopes.has(scope.id) && !Object.is(scope.s[slot], source[0].s[source[1]])) set(scope, slot, source[0].s[source[1]]);
740
- }
741
- }
742
- /** True while a new value of a memo loads, or while the server still streams its value. */
743
- function pending(scope, slot) {
744
- return loadingSlots.has(`${scope.id}:${slot}`) || scope.s[slot] instanceof Promise;
745
- }
746
- /** `x++`, `x--`, `++x` and `--x`. */
747
- function update(scope, slot, delta, prefix) {
748
- const previous = get(scope, slot);
749
- set(scope, slot, previous + delta);
750
- return prefix ? previous + delta : previous;
751
- }
752
- function rootOf(scope) {
753
- let current = scope;
754
- while (current.p) current = current.p;
755
- return current;
756
- }
757
- function trigger(scope, slot) {
758
- const id = rootOf(scope).m;
759
- const module = modules.get(id);
760
- const updates = loadedParts.get(`${id}:update`);
761
- if (module && updates && loadingTemplates.size === 0) {
762
- run(module, updates, scope, slot);
763
- return;
764
- }
765
- loadModule(id).then(async (loaded) => {
766
- if (loaded.g[scope.k]?.[slot]) {
767
- const code = await loadPart(loaded, "update");
768
- await Promise.allSettled(loadingTemplates);
769
- run(loaded, code, scope, slot);
770
- }
771
- });
772
- }
773
- const pendingEffects = /* @__PURE__ */ new Map();
774
- let running = 0;
775
- let flushing = false;
776
- /** Counts changes to the scope tree, so `run` knows when the descendants it found are out of date. */
777
- let treeChanges = 0;
778
- function run(module, updates, scope, slot) {
779
- running++;
780
- let found;
781
- let foundAt = treeChanges;
782
- try {
783
- for (const [kind, name] of module.g[scope.k]?.[slot] ?? []) {
784
- const fn = updates[name];
785
- if (kind === scope.k) {
786
- runUpdate(fn, name, scope);
787
- continue;
788
- }
789
- if (foundAt !== treeChanges) {
790
- found = void 0;
791
- foundAt = treeChanges;
792
- }
793
- found ??= /* @__PURE__ */ new Map();
794
- let targets = found.get(kind);
795
- if (!targets) {
796
- targets = descendants(scope, kind);
797
- found.set(kind, targets);
798
- }
799
- for (const target of targets) runUpdate(fn, name, target);
800
- }
801
- } finally {
802
- running--;
803
- }
804
- if (running === 0) flushEffects();
805
- }
806
- /** Runs an update on a scope. An effect waits until the other updates have run. */
807
- function runUpdate(fn, name, target) {
808
- if (name[0] === "e") pendingEffects.set(`${target.id}:${name}`, () => {
809
- if (scopes.has(target.id)) fn(target);
810
- });
811
- else fn(target);
812
- }
813
- /**
814
- * Runs the effects that writes scheduled, in the order they were first
815
- * scheduled. Writes in an effect schedule more effects, which run after.
816
- */
817
- function flushEffects() {
818
- if (flushing) return;
819
- flushing = true;
820
- try {
821
- for (const [key, runEffect] of pendingEffects) {
822
- pendingEffects.delete(key);
823
- runEffect();
824
- }
825
- } finally {
826
- flushing = false;
827
- }
828
- }
829
- function descendants(scope, kind, result = []) {
830
- for (const child of scope.children) {
831
- if (child.k === kind) result.push(child);
832
- descendants(child, kind, result);
833
- }
834
- return result;
835
- }
836
- function destroy(scope) {
837
- for (const child of Array.from(scope.children)) destroy(child);
838
- const start = comments.get(`P${scope.id}`);
839
- if (start?.isConnected) {
840
- const end = rangeEnd(start);
841
- clear(start, end);
842
- start.remove();
843
- end.remove();
844
- }
845
- removeScope(scope);
846
- }
847
- /**
848
- * Removes a scope from the page: its cleanups run, and its memo state goes,
849
- * so a load that settles later is not written. Its markup stays.
850
- */
851
- function removeScope(scope) {
852
- for (const cleanup of scope.cleanups?.values() ?? []) cleanup();
853
- for (let slot = 0; slot < scope.s.length; slot++) {
854
- const key = `${scope.id}:${slot}`;
855
- memoControllers.get(key)?.abort();
856
- memoControllers.delete(key);
857
- clearTimeout(timers.get(key)?.timer);
858
- timers.delete(key);
859
- loads.delete(key);
860
- loadingSlots.delete(key);
861
- guesses?.map.delete(key);
862
- }
863
- if (scope.h) hiding?.bodies.delete(scope.h);
864
- scopes.delete(scope.id);
865
- scope.p?.children.delete(scope);
866
- treeChanges++;
867
- }
868
- function disposeKinds(scope, kinds) {
869
- for (const child of Array.from(scope.children)) if (kinds.includes(child.k)) destroy(child);
870
- }
871
- const MARKER = /^[btilP]-?\d/;
872
- function markerKey(data) {
873
- return data[0] === "b" ? data.split(":").slice(0, 2).join(":") : data;
874
- }
875
- function indexMarkers(root) {
876
- const walker = document.createTreeWalker(root, NodeFilter.SHOW_COMMENT);
877
- for (let node = walker.nextNode(); node; node = walker.nextNode()) {
878
- const data = node.data;
879
- if (MARKER.test(data)) comments.set(markerKey(data), node);
880
- }
881
- }
882
- /** Indexes the markers in the nodes that mutation records added. */
883
- function indexAdded(records) {
884
- for (const record of records) for (const node of record.addedNodes) if (node.nodeType === 1) indexMarkers(node);
885
- else if (node.nodeType === 8 && MARKER.test(node.data)) comments.set(markerKey(node.data), node);
886
- }
887
- /**
888
- * Finds a marker in the page. Returns undefined when the markup is not in
889
- * the page, such as `children` inside a block that is closed. Updates to it
890
- * do nothing, and it updates when the client inserts it again.
891
- */
892
- function findComment(key) {
893
- let comment = comments.get(key);
894
- if (!comment?.isConnected) {
895
- if (markerObserver) indexAdded(markerObserver.takeRecords());
896
- else indexMarkers(document.documentElement);
897
- comment = comments.get(key);
898
- }
899
- return comment?.isConnected ? comment : void 0;
900
- }
901
- function findElement(scope, anchor) {
902
- const key = `${scope}:${anchor}`;
903
- let element = elements.get(key);
904
- if (!element?.isConnected) {
905
- element = document.querySelector(`[b-b="${key}"]`) ?? void 0;
906
- if (element) elements.set(key, element);
907
- }
908
- return element ?? null;
909
- }
910
- /** Returns the marker that ends the range that `start` opens. */
911
- function rangeEnd(start) {
912
- let depth = 0;
913
- for (let node = start.nextSibling; node; node = node.nextSibling) if (node.nodeType === 8) {
914
- const data = node.data;
915
- if (data[0] === "/") {
916
- if (depth === 0) return node;
917
- depth--;
918
- } else if (MARKER.test(data)) depth++;
919
- }
920
- throw new Error(`Missing end marker for <!--${start.data}-->`);
921
- }
922
- function clear(start, end) {
923
- while (start.nextSibling && start.nextSibling !== end) start.nextSibling.remove();
924
- }
925
- /**
926
- * Inserts rendered HTML before a node, or at the end of a parent, and runs
927
- * the work that waits for it.
928
- */
929
- function insertHTML(before, html) {
930
- const template = document.createElement("template");
931
- template.innerHTML = trustedHTML(html);
932
- const fragment = template.content;
933
- if (markerObserver) indexAdded(markerObserver.takeRecords());
934
- indexMarkers(fragment);
935
- options.listen(fragment);
936
- watchTitles(fragment);
937
- const server = serverScopes(fragment);
938
- const added = hiding?.bodies.size ? [...fragment.childNodes] : void 0;
939
- if ("parent" in before) before.parent.append(fragment);
940
- else before.parentNode.insertBefore(fragment, before);
941
- if (added) hiding.added(added);
942
- markerObserver?.takeRecords();
943
- for (const fn of afterInsert.splice(0)) fn();
944
- if (server.length > 0) refresh(server);
945
- }
946
- /**
947
- * Returns the scopes from the server that markup refers to. Markup that the
948
- * client renders has client scopes, so these come from HTML that the server
949
- * rendered, such as the `children` of a component.
950
- */
951
- function serverScopes(root) {
952
- const ids = /* @__PURE__ */ new Set();
953
- for (const element of root.querySelectorAll("[b-b]")) ids.add(parseId(element.getAttribute("b-b").split(":")[0]));
954
- const walker = document.createTreeWalker(root, NodeFilter.SHOW_COMMENT);
955
- for (let node = walker.nextNode(); node; node = walker.nextNode()) {
956
- const match = /^[tb]([\d.]+):/.exec(node.data);
957
- if (match) ids.add(parseId(match[1]));
958
- }
959
- return [...ids].flatMap((id) => {
960
- const scope = scopes.get(id);
961
- return scope ? [scope] : [];
962
- });
963
- }
964
- /**
965
- * Renders a `#portal` body into its target, between markers with its scope
966
- * ID. `kinds` names the body, so a new target first removes it from the old one.
967
- */
968
- function portalTo(scope, target, render, kinds = []) {
969
- disposeKinds(scope, kinds);
970
- const element = typeof target === "string" ? document.querySelector(target) : target;
971
- if (!(element instanceof Element)) return;
972
- insertHTML({ parent: element }, `<!--P${nextId}-->${render(scope)}<!--/P-->`);
973
- }
974
- /** Writes an inserted value between `<!--t…-->` and `<!--/t-->`. */
975
- function text(scope, anchor, value) {
976
- const start = findComment(`t${scope.id}:${anchor}`);
977
- if (start) write(start.parentNode, start.nextSibling, rangeEnd(start), value);
978
- }
979
- /** Writes an inserted value that is the whole content of the element with the anchor. */
980
- function content(scope, anchor, value) {
981
- const element = findElement(scope.id, anchor);
982
- if (element) write(element, element.firstChild, null, value);
983
- }
984
- /**
985
- * Writes the text of an element whose content is text, such as a `<textarea>`
986
- * or a `<title>`. A `<textarea>` gets the value too, as `value` does on an
987
- * `<input>`, so a change replaces what the user typed.
988
- */
989
- function writeText(scope, anchor, value) {
990
- const element = findElement(scope.id, anchor);
991
- if (element) {
992
- const written = textValue(value);
993
- element.textContent = written;
994
- if (element instanceof HTMLTextAreaElement) element.value = written;
995
- }
996
- }
997
- /**
998
- * Replaces the nodes of `parent` from `first` up to `end`, or to its last
999
- * child, with a value. A text node that stays a text node keeps its node.
1000
- */
1001
- function write(parent, first, end, value) {
1002
- if ((typeof value === "string" || typeof value === "number") && first?.nodeType === 3 && first.nextSibling === end) {
1003
- const saved = hiding?.nodes.get(first);
1004
- if (saved) saved.v = String(value);
1005
- else first.data = String(value);
1006
- return;
1007
- }
1008
- while (first && first !== end) {
1009
- const next = first.nextSibling;
1010
- first.remove();
1011
- first = next;
1012
- }
1013
- insertHTML(end ?? { parent }, insertValue(value));
1014
- }
1015
- function setAttribute(element, name, value) {
1016
- if (name === "value" || name === "checked") element[name] = name === "checked" ? Boolean(value) : value ?? "";
1017
- const written = attributeText(name, value);
1018
- if (written === void 0) element.removeAttribute(name);
1019
- else element.setAttribute(name, written);
1020
- if (name === "style") hiding?.styled(element);
1021
- }
1022
- function attr(scope, anchor, name, value) {
1023
- const element = findElement(scope.id, anchor);
1024
- if (element) setAttribute(element, name, value);
1025
- }
1026
- /** The attributes that the last update of a spread wrote, so the next one removes those it drops. */
1027
- const spreadNames = /* @__PURE__ */ new WeakMap();
1028
- function attrs(scope, anchor, values, own = []) {
1029
- const element = findElement(scope.id, anchor);
1030
- if (element) {
1031
- if (!spreadNames.has(element)) spreadNames.set(element, new Set(element.getAttributeNames().filter((name) => !own.includes(name) && !/^(b-|data-prefetch$)/.test(name))));
1032
- const names = /* @__PURE__ */ new Set();
1033
- for (const [name, value] of Object.entries(values)) if (name !== "children" && !/^on[A-Z]/.test(name)) {
1034
- names.add(name);
1035
- setAttribute(element, name, value);
1036
- }
1037
- for (const name of spreadNames.get(element) ?? []) if (!names.has(name)) setAttribute(element, name, void 0);
1038
- spreadNames.set(element, names);
1039
- }
1040
- }
1041
- /** Shows the branch at `index` of `#if` or `#switch`, or nothing for -1. */
1042
- function branch(scope, anchor, index, renders, kinds) {
1043
- const start = findComment(`b${scope.id}:${anchor}`);
1044
- if (!start) return;
1045
- if (Number(start.data.split(":")[2]) === index) return;
1046
- const end = rangeEnd(start);
1047
- disposeKinds(scope, kinds);
1048
- clear(start, end);
1049
- start.data = `b${scope.id}:${anchor}:${index}`;
1050
- const render = renders[index];
1051
- if (render) insertHTML(end, render(scope));
1052
- }
1053
- /** Renders the body of `#key` again when its value changes. */
1054
- function rekey(scope, anchor, value, render, kinds) {
1055
- scope.keys ??= /* @__PURE__ */ new Map();
1056
- if (scope.keys.has(anchor) && Object.is(scope.keys.get(anchor), value)) return;
1057
- scope.keys.set(anchor, value);
1058
- const start = findComment(`b${scope.id}:${anchor}`);
1059
- if (!start) return;
1060
- const end = rangeEnd(start);
1061
- disposeKinds(scope, kinds);
1062
- clear(start, end);
1063
- insertHTML(end, render(scope));
1064
- }
1065
- /**
1066
- * Renders the body of a `#try` block again, for its `reset` function. If the
1067
- * body throws, the fallback renders again with the error.
1068
- */
1069
- async function reset(scope, anchor, id, body, fallback, kinds) {
1070
- const updates = await loadPart(await loadModule(id), "update");
1071
- const start = findComment(`b${scope.id}:${anchor}`);
1072
- if (!start) return;
1073
- const end = rangeEnd(start);
1074
- disposeKinds(scope, kinds);
1075
- clear(start, end);
1076
- let html;
1077
- try {
1078
- html = updates[body](scope);
1079
- } catch (error) {
1080
- disposeKinds(scope, kinds);
1081
- afterInsert.length = 0;
1082
- html = updates[fallback](scope, error);
1083
- }
1084
- insertHTML(end, html);
1085
- }
1086
- /**
1087
- * Renders the body of a `#client` block once, in place of its fallback.
1088
- * `kinds` are the scope IDs inside the fallback, whose instances go.
1089
- */
1090
- function fill(scope, anchor, render, kinds = []) {
1091
- const start = findComment(`b${scope.id}:${anchor}`);
1092
- if (!start || start.data.split(":").length > 2) return;
1093
- start.data += ":1";
1094
- const end = rangeEnd(start);
1095
- disposeKinds(scope, kinds);
1096
- clear(start, end);
1097
- insertHTML(end, render(scope));
1098
- }
1099
- /**
1100
- * Shows or hides the body of an `#offscreen` block. A body that the page
1101
- * does not have yet renders the first time it shows. `kind` is the body's
1102
- * scope ID in the compiled component.
1103
- */
1104
- function offscreen(scope, anchor, value, render, kind) {
1105
- const start = findComment(`b${scope.id}:${anchor}`);
1106
- if (!start) return;
1107
- const visible = Boolean(value);
1108
- const state = start.data.split(":")[2];
1109
- if (visible ? state === "1" : state !== "1") return;
1110
- start.data = `b${scope.id}:${anchor}:${visible ? 1 : 0}`;
1111
- const end = rangeEnd(start);
1112
- if (state === "-1") {
1113
- insertHTML(end, render(scope));
1114
- return;
1115
- }
1116
- let body;
1117
- for (const child of scope.children) if (child.k === kind) body = child;
1118
- if (body) {
1119
- hiding ??= createHiding();
1120
- if (visible) hiding.show(body);
1121
- else hiding.hide(body, start, end);
1122
- }
1123
- }
1124
- /**
1125
- * The code of hidden `#offscreen` bodies. The first body that hides sets it
1126
- * up, so the core of a page without one only checks that it is not there.
1127
- * A body is hidden with an inline `display: none !important` on each element
1128
- * at its top level, which no stylesheet overrides, and empty text for each
1129
- * text node. The nodes stay in the page, so form fields, focus and media
1130
- * keep their state.
1131
- */
1132
- let hiding;
1133
- /** True for a scope that is `ancestor` or inside it, also through the owner of a component. */
1134
- function within(scope, ancestor) {
1135
- for (let current = scope; current; current = current.p ?? current.o) if (current === ancestor) return true;
1136
- return false;
1137
- }
1138
- /** True for a scope in a hidden `#offscreen` body. */
1139
- function hidden(scope) {
1140
- for (let current = scope; current; current = current.p ?? current.o) if (current.h) return true;
1141
- return false;
1142
- }
1143
- /** The nodes between two markers. */
1144
- function between([start, end]) {
1145
- const result = [];
1146
- for (let node = start.nextSibling; node && node !== end; node = node.nextSibling) result.push(node);
1147
- return result;
1148
- }
1149
- /** The inline style of an element, which an element that is not HTML or SVG may not have. */
1150
- function display(element) {
1151
- return element.style;
1152
- }
1153
- function createHiding() {
1154
- const bodies = /* @__PURE__ */ new Set();
1155
- const nodes = /* @__PURE__ */ new WeakMap();
1156
- const hideNode = (node) => {
1157
- const saved = nodes.get(node);
1158
- if (saved) {
1159
- saved.n++;
1160
- return;
1161
- }
1162
- if (node.nodeType === 3) {
1163
- nodes.set(node, {
1164
- n: 1,
1165
- v: node.data
1166
- });
1167
- node.data = "";
1168
- return;
1169
- }
1170
- const style = node.nodeType === 1 ? display(node) : void 0;
1171
- if (style) {
1172
- nodes.set(node, {
1173
- n: 1,
1174
- v: style.getPropertyValue("display"),
1175
- p: style.getPropertyPriority("display"),
1176
- a: node.hasAttribute("style")
1177
- });
1178
- style.setProperty("display", "none", "important");
1179
- }
1180
- };
1181
- const showNode = (node) => {
1182
- const saved = nodes.get(node);
1183
- if (!saved || --saved.n > 0) return;
1184
- nodes.delete(node);
1185
- if (node.nodeType === 3) {
1186
- node.data = saved.v;
1187
- return;
1188
- }
1189
- const element = node;
1190
- const style = display(element);
1191
- if (saved.v) style.setProperty("display", saved.v, saved.p);
1192
- else style.removeProperty("display");
1193
- if (!saved.a && element.getAttribute("style") === "") element.removeAttribute("style");
1194
- };
1195
- return {
1196
- bodies,
1197
- nodes,
1198
- hidden,
1199
- hide(body, start, end) {
1200
- const record = {
1201
- scope: body,
1202
- ranges: [[start, end]]
1203
- };
1204
- body.h = record;
1205
- bodies.add(record);
1206
- const inside = [...scopes.values()].filter((scope) => within(scope, body));
1207
- for (const scope of inside) {
1208
- const portal = comments.get(`P${scope.id}`);
1209
- if (portal?.isConnected) record.ranges.push([portal, rangeEnd(portal)]);
1210
- }
1211
- for (const range of record.ranges) between(range).forEach(hideNode);
1212
- for (const scope of inside) {
1213
- for (const [name, cleanup] of scope.cleanups ?? []) {
1214
- cleanup();
1215
- (scope.w ??= /* @__PURE__ */ new Set()).add(name);
1216
- }
1217
- scope.cleanups?.clear();
1218
- }
1219
- },
1220
- show(body) {
1221
- const record = body.h;
1222
- if (!record) return;
1223
- body.h = void 0;
1224
- bodies.delete(record);
1225
- for (const range of record.ranges) if (range[0].isConnected) between(range).forEach(showNode);
1226
- for (const scope of scopes.values()) if (scope.w && within(scope, body) && !hidden(scope)) {
1227
- const ref = `${rootOf(scope).m}#`;
1228
- for (const name of scope.w) call(ref + name, scope, []);
1229
- scope.w = void 0;
1230
- }
1231
- },
1232
- paused(scope, name) {
1233
- if (bodies.size === 0 || !hidden(scope)) return false;
1234
- (scope.w ??= /* @__PURE__ */ new Set()).add(name);
1235
- return true;
1236
- },
1237
- added(added) {
1238
- for (const node of added) for (const record of bodies) {
1239
- for (const [start, end] of record.ranges) if (node.parentNode === start.parentNode && start.compareDocumentPosition(node) & 4 && node.compareDocumentPosition(end) & 4) hideNode(node);
1240
- const data = node.nodeType === 8 ? node.data : "";
1241
- const scope = /^P-?[\d.]+$/.test(data) ? scopes.get(parseId(data.slice(1))) : void 0;
1242
- if (scope && within(scope, record.scope)) record.ranges.push([node, rangeEnd(node)]);
1243
- }
1244
- },
1245
- styled(element) {
1246
- const saved = nodes.get(element);
1247
- const style = display(element);
1248
- if (saved && style) {
1249
- saved.v = style.getPropertyValue("display");
1250
- saved.p = style.getPropertyPriority("display");
1251
- saved.a = element.hasAttribute("style");
1252
- style.setProperty("display", "none", "important");
1253
- }
1254
- }
1255
- };
1256
- }
1257
- /** Updates the items of `#for` or `#repeat`, keeping the elements of items whose keys stay. */
1258
- function list(scope, anchor, items, key, render, assign, fallback, fallbackKinds) {
1259
- const start = findComment(`b${scope.id}:${anchor}`);
1260
- if (!start) return;
1261
- const end = rangeEnd(start);
1262
- const previous = /* @__PURE__ */ new Map();
1263
- const others = [];
1264
- let position = 0;
1265
- for (let node = start.nextSibling; node && node !== end;) {
1266
- const data = node.nodeType === 8 ? node.data : void 0;
1267
- const id = data ? /^i(-?[\d.]+)$/.exec(data)?.[1] : node.getAttribute?.("b-b")?.split(":")[0];
1268
- const item = id === void 0 ? void 0 : scopes.get(parseId(id));
1269
- let last = node;
1270
- if (item && (data || item.p === scope && !fallbackKinds.includes(item.k))) {
1271
- if (data) last = rangeEnd(node);
1272
- const nodes = [node];
1273
- for (let current = node; current !== last;) nodes.push(current = current.nextSibling);
1274
- previous.set(key(item.i, position++), {
1275
- scope: item,
1276
- nodes
1277
- });
1278
- } else others.push(node);
1279
- node = last.nextSibling;
1280
- }
1281
- if (items.length === 0) {
1282
- if (previous.size === 0 && others.length > 0) return;
1283
- for (const item of previous.values()) destroy(item.scope);
1284
- clear(start, end);
1285
- if (fallback) insertHTML(end, fallback(scope));
1286
- return;
1287
- }
1288
- disposeKinds(scope, fallbackKinds);
1289
- for (const node of others) node.parentNode?.removeChild(node);
1290
- const parent = end.parentNode;
1291
- let cursor = start.nextSibling;
1292
- let added = "";
1293
- const flush = () => {
1294
- if (added) {
1295
- insertHTML(cursor, added);
1296
- added = "";
1297
- }
1298
- };
1299
- items.forEach((value, index) => {
1300
- const id = key(value, index);
1301
- const item = previous.get(id);
1302
- if (item) {
1303
- flush();
1304
- previous.delete(id);
1305
- assign(item.scope, value, index);
1306
- if (item.nodes[0] === cursor) cursor = item.nodes.at(-1).nextSibling;
1307
- else for (const node of item.nodes) parent.insertBefore(node, cursor);
1308
- } else added += render(scope, value, index);
1309
- });
1310
- flush();
1311
- for (const item of previous.values()) {
1312
- destroy(item.scope);
1313
- for (const node of item.nodes) node.parentNode?.removeChild(node);
1314
- }
1315
- }
1316
- /** Sets a style variable on the top-level elements of a component instance. */
1317
- function setVar(scope, name, value) {
1318
- const css = value === null || value === void 0 || value === false ? "" : String(value);
1319
- for (const element of document.querySelectorAll(`[b-v="${scope.id}"]`)) element.style.setProperty(name, css);
1320
- }
1321
- /** The scope that provides a context to `scope`: itself or the nearest one above it. */
1322
- function findProvider(scope, key) {
1323
- for (let current = scope; current; current = current.t ?? current.p ?? current.o) if (current.v && key in current.v) return current;
1324
- }
1325
- /** Writes a provided value, and the slot of every scope that reads it from this provider. */
1326
- function updateContext(scope, context, value) {
1327
- scope.v ??= {};
1328
- scope.v[context.key] = value;
1329
- for (const target of scopes.values()) {
1330
- const slot = target.x?.[context.key];
1331
- if (slot !== void 0 && findProvider(target, context.key) === scope) set(target, slot, value);
1332
- }
1333
- }
1334
- /** Sets the title of the page. */
1335
- function title(value) {
1336
- setTitle(value === null || value === void 0 ? "" : String(value));
1337
- }
1338
- /**
1339
- * Sets `document.title`, which writes the first `<title>` of the document.
1340
- * The head gets a `<title>` of its own first, so a `<title>` in markup keeps its text.
1341
- */
1342
- function setTitle(text) {
1343
- if (!document.head.querySelector("title:not([b-t])")) document.head.prepend(document.createElement("title"));
1344
- document.title = text;
1345
- }
1346
- /** Watches the `<title>` elements of markup, once one is in the page. */
1347
- let titleObserver;
1348
- const PAGE_TITLE = "title[b-t]";
1349
- /** True when a node is or holds a `<title>` of markup. */
1350
- function holdsTitle(node) {
1351
- return node instanceof Element && (node.matches(PAGE_TITLE) || !!node.querySelector(PAGE_TITLE));
1352
- }
1353
- /** Gives the page the text of its last `<title>` in markup. Without one, the title stays. */
1354
- function syncTitle() {
1355
- const titles = document.querySelectorAll(PAGE_TITLE);
1356
- const last = titles[titles.length - 1];
1357
- if (last && document.title !== last.textContent) setTitle(last.textContent ?? "");
1358
- }
1359
- /**
1360
- * Starts to follow the `<title>` elements of markup, when `root` has one. A
1361
- * block that adds or removes one, or a change of its text, sets the title.
1362
- */
1363
- function watchTitles(root) {
1364
- if (titleObserver || !root.querySelector(PAGE_TITLE)) return;
1365
- titleObserver = new MutationObserver((records) => {
1366
- for (const record of records) if ((record.target instanceof Element ? record.target : record.target.parentElement)?.closest(PAGE_TITLE) || [...record.addedNodes].some(holdsTitle) || [...record.removedNodes].some(holdsTitle)) {
1367
- syncTitle();
1368
- return;
1369
- }
1370
- });
1371
- titleObserver.observe(document, {
1372
- childList: true,
1373
- subtree: true,
1374
- characterData: true
1375
- });
1376
- }
1377
- /** Writes a prop of a child component. */
1378
- function prop(scope, anchor, name, value) {
1379
- const id = scope.c?.[anchor];
1380
- const child = id === void 0 ? void 0 : scopes.get(id);
1381
- if (!child?.m) return;
1382
- const apply = (module) => {
1383
- const slot = module.props[name];
1384
- if (slot !== void 0) {
1385
- if (isThenable(value)) asyncMemo(child, slot, Promise.resolve(value));
1386
- else set(child, slot, value);
1387
- return;
1388
- }
1389
- const all = module.props["*"];
1390
- if (all !== void 0) set(child, all, {
1391
- ...get(child, all),
1392
- [name]: value
1393
- });
1394
- };
1395
- const module = modules.get(child.m);
1396
- if (module) apply(module);
1397
- else loadModule(child.m).then(apply);
1398
- }
1399
- /** Runs an effect or a `$client` callback, after the cleanup of its previous run. */
1400
- function effect(scope, name, callback) {
1401
- if (hiding?.paused(scope, name)) return;
1402
- scope.cleanups ??= /* @__PURE__ */ new Map();
1403
- scope.cleanups.get(name)?.();
1404
- let cleanup;
1405
- try {
1406
- cleanup = callback();
1407
- } catch (error) {
1408
- fail(scope, error);
1409
- }
1410
- scope.cleanups.set(name, typeof cleanup === "function" ? cleanup : noop);
1411
- }
1412
- function noop() {}
1413
- /** Renders the attributes of a form or button that posts to a server function. */
1414
- function action(name, id, method, result) {
1415
- return actionAttributes(rpcPrefix, name, id, method, result, build);
1416
- }
1417
- /**
1418
- * Sends a form of an `$action`. The code is in `rpc.ts`, which every server
1419
- * call loads, so the core holds only this.
1420
- */
1421
- function submitAction(form, submitter) {
1422
- import("./rpc-B-Zkn6Ch.js").then((module) => module.sendAction(form, submitter));
1423
- }
1424
- /** Calls a server function. */
1425
- async function rpc(id, args) {
1426
- const signal = memoSignal();
1427
- return track(import("./rpc-B-Zkn6Ch.js").then((module) => module.call(rpcPrefix, id, args, signal)));
1428
- }
1429
- /** Renders a value inserted in markup. Children render here, at `scope`, for their contexts. */
1430
- function insert(value, _ctx, scope) {
1431
- return value instanceof Children ? value.render(resolveScope(scope)) : insertValue(value);
1432
- }
1433
- /** Children that render where they are inserted, and the scopes that they find contexts from. */
1434
- const insertedChildren = [];
1435
- /**
1436
- * Children of a component that the client renders. They render where the
1437
- * component inserts them, so they read the contexts that it provides. `from`
1438
- * is the scope of the markup that wrote them.
1439
- */
1440
- var Children = class {
1441
- from;
1442
- html;
1443
- constructor(from, html) {
1444
- this.from = resolveScope(from);
1445
- this.html = html;
1446
- }
1447
- render(at) {
1448
- if (!at) return this.html();
1449
- const entry = {
1450
- from: this.from,
1451
- at
1452
- };
1453
- insertedChildren.push(entry);
1454
- try {
1455
- return this.html();
1456
- } finally {
1457
- insertedChildren.splice(insertedChildren.indexOf(entry), 1);
1458
- }
1459
- }
1460
- };
1461
- /** The children of a component that the client renders. See `Children`. */
1462
- function children(from, html) {
1463
- return new Children(from, html);
1464
- }
1465
- function resolveScope(scope) {
1466
- return typeof scope === "object" ? scope : scope === void 0 ? void 0 : scopes.get(scope);
1467
- }
1468
- /**
1469
- * Renders a child component on the client. With `scope` and `anchor`,
1470
- * records the child's scope so the parent can update its props.
1471
- */
1472
- function component(Child, props, _ctx, scope, anchor) {
1473
- const parent = resolveScope(scope);
1474
- if (parent && anchor !== void 0) {
1475
- parent.c ??= {};
1476
- parent.c[anchor] = nextId;
1477
- }
1478
- if (parent) setOwner(parent);
1479
- return Child(props);
1480
- }
1481
- function dynamic(tag, attributes, inner, _ctx, scope) {
1482
- if (typeof tag === "function") return component(tag, inner ? {
1483
- ...attributes,
1484
- children: inner
1485
- } : attributes, void 0, scope);
1486
- const name = String(tag);
1487
- const open = `<${name}${spread(attributes)}>`;
1488
- return VOID_ELEMENTS.has(name) ? open : `${open}${inner ? insert(inner, void 0, scope) : ""}</${name}>`;
1489
- }
1490
- /** Work that waits until rendered markup is in the page. */
1491
- const afterInsert = [];
1492
- /** The scope that renders the next component, which becomes its owner. */
1493
- let owner;
1494
- /** Creates a scope for markup that the client renders, and returns its ID. */
1495
- function createScope(kind, parent, module) {
1496
- const id = nextId--;
1497
- const parentScope = resolveScope(parent);
1498
- const created = {
1499
- id,
1500
- k: kind,
1501
- p: parentScope,
1502
- m: module,
1503
- s: [],
1504
- children: /* @__PURE__ */ new Set()
1505
- };
1506
- if (module !== void 0 && owner) {
1507
- created.o = owner;
1508
- owner = void 0;
1509
- }
1510
- const up = created.o ?? parentScope;
1511
- const inserted = insertedChildren.findLast((entry) => entry.from === up);
1512
- if (inserted) created.t = inserted.at;
1513
- parentScope?.children.add(created);
1514
- treeChanges++;
1515
- scopes.set(id, created);
1516
- return id;
1517
- }
1518
- /**
1519
- * The ID that `$id()` makes, as the server does. Client scope IDs are
1520
- * negative, so they differ from the server's.
1521
- */
1522
- function makeId(scope, index) {
1523
- return `b${String(resolveScope(scope).id).replace(/\./g, "_")}-${index}`;
1524
- }
1525
- /** Sets the owner of the next component's root scope. */
1526
- function setOwner(scope) {
1527
- owner = resolveScope(scope);
1528
- }
1529
- /** Provides a value of a context to the scopes inside `scope`. */
1530
- function provideValue(context, value, scope) {
1531
- const target = resolveScope(scope);
1532
- target.v ??= {};
1533
- target.v[context.key] = value;
1534
- }
1535
- /** Reads a context, and records its slot so that changes to the provided value reach it. */
1536
- function consumeContext(context, scope, slot) {
1537
- const target = resolveScope(scope);
1538
- if (slot !== void 0) {
1539
- target.x ??= {};
1540
- target.x[context.key] = slot;
1541
- }
1542
- const provider = findProvider(target, context.key);
1543
- if (provider) return provider.v[context.key];
1544
- if (context.key === "brumate.route" && !globals.has("brumate.route")) globals.set(ROUTE_KEY, currentRoute());
1545
- return globals.has(context.key) ? globals.get(context.key) : context.defaultValue;
1546
- }
1547
- /** The values of the contexts that the runtime provides to the whole page, by key. */
1548
- const globals = /* @__PURE__ */ new Map();
1549
- /** The route of the page: `location`, and the params of the route from the last page state. */
1550
- function currentRoute() {
1551
- const records = document.querySelectorAll("script[b-state][b-params]");
1552
- const params = records[records.length - 1]?.getAttribute("b-params");
1553
- const url = new URL(location.href);
1554
- return {
1555
- url,
1556
- search: url.searchParams,
1557
- params: params ? JSON.parse(params) : {}
1558
- };
1559
- }
1560
- /**
1561
- * Writes a context that the runtime provides to the whole page, and the
1562
- * slot of every scope that reads it without a provider above it.
1563
- */
1564
- function setGlobal(key, value) {
1565
- globals.set(key, value);
1566
- for (const target of scopes.values()) {
1567
- const slot = target.x?.[key];
1568
- if (slot !== void 0 && !findProvider(target, key)) set(target, slot, value);
1569
- }
1570
- }
1571
- /** Reads the route of the page again, after navigation changed it. */
1572
- function updateRoute() {
1573
- setGlobal(ROUTE_KEY, currentRoute());
1574
- }
1575
- /**
1576
- * Slots that the render of a component in the browser filled with a promise,
1577
- * by `scope:slot`. The first compute of their memo takes that promise
1578
- * instead of calling again, so a server function is called once per row.
1579
- */
1580
- const renderedLoads = /* @__PURE__ */ new Set();
1581
- function setSlots(id, values) {
1582
- scopes.get(id).s = values;
1583
- values.forEach((value, slot) => {
1584
- if (isThenable(value)) {
1585
- renderedLoads.add(`${id}:${slot}`);
1586
- Promise.resolve(value).catch(noop);
1587
- }
1588
- });
1589
- }
1590
- function setItem(id, value) {
1591
- scopes.get(id).i = value;
1592
- }
1593
- function elementAt(scope, anchor) {
1594
- return new ElementReference(scope, anchor);
1595
- }
1596
- /**
1597
- * Warns in development that the browser renders a `#defer` block of a
1598
- * component, such as inside a `#client` body of its parent. Only the server
1599
- * renders the body, so the fallback stays.
1600
- */
1601
- function deferInBrowser(id) {
1602
- if (DEV) console.warn(`${id} renders a #defer block in the browser, so it shows its fallback only. Render the component in markup that the server renders.`);
1603
- }
1604
- /** Adds the markup of a `#head` block to the document's head. */
1605
- function addHead(html) {
1606
- afterInsert.push(() => insertHTML({ parent: document.head }, html));
1607
- }
1608
- /** Adds the markup of a `#portal` block at the end of the page, after the markup around it. */
1609
- function addPortal(html) {
1610
- afterInsert.push(() => insertHTML({ parent: document.body }, html));
1611
- }
1612
- /** Adds a component's stylesheet to the page, if the server did not. */
1613
- function addStyle(id, css) {
1614
- if (!document.querySelector(`style[b-style="${id}"]`)) {
1615
- const sheet = document.createElement("style");
1616
- sheet.setAttribute("b-style", id);
1617
- sheet.textContent = css;
1618
- document.head.append(sheet);
1619
- }
1620
- return "";
1621
- }
1622
- /** A function prop rendered on the client calls the parent's exported handler. */
1623
- function handlerProp(value, ref, scope) {
1624
- if (typeof value !== "function" || isTemplate(ref)) return value;
1625
- const instance = resolveScope(scope);
1626
- return (...args) => call(ref, instance, args);
1627
- }
1628
- /**
1629
- * What the loader calls. It is one export, so the loader's import of the core
1630
- * names one export, and a build keeps the rest only when components call them.
1631
- */
1632
- const loader = {
1633
- init,
1634
- handle,
1635
- dispose,
1636
- submitAction,
1637
- setGlobal,
1638
- updateRoute
1639
- };
1640
- //#endregion
1641
- export { rpcPrefix as $, handlerProp as A, makeId as B, elementAt as C, get as D, forget as E, leaveRoutes as F, prop as G, offscreen as H, list as I, remount as J, provideValue as K, loadModule as L, init as M, insert as N, guess as O, leavePage as P, rpc as Q, loadPart as R, effect as S, fill as T, pending as U, memo as V, portalTo as W, removeScope as X, removeRoot as Y, reset as Z, createScope as _, writeText as _t, addStyle as a, setOwner as at, dispose as b, attr as c, skew as ct, build as d, timed as dt, scopeOf as et, cells as f, title as ft, content as g, updateRoute as gt, consumeContext as h, updateContext as ht, addPortal as i, setItem as it, hooks as j, handle as k, attrs as l, submitAction as lt, component as m, update as mt, action as n, set as nt, afterInsert as o, setSlots as ot, children as p, track as pt, rekey as q, addHead as r, setGlobal as rt, asyncMemo as s, setVar as st, DEVTOOLS as t, scopes as tt, branch as u, text as ut, deferInBrowser as v, fail as w, dynamic as x, descendants as y, loader as z };
1642
-
1643
- //# sourceMappingURL=client-C0UCNL3J.js.map