brumate 0.3.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.
- package/README.md +2 -2
- package/check.d.ts +49 -0
- package/dist/{boot-C25Sa6vq.d.ts → boot-DLlRmD9z.d.ts} +8 -10
- package/dist/cli.js +275 -15
- package/dist/cli.js.map +1 -1
- package/dist/client-BvDsC6Qr.js +3463 -0
- package/dist/client-BvDsC6Qr.js.map +1 -0
- package/dist/{client-D0CqSxoO.d.ts → client-DBwL2wKQ.d.ts} +390 -181
- package/dist/{client-routes-JTbGtukf.js → client-routes-BtGpYCef.js} +60 -30
- package/dist/client-routes-BtGpYCef.js.map +1 -0
- package/dist/{cookies-BzllthiF.js → cookies-Cnbf4w-S.js} +14 -7
- package/dist/cookies-Cnbf4w-S.js.map +1 -0
- package/dist/{defer-BbD7JQhj.js → defer-CKWZLv2K.js} +4 -4
- package/dist/defer-CKWZLv2K.js.map +1 -0
- package/dist/{devtools-CQgD7NPB.js → devtools-4uTPwR_v.js} +56 -82
- package/dist/devtools-4uTPwR_v.js.map +1 -0
- package/dist/{devtools-panel-BVB-W5q4.js → devtools-panel-Be9pxYgv.js} +397 -333
- package/dist/devtools-panel-Be9pxYgv.js.map +1 -0
- package/dist/{events-CSs7OwIC.js → events-LgLdp1R7.js} +34 -5
- package/dist/events-LgLdp1R7.js.map +1 -0
- package/dist/features-CHY5Ekku.js +30 -0
- package/dist/features-CHY5Ekku.js.map +1 -0
- package/dist/files-WW4RlajL.js +506 -0
- package/dist/files-WW4RlajL.js.map +1 -0
- package/dist/{forms-Cl21hkHt.d.ts → forms-j1xPAR14.d.ts} +3 -3
- package/dist/{forms-Bg-CwGqX.js → forms-nXK7-Lv_.js} +78 -44
- package/dist/forms-nXK7-Lv_.js.map +1 -0
- package/dist/highlight.js +31 -22
- package/dist/highlight.js.map +1 -1
- package/dist/{html-mNTKzaDD.js → html-BtKRyi7g.js} +86 -22
- package/dist/html-BtKRyi7g.js.map +1 -0
- package/dist/{html-Bq1rFxX_.d.ts → html-Bw9Qwu6F.d.ts} +20 -5
- package/dist/index.d.ts +96 -10
- package/dist/index.js +639 -104
- package/dist/index.js.map +1 -1
- package/dist/{inspect-BHpOw1LF.js → inspect-Y6y4uNZp.js} +3751 -676
- package/dist/inspect-Y6y4uNZp.js.map +1 -0
- package/dist/{loader-Dos_3NP0.js → loader-BjKGJzLN.js} +20 -14
- package/dist/loader-BjKGJzLN.js.map +1 -0
- package/dist/logo-DQ_OYhBi.js +10 -0
- package/dist/logo-DQ_OYhBi.js.map +1 -0
- package/dist/{navigate-O78d_zGN.js → navigate-CfOr-TRF.js} +94 -31
- package/dist/navigate-CfOr-TRF.js.map +1 -0
- package/dist/nesting-DA5vJw9X.js +265 -0
- package/dist/nesting-DA5vJw9X.js.map +1 -0
- package/dist/{produce-Doc91GkX.js → produce-B7X9ori9.js} +137 -9
- package/dist/produce-B7X9ori9.js.map +1 -0
- package/dist/{produce-D0V0ICkz.d.ts → produce-DIgHBVwL.d.ts} +39 -5
- package/dist/{rpc-ygMRPmN4.js → rpc-Q-XkLwFh.js} +11 -6
- package/dist/rpc-Q-XkLwFh.js.map +1 -0
- package/dist/runtime/batch.js +9 -7
- package/dist/runtime/batch.js.map +1 -1
- package/dist/runtime/client.d.ts +4 -4
- package/dist/runtime/client.js +4 -4
- package/dist/runtime/devtools.d.ts +11 -29
- package/dist/runtime/devtools.js +3 -1
- package/dist/runtime/env.js +14 -2
- package/dist/runtime/env.js.map +1 -1
- package/dist/runtime/events.d.ts +23 -1
- package/dist/runtime/events.js +1 -1
- package/dist/runtime/forms.d.ts +2 -2
- package/dist/runtime/forms.js +1 -1
- package/dist/runtime/hot.d.ts +1 -1
- package/dist/runtime/hot.js +5 -2
- package/dist/runtime/hot.js.map +1 -1
- package/dist/runtime/html.d.ts +2 -2
- package/dist/runtime/html.js +1 -1
- package/dist/runtime/loader.d.ts +2 -2
- package/dist/runtime/loader.js +2 -2
- package/dist/runtime/routes.js +4 -1
- package/dist/runtime/routes.js.map +1 -1
- package/dist/runtime/server.d.ts +5 -5
- package/dist/runtime/server.js +5 -5
- package/dist/runtime/signals.d.ts +38 -1
- package/dist/runtime/signals.js +33 -1
- package/dist/runtime/signals.js.map +1 -1
- package/dist/runtime/static-loader.d.ts +2 -2
- package/dist/runtime/static-loader.js +1 -1
- package/dist/runtime/testing.d.ts +18 -3
- package/dist/runtime/testing.js +150 -33
- package/dist/runtime/testing.js.map +1 -1
- package/dist/{server-DuFYRXrd.d.ts → server-BvPIg9pT.d.ts} +84 -22
- package/dist/{server-CxVDYE_V.js → server-DjyOVJtJ.js} +598 -164
- package/dist/server-DjyOVJtJ.js.map +1 -0
- package/dist/{static-loader-Drpr0AzI.d.ts → static-loader-BGgdp_Y5.d.ts} +2 -2
- package/dist/{static-loader-nZssh66R.js → static-loader-BZR17hSw.js} +78 -99
- package/dist/static-loader-BZR17hSw.js.map +1 -0
- package/dist/{stream-DCYZO4IF.js → stream-BP5AVmg5.js} +102 -16
- package/dist/stream-BP5AVmg5.js.map +1 -0
- package/dist/vite.d.ts +13 -20
- package/dist/vite.js +1890 -548
- package/dist/vite.js.map +1 -1
- package/elements.d.ts +951 -0
- package/forms/forms.brm.ts +3 -3
- package/package.json +12 -3
- package/skills/brumate/SKILL.md +58 -0
- package/skills/brumate/references/errors.md +2078 -0
- package/skills/brumate/references/events.md +117 -0
- package/skills/brumate/references/signals.md +61 -0
- package/skills/brumate/references/state.md +235 -0
- package/skills/brumate/references/syntax.md +250 -0
- package/types.d.ts +4 -4
- package/dist/client-BD6XT-s5.js +0 -1968
- package/dist/client-BD6XT-s5.js.map +0 -1
- package/dist/client-routes-JTbGtukf.js.map +0 -1
- package/dist/cookies-BzllthiF.js.map +0 -1
- package/dist/defer-BbD7JQhj.js.map +0 -1
- package/dist/devtools-CQgD7NPB.js.map +0 -1
- package/dist/devtools-panel-BVB-W5q4.js.map +0 -1
- package/dist/events-CSs7OwIC.js.map +0 -1
- package/dist/files-BXSSrQgc.js +0 -205
- package/dist/files-BXSSrQgc.js.map +0 -1
- package/dist/forms-Bg-CwGqX.js.map +0 -1
- package/dist/html-mNTKzaDD.js.map +0 -1
- package/dist/inspect-BHpOw1LF.js.map +0 -1
- package/dist/loader-Dos_3NP0.js.map +0 -1
- package/dist/navigate-O78d_zGN.js.map +0 -1
- package/dist/produce-Doc91GkX.js.map +0 -1
- package/dist/rpc-ygMRPmN4.js.map +0 -1
- package/dist/server-CxVDYE_V.js.map +0 -1
- package/dist/static-loader-nZssh66R.js.map +0 -1
- package/dist/stream-DCYZO4IF.js.map +0 -1
|
@@ -0,0 +1,3463 @@
|
|
|
1
|
+
import { S as spread$1, _ as onAttribute, f as handlerEntry, n as VOID_ELEMENTS, o as attributeText, p as insertValue, r as actionAttributes, s as bindEntry, v as onValue, w as textValue, y as ownValue } from "./html-BtKRyi7g.js";
|
|
2
|
+
import { a as HAS_CLIENT_HTML, c as HAS_TEMPLATES, i as HAS_CHILDREN, l as HAS_TITLES, n as HAS_BINDINGS, o as HAS_LATE_MARKUP } from "./features-CHY5Ekku.js";
|
|
3
|
+
import { C as useSerialEntry, _ as mergeHandlers, a as ElementReference, b as parseId, c as PARTS, d as SlotReference, f as addEntryHandlers, g as isSlotReference, h as isPropWrapper, i as ChildrenReference, m as isBinding, n as Binding, o as FunctionReference, p as isAsyncIterable, s as MemoError, x as stateValues } from "./produce-B7X9ori9.js";
|
|
4
|
+
import { n as ROUTE_KEY } from "./context-CTiSzQRc.js";
|
|
5
|
+
import { n as trustedHTML, t as loadOnce } from "./skew-FRmAy9iy.js";
|
|
6
|
+
//#region src/runtime/client/state.ts
|
|
7
|
+
/** The part that holds an export: `h<n>` and `c<n>` have their own, the rest are in `update`. */
|
|
8
|
+
function getPart(name) {
|
|
9
|
+
return /^[hct]\d/.test(name) ? name : "update";
|
|
10
|
+
}
|
|
11
|
+
let options;
|
|
12
|
+
/** The URL prefix of server function calls, from the options. */
|
|
13
|
+
let rpcPrefix = "/_brumate";
|
|
14
|
+
/** The ID of the build of the page, from the loader. Undefined on the dev server. */
|
|
15
|
+
let build;
|
|
16
|
+
/** Sets the options of the page, with the prefix of server function calls and the build. */
|
|
17
|
+
function configure(value) {
|
|
18
|
+
options = value;
|
|
19
|
+
rpcPrefix = value.rpc ?? "/_brumate";
|
|
20
|
+
build = value.build;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Tells the page that the site changed since it loaded: a call answered 409
|
|
24
|
+
* with another build, or the code of a handler is gone. The loader then
|
|
25
|
+
* loads the next page in full, and an app can save a draft first.
|
|
26
|
+
*/
|
|
27
|
+
function skew(reason) {
|
|
28
|
+
document.dispatchEvent(new CustomEvent("brumate:skew", { detail: reason }));
|
|
29
|
+
}
|
|
30
|
+
/** The scopes of the page, by ID. */
|
|
31
|
+
const scopes = /* @__PURE__ */ new Map();
|
|
32
|
+
/** What modules of optional features add to the core. See `client-routes.ts` and `hot`. */
|
|
33
|
+
const hooks = {};
|
|
34
|
+
/** The global key of the devtools hooks. */
|
|
35
|
+
const DEVTOOLS = Symbol.for("brumate.devtools");
|
|
36
|
+
/**
|
|
37
|
+
* True on the dev server, which has the devtools. A Vite build replaces it
|
|
38
|
+
* with `false`, so the calls of the devtools hooks are left out. Without
|
|
39
|
+
* Vite, `import.meta.env` is undefined, and the hooks are off.
|
|
40
|
+
*/
|
|
41
|
+
const DEV = import.meta.env?.DEV;
|
|
42
|
+
function devtools() {
|
|
43
|
+
return globalThis[DEVTOOLS];
|
|
44
|
+
}
|
|
45
|
+
const modules = /* @__PURE__ */ new Map();
|
|
46
|
+
const loading = /* @__PURE__ */ new Map();
|
|
47
|
+
const valueRenders = {};
|
|
48
|
+
const parts = /* @__PURE__ */ new Map();
|
|
49
|
+
const loadedParts = /* @__PURE__ */ new Map();
|
|
50
|
+
const comments = /* @__PURE__ */ new Map();
|
|
51
|
+
const elements = /* @__PURE__ */ new Map();
|
|
52
|
+
const refs = /* @__PURE__ */ new Map();
|
|
53
|
+
const loads = /* @__PURE__ */ new Map();
|
|
54
|
+
const loadingSlots = /* @__PURE__ */ new Set();
|
|
55
|
+
/** The timers of debounced and throttled memos, by `scope:slot`, with when each last computed. */
|
|
56
|
+
const timers = /* @__PURE__ */ new Map();
|
|
57
|
+
/** Aborts the server function calls of a memo's compute, by memo key. */
|
|
58
|
+
const memoControllers = /* @__PURE__ */ new Map();
|
|
59
|
+
/**
|
|
60
|
+
* The guesses of `$optimistic` values. The first `guess` sets it, so the
|
|
61
|
+
* code that reads guesses is left out of a build that makes none.
|
|
62
|
+
*/
|
|
63
|
+
let guesses;
|
|
64
|
+
/** Sets the guesses, which the first `guess` makes. */
|
|
65
|
+
function setGuesses(value) {
|
|
66
|
+
return guesses = value;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The code of hidden `#offscreen` bodies. The first body that hides sets it
|
|
70
|
+
* up, so the core of a page without one only checks that it is not there.
|
|
71
|
+
* A body is hidden with an inline `display: none !important` on each element
|
|
72
|
+
* at its top level, which no stylesheet overrides, and empty text for each
|
|
73
|
+
* text node. The nodes stay in the page, so form fields, focus and media
|
|
74
|
+
* keep their state.
|
|
75
|
+
*/
|
|
76
|
+
let hiding;
|
|
77
|
+
/** Sets the code of hidden bodies, which the first body that hides makes, or drops it. */
|
|
78
|
+
function setHiding(value) {
|
|
79
|
+
return hiding = value;
|
|
80
|
+
}
|
|
81
|
+
/** Children that render where they are inserted, and the scopes that they find contexts from. */
|
|
82
|
+
const insertedChildren = [];
|
|
83
|
+
/**
|
|
84
|
+
* Slots that the render of a component in the browser filled with a promise,
|
|
85
|
+
* by `scope:slot`. The first compute of their memo takes that promise
|
|
86
|
+
* instead of calling again, so a server function is called once per row.
|
|
87
|
+
*/
|
|
88
|
+
const renderedLoads = /* @__PURE__ */ new Set();
|
|
89
|
+
/**
|
|
90
|
+
* The promises that the page knows are values still loading: a memo that the server streams, and
|
|
91
|
+
* a memo that the browser rendered. Any other promise in a slot is a plain value.
|
|
92
|
+
*/
|
|
93
|
+
const loadingValues = /* @__PURE__ */ new WeakSet();
|
|
94
|
+
let exempt = 0;
|
|
95
|
+
/** Runs code that updates the page: an update of a write, a refresh, or children that render late. */
|
|
96
|
+
function updating(run) {
|
|
97
|
+
exempt++;
|
|
98
|
+
try {
|
|
99
|
+
return run();
|
|
100
|
+
} finally {
|
|
101
|
+
exempt--;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** What `$resolve` waits in, with the scope of each, checked when a slot changes or a scope leaves. */
|
|
105
|
+
const waiters = /* @__PURE__ */ new Map();
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/runtime/client/blocks.ts
|
|
108
|
+
/**
|
|
109
|
+
* Indexes the markers of markup that others insert, such as streamed
|
|
110
|
+
* `#loading` bodies and the pages that navigation adds, so `findComment` can
|
|
111
|
+
* take its records at once.
|
|
112
|
+
*/
|
|
113
|
+
let markerObserver;
|
|
114
|
+
/** Indexes the markers of markup that others insert from now on. */
|
|
115
|
+
function observeMarkers() {
|
|
116
|
+
markerObserver = new MutationObserver(indexAdded);
|
|
117
|
+
markerObserver.observe(document, {
|
|
118
|
+
childList: true,
|
|
119
|
+
subtree: true
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/** Stops indexing the markup that others insert. */
|
|
123
|
+
function stopObserving() {
|
|
124
|
+
markerObserver?.disconnect();
|
|
125
|
+
markerObserver = void 0;
|
|
126
|
+
}
|
|
127
|
+
const MARKER = /^[btilP]-?\d/;
|
|
128
|
+
function markerKey(data) {
|
|
129
|
+
return data[0] === "b" ? data.split(":").slice(0, 2).join(":") : data;
|
|
130
|
+
}
|
|
131
|
+
const SCOPE_MARKER = /^[tb](-?[\d.]+):/;
|
|
132
|
+
/** True for markup that names a scope from the server: a `b-b` or marker with an ID that is not negative. */
|
|
133
|
+
const SERVER_SCOPES = /b-b="\d|<!--[tb]\d/;
|
|
134
|
+
/** The scopes that the client made since the last insert, which the next insert holds. */
|
|
135
|
+
const rendered = [];
|
|
136
|
+
/** How many `#portal` markers the page has had, so a scope looks for its own only when any have. */
|
|
137
|
+
let portals = 0;
|
|
138
|
+
/**
|
|
139
|
+
* Names a comment of cloned markup that names a scope, such as the start of an item. The start
|
|
140
|
+
* of a portal, which its scope looks for when it goes, is indexed.
|
|
141
|
+
*/
|
|
142
|
+
function nameComment(place, data) {
|
|
143
|
+
place.data = data;
|
|
144
|
+
if (data[0] === "P") {
|
|
145
|
+
comments.set(data, place);
|
|
146
|
+
portals++;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Indexes the markers and the bound elements under `root`, so that updates find them without a
|
|
151
|
+
* search of the page. A search for each row of a long list takes time that grows with the list.
|
|
152
|
+
* With `ids`, it also collects the IDs of the scopes that the markup names. The IDs of scopes
|
|
153
|
+
* that the client made are negative.
|
|
154
|
+
*/
|
|
155
|
+
function indexMarkers(root, ids, withElements = true) {
|
|
156
|
+
if (withElements) indexElements(root, ids);
|
|
157
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_COMMENT);
|
|
158
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
159
|
+
const data = node.data;
|
|
160
|
+
if (!MARKER.test(data)) continue;
|
|
161
|
+
comments.set(markerKey(data), node);
|
|
162
|
+
if (data[0] === "P") portals++;
|
|
163
|
+
const match = ids && SCOPE_MARKER.exec(data);
|
|
164
|
+
if (match) ids.add(parseId(match[1]));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/** The size of the indexes at which they next drop the nodes that left the page. */
|
|
168
|
+
let sweepAt = 4096;
|
|
169
|
+
/**
|
|
170
|
+
* Drops the nodes that left the page from the indexes, once they have grown to twice their size
|
|
171
|
+
* after the last sweep, so a list that the client fills and empties again does not keep its old
|
|
172
|
+
* rows. A node that comes back is indexed again when it is inserted.
|
|
173
|
+
*/
|
|
174
|
+
function sweepIndexes() {
|
|
175
|
+
if (comments.size + elements.size < sweepAt) return;
|
|
176
|
+
for (const [key, node] of comments) if (!node.isConnected) comments.delete(key);
|
|
177
|
+
for (const [key, node] of elements) if (!node.isConnected) elements.delete(key);
|
|
178
|
+
sweepAt = Math.max(4096, 2 * (comments.size + elements.size));
|
|
179
|
+
}
|
|
180
|
+
function indexElements(root, ids) {
|
|
181
|
+
if (root.nodeType === 1) indexElement(root, ids);
|
|
182
|
+
if (root.nodeType === 1 || root.nodeType === 9 || root.nodeType === 11) for (const element of root.querySelectorAll("[b-b]")) indexElement(element, ids);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* The nodes that inserts of the client put in the page, from the first to the last of each,
|
|
186
|
+
* whose bound elements are not indexed yet. A list that the client creates is often never
|
|
187
|
+
* updated row by row, so its elements are indexed when an update first needs one of them.
|
|
188
|
+
*/
|
|
189
|
+
const unindexed = [];
|
|
190
|
+
function indexInserts() {
|
|
191
|
+
for (const [first, last] of unindexed.splice(0)) if (first.isConnected) for (let node = first; node; node = node.nextSibling) {
|
|
192
|
+
indexElements(node);
|
|
193
|
+
if (node === last) break;
|
|
194
|
+
}
|
|
195
|
+
else if (last.isConnected) for (let node = last; node; node = node.previousSibling) indexElements(node);
|
|
196
|
+
}
|
|
197
|
+
function indexElement(element, ids) {
|
|
198
|
+
const key = element.getAttribute("b-b");
|
|
199
|
+
if (key === null) return;
|
|
200
|
+
elements.set(key, element);
|
|
201
|
+
ids?.add(parseId(key.slice(0, key.indexOf(":"))));
|
|
202
|
+
}
|
|
203
|
+
/** Indexes the markers in the nodes that mutation records added. */
|
|
204
|
+
function indexAdded(records) {
|
|
205
|
+
for (const record of records) for (const node of record.addedNodes) if (node.nodeType === 1) indexMarkers(node);
|
|
206
|
+
else if (node.nodeType === 8 && MARKER.test(node.data)) {
|
|
207
|
+
const data = node.data;
|
|
208
|
+
comments.set(markerKey(data), node);
|
|
209
|
+
if (data[0] === "P") portals++;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Finds a marker in the page. Returns undefined when the markup is not in
|
|
214
|
+
* the page, such as `children` inside a block that is closed. Updates to it
|
|
215
|
+
* do nothing, and it updates when the client inserts it again.
|
|
216
|
+
*/
|
|
217
|
+
function findComment(key) {
|
|
218
|
+
let comment = comments.get(key);
|
|
219
|
+
if (!comment?.isConnected) {
|
|
220
|
+
if (HAS_LATE_MARKUP && markerObserver) indexAdded(markerObserver.takeRecords());
|
|
221
|
+
else if (HAS_LATE_MARKUP || document.readyState === "loading") indexMarkers(document.documentElement);
|
|
222
|
+
comment = comments.get(key);
|
|
223
|
+
}
|
|
224
|
+
return comment?.isConnected ? comment : void 0;
|
|
225
|
+
}
|
|
226
|
+
function findElement(scope, anchor) {
|
|
227
|
+
const own = scopes.get(scope)?.n?.[anchor];
|
|
228
|
+
if (own?.isConnected) return own;
|
|
229
|
+
const key = `${scope}:${anchor}`;
|
|
230
|
+
let element = elements.get(key);
|
|
231
|
+
if (!element?.isConnected) {
|
|
232
|
+
if (HAS_LATE_MARKUP && markerObserver) indexAdded(markerObserver.takeRecords());
|
|
233
|
+
if (HAS_CLIENT_HTML) indexInserts();
|
|
234
|
+
element = elements.get(key);
|
|
235
|
+
if (!element?.isConnected) {
|
|
236
|
+
element = document.querySelector(`[b-b="${key}"]`) ?? void 0;
|
|
237
|
+
if (element) elements.set(key, element);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return element?.isConnected ? element : null;
|
|
241
|
+
}
|
|
242
|
+
/** The start marker of a block: kept on its scope in a clone, or found in the page. */
|
|
243
|
+
function blockStart(scope, anchor) {
|
|
244
|
+
const kept = scope.n?.[anchor];
|
|
245
|
+
return kept?.isConnected && kept.nodeType === 8 ? kept : findComment(`b${scope.id}:${anchor}`);
|
|
246
|
+
}
|
|
247
|
+
/** Returns the marker that ends the range that `start` opens. */
|
|
248
|
+
function rangeEnd(start) {
|
|
249
|
+
let depth = 0;
|
|
250
|
+
for (let node = start.nextSibling; node; node = node.nextSibling) if (node.nodeType === 8) {
|
|
251
|
+
const data = node.data;
|
|
252
|
+
if (data[0] === "/") {
|
|
253
|
+
if (depth === 0) return node;
|
|
254
|
+
depth--;
|
|
255
|
+
} else if (MARKER.test(data)) depth++;
|
|
256
|
+
}
|
|
257
|
+
throw new Error(`Missing end marker for <!--${start.data}-->. A <p> or another element that closes early around a block, such as a <div>, splits its markers.`);
|
|
258
|
+
}
|
|
259
|
+
function clear(start, end) {
|
|
260
|
+
if (start.nextSibling === end || !start.nextSibling) return;
|
|
261
|
+
const range = document.createRange();
|
|
262
|
+
range.setStartAfter(start);
|
|
263
|
+
range.setEndBefore(end);
|
|
264
|
+
range.deleteContents();
|
|
265
|
+
}
|
|
266
|
+
//#endregion
|
|
267
|
+
//#region src/runtime/client/code.ts
|
|
268
|
+
function loadModule(id) {
|
|
269
|
+
let promise = loading.get(id);
|
|
270
|
+
if (!promise) {
|
|
271
|
+
promise = Promise.resolve(options.load(id)).then((loaded) => {
|
|
272
|
+
const module = loaded ?? {
|
|
273
|
+
id,
|
|
274
|
+
props: {},
|
|
275
|
+
g: {}
|
|
276
|
+
};
|
|
277
|
+
modules.set(id, module);
|
|
278
|
+
for (const render of module.components ?? []) valueRenders[render.brumateId] = render;
|
|
279
|
+
return module;
|
|
280
|
+
});
|
|
281
|
+
loading.set(id, promise);
|
|
282
|
+
}
|
|
283
|
+
return promise;
|
|
284
|
+
}
|
|
285
|
+
function loadPart(module, name) {
|
|
286
|
+
const key = `${module.id}:${name}`;
|
|
287
|
+
let promise = parts.get(key);
|
|
288
|
+
if (!promise) {
|
|
289
|
+
let loaded;
|
|
290
|
+
if (!module.load) loaded = Promise.resolve(module);
|
|
291
|
+
else loaded = options.loadPart ? options.loadPart(module, name) : module.load(name);
|
|
292
|
+
promise = loaded.then((part) => {
|
|
293
|
+
loadedParts.set(key, part);
|
|
294
|
+
return part;
|
|
295
|
+
});
|
|
296
|
+
parts.set(key, promise);
|
|
297
|
+
}
|
|
298
|
+
return promise;
|
|
299
|
+
}
|
|
300
|
+
/** True for a reference to a `#template` that a component got as a prop, such as `Page#t3`. */
|
|
301
|
+
function isTemplate(ref) {
|
|
302
|
+
return /#t\d/.test(ref);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* The code of the components on the page that is still loading. Handlers, updates and calls of
|
|
306
|
+
* functions of other components wait for it, so the code that they call has loaded.
|
|
307
|
+
*/
|
|
308
|
+
const loadingCode = /* @__PURE__ */ new Set();
|
|
309
|
+
/** The components whose code loads, or has loaded, with the page. */
|
|
310
|
+
const pageComponents = /* @__PURE__ */ new Set();
|
|
311
|
+
/** Loads the code of a component and all of its parts, as the page reads its scopes. */
|
|
312
|
+
function loadComponent(id) {
|
|
313
|
+
if (options === void 0 || pageComponents.has(id)) return;
|
|
314
|
+
pageComponents.add(id);
|
|
315
|
+
const code = loadModule(id).catch(() => {}).finally(() => loadingCode.delete(code));
|
|
316
|
+
loadingCode.add(code);
|
|
317
|
+
}
|
|
318
|
+
/** Resolves when the code of the components on the page has loaded. */
|
|
319
|
+
function codeReady() {
|
|
320
|
+
return loadingCode.size > 0 ? Promise.allSettled(loadingCode) : void 0;
|
|
321
|
+
}
|
|
322
|
+
/** The function of a reference, such as `Page#h3`, once its code has loaded. */
|
|
323
|
+
function loadedFunction(ref) {
|
|
324
|
+
const [id, name] = ref.split("#");
|
|
325
|
+
const module = modules.get(id);
|
|
326
|
+
if (!module) return;
|
|
327
|
+
return (module.load ? loadedParts.get(`${id}:${getPart(name)}`) : module)?.[name];
|
|
328
|
+
}
|
|
329
|
+
//#endregion
|
|
330
|
+
//#region src/runtime/client/scopes.ts
|
|
331
|
+
let nextId = -1;
|
|
332
|
+
/** Starts the IDs of the scopes that the client makes again, for a new page. */
|
|
333
|
+
function resetIds() {
|
|
334
|
+
nextId = -1;
|
|
335
|
+
}
|
|
336
|
+
/** Returns the instance of a scope by its ID. */
|
|
337
|
+
function getScope(id) {
|
|
338
|
+
return scopes.get(id);
|
|
339
|
+
}
|
|
340
|
+
function findRoot(scope) {
|
|
341
|
+
let current = scope;
|
|
342
|
+
while (current.p) current = current.p;
|
|
343
|
+
return current;
|
|
344
|
+
}
|
|
345
|
+
/** Counts changes to the scope tree, so `run` knows when the descendants it found are out of date. */
|
|
346
|
+
let treeChanges = 0;
|
|
347
|
+
/** Counts a change to the scope tree that `addScopes` makes. */
|
|
348
|
+
function treeChanged() {
|
|
349
|
+
treeChanges++;
|
|
350
|
+
}
|
|
351
|
+
function descendants(scope, kind, result = []) {
|
|
352
|
+
for (const child of scope.children ?? []) {
|
|
353
|
+
if (child.k === kind) result.push(child);
|
|
354
|
+
descendants(child, kind, result);
|
|
355
|
+
}
|
|
356
|
+
return result;
|
|
357
|
+
}
|
|
358
|
+
function destroy(scope) {
|
|
359
|
+
if (scopes.get(scope.id) !== scope) return;
|
|
360
|
+
if (scope.children?.size) for (const child of Array.from(scope.children ?? [])) destroy(child);
|
|
361
|
+
if (scope.owned) for (const child of Array.from(scope.owned)) destroy(child);
|
|
362
|
+
const start = portals > 0 ? comments.get(`P${scope.id}`) : void 0;
|
|
363
|
+
if (start?.isConnected) {
|
|
364
|
+
const end = rangeEnd(start);
|
|
365
|
+
destroyRange(scope, start, end);
|
|
366
|
+
clear(start, end);
|
|
367
|
+
start.remove();
|
|
368
|
+
end.remove();
|
|
369
|
+
}
|
|
370
|
+
removeScope(scope);
|
|
371
|
+
sweepIndexes();
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Removes a scope from the page: its cleanups run, and its memo state goes,
|
|
375
|
+
* so a load that settles later is not written. Its markup stays.
|
|
376
|
+
*/
|
|
377
|
+
function removeScope(scope) {
|
|
378
|
+
if (scope.cleanups) for (const cleanup of scope.cleanups.values()) cleanup();
|
|
379
|
+
const waiting = memoControllers.size + timers.size + loads.size + loadingSlots.size + (guesses?.map.size ?? 0);
|
|
380
|
+
for (let slot = 0; waiting > 0 && slot < scope.s.length; slot++) {
|
|
381
|
+
const key = `${scope.id}:${slot}`;
|
|
382
|
+
memoControllers.get(key)?.abort();
|
|
383
|
+
memoControllers.delete(key);
|
|
384
|
+
clearTimeout(timers.get(key)?.timer);
|
|
385
|
+
timers.delete(key);
|
|
386
|
+
loads.delete(key);
|
|
387
|
+
loadingSlots.delete(key);
|
|
388
|
+
guesses?.map.delete(key);
|
|
389
|
+
}
|
|
390
|
+
if (scope.h) hiding?.bodies.delete(scope.h);
|
|
391
|
+
scopes.delete(scope.id);
|
|
392
|
+
for (const [check, owner] of waiters) if (owner === scope) check();
|
|
393
|
+
scope.p?.children?.delete(scope);
|
|
394
|
+
scope.o?.owned?.delete(scope);
|
|
395
|
+
treeChanges++;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Removes the markup of a block of `scope`, between its markers, with the scopes in it: the
|
|
399
|
+
* scopes of its kinds, and the scopes whose markup is in it. Those are the components that the
|
|
400
|
+
* block rendered, whose owner can be `scope` itself, and the children that a component inserted
|
|
401
|
+
* there. A scope that holds `scope` keeps its markers in the block, so it stays.
|
|
402
|
+
*/
|
|
403
|
+
function removeBlock(scope, kinds, start, end) {
|
|
404
|
+
disposeKinds(scope, kinds);
|
|
405
|
+
destroyRange(scope, start, end);
|
|
406
|
+
clear(start, end);
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Destroys the scopes whose markup is between two nodes, such as the components of a block that
|
|
410
|
+
* closes, but not the scope of the block or those around it.
|
|
411
|
+
*/
|
|
412
|
+
function destroyRange(scope, start, end) {
|
|
413
|
+
if (scopes.size > 0 && start.nextSibling !== end) {
|
|
414
|
+
const above = /* @__PURE__ */ new Set();
|
|
415
|
+
for (let current = scope; current; current = current.p ?? current.o) above.add(current);
|
|
416
|
+
const inside = /* @__PURE__ */ new Set();
|
|
417
|
+
function add(id) {
|
|
418
|
+
const found = scopes.get(parseId(id));
|
|
419
|
+
if (found && !above.has(found)) inside.add(found);
|
|
420
|
+
}
|
|
421
|
+
for (let node = start.nextSibling; node && node !== end; node = node.nextSibling) {
|
|
422
|
+
const walker = document.createTreeWalker(node, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT);
|
|
423
|
+
for (let current = walker.currentNode; current; current = walker.nextNode()) if (current.nodeType === 1) {
|
|
424
|
+
const key = current.getAttribute("b-b");
|
|
425
|
+
if (key !== null) add(key.slice(0, key.indexOf(":")));
|
|
426
|
+
} else if (MARKER.test(current.data)) add(current.data.slice(1).split(":")[0]);
|
|
427
|
+
}
|
|
428
|
+
for (const found of inside) destroy(found);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
function disposeKinds(scope, kinds) {
|
|
432
|
+
for (const child of Array.from(scope.children ?? [])) if (kinds.includes(child.k)) destroy(child);
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* The component that holds a block of `scope` in its children, from the kind of the scope that
|
|
436
|
+
* renders the component and the anchor of the component there, which the compiler gives.
|
|
437
|
+
*/
|
|
438
|
+
function findHolder(scope, kind, anchor) {
|
|
439
|
+
if (kind === void 0 || anchor === void 0) return;
|
|
440
|
+
for (let current = scope; current; current = current.p) if (current.k === kind) {
|
|
441
|
+
const id = current.c?.[anchor];
|
|
442
|
+
const holder = id === void 0 ? void 0 : scopes.get(id);
|
|
443
|
+
return holder?.f ?? holder;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* A render of a body of a block in the children of a component, which renders as children that
|
|
448
|
+
* the component inserted, so the scopes that it makes read the contexts there, as the scopes
|
|
449
|
+
* that the server made do.
|
|
450
|
+
*/
|
|
451
|
+
function inPlace(scope, render, kind, anchor) {
|
|
452
|
+
const holder = render && findHolder(scope, kind, anchor);
|
|
453
|
+
if (!holder) return render;
|
|
454
|
+
return (parent, item, index) => {
|
|
455
|
+
const entry = {
|
|
456
|
+
from: scope,
|
|
457
|
+
at: holder
|
|
458
|
+
};
|
|
459
|
+
insertedChildren.push(entry);
|
|
460
|
+
try {
|
|
461
|
+
return render(parent, item, index);
|
|
462
|
+
} finally {
|
|
463
|
+
insertedChildren.splice(insertedChildren.indexOf(entry), 1);
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
function resolveScope(scope) {
|
|
468
|
+
if (typeof scope === "object") return scope;
|
|
469
|
+
return scope === void 0 ? void 0 : scopes.get(scope);
|
|
470
|
+
}
|
|
471
|
+
/** Work that waits until rendered markup is in the page. */
|
|
472
|
+
const afterInsert = [];
|
|
473
|
+
/** The scope that renders the next component, which becomes its owner. */
|
|
474
|
+
let owner;
|
|
475
|
+
/** Creates a scope for markup that the client renders, and returns its ID. */
|
|
476
|
+
function createScope(kind, parent, module, nodes) {
|
|
477
|
+
const id = nextId--;
|
|
478
|
+
const parentScope = resolveScope(parent);
|
|
479
|
+
const created = {
|
|
480
|
+
id,
|
|
481
|
+
k: kind,
|
|
482
|
+
p: parentScope,
|
|
483
|
+
m: module,
|
|
484
|
+
s: []
|
|
485
|
+
};
|
|
486
|
+
if (nodes) created.n = new Array(nodes);
|
|
487
|
+
rendered.push(created);
|
|
488
|
+
if (module !== void 0) loadComponent(module);
|
|
489
|
+
if (module !== void 0 && owner) {
|
|
490
|
+
created.o = owner;
|
|
491
|
+
(owner.owned ??= /* @__PURE__ */ new Set()).add(created);
|
|
492
|
+
owner = void 0;
|
|
493
|
+
}
|
|
494
|
+
const up = created.o ?? parentScope;
|
|
495
|
+
for (let index = insertedChildren.length - 1; index >= 0; index--) if (insertedChildren[index].from === up) {
|
|
496
|
+
created.t = insertedChildren[index].at;
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
if (parentScope) (parentScope.children ??= /* @__PURE__ */ new Set()).add(created);
|
|
500
|
+
treeChanges++;
|
|
501
|
+
scopes.set(id, created);
|
|
502
|
+
return id;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* The ID that `$id()` makes, as the server does. Client scope IDs are
|
|
506
|
+
* negative, so they differ from the server's.
|
|
507
|
+
*/
|
|
508
|
+
function makeId(scope, index) {
|
|
509
|
+
return `b${String(resolveScope(scope).id).replace(/\./g, "_")}-${index}`;
|
|
510
|
+
}
|
|
511
|
+
/** Sets the owner of the next component's root scope. */
|
|
512
|
+
function setOwner(scope) {
|
|
513
|
+
owner = resolveScope(scope);
|
|
514
|
+
}
|
|
515
|
+
/** The scope that `unbind` collects the promise props of its component in. */
|
|
516
|
+
function sink(scope) {
|
|
517
|
+
return scopes.get(scope);
|
|
518
|
+
}
|
|
519
|
+
//#endregion
|
|
520
|
+
//#region src/runtime/client/titles.ts
|
|
521
|
+
/** Sets the title of the page. */
|
|
522
|
+
function title(value) {
|
|
523
|
+
setTitle(value === null || value === void 0 ? "" : String(value));
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Sets `document.title`, which writes the first `<title>` of the document.
|
|
527
|
+
* The head gets a `<title>` of its own first, so a `<title>` in markup keeps its text.
|
|
528
|
+
*/
|
|
529
|
+
function setTitle(text) {
|
|
530
|
+
if (!document.head.querySelector("title:not([b-t])")) document.head.prepend(document.createElement("title"));
|
|
531
|
+
document.title = text;
|
|
532
|
+
}
|
|
533
|
+
/** Watches the `<title>` elements of markup, once one is in the page. */
|
|
534
|
+
let titleObserver;
|
|
535
|
+
const PAGE_TITLE = "title[b-t]";
|
|
536
|
+
/** True when a node is or holds a `<title>` of markup. */
|
|
537
|
+
function holdsTitle(node) {
|
|
538
|
+
return node instanceof Element && (node.matches(PAGE_TITLE) || !!node.querySelector(PAGE_TITLE));
|
|
539
|
+
}
|
|
540
|
+
/** Gives the page the text of its last `<title>` in markup. Without one, the title stays. */
|
|
541
|
+
function syncTitle() {
|
|
542
|
+
const titles = document.querySelectorAll(PAGE_TITLE);
|
|
543
|
+
const last = titles[titles.length - 1];
|
|
544
|
+
if (last && document.title !== last.textContent) setTitle(last.textContent ?? "");
|
|
545
|
+
}
|
|
546
|
+
function hasTitle(nodes) {
|
|
547
|
+
for (const node of nodes) if (holdsTitle(node)) return true;
|
|
548
|
+
return false;
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Starts to follow the `<title>` elements of markup, when `root` has one. A
|
|
552
|
+
* block that adds or removes one, or a change of its text, sets the title.
|
|
553
|
+
*/
|
|
554
|
+
function watchTitles(root) {
|
|
555
|
+
if (!HAS_TITLES || titleObserver || !root.querySelector(PAGE_TITLE)) return;
|
|
556
|
+
titleObserver = new MutationObserver((records) => {
|
|
557
|
+
for (const record of records) if ((record.target instanceof Element ? record.target : record.target.parentElement)?.closest(PAGE_TITLE) || hasTitle(record.addedNodes) || hasTitle(record.removedNodes)) {
|
|
558
|
+
syncTitle();
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
titleObserver.observe(document, {
|
|
563
|
+
childList: true,
|
|
564
|
+
subtree: true,
|
|
565
|
+
characterData: true
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
//#endregion
|
|
569
|
+
//#region src/runtime/client/insert.ts
|
|
570
|
+
/** True while `refresh` runs the updates of scopes that the server rendered. */
|
|
571
|
+
let refreshing = false;
|
|
572
|
+
/**
|
|
573
|
+
* Updates scopes that the server rendered before values changed on the
|
|
574
|
+
* client, such as the body of a `#loading` block that streams in after an
|
|
575
|
+
* event. Effects do not run: they run only when a value changes. With
|
|
576
|
+
* `memosOnly`, as for scopes that the client just rendered with its values,
|
|
577
|
+
* only the memos run, so the ones that load start.
|
|
578
|
+
*/
|
|
579
|
+
function refresh(added, memosOnly = false) {
|
|
580
|
+
const byModule = /* @__PURE__ */ new Map();
|
|
581
|
+
for (const scope of added) {
|
|
582
|
+
const id = findRoot(scope).m;
|
|
583
|
+
if (!id) continue;
|
|
584
|
+
const list = byModule.get(id);
|
|
585
|
+
if (list) list.push(scope);
|
|
586
|
+
else byModule.set(id, [scope]);
|
|
587
|
+
}
|
|
588
|
+
for (const [id, list] of byModule) loadModule(id).then(async (module) => {
|
|
589
|
+
const byKind = /* @__PURE__ */ new Map();
|
|
590
|
+
for (const scope of list) if (!byKind.has(scope.k)) byKind.set(scope.k, refreshNames(module, scope.k, memosOnly));
|
|
591
|
+
let any = false;
|
|
592
|
+
for (const names of byKind.values()) any ||= names.length > 0;
|
|
593
|
+
if (!any) return;
|
|
594
|
+
const updates = await loadPart(module, "update");
|
|
595
|
+
await codeReady();
|
|
596
|
+
refreshing = true;
|
|
597
|
+
try {
|
|
598
|
+
updating(() => {
|
|
599
|
+
for (const scope of list) for (const name of byKind.get(scope.k)) if (scopes.has(scope.id)) updates[name](scope);
|
|
600
|
+
});
|
|
601
|
+
} finally {
|
|
602
|
+
refreshing = false;
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
/** The names of the updates of a kind of scope that `refresh` runs, memos first. */
|
|
607
|
+
function refreshNames(module, kind, memosOnly) {
|
|
608
|
+
const memos = /* @__PURE__ */ new Set();
|
|
609
|
+
const others = /* @__PURE__ */ new Set();
|
|
610
|
+
for (const slots of Object.values(module.g)) for (const entries of Object.values(slots)) for (const [entryKind, name] of entries) {
|
|
611
|
+
if (entryKind !== kind || name[0] === "e") continue;
|
|
612
|
+
if (name[0] === "m") memos.add(name);
|
|
613
|
+
else if (!memosOnly) others.add(name);
|
|
614
|
+
}
|
|
615
|
+
return [...memos, ...others];
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Inserts rendered HTML before a node, or at the end of a parent, and runs
|
|
619
|
+
* the work that waits for it.
|
|
620
|
+
*/
|
|
621
|
+
function insertHTML(before, html) {
|
|
622
|
+
const cloned = typeof html !== "string";
|
|
623
|
+
let fragments;
|
|
624
|
+
if (typeof html === "string") fragments = [parseHTML(html, "parent" in before ? before.parent : before)];
|
|
625
|
+
else fragments = Array.isArray(html) ? html : [html];
|
|
626
|
+
if (HAS_LATE_MARKUP && markerObserver) indexAdded(markerObserver.takeRecords());
|
|
627
|
+
const client = rendered.splice(0);
|
|
628
|
+
const fromServer = !cloned && SERVER_SCOPES.test(html);
|
|
629
|
+
const ids = fromServer ? /* @__PURE__ */ new Set() : void 0;
|
|
630
|
+
const markup = clonedMarkup;
|
|
631
|
+
for (const fragment of fragments) {
|
|
632
|
+
if (!cloned || markup) {
|
|
633
|
+
indexMarkers(fragment, ids, fromServer || cloned || !HAS_CLIENT_HTML);
|
|
634
|
+
options.listen(fragment);
|
|
635
|
+
}
|
|
636
|
+
watchTitles(fragment);
|
|
637
|
+
}
|
|
638
|
+
clonedMarkup = false;
|
|
639
|
+
const server = [];
|
|
640
|
+
for (const id of ids ?? []) {
|
|
641
|
+
const scope = typeof id === "number" && id < 0 ? void 0 : scopes.get(id);
|
|
642
|
+
if (scope) server.push(scope);
|
|
643
|
+
}
|
|
644
|
+
const fragment = fragments[0];
|
|
645
|
+
if (HAS_CLIENT_HTML && !cloned && !fromServer && fragment.firstChild) {
|
|
646
|
+
unindexed.push([fragment.firstChild, fragment.lastChild]);
|
|
647
|
+
if (unindexed.length > 256) indexInserts();
|
|
648
|
+
}
|
|
649
|
+
let added;
|
|
650
|
+
if (hiding?.bodies.size) {
|
|
651
|
+
added = [];
|
|
652
|
+
for (const part of fragments) added.push(...part.childNodes);
|
|
653
|
+
}
|
|
654
|
+
for (const part of fragments) if ("parent" in before) before.parent.append(part);
|
|
655
|
+
else before.parentNode.insertBefore(part, before);
|
|
656
|
+
if (added) hiding.added(added);
|
|
657
|
+
if (HAS_LATE_MARKUP) markerObserver?.takeRecords();
|
|
658
|
+
options.inserted?.();
|
|
659
|
+
const queued = afterInsert.splice(0);
|
|
660
|
+
try {
|
|
661
|
+
for (let fn = queued.shift(); fn; fn = queued.shift()) {
|
|
662
|
+
fn();
|
|
663
|
+
queued.push(...afterInsert.splice(0));
|
|
664
|
+
}
|
|
665
|
+
} finally {
|
|
666
|
+
afterInsert.length = 0;
|
|
667
|
+
}
|
|
668
|
+
if (server.length > 0) refresh(server);
|
|
669
|
+
const live = [];
|
|
670
|
+
for (const scope of client) if (scopes.has(scope.id)) live.push(scope);
|
|
671
|
+
if (live.length > 0) refresh(live, true);
|
|
672
|
+
}
|
|
673
|
+
/** True when a clone holds markup that it rendered as HTML, whose markers its insert indexes. */
|
|
674
|
+
let clonedMarkup = false;
|
|
675
|
+
/** Notes that a clone holds markup that it rendered as HTML. */
|
|
676
|
+
function markCloned() {
|
|
677
|
+
clonedMarkup = true;
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* The clones of components that HTML holds a place for, by the number in the place,
|
|
681
|
+
* `<!--f` and the number. HTML that the browser parses gets them back in their places.
|
|
682
|
+
*/
|
|
683
|
+
const placed = /* @__PURE__ */ new Map();
|
|
684
|
+
let placedCount = 0;
|
|
685
|
+
/**
|
|
686
|
+
* `svg` or `math` when markup in an element, or next to a node, is SVG or MathML. HTML that the
|
|
687
|
+
* browser parses there needs one around it, as the parser of a page would have one.
|
|
688
|
+
*/
|
|
689
|
+
function findForeign(near) {
|
|
690
|
+
const element = near?.nodeType === 1 ? near : near?.parentNode;
|
|
691
|
+
if (element?.nodeType !== 1 || element.localName === "foreignObject") return;
|
|
692
|
+
const space = element.namespaceURI;
|
|
693
|
+
if (space === "http://www.w3.org/2000/svg") return "svg";
|
|
694
|
+
return space === "http://www.w3.org/1998/Math/MathML" ? "math" : void 0;
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Parses HTML that the browser renders, with the clones that it holds places for. `near` is
|
|
698
|
+
* the element that it goes in, or a node next to where it goes.
|
|
699
|
+
*/
|
|
700
|
+
function parseHTML(html, near, foreign = findForeign(near)) {
|
|
701
|
+
const template = document.createElement("template");
|
|
702
|
+
template.innerHTML = trustedHTML(foreign ? `<${foreign}>${html}</${foreign}>` : html);
|
|
703
|
+
const fragment = template.content;
|
|
704
|
+
if (foreign) fragment.replaceChildren(...fragment.firstChild.childNodes);
|
|
705
|
+
if (HAS_CLIENT_HTML && placed.size > 0 && html.includes("<!--f")) {
|
|
706
|
+
const walker = document.createTreeWalker(fragment, NodeFilter.SHOW_COMMENT);
|
|
707
|
+
const places = [];
|
|
708
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) if (/^f\d+$/.test(node.data)) places.push(node);
|
|
709
|
+
for (const place of places) {
|
|
710
|
+
const key = Number(place.data.slice(1));
|
|
711
|
+
const nodes = placed.get(key);
|
|
712
|
+
if (nodes) {
|
|
713
|
+
placed.delete(key);
|
|
714
|
+
place.replaceWith(nodes);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
return fragment;
|
|
719
|
+
}
|
|
720
|
+
/** The markup of a render, or for a clone, a comment that holds its place, which a parse fills. */
|
|
721
|
+
function holdPlace(out) {
|
|
722
|
+
if (typeof out === "string") return out;
|
|
723
|
+
const key = placedCount++;
|
|
724
|
+
placed.set(key, out);
|
|
725
|
+
return `<!--f${key}-->`;
|
|
726
|
+
}
|
|
727
|
+
//#endregion
|
|
728
|
+
//#region src/runtime/client/references.ts
|
|
729
|
+
/** Waits forever once the page loads again, so no error shows before it goes. */
|
|
730
|
+
let reloading;
|
|
731
|
+
async function resolve$1(ref) {
|
|
732
|
+
const [id, name] = ref.split("#");
|
|
733
|
+
try {
|
|
734
|
+
return (await loadPart(await loadModule(id), getPart(name)))[name];
|
|
735
|
+
} catch (error) {
|
|
736
|
+
const next = await serverBuild();
|
|
737
|
+
if (!reloading && next !== void 0 && loadOnce(next)) {
|
|
738
|
+
skew("code");
|
|
739
|
+
location.reload();
|
|
740
|
+
reloading = new Promise(() => {});
|
|
741
|
+
}
|
|
742
|
+
if (reloading) return reloading;
|
|
743
|
+
throw error;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* Asks the server for its build, after the code of a handler failed to
|
|
748
|
+
* load. Returns it when it is not the build of the page. Undefined without
|
|
749
|
+
* a build, when the request fails, or when the builds are the same.
|
|
750
|
+
*/
|
|
751
|
+
async function serverBuild() {
|
|
752
|
+
if (build === void 0) return;
|
|
753
|
+
try {
|
|
754
|
+
const current = (await fetch(`${rpcPrefix}/`, {
|
|
755
|
+
method: "POST",
|
|
756
|
+
headers: { "x-brumate-build": build }
|
|
757
|
+
})).headers.get("x-brumate-build");
|
|
758
|
+
return current !== null && current !== build ? current : void 0;
|
|
759
|
+
} catch {
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
let inPass = (run) => run();
|
|
764
|
+
function setPass(pass) {
|
|
765
|
+
inPass = pass;
|
|
766
|
+
}
|
|
767
|
+
let waiting$1 = 0;
|
|
768
|
+
/**
|
|
769
|
+
* Calls a function of another component at once, as a pass, when its code has loaded and no call
|
|
770
|
+
* waits before it, so the caller reads what the function wrote. Returns false when it did not
|
|
771
|
+
* call, and `call` is the way then.
|
|
772
|
+
*/
|
|
773
|
+
function callNow(ref, scope, args) {
|
|
774
|
+
const fn = waiting$1 === 0 && !codeReady() ? loadedFunction(ref) : void 0;
|
|
775
|
+
if (!fn) return false;
|
|
776
|
+
inPass(() => fn(scope, ...args));
|
|
777
|
+
return true;
|
|
778
|
+
}
|
|
779
|
+
let lastCall = Promise.resolve();
|
|
780
|
+
/**
|
|
781
|
+
* Calls a function of another component, such as a function prop or a provided function. The
|
|
782
|
+
* code of each loads in parallel, but the calls start in the order that they were made, so an
|
|
783
|
+
* `onChange` that an input event calls runs before an `onKeyDown` of a later key. The result is
|
|
784
|
+
* wrapped, so a call that its code makes does not wait for the call around it.
|
|
785
|
+
*/
|
|
786
|
+
function call(ref, scope, args) {
|
|
787
|
+
const code = codeReady();
|
|
788
|
+
const loaded = code ? Promise.all([resolve$1(ref), code]).then(([fn]) => fn) : resolve$1(ref);
|
|
789
|
+
waiting$1++;
|
|
790
|
+
const started = lastCall.then(() => loaded).finally(() => {
|
|
791
|
+
waiting$1--;
|
|
792
|
+
}).then((fn) => ({ value: inPass(() => fn(scope, ...args)) }));
|
|
793
|
+
lastCall = started.catch(() => {});
|
|
794
|
+
return started.then((result) => result.value);
|
|
795
|
+
}
|
|
796
|
+
//#endregion
|
|
797
|
+
//#region src/runtime/client/children.ts
|
|
798
|
+
/**
|
|
799
|
+
* A `#template` of another component, which renders at once, as markup does. Its code loads
|
|
800
|
+
* with the page, and updates wait for it.
|
|
801
|
+
*/
|
|
802
|
+
function templateReference(ref, scope) {
|
|
803
|
+
return (...args) => new TemplateCall(ref, scope, args, () => {
|
|
804
|
+
const render = loadedFunction(ref);
|
|
805
|
+
return render ? insertValue(render(scope, ...args)) : "";
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
/** Renders a value inserted in markup. Children render here, at `scope`, for their contexts. */
|
|
809
|
+
function insert(value, _ctx, scope) {
|
|
810
|
+
if (!((HAS_CHILDREN || HAS_TEMPLATES) && value instanceof Children)) return insertValue(value);
|
|
811
|
+
const out = value.render(resolveScope(scope));
|
|
812
|
+
return HAS_CLIENT_HTML ? holdPlace(out) : out;
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Children of a component that the client renders. They render where the
|
|
816
|
+
* component inserts them, so they read the contexts that it provides. `from`
|
|
817
|
+
* is the scope of the markup that wrote them.
|
|
818
|
+
*/
|
|
819
|
+
var Children = class {
|
|
820
|
+
given;
|
|
821
|
+
html;
|
|
822
|
+
constructor(from, html) {
|
|
823
|
+
this.given = from;
|
|
824
|
+
this.html = html;
|
|
825
|
+
}
|
|
826
|
+
/** The scope that the children were written in, found when they render. */
|
|
827
|
+
get from() {
|
|
828
|
+
const scope = resolveScope(this.given);
|
|
829
|
+
if (scope) this.given = scope;
|
|
830
|
+
return scope;
|
|
831
|
+
}
|
|
832
|
+
render(at) {
|
|
833
|
+
if (!at) return this.html();
|
|
834
|
+
let target = at;
|
|
835
|
+
for (let index = insertedChildren.length - 1; index >= 0; index--) if (insertedChildren[index].from === at) {
|
|
836
|
+
target = insertedChildren[index].at;
|
|
837
|
+
at.f = target;
|
|
838
|
+
break;
|
|
839
|
+
}
|
|
840
|
+
const entry = {
|
|
841
|
+
from: this.from,
|
|
842
|
+
at: target
|
|
843
|
+
};
|
|
844
|
+
insertedChildren.push(entry);
|
|
845
|
+
try {
|
|
846
|
+
return this.html();
|
|
847
|
+
} finally {
|
|
848
|
+
insertedChildren.splice(insertedChildren.indexOf(entry), 1);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
/** A call of a `#template` of another component, which an insert can update in place. */
|
|
853
|
+
var TemplateCall = class extends Children {
|
|
854
|
+
ref;
|
|
855
|
+
/** The scope of the component that wrote the template. */
|
|
856
|
+
owner;
|
|
857
|
+
args;
|
|
858
|
+
constructor(ref, owner, args, html) {
|
|
859
|
+
super(owner, html);
|
|
860
|
+
this.ref = ref;
|
|
861
|
+
this.owner = owner;
|
|
862
|
+
this.args = args;
|
|
863
|
+
}
|
|
864
|
+
};
|
|
865
|
+
/**
|
|
866
|
+
* Children that the server rendered, which the browser renders with their own export each time
|
|
867
|
+
* a component inserts them, so each insert makes new scopes, as the body of a block does. The
|
|
868
|
+
* code of the export loads before the code that can insert them runs. See `childrenReady`.
|
|
869
|
+
*/
|
|
870
|
+
var RenderedChildren = class extends Children {
|
|
871
|
+
ref;
|
|
872
|
+
/** The scope and the anchor of the insert where the server rendered them, if it did. */
|
|
873
|
+
at;
|
|
874
|
+
anchor;
|
|
875
|
+
code;
|
|
876
|
+
loading;
|
|
877
|
+
constructor(scope, ref, at, anchor) {
|
|
878
|
+
super(scope, () => {
|
|
879
|
+
const out = this.code(this.from);
|
|
880
|
+
return out instanceof DocumentFragment ? out : String(out);
|
|
881
|
+
});
|
|
882
|
+
this.ref = ref;
|
|
883
|
+
this.at = at;
|
|
884
|
+
this.anchor = anchor;
|
|
885
|
+
}
|
|
886
|
+
render(at) {
|
|
887
|
+
if (this.code && this.from) return super.render(at);
|
|
888
|
+
const marker = `C${nextChildren += 1}`;
|
|
889
|
+
const fill = (tries) => {
|
|
890
|
+
const node = findMarker(marker);
|
|
891
|
+
if (!node) return;
|
|
892
|
+
if (!this.from) {
|
|
893
|
+
if (tries > 0) setTimeout(() => fill(tries - 1), 0);
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
insertHTML(node, updating(() => super.render(at)));
|
|
897
|
+
node.remove();
|
|
898
|
+
};
|
|
899
|
+
this.load().then(() => fill(100));
|
|
900
|
+
return `<!--${marker}-->`;
|
|
901
|
+
}
|
|
902
|
+
get loaded() {
|
|
903
|
+
return this.code !== void 0;
|
|
904
|
+
}
|
|
905
|
+
load() {
|
|
906
|
+
this.loading ??= resolve$1(this.ref).then((handler) => {
|
|
907
|
+
this.code = handler;
|
|
908
|
+
});
|
|
909
|
+
return this.loading;
|
|
910
|
+
}
|
|
911
|
+
};
|
|
912
|
+
/** The count of the markers of children that wait for their code. */
|
|
913
|
+
let nextChildren = 0;
|
|
914
|
+
/** The comment of a marker, such as of children that wait for their code. */
|
|
915
|
+
function findMarker(data) {
|
|
916
|
+
const walker = document.createTreeWalker(document, NodeFilter.SHOW_COMMENT);
|
|
917
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) if (node.data === data) return node;
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* Loads the code of the children that the scope, or a component around it, holds, so code that
|
|
921
|
+
* runs in the scope can insert them at once. Undefined when there is nothing to load.
|
|
922
|
+
*/
|
|
923
|
+
function childrenReady(scope) {
|
|
924
|
+
if (!HAS_CHILDREN) return;
|
|
925
|
+
const waiting = [];
|
|
926
|
+
const seen = /* @__PURE__ */ new Set();
|
|
927
|
+
for (let current = scope; current && !seen.has(current); current = current.p ?? current.o) {
|
|
928
|
+
seen.add(current);
|
|
929
|
+
for (const value of current.s) if (value instanceof RenderedChildren && !value.loaded) waiting.push(value.load());
|
|
930
|
+
}
|
|
931
|
+
return waiting.length > 0 ? Promise.allSettled(waiting) : void 0;
|
|
932
|
+
}
|
|
933
|
+
/** The children of a component that the client renders. See `Children`. */
|
|
934
|
+
function children(from, html) {
|
|
935
|
+
return new Children(from, html);
|
|
936
|
+
}
|
|
937
|
+
//#endregion
|
|
938
|
+
//#region src/runtime/client/errors.ts
|
|
939
|
+
/**
|
|
940
|
+
* Reports an error of client code: a handler, an effect, a `$client`
|
|
941
|
+
* callback or a memo that loads. It renders the `#catch` body of the nearest
|
|
942
|
+
* `#try` around the scope of the code, through the parents and the owners of
|
|
943
|
+
* scopes. Without one, the page gets a `brumate:error` event on `window`,
|
|
944
|
+
* with the error and the scope, and the error is logged.
|
|
945
|
+
*/
|
|
946
|
+
function fail(scope, error) {
|
|
947
|
+
options.onError?.(error, { scope: scope?.id });
|
|
948
|
+
(async () => {
|
|
949
|
+
for (let current = scope; current; current = current.p ?? current.o) {
|
|
950
|
+
const id = findRoot(current).m;
|
|
951
|
+
if (!id) continue;
|
|
952
|
+
const module = await loadModule(id).catch(() => void 0);
|
|
953
|
+
const entry = module?.t?.[current.k];
|
|
954
|
+
if (!entry) continue;
|
|
955
|
+
let owner = current.p;
|
|
956
|
+
while (owner && owner.k !== entry[0]) owner = owner.p;
|
|
957
|
+
if (owner) {
|
|
958
|
+
await catchInto(owner, module, entry, error);
|
|
959
|
+
return;
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
unhandled(error, scope);
|
|
963
|
+
})();
|
|
964
|
+
}
|
|
965
|
+
/** Tells the page about an error that no `#try` caught. */
|
|
966
|
+
function unhandled(error, scope) {
|
|
967
|
+
window.dispatchEvent(new CustomEvent("brumate:error", { detail: {
|
|
968
|
+
error,
|
|
969
|
+
scope
|
|
970
|
+
} }));
|
|
971
|
+
console.error(error);
|
|
972
|
+
}
|
|
973
|
+
/**
|
|
974
|
+
* Shows the `#catch` body that the server rendered, in the template `b-c`, in place of the body
|
|
975
|
+
* of a `#try`. The server sends it when a `#loading` body inside the `#try` failed. `key` is the
|
|
976
|
+
* scope and the anchor of the block.
|
|
977
|
+
*/
|
|
978
|
+
function replaceTry(key) {
|
|
979
|
+
const template = document.querySelector(`template[b-c="${key}"]`);
|
|
980
|
+
const [id, anchor] = key.split(":");
|
|
981
|
+
const owner = scopes.get(parseId(id));
|
|
982
|
+
const start = owner && blockStart(owner, Number(anchor));
|
|
983
|
+
if (template && start) {
|
|
984
|
+
const end = rangeEnd(start);
|
|
985
|
+
removeBlock(owner, [], start, end);
|
|
986
|
+
insertHTML(end, template.innerHTML);
|
|
987
|
+
}
|
|
988
|
+
template?.remove();
|
|
989
|
+
}
|
|
990
|
+
/** Reports the error of a `#loading` body that failed on the server, with no `#try` around it. */
|
|
991
|
+
function failBoundary(error) {
|
|
992
|
+
options.onError?.(error, {});
|
|
993
|
+
unhandled(error);
|
|
994
|
+
}
|
|
995
|
+
/** Renders the `#catch` body of a `#try` with an error, in place of what the block shows. */
|
|
996
|
+
async function catchInto(owner, module, [, anchor, , fallback, kinds, holderKind, holderAnchor], error) {
|
|
997
|
+
const updates = await loadPart(module, "update");
|
|
998
|
+
const start = blockStart(owner, anchor);
|
|
999
|
+
if (!start) return;
|
|
1000
|
+
const end = rangeEnd(start);
|
|
1001
|
+
removeBlock(owner, kinds, start, end);
|
|
1002
|
+
insertHTML(end, inPlace(owner, updates[fallback], holderKind, holderAnchor)(owner, error));
|
|
1003
|
+
}
|
|
1004
|
+
//#endregion
|
|
1005
|
+
//#region src/runtime/client/updates.ts
|
|
1006
|
+
function get(scope, slot) {
|
|
1007
|
+
if (dirty.size > 0 && !draining) settle(scope);
|
|
1008
|
+
const value = scope.s[slot];
|
|
1009
|
+
if (value instanceof Promise && !scope.plain?.has(slot)) {
|
|
1010
|
+
if (computing > 0) throw new NotReady(value);
|
|
1011
|
+
if (loadingValues.has(value)) {
|
|
1012
|
+
if (runningEffects > 0) throw new NotReady(value);
|
|
1013
|
+
if (exempt === 0) throw new Error("This value is still loading. Wait for it with await $resolve(memo), or check $isPending(memo) first");
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
if (value instanceof MemoError) throw value.error;
|
|
1017
|
+
if (value instanceof ElementReference) {
|
|
1018
|
+
const element = findElement(value.scope, value.anchor);
|
|
1019
|
+
if (element) scope.s[slot] = element;
|
|
1020
|
+
return element ?? void 0;
|
|
1021
|
+
}
|
|
1022
|
+
return value;
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* A memo that threw holds the error in place of a value, and its readers update. Each one that
|
|
1026
|
+
* reads it throws into the nearest `#try` around it, and the code that does not read it runs as
|
|
1027
|
+
* usual. A memo that computes again without a throw holds its value again.
|
|
1028
|
+
*/
|
|
1029
|
+
function hold(scope, slot, key, error) {
|
|
1030
|
+
loadingSlots.delete(key);
|
|
1031
|
+
loads.delete(key);
|
|
1032
|
+
guesses?.map.delete(key);
|
|
1033
|
+
set(scope, slot, new MemoError(error));
|
|
1034
|
+
}
|
|
1035
|
+
let writes = 0;
|
|
1036
|
+
/** Starts the count of writes again. */
|
|
1037
|
+
function resetWrites() {
|
|
1038
|
+
writes = 0;
|
|
1039
|
+
}
|
|
1040
|
+
/**
|
|
1041
|
+
* Writes a slot from code of a component. A prop that a parent bound with
|
|
1042
|
+
* `$bind` writes the parent's slot too, through any number of levels, and
|
|
1043
|
+
* the parent's update of the prop then finds the value already there. A
|
|
1044
|
+
* binding with a setter, `$bind(name, fn)`, calls it in place of the write,
|
|
1045
|
+
* and the parent's update of the prop writes the value that it keeps, before
|
|
1046
|
+
* the call returns.
|
|
1047
|
+
*/
|
|
1048
|
+
function set(scope, slot, value) {
|
|
1049
|
+
const bound = scope.b?.[slot];
|
|
1050
|
+
if (bound === void 0 || isSlotReference(bound)) {
|
|
1051
|
+
const target = bound && scopes.get(bound.scope);
|
|
1052
|
+
if (target) pass(() => {
|
|
1053
|
+
writeSlot(scope, slot, value);
|
|
1054
|
+
set(target, bound.slot, value);
|
|
1055
|
+
});
|
|
1056
|
+
else writeSlot(scope, slot, value);
|
|
1057
|
+
} else {
|
|
1058
|
+
send(bound, value);
|
|
1059
|
+
flush();
|
|
1060
|
+
}
|
|
1061
|
+
return value;
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Calls the setter of a binding. An element reference arrives only while a render writes its ref,
|
|
1065
|
+
* so the element goes once the render has inserted it.
|
|
1066
|
+
*/
|
|
1067
|
+
function send(bound, value) {
|
|
1068
|
+
if (value instanceof ElementReference) afterInsert.push(() => {
|
|
1069
|
+
const element = findElement(value.scope, value.anchor);
|
|
1070
|
+
if (element) send(bound, element);
|
|
1071
|
+
});
|
|
1072
|
+
else if (bound instanceof FunctionReference) {
|
|
1073
|
+
const args = [...bound.args ?? [], value];
|
|
1074
|
+
if (!callNow(bound.ref, scopes.get(bound.scope), args)) call(bound.ref, scopes.get(bound.scope), args);
|
|
1075
|
+
} else if (typeof bound === "function") bound(value);
|
|
1076
|
+
}
|
|
1077
|
+
/** Writes a slot, and updates what reads it. */
|
|
1078
|
+
function writeSlot(scope, slot, value) {
|
|
1079
|
+
if (!Object.is(scope.s[slot], value)) {
|
|
1080
|
+
writes++;
|
|
1081
|
+
scope.s[slot] = value;
|
|
1082
|
+
if (DEV) devtools()?.write?.(scope, slot, value);
|
|
1083
|
+
trigger(scope, slot);
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
/** What `$bind(name)` passes a child, in markup that the browser renders. */
|
|
1087
|
+
function binding(value, scope, slot, setter) {
|
|
1088
|
+
return new Binding(value, new SlotReference(typeof scope === "object" ? scope.id : scope, slot), setter);
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* Records the props that a parent bound with `$bind`, among the props that a
|
|
1092
|
+
* component the browser renders writes: `slots` has the slot of each, by its name.
|
|
1093
|
+
*/
|
|
1094
|
+
function bindings(scope, props, slots) {
|
|
1095
|
+
const target = typeof scope === "object" ? scope : scopes.get(scope);
|
|
1096
|
+
for (const [name, slot] of Object.entries(slots)) {
|
|
1097
|
+
const value = props[name];
|
|
1098
|
+
if (target && HAS_BINDINGS && isBinding(value)) {
|
|
1099
|
+
(target.b ??= {})[slot] = value.target;
|
|
1100
|
+
const written = target.s[slot];
|
|
1101
|
+
if (written instanceof ElementReference) afterInsert.push(() => {
|
|
1102
|
+
if (scopes.get(target.id) === target) set(target, slot, written);
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
/**
|
|
1108
|
+
* The names of a call of a module function, for its shared code. Each ref
|
|
1109
|
+
* is a slot of `scope`, `[scope, slot]` for a slot of another scope, or
|
|
1110
|
+
* `[value]` for a value that does not change, such as a literal argument.
|
|
1111
|
+
*/
|
|
1112
|
+
function cells(scope, links, names) {
|
|
1113
|
+
const result = {};
|
|
1114
|
+
for (const [index, name] of names.entries()) {
|
|
1115
|
+
const ref = links[index];
|
|
1116
|
+
let owner = scope;
|
|
1117
|
+
let slot;
|
|
1118
|
+
if (typeof ref === "number") slot = ref;
|
|
1119
|
+
else if (ref.length === 2) [owner, slot] = ref;
|
|
1120
|
+
else {
|
|
1121
|
+
Object.defineProperty(result, name, {
|
|
1122
|
+
get: () => ref[0],
|
|
1123
|
+
set: () => {}
|
|
1124
|
+
});
|
|
1125
|
+
continue;
|
|
1126
|
+
}
|
|
1127
|
+
Object.defineProperty(result, name, {
|
|
1128
|
+
get: () => get(owner, slot),
|
|
1129
|
+
set: (value) => set(owner, slot, value)
|
|
1130
|
+
});
|
|
1131
|
+
}
|
|
1132
|
+
return result;
|
|
1133
|
+
}
|
|
1134
|
+
let loadCount = 0;
|
|
1135
|
+
/** Marks a memo as loading. Its readers update the first time, so `$isPending` becomes true. */
|
|
1136
|
+
function startLoading(scope, slot, key) {
|
|
1137
|
+
if (!loadingSlots.has(key)) {
|
|
1138
|
+
loadingSlots.add(key);
|
|
1139
|
+
trigger(scope, slot);
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
/**
|
|
1143
|
+
* Computes a memo of `$debounced` after its value stops changing for `ms`,
|
|
1144
|
+
* or one of `$throttled` at most once every `ms`, with the last value after
|
|
1145
|
+
* the wait. The memo is pending while it waits.
|
|
1146
|
+
*/
|
|
1147
|
+
function timed(scope, slot, ms, throttle, compute) {
|
|
1148
|
+
const key = `${scope.id}:${slot}`;
|
|
1149
|
+
const state = timers.get(key) ?? { last: -Infinity };
|
|
1150
|
+
timers.set(key, state);
|
|
1151
|
+
function recompute() {
|
|
1152
|
+
state.timer = void 0;
|
|
1153
|
+
state.last = Date.now();
|
|
1154
|
+
if (scopes.get(scope.id) === scope) memo(scope, slot, compute);
|
|
1155
|
+
}
|
|
1156
|
+
const since = Date.now() - state.last;
|
|
1157
|
+
if (throttle && state.timer === void 0 && since >= ms) {
|
|
1158
|
+
recompute();
|
|
1159
|
+
return;
|
|
1160
|
+
}
|
|
1161
|
+
if (!throttle) {
|
|
1162
|
+
clearTimeout(state.timer);
|
|
1163
|
+
state.timer = void 0;
|
|
1164
|
+
}
|
|
1165
|
+
state.timer ??= setTimeout(recompute, throttle ? ms - since : ms);
|
|
1166
|
+
startLoading(scope, slot, key);
|
|
1167
|
+
}
|
|
1168
|
+
/** True for a promise, or any object with a `then` method. */
|
|
1169
|
+
function isThenable(value) {
|
|
1170
|
+
return value !== null && typeof value === "object" && typeof value.then === "function";
|
|
1171
|
+
}
|
|
1172
|
+
/**
|
|
1173
|
+
* Writes the result of an async memo when it arrives. Until then the slot
|
|
1174
|
+
* keeps its value, and `pending` is true. Only the latest load is written.
|
|
1175
|
+
*/
|
|
1176
|
+
function asyncMemo(scope, slot, promise) {
|
|
1177
|
+
const key = `${scope.id}:${slot}`;
|
|
1178
|
+
const load = ++loadCount;
|
|
1179
|
+
loads.set(key, load);
|
|
1180
|
+
startLoading(scope, slot, key);
|
|
1181
|
+
function settle(value) {
|
|
1182
|
+
if (loads.get(key) !== load) return;
|
|
1183
|
+
loadingSlots.delete(key);
|
|
1184
|
+
if (!Object.is(scope.s[slot], value)) set(scope, slot, value);
|
|
1185
|
+
else trigger(scope, slot);
|
|
1186
|
+
}
|
|
1187
|
+
function failed(error) {
|
|
1188
|
+
if (loads.get(key) === load) hold(scope, slot, key, error);
|
|
1189
|
+
}
|
|
1190
|
+
promise.then((value) => {
|
|
1191
|
+
if (!isAsyncIterable(value)) {
|
|
1192
|
+
settle(value);
|
|
1193
|
+
return;
|
|
1194
|
+
}
|
|
1195
|
+
(async () => {
|
|
1196
|
+
try {
|
|
1197
|
+
for await (const item of value) {
|
|
1198
|
+
if (loads.get(key) !== load) break;
|
|
1199
|
+
if (loadingSlots.has(key)) settle(item);
|
|
1200
|
+
else if (!Object.is(scope.s[slot], item)) set(scope, slot, item);
|
|
1201
|
+
}
|
|
1202
|
+
} catch (error) {
|
|
1203
|
+
failed(error);
|
|
1204
|
+
}
|
|
1205
|
+
})();
|
|
1206
|
+
}, failed);
|
|
1207
|
+
}
|
|
1208
|
+
/** Thrown when a memo reads a value that is still loading. */
|
|
1209
|
+
var NotReady = class extends Error {
|
|
1210
|
+
source;
|
|
1211
|
+
constructor(source) {
|
|
1212
|
+
super("The value is still loading");
|
|
1213
|
+
this.source = source;
|
|
1214
|
+
}
|
|
1215
|
+
};
|
|
1216
|
+
let computing = 0;
|
|
1217
|
+
let runningEffects = 0;
|
|
1218
|
+
/**
|
|
1219
|
+
* `$resolve(memo, { timeout, signal })`: resolves with the value of the memo once it is no longer
|
|
1220
|
+
* pending, also after a load that its inputs started. `isLoading` tells whether the memo or a
|
|
1221
|
+
* memo that it reads loads, and `read` gives the value or throws the error of the memo.
|
|
1222
|
+
*/
|
|
1223
|
+
function resolve(scope, isLoading, read, options) {
|
|
1224
|
+
return new Promise((done, reject) => {
|
|
1225
|
+
let timer;
|
|
1226
|
+
function end(finish) {
|
|
1227
|
+
clearTimeout(timer);
|
|
1228
|
+
waiters.delete(check);
|
|
1229
|
+
options?.signal?.removeEventListener("abort", abort);
|
|
1230
|
+
finish();
|
|
1231
|
+
}
|
|
1232
|
+
function abort() {
|
|
1233
|
+
end(() => reject(options.signal.reason));
|
|
1234
|
+
}
|
|
1235
|
+
function check() {
|
|
1236
|
+
if (scopes.get(scope.id) !== scope) {
|
|
1237
|
+
const gone = /* @__PURE__ */ new Error("The component left the page");
|
|
1238
|
+
gone.name = "AbortError";
|
|
1239
|
+
end(() => reject(gone));
|
|
1240
|
+
return;
|
|
1241
|
+
}
|
|
1242
|
+
if (loadingUpdates.size > 0) {
|
|
1243
|
+
Promise.allSettled(loadingUpdates).then(check);
|
|
1244
|
+
return;
|
|
1245
|
+
}
|
|
1246
|
+
if (!draining) for (const scope of dirty) settle(scope);
|
|
1247
|
+
if (isLoading()) return;
|
|
1248
|
+
try {
|
|
1249
|
+
const value = read();
|
|
1250
|
+
end(() => done(value));
|
|
1251
|
+
} catch (error) {
|
|
1252
|
+
end(() => reject(error));
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
if (options?.signal?.aborted) {
|
|
1256
|
+
abort();
|
|
1257
|
+
return;
|
|
1258
|
+
}
|
|
1259
|
+
options?.signal?.addEventListener("abort", abort);
|
|
1260
|
+
if (options?.timeout !== void 0) timer = setTimeout(() => end(() => reject(/* @__PURE__ */ new Error(`The value did not load in ${options.timeout} ms`))), options.timeout);
|
|
1261
|
+
waiters.set(check, scope);
|
|
1262
|
+
check();
|
|
1263
|
+
});
|
|
1264
|
+
}
|
|
1265
|
+
const skipped = /* @__PURE__ */ new Map();
|
|
1266
|
+
/** The key of the memo that computes, if any. */
|
|
1267
|
+
let memoKey;
|
|
1268
|
+
/** The signal for server function calls while a memo computes. It is made on the first call. */
|
|
1269
|
+
function memoSignal() {
|
|
1270
|
+
if (memoKey === void 0) return;
|
|
1271
|
+
let controller = memoControllers.get(memoKey);
|
|
1272
|
+
if (!controller) {
|
|
1273
|
+
controller = new AbortController();
|
|
1274
|
+
memoControllers.set(memoKey, controller);
|
|
1275
|
+
}
|
|
1276
|
+
return controller.signal;
|
|
1277
|
+
}
|
|
1278
|
+
/**
|
|
1279
|
+
* Computes a memo. A promise loads, and the memo keeps its value until it
|
|
1280
|
+
* settles. A memo that reads a value that is still loading keeps its value
|
|
1281
|
+
* too, and is pending until that value arrives and the memo computes again.
|
|
1282
|
+
* A throw is held as the value of the memo, and its readers throw.
|
|
1283
|
+
*/
|
|
1284
|
+
function memo(scope, slot, compute) {
|
|
1285
|
+
const key = `${scope.id}:${slot}`;
|
|
1286
|
+
if (guesses?.same(key)) return;
|
|
1287
|
+
const rendered = scope.s[slot];
|
|
1288
|
+
if (renderedLoads.delete(key) && isThenable(rendered)) {
|
|
1289
|
+
asyncMemo(scope, slot, Promise.resolve(rendered));
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
let value;
|
|
1293
|
+
let threw = false;
|
|
1294
|
+
memoControllers.get(key)?.abort();
|
|
1295
|
+
memoControllers.delete(key);
|
|
1296
|
+
const outer = memoKey;
|
|
1297
|
+
memoKey = key;
|
|
1298
|
+
computing++;
|
|
1299
|
+
try {
|
|
1300
|
+
value = compute();
|
|
1301
|
+
} catch (error) {
|
|
1302
|
+
if (error instanceof NotReady) {
|
|
1303
|
+
startLoading(scope, slot, key);
|
|
1304
|
+
return;
|
|
1305
|
+
}
|
|
1306
|
+
threw = true;
|
|
1307
|
+
value = error;
|
|
1308
|
+
} finally {
|
|
1309
|
+
computing--;
|
|
1310
|
+
memoKey = outer;
|
|
1311
|
+
}
|
|
1312
|
+
if (threw) {
|
|
1313
|
+
hold(scope, slot, key, value);
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
if (isThenable(value) || isAsyncIterable(value)) {
|
|
1317
|
+
asyncMemo(scope, slot, Promise.resolve(value));
|
|
1318
|
+
return;
|
|
1319
|
+
}
|
|
1320
|
+
const waited = loadingSlots.delete(key);
|
|
1321
|
+
loads.delete(key);
|
|
1322
|
+
guesses?.map.delete(key);
|
|
1323
|
+
if (!Object.is(scope.s[slot], value)) set(scope, slot, value);
|
|
1324
|
+
else if (waited) trigger(scope, slot);
|
|
1325
|
+
}
|
|
1326
|
+
/** True while a new value of a memo loads, or while the server still streams its value. */
|
|
1327
|
+
function pending(scope, slot) {
|
|
1328
|
+
return loadingSlots.has(`${scope.id}:${slot}`) || scope.s[slot] instanceof Promise && !scope.plain?.has(slot);
|
|
1329
|
+
}
|
|
1330
|
+
/** True when a parent bound the prop of the slot with `$bind`. */
|
|
1331
|
+
function isBound(scope, slot) {
|
|
1332
|
+
return scope.b?.[slot] !== void 0;
|
|
1333
|
+
}
|
|
1334
|
+
/** `x++`, `x--`, `++x` and `--x`. */
|
|
1335
|
+
function update(scope, slot, delta, prefix) {
|
|
1336
|
+
const previous = get(scope, slot);
|
|
1337
|
+
set(scope, slot, previous + delta);
|
|
1338
|
+
return prefix ? previous + delta : previous;
|
|
1339
|
+
}
|
|
1340
|
+
function trigger(scope, slot) {
|
|
1341
|
+
for (const check of waiters.keys()) check();
|
|
1342
|
+
const id = findRoot(scope).m;
|
|
1343
|
+
const children = childrenReady(scope);
|
|
1344
|
+
if (modules.has(id) && loadedParts.has(`${id}:update`) && loadingCode.size === 0 && !children) {
|
|
1345
|
+
pass(() => queueWrite(scope, slot));
|
|
1346
|
+
return;
|
|
1347
|
+
}
|
|
1348
|
+
if (waiting) {
|
|
1349
|
+
waiting.push([
|
|
1350
|
+
scope,
|
|
1351
|
+
slot,
|
|
1352
|
+
updateCode(scope, slot).catch((error) => fail(scope, error))
|
|
1353
|
+
]);
|
|
1354
|
+
return;
|
|
1355
|
+
}
|
|
1356
|
+
const list = [[scope, slot]];
|
|
1357
|
+
if (running > 0) waiting = list;
|
|
1358
|
+
const loading = loadModule(id).then(async (loaded) => {
|
|
1359
|
+
await (loaded.g[scope.k]?.[slot] && loadPart(loaded, "update"));
|
|
1360
|
+
const waits = [...loadingCode, children];
|
|
1361
|
+
for (const [, , load] of list) waits.push(load);
|
|
1362
|
+
await Promise.allSettled(waits);
|
|
1363
|
+
pass(() => {
|
|
1364
|
+
for (const [target, at] of list) queueWrite(target, at);
|
|
1365
|
+
});
|
|
1366
|
+
}).catch((error) => {
|
|
1367
|
+
fail(scope, error);
|
|
1368
|
+
flushEffects();
|
|
1369
|
+
});
|
|
1370
|
+
loadingUpdates.add(loading);
|
|
1371
|
+
loading.finally(() => loadingUpdates.delete(loading));
|
|
1372
|
+
}
|
|
1373
|
+
/** Loads the code of a component, its update code when something reads the slot, and its children. */
|
|
1374
|
+
async function updateCode(scope, slot) {
|
|
1375
|
+
await loadUpdate(scope, slot);
|
|
1376
|
+
return childrenReady(scope);
|
|
1377
|
+
}
|
|
1378
|
+
/** Loads the module of a component and its update code, when something reads the slot. */
|
|
1379
|
+
function loadUpdate(scope, slot) {
|
|
1380
|
+
return loadModule(findRoot(scope).m).then((loaded) => {
|
|
1381
|
+
const reads = loaded.g[scope.k];
|
|
1382
|
+
return (slot === void 0 ? reads : reads?.[slot]) && loadPart(loaded, "update");
|
|
1383
|
+
});
|
|
1384
|
+
}
|
|
1385
|
+
/**
|
|
1386
|
+
* Loads the update code that a write of a handler can need, so its writes run before it returns.
|
|
1387
|
+
* A handler that is not in `w` of its module writes only the slots of its own component, and
|
|
1388
|
+
* waits for the code of that component. A handler in `w` also waits for the components above it, which a
|
|
1389
|
+
* write of a bound prop reaches, and for the scope that its children render in.
|
|
1390
|
+
*/
|
|
1391
|
+
async function updateReady(scope, ref) {
|
|
1392
|
+
if (!scope) return;
|
|
1393
|
+
const root = findRoot(scope);
|
|
1394
|
+
const own = ref && !(await loadModule(root.m)).w?.includes(ref.split("#")[1]);
|
|
1395
|
+
return Promise.all([loadUpdate(scope), own ? loadUpdate(root) : Promise.all([updateReady(scope.p ?? scope.o), updateReady(scope.t)])]);
|
|
1396
|
+
}
|
|
1397
|
+
/** The writes of the pass that runs that wait for code, to run as one pass of their own. */
|
|
1398
|
+
let waiting;
|
|
1399
|
+
/** The updates that wait for their code. */
|
|
1400
|
+
const loadingUpdates = /* @__PURE__ */ new Set();
|
|
1401
|
+
/**
|
|
1402
|
+
* Resolves when the updates of the writes so far have run, including those
|
|
1403
|
+
* whose code is loading. `transition` of `brumate/events` waits for it.
|
|
1404
|
+
*/
|
|
1405
|
+
async function updated() {
|
|
1406
|
+
while (loadingUpdates.size > 0) await Promise.allSettled(loadingUpdates);
|
|
1407
|
+
}
|
|
1408
|
+
globalThis[Symbol.for("brumate.updated")] = updated;
|
|
1409
|
+
const pendingEffects = /* @__PURE__ */ new Map();
|
|
1410
|
+
let levels = [];
|
|
1411
|
+
let running = 0;
|
|
1412
|
+
let flushing = false;
|
|
1413
|
+
/** The updates that ran since the outermost pass started, and when it started, for the devtools. */
|
|
1414
|
+
let updateCount = 0;
|
|
1415
|
+
let updateStart = 0;
|
|
1416
|
+
/**
|
|
1417
|
+
* Runs a pass: `write` queues updates. The outermost pass runs them by the
|
|
1418
|
+
* depth of their scope, so the bindings of a component, such as its props
|
|
1419
|
+
* and contexts, reach the components inside before their memos compute. At
|
|
1420
|
+
* each depth, the memos in the order of their ranks, then the bindings as
|
|
1421
|
+
* one round. Effects run after them, the deepest first, so the effects of
|
|
1422
|
+
* a child run before those of its parent.
|
|
1423
|
+
*/
|
|
1424
|
+
function pass(write) {
|
|
1425
|
+
if (DEV && running === 0) {
|
|
1426
|
+
updateCount = 0;
|
|
1427
|
+
updateStart = performance.now();
|
|
1428
|
+
}
|
|
1429
|
+
if (running++ === 0) failed = false;
|
|
1430
|
+
let result;
|
|
1431
|
+
let held = false;
|
|
1432
|
+
try {
|
|
1433
|
+
result = write();
|
|
1434
|
+
} finally {
|
|
1435
|
+
if (running === 1) drain();
|
|
1436
|
+
if (--running === 0) {
|
|
1437
|
+
for (const level of levels.reverse()) for (const [key, runEffect] of level?.e ?? []) pendingEffects.set(key, runEffect);
|
|
1438
|
+
levels = [];
|
|
1439
|
+
held = waiting !== void 0;
|
|
1440
|
+
waiting = void 0;
|
|
1441
|
+
depths.clear();
|
|
1442
|
+
failed = false;
|
|
1443
|
+
stuck = false;
|
|
1444
|
+
dirty.clear();
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
if (running === 0) {
|
|
1448
|
+
if (!held) flushEffects();
|
|
1449
|
+
if (DEV) devtools()?.updated?.(updateCount, updateStart, performance.now());
|
|
1450
|
+
}
|
|
1451
|
+
return result;
|
|
1452
|
+
}
|
|
1453
|
+
setPass(pass);
|
|
1454
|
+
const dirty = /* @__PURE__ */ new Set();
|
|
1455
|
+
/** Computes the memos of a scope that wait, so code that reads one in a handler reads the new value. */
|
|
1456
|
+
function settle(scope) {
|
|
1457
|
+
if (dirty.delete(scope)) for (const [rank, bucket] of (levels[getDepth(scope)]?.m ?? []).entries()) {
|
|
1458
|
+
const key = `${scope.id}:m${rank}`;
|
|
1459
|
+
const compute = bucket?.get(key);
|
|
1460
|
+
bucket?.delete(key);
|
|
1461
|
+
compute?.();
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
let draining = false;
|
|
1465
|
+
/**
|
|
1466
|
+
* Runs the memos and bindings that wait in the pass at once, such as for a handler that must focus
|
|
1467
|
+
* or measure what it rendered. Effects still wait for the end of the pass.
|
|
1468
|
+
*/
|
|
1469
|
+
function flush() {
|
|
1470
|
+
if (running > 0 && !draining) drain();
|
|
1471
|
+
}
|
|
1472
|
+
globalThis[Symbol.for("brumate.flush")] = flush;
|
|
1473
|
+
/**
|
|
1474
|
+
* Queues the updates that read a slot, on the scope and on its descendants of
|
|
1475
|
+
* each kind, once the code of the component has loaded.
|
|
1476
|
+
*/
|
|
1477
|
+
function queueWrite(scope, slot) {
|
|
1478
|
+
const id = findRoot(scope).m;
|
|
1479
|
+
const module = modules.get(id);
|
|
1480
|
+
const updates = loadedParts.get(`${id}:update`);
|
|
1481
|
+
if (!module || !updates) return;
|
|
1482
|
+
for (const [kind, name] of module.g[scope.k]?.[slot] ?? []) for (const target of kind === scope.k ? [scope] : descendants(scope, kind)) queue(updates[name], name, target);
|
|
1483
|
+
}
|
|
1484
|
+
/** Queues an update of a scope, once. */
|
|
1485
|
+
function queue(fn, name, target) {
|
|
1486
|
+
if (DEV) updateCount++;
|
|
1487
|
+
const key = `${target.id}:${name}`;
|
|
1488
|
+
function later() {
|
|
1489
|
+
if (!failed && scopes.get(target.id) === target) try {
|
|
1490
|
+
if (stuck) {
|
|
1491
|
+
failed = true;
|
|
1492
|
+
throw new Error(DEV ? "The updates of a write did not settle after 10000 rounds. An update writes a value that it reads, such as through ref=$bind." : "Update loop");
|
|
1493
|
+
}
|
|
1494
|
+
updating(() => fn(target));
|
|
1495
|
+
} catch (error) {
|
|
1496
|
+
fail(target, error);
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
const depth = getDepth(target);
|
|
1500
|
+
const level = levels[depth] ??= {
|
|
1501
|
+
m: [],
|
|
1502
|
+
b: /* @__PURE__ */ new Map(),
|
|
1503
|
+
e: /* @__PURE__ */ new Map()
|
|
1504
|
+
};
|
|
1505
|
+
if (name[0] === "m") {
|
|
1506
|
+
(level.m[+name.slice(1)] ??= /* @__PURE__ */ new Map()).set(key, later);
|
|
1507
|
+
dirty.add(target);
|
|
1508
|
+
} else (name[0] === "e" ? level.e : level.b).set(key, later);
|
|
1509
|
+
}
|
|
1510
|
+
/**
|
|
1511
|
+
* The depth of a scope: one more than the deeper of the scope where it renders, for children
|
|
1512
|
+
* and templates that another component inserts, and of its parent. So children that a provider
|
|
1513
|
+
* inserts come after the provider, and a template that a shallower component renders comes after
|
|
1514
|
+
* the component that wrote it.
|
|
1515
|
+
*/
|
|
1516
|
+
function getDepth(scope) {
|
|
1517
|
+
if (!scope) return -1;
|
|
1518
|
+
if (!scope.t) return 1 + getDepth(scope.p ?? scope.o);
|
|
1519
|
+
let depth = depths.get(scope);
|
|
1520
|
+
if (depth === void 0) {
|
|
1521
|
+
depth = 1 + Math.max(getDepth(scope.t), getDepth(scope.p ?? scope.o));
|
|
1522
|
+
depths.set(scope, depth);
|
|
1523
|
+
}
|
|
1524
|
+
return depth;
|
|
1525
|
+
}
|
|
1526
|
+
const depths = /* @__PURE__ */ new Map();
|
|
1527
|
+
let failed = false;
|
|
1528
|
+
let stuck = false;
|
|
1529
|
+
/**
|
|
1530
|
+
* Runs what waits, the lowest depth first: its memo of the lowest rank, until
|
|
1531
|
+
* none waits, then its bindings as one round. After each, it looks again from
|
|
1532
|
+
* the top, since a write can queue updates of a lower depth, such as a `$bind`
|
|
1533
|
+
* write into a parent.
|
|
1534
|
+
*/
|
|
1535
|
+
function drain() {
|
|
1536
|
+
let rounds = 0;
|
|
1537
|
+
let depth = 0;
|
|
1538
|
+
draining = true;
|
|
1539
|
+
try {
|
|
1540
|
+
while (depth < levels.length) {
|
|
1541
|
+
const level = levels[depth];
|
|
1542
|
+
if (!level) {
|
|
1543
|
+
depth++;
|
|
1544
|
+
continue;
|
|
1545
|
+
}
|
|
1546
|
+
const memos = level.m;
|
|
1547
|
+
let rank = 0;
|
|
1548
|
+
while (rank < memos.length && !memos[rank]?.size) rank++;
|
|
1549
|
+
const bucket = memos[rank];
|
|
1550
|
+
if (bucket) for (const [key, compute] of bucket) {
|
|
1551
|
+
bucket.delete(key);
|
|
1552
|
+
compute();
|
|
1553
|
+
}
|
|
1554
|
+
else if (level.b.size > 0) {
|
|
1555
|
+
stuck = ++rounds > 1e4;
|
|
1556
|
+
const round = [...level.b.values()];
|
|
1557
|
+
level.b.clear();
|
|
1558
|
+
for (const waiter of round) waiter();
|
|
1559
|
+
} else {
|
|
1560
|
+
depth++;
|
|
1561
|
+
continue;
|
|
1562
|
+
}
|
|
1563
|
+
depth = 0;
|
|
1564
|
+
}
|
|
1565
|
+
} finally {
|
|
1566
|
+
draining = false;
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
/**
|
|
1570
|
+
* Runs the effects that writes scheduled, in the order they were first
|
|
1571
|
+
* scheduled. Writes in an effect schedule more effects, which run after.
|
|
1572
|
+
*/
|
|
1573
|
+
function flushEffects() {
|
|
1574
|
+
if (flushing) return;
|
|
1575
|
+
flushing = true;
|
|
1576
|
+
try {
|
|
1577
|
+
while (pendingEffects.size > 0) {
|
|
1578
|
+
const run = [...pendingEffects.values()];
|
|
1579
|
+
pendingEffects.clear();
|
|
1580
|
+
pass(() => {
|
|
1581
|
+
for (const runEffect of run) runEffect();
|
|
1582
|
+
});
|
|
1583
|
+
}
|
|
1584
|
+
} finally {
|
|
1585
|
+
flushing = false;
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
/**
|
|
1589
|
+
* Runs an effect, or a `$client` callback when `client` is set, after the cleanup of its previous
|
|
1590
|
+
* run. An effect that reads a value that is still loading skips the run, and runs again when the
|
|
1591
|
+
* value arrives. A `$client` callback is a handler: the same read throws.
|
|
1592
|
+
*/
|
|
1593
|
+
function effect(scope, name, callback, client) {
|
|
1594
|
+
if (hiding?.paused(scope, name)) return;
|
|
1595
|
+
const key = `${scope.id}:${name}`;
|
|
1596
|
+
skipped.delete(key);
|
|
1597
|
+
scope.cleanups ??= /* @__PURE__ */ new Map();
|
|
1598
|
+
scope.cleanups.get(name)?.();
|
|
1599
|
+
let cleanup;
|
|
1600
|
+
const start = DEV ? performance.now() : 0;
|
|
1601
|
+
if (!client) runningEffects++;
|
|
1602
|
+
try {
|
|
1603
|
+
cleanup = callback();
|
|
1604
|
+
} catch (error) {
|
|
1605
|
+
if (error instanceof NotReady) {
|
|
1606
|
+
const token = {};
|
|
1607
|
+
skipped.set(key, token);
|
|
1608
|
+
scope.cleanups.set(name, noop);
|
|
1609
|
+
function again() {
|
|
1610
|
+
if (skipped.get(key) === token && scopes.get(scope.id) === scope) effect(scope, name, callback);
|
|
1611
|
+
}
|
|
1612
|
+
error.source.then(() => Promise.allSettled(loadingUpdates).then(again), (reason) => {
|
|
1613
|
+
if (skipped.get(key) === token) {
|
|
1614
|
+
skipped.delete(key);
|
|
1615
|
+
fail(scope, reason);
|
|
1616
|
+
}
|
|
1617
|
+
});
|
|
1618
|
+
return;
|
|
1619
|
+
}
|
|
1620
|
+
fail(scope, error);
|
|
1621
|
+
} finally {
|
|
1622
|
+
if (!client) runningEffects--;
|
|
1623
|
+
}
|
|
1624
|
+
let stored = typeof cleanup === "function" ? cleanup : noop;
|
|
1625
|
+
if (DEV) {
|
|
1626
|
+
devtools()?.effect?.(scope, name, "run", performance.now() - start);
|
|
1627
|
+
const own = stored;
|
|
1628
|
+
stored = () => {
|
|
1629
|
+
devtools()?.effect?.(scope, name, "cleanup", 0);
|
|
1630
|
+
own();
|
|
1631
|
+
};
|
|
1632
|
+
}
|
|
1633
|
+
scope.cleanups.set(name, stored);
|
|
1634
|
+
}
|
|
1635
|
+
function noop() {}
|
|
1636
|
+
//#endregion
|
|
1637
|
+
//#region src/runtime/client/context.ts
|
|
1638
|
+
/** The scope that provides a context to `scope`: itself or the nearest one above it. */
|
|
1639
|
+
function findProvider(scope, key) {
|
|
1640
|
+
for (let current = scope.t ?? scope.p ?? scope.o; current; current = current.t ?? current.p ?? current.o) if (current.v && key in current.v) return current;
|
|
1641
|
+
}
|
|
1642
|
+
/**
|
|
1643
|
+
* Writes a provided value, and the slot of every scope that reads it from this provider. The
|
|
1644
|
+
* provider can read the same context from above, which this does not write.
|
|
1645
|
+
*/
|
|
1646
|
+
function updateContext(scope, context, value) {
|
|
1647
|
+
scope.v ??= {};
|
|
1648
|
+
scope.v[context.key] = value;
|
|
1649
|
+
for (const target of scopes.values()) {
|
|
1650
|
+
const slot = target.x?.[context.key];
|
|
1651
|
+
if (slot !== void 0 && findProvider(target, context.key) === scope) set(target, slot, value);
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
/** Provides a value of a context to the scopes inside `scope`. */
|
|
1655
|
+
function provideValue(context, value, scope) {
|
|
1656
|
+
const target = resolveScope(scope);
|
|
1657
|
+
target.v ??= {};
|
|
1658
|
+
target.v[context.key] = value;
|
|
1659
|
+
}
|
|
1660
|
+
/** Reads a context, and records its slot so that changes to the provided value reach it. */
|
|
1661
|
+
function consumeContext(context, scope, slot) {
|
|
1662
|
+
const target = resolveScope(scope);
|
|
1663
|
+
if (slot !== void 0) {
|
|
1664
|
+
target.x ??= {};
|
|
1665
|
+
target.x[context.key] = slot;
|
|
1666
|
+
}
|
|
1667
|
+
const provider = findProvider(target, context.key);
|
|
1668
|
+
if (provider) return provider.v[context.key];
|
|
1669
|
+
if (context.key === "brumate.route" && !globals.has("brumate.route")) globals.set(ROUTE_KEY, currentRoute());
|
|
1670
|
+
return globals.has(context.key) ? globals.get(context.key) : context.defaultValue;
|
|
1671
|
+
}
|
|
1672
|
+
/** The values of the contexts that the runtime provides to the whole page, by key. */
|
|
1673
|
+
const globals = /* @__PURE__ */ new Map();
|
|
1674
|
+
/** The route of the page: `location`, and the params of the route from the last page state. */
|
|
1675
|
+
function currentRoute() {
|
|
1676
|
+
const records = document.querySelectorAll("script[b-state][b-params]");
|
|
1677
|
+
const params = records[records.length - 1]?.getAttribute("b-params");
|
|
1678
|
+
const url = new URL(location.href);
|
|
1679
|
+
return {
|
|
1680
|
+
url,
|
|
1681
|
+
search: url.searchParams,
|
|
1682
|
+
params: params ? JSON.parse(params) : {}
|
|
1683
|
+
};
|
|
1684
|
+
}
|
|
1685
|
+
/**
|
|
1686
|
+
* Writes a context that the runtime provides to the whole page, and the
|
|
1687
|
+
* slot of every scope that reads it without a provider above it.
|
|
1688
|
+
*/
|
|
1689
|
+
function setGlobal(key, value) {
|
|
1690
|
+
globals.set(key, value);
|
|
1691
|
+
for (const target of scopes.values()) {
|
|
1692
|
+
const slot = target.x?.[key];
|
|
1693
|
+
if (slot !== void 0 && !findProvider(target, key)) set(target, slot, value);
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
/** Reads the route of the page again, after navigation changed it. */
|
|
1697
|
+
function updateRoute() {
|
|
1698
|
+
setGlobal(ROUTE_KEY, currentRoute());
|
|
1699
|
+
}
|
|
1700
|
+
//#endregion
|
|
1701
|
+
//#region src/runtime/client/guesses.ts
|
|
1702
|
+
/** The values of promises that the server streamed, so a guess knows its source did not change. */
|
|
1703
|
+
const settled = /* @__PURE__ */ new WeakMap();
|
|
1704
|
+
/** True while the source of a guess holds the value it had when the guess was made. */
|
|
1705
|
+
function sameSource(key) {
|
|
1706
|
+
const entry = guesses.map.get(key);
|
|
1707
|
+
if (!entry?.source) return false;
|
|
1708
|
+
const current = entry.source[0].s[entry.source[1]];
|
|
1709
|
+
return Object.is(current, entry.base) || entry.base instanceof Promise && settled.get(entry.base) === current;
|
|
1710
|
+
}
|
|
1711
|
+
/** Server calls that have not finished, from forms and from `rpc`. */
|
|
1712
|
+
let calling = 0;
|
|
1713
|
+
/** Counts the pages that client navigation left, so calls of an old page are not counted down. */
|
|
1714
|
+
let callEpoch = 0;
|
|
1715
|
+
/** Calls of the old page that are still out no longer count for its guesses. */
|
|
1716
|
+
function leaveGuesses() {
|
|
1717
|
+
calling = 0;
|
|
1718
|
+
callEpoch++;
|
|
1719
|
+
}
|
|
1720
|
+
/**
|
|
1721
|
+
* Writes a guess into an optimistic value. It goes back to its source when
|
|
1722
|
+
* the source changes, which computes it again, or when every server call of
|
|
1723
|
+
* the page finishes, such as after a post failed.
|
|
1724
|
+
*/
|
|
1725
|
+
function guess(scope, slot, ...rest) {
|
|
1726
|
+
const value = rest.at(-1);
|
|
1727
|
+
const source = rest.length === 3 ? [rest[0], rest[1]] : void 0;
|
|
1728
|
+
const base = source && source[0].s[source[1]];
|
|
1729
|
+
(guesses ?? setGuesses({
|
|
1730
|
+
map: /* @__PURE__ */ new Map(),
|
|
1731
|
+
same: sameSource,
|
|
1732
|
+
reset: resetGuesses
|
|
1733
|
+
})).map.set(`${scope.id}:${slot}`, {
|
|
1734
|
+
scope,
|
|
1735
|
+
slot,
|
|
1736
|
+
source,
|
|
1737
|
+
base
|
|
1738
|
+
});
|
|
1739
|
+
return set(scope, slot, value);
|
|
1740
|
+
}
|
|
1741
|
+
/** Counts a server call, and puts guesses back when the last one finishes. */
|
|
1742
|
+
function track(request) {
|
|
1743
|
+
calling++;
|
|
1744
|
+
const epoch = callEpoch;
|
|
1745
|
+
function done() {
|
|
1746
|
+
if (epoch !== callEpoch) return;
|
|
1747
|
+
calling--;
|
|
1748
|
+
setTimeout(() => calling || guesses?.reset(), 0);
|
|
1749
|
+
}
|
|
1750
|
+
request.then(done, done);
|
|
1751
|
+
return request;
|
|
1752
|
+
}
|
|
1753
|
+
function resetGuesses() {
|
|
1754
|
+
for (const [key, { scope, slot, source }] of guesses.map) {
|
|
1755
|
+
if (source && pending(source[0], source[1])) continue;
|
|
1756
|
+
guesses.map.delete(key);
|
|
1757
|
+
if (source && scopes.has(scope.id) && !Object.is(scope.s[slot], source[0].s[source[1]])) set(scope, slot, source[0].s[source[1]]);
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
//#endregion
|
|
1761
|
+
//#region src/runtime/client/handlers.ts
|
|
1762
|
+
/**
|
|
1763
|
+
* The handlers that the core makes from references, with their references,
|
|
1764
|
+
* so a spread in markup that the browser renders passes them on in `b-on`,
|
|
1765
|
+
* as a spread on the server does.
|
|
1766
|
+
*/
|
|
1767
|
+
const handlerReferences = /* @__PURE__ */ new WeakMap();
|
|
1768
|
+
/** The modifiers that the wrappers of `brumate/events` put on a handler. */
|
|
1769
|
+
const MODIFIERS = Symbol.for("brumate.modifiers");
|
|
1770
|
+
/**
|
|
1771
|
+
* Renders an object of attributes. With `on`, a handler in it that the core
|
|
1772
|
+
* made from a reference goes into `on` as a `b-on` entry, with the scope and
|
|
1773
|
+
* the arguments of its reference and the modifiers of its wrappers.
|
|
1774
|
+
*/
|
|
1775
|
+
function spread(attributes, on, names) {
|
|
1776
|
+
if (on) {
|
|
1777
|
+
on.push(...spreadEntries(attributes));
|
|
1778
|
+
return spread$1({
|
|
1779
|
+
...attributes,
|
|
1780
|
+
"b-on": void 0
|
|
1781
|
+
}, names);
|
|
1782
|
+
}
|
|
1783
|
+
return spread$1(attributes, names);
|
|
1784
|
+
}
|
|
1785
|
+
/** The `b-on` entries of the handlers and bindings of an object of attributes. */
|
|
1786
|
+
function spreadEntries(attributes) {
|
|
1787
|
+
const on = [];
|
|
1788
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
1789
|
+
if (name === "b-on" && ownValue(name, value)) on.unshift(ownValue(name, value));
|
|
1790
|
+
const parts = /^on[A-Z]/.test(name) && typeof value === "function" ? value[PARTS] ?? [value] : [];
|
|
1791
|
+
for (const part of parts) {
|
|
1792
|
+
const reference = handlerReferences.get(part);
|
|
1793
|
+
if (reference) on.push(handlerEntry(name, reference.ref, reference.scope, reference.modifiers, reference.args));
|
|
1794
|
+
}
|
|
1795
|
+
if ((name === "value" || name === "checked") && HAS_BINDINGS && isBinding(value)) on.push(bindEntry(name, value.ref, setterReference(value.set)));
|
|
1796
|
+
}
|
|
1797
|
+
return on;
|
|
1798
|
+
}
|
|
1799
|
+
/** The reference of the setter of a binding: as the state has it, or of a handler that the core made. */
|
|
1800
|
+
function setterReference(setter) {
|
|
1801
|
+
if (setter instanceof FunctionReference) return setter;
|
|
1802
|
+
return typeof setter === "function" ? handlerReferences.get(setter) : void 0;
|
|
1803
|
+
}
|
|
1804
|
+
/** Replaces function references with functions that call them. */
|
|
1805
|
+
function callable(value, seen) {
|
|
1806
|
+
if (HAS_CHILDREN && value instanceof ChildrenReference) return new RenderedChildren(value.scope, value.ref, value.at, value.anchor);
|
|
1807
|
+
if (value instanceof FunctionReference) {
|
|
1808
|
+
if (HAS_TEMPLATES && isTemplate(value.ref)) return templateReference(value.ref, scopes.get(value.scope));
|
|
1809
|
+
const handler = (...args) => {
|
|
1810
|
+
const fn = loadedFunction(value.ref);
|
|
1811
|
+
const all = [...value.args ?? [], ...args];
|
|
1812
|
+
return fn ? fn(scopes.get(value.scope), ...all) : call(value.ref, scopes.get(value.scope), all);
|
|
1813
|
+
};
|
|
1814
|
+
handlerReferences.set(handler, value);
|
|
1815
|
+
return handler;
|
|
1816
|
+
}
|
|
1817
|
+
if (typeof value === "object" && value !== null && !seen.has(value)) {
|
|
1818
|
+
seen.add(value);
|
|
1819
|
+
const prototype = Object.getPrototypeOf(value);
|
|
1820
|
+
if (Array.isArray(value) || prototype === Object.prototype || prototype === null) {
|
|
1821
|
+
const record = value;
|
|
1822
|
+
for (const key of Object.keys(record)) record[key] = callable(record[key], seen);
|
|
1823
|
+
if (PARTS in record) return mergeHandlers(value);
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
return value;
|
|
1827
|
+
}
|
|
1828
|
+
let lastHandler = Promise.resolve();
|
|
1829
|
+
/** Forgets the handler of the previous event, for a new page. */
|
|
1830
|
+
function resetHandler() {
|
|
1831
|
+
lastHandler = Promise.resolve();
|
|
1832
|
+
}
|
|
1833
|
+
/**
|
|
1834
|
+
* Keeps `currentTarget` on an event. The handler runs after the event has
|
|
1835
|
+
* finished dispatching, when the browser has already cleared it.
|
|
1836
|
+
*/
|
|
1837
|
+
function withTarget(event, element) {
|
|
1838
|
+
return new Proxy(event, { get(target, key) {
|
|
1839
|
+
if (key === "currentTarget") return element;
|
|
1840
|
+
const value = Reflect.get(target, key);
|
|
1841
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
1842
|
+
} });
|
|
1843
|
+
}
|
|
1844
|
+
/** Whether an input is a checkbox or a radio, which a binding reads by `checked`. */
|
|
1845
|
+
function isBox(field) {
|
|
1846
|
+
return /^(checkbox|radio)$/i.test(field.type);
|
|
1847
|
+
}
|
|
1848
|
+
/** The value a binding writes: of a field of a part, whether a box is checked, or the value. */
|
|
1849
|
+
function fieldValue(element, typed) {
|
|
1850
|
+
const field = element;
|
|
1851
|
+
if (typed) return typed.value;
|
|
1852
|
+
return isBox(field) ? field.checked : field.value;
|
|
1853
|
+
}
|
|
1854
|
+
/**
|
|
1855
|
+
* Runs the handler `ref` for an element. The scope comes from the element's
|
|
1856
|
+
* `b-b` anchor, or from the nearest element around it that has one, which
|
|
1857
|
+
* the compiler checks is in the same scope.
|
|
1858
|
+
*/
|
|
1859
|
+
function handle(element, entry, event, typed) {
|
|
1860
|
+
const [ref, owner, encoded] = entry.split("@");
|
|
1861
|
+
const bound = encoded ? JSON.parse(decodeURIComponent(encoded)) : [];
|
|
1862
|
+
const anchor = owner ?? element.closest("[b-b]")?.getAttribute("b-b")?.split(":")[0];
|
|
1863
|
+
const scope = anchor ? scopes.get(parseId(anchor)) : void 0;
|
|
1864
|
+
if (anchor && !scope && !element.isConnected) return Promise.resolve();
|
|
1865
|
+
if (scope && hiding?.hidden(scope) && element.hasAttribute("b-listen")) return Promise.resolve();
|
|
1866
|
+
if (ref.startsWith("$set:")) return call(ref.slice(5), scope, [fieldValue(element, typed)]);
|
|
1867
|
+
if (ref.startsWith("$bind#")) {
|
|
1868
|
+
const [hopsText, slotText] = ref.slice(6).split(".");
|
|
1869
|
+
const hops = Number(hopsText ?? 0);
|
|
1870
|
+
const slot = Number(slotText ?? 0);
|
|
1871
|
+
let target = scope;
|
|
1872
|
+
for (let hop = 0; hop < hops; hop++) target = target?.p;
|
|
1873
|
+
if (target) set(target, slot, fieldValue(element, typed));
|
|
1874
|
+
return Promise.resolve();
|
|
1875
|
+
}
|
|
1876
|
+
const args = event ? [...bound, withTarget(event, element)] : bound;
|
|
1877
|
+
const begun = DEV ? performance.now() : 0;
|
|
1878
|
+
if (DEV) devtools()?.handler?.(ref, element, event);
|
|
1879
|
+
const children = childrenReady(scope);
|
|
1880
|
+
const code = codeReady();
|
|
1881
|
+
const loaded = Promise.all([
|
|
1882
|
+
resolve$1(ref),
|
|
1883
|
+
children,
|
|
1884
|
+
code,
|
|
1885
|
+
updateReady(scope, ref)
|
|
1886
|
+
]).then(([handler]) => handler);
|
|
1887
|
+
const started = lastHandler.then(() => loaded).then((handler) => {
|
|
1888
|
+
if (!DEV) return { value: pass(() => handler(scope, ...args)) };
|
|
1889
|
+
const ready = performance.now();
|
|
1890
|
+
const value = pass(() => handler(scope, ...args));
|
|
1891
|
+
devtools()?.handled?.({
|
|
1892
|
+
ref,
|
|
1893
|
+
element,
|
|
1894
|
+
event: event?.type,
|
|
1895
|
+
begun,
|
|
1896
|
+
ready,
|
|
1897
|
+
end: performance.now(),
|
|
1898
|
+
result: value
|
|
1899
|
+
});
|
|
1900
|
+
return { value };
|
|
1901
|
+
});
|
|
1902
|
+
lastHandler = started.catch(() => {});
|
|
1903
|
+
return started.then((result) => result.value).catch((error) => {
|
|
1904
|
+
fail(scope, error);
|
|
1905
|
+
});
|
|
1906
|
+
}
|
|
1907
|
+
/** Renders the attributes of a form or button that posts to a server function. */
|
|
1908
|
+
function action(name, id, method, result) {
|
|
1909
|
+
return actionAttributes(rpcPrefix, name, id, method, result, build);
|
|
1910
|
+
}
|
|
1911
|
+
/**
|
|
1912
|
+
* Sends a form of an `$action`. The code is in `rpc.ts`, which every server
|
|
1913
|
+
* call loads, so the core holds only this.
|
|
1914
|
+
*/
|
|
1915
|
+
function submitAction(form, submitter) {
|
|
1916
|
+
import("./rpc-Q-XkLwFh.js").then((module) => module.sendAction(form, submitter));
|
|
1917
|
+
}
|
|
1918
|
+
/** Calls a server function. */
|
|
1919
|
+
async function rpc(id, args) {
|
|
1920
|
+
const signal = memoSignal();
|
|
1921
|
+
return track(import("./rpc-Q-XkLwFh.js").then((module) => module.call(rpcPrefix, id, args, signal)));
|
|
1922
|
+
}
|
|
1923
|
+
/** A function prop rendered on the client calls the parent's exported handler. */
|
|
1924
|
+
function handlerProp(value, ref, scope, bound = [], modifiers) {
|
|
1925
|
+
if (typeof value !== "function" || isTemplate(ref)) return value;
|
|
1926
|
+
const instance = resolveScope(scope);
|
|
1927
|
+
function handler(...args) {
|
|
1928
|
+
const fn = loadedFunction(ref);
|
|
1929
|
+
const all = [...bound, ...args];
|
|
1930
|
+
return fn ? fn(instance, ...all) : value(...all);
|
|
1931
|
+
}
|
|
1932
|
+
handlerReferences.set(handler, new FunctionReference(ref, instance?.id ?? scope, bound.length > 0 ? bound : void 0, modifiers ?? value[MODIFIERS]));
|
|
1933
|
+
return handler;
|
|
1934
|
+
}
|
|
1935
|
+
//#endregion
|
|
1936
|
+
//#region src/runtime/client/resume.ts
|
|
1937
|
+
/**
|
|
1938
|
+
* The records that ran before the core started, or while it stopped. Each
|
|
1939
|
+
* state script hands its record to `push`, which the core replaces while it
|
|
1940
|
+
* runs, so it reads later records at once.
|
|
1941
|
+
*/
|
|
1942
|
+
function stateQueue() {
|
|
1943
|
+
const global = globalThis;
|
|
1944
|
+
return global.$brm ??= [];
|
|
1945
|
+
}
|
|
1946
|
+
/** Reads the records that later state scripts hand over. Set while the core runs. */
|
|
1947
|
+
function pushRecords(...added) {
|
|
1948
|
+
for (const record of added) readRecord(record);
|
|
1949
|
+
return 0;
|
|
1950
|
+
}
|
|
1951
|
+
/**
|
|
1952
|
+
* Reads a state record. The first record holds the page state, and later
|
|
1953
|
+
* records settle the promises in it.
|
|
1954
|
+
*/
|
|
1955
|
+
function readRecord([script, code]) {
|
|
1956
|
+
if (!script?.isConnected) return;
|
|
1957
|
+
const root = script.getAttribute("b-root") ?? "0";
|
|
1958
|
+
if (!refs.has(root)) refs.set(root, []);
|
|
1959
|
+
const decoded = code(refs.get(root), stateValues);
|
|
1960
|
+
const catches = script.getAttribute("b-catch");
|
|
1961
|
+
if (catches !== null) {
|
|
1962
|
+
replaceTry(catches);
|
|
1963
|
+
return;
|
|
1964
|
+
}
|
|
1965
|
+
if (script.getAttribute("b-fail") !== null) {
|
|
1966
|
+
failBoundary(decoded);
|
|
1967
|
+
return;
|
|
1968
|
+
}
|
|
1969
|
+
if (script.hasAttribute("b-scopes")) {
|
|
1970
|
+
const unpacked = unpackState(decoded);
|
|
1971
|
+
const added = addScopes(hooks.record ? hooks.record(script, unpacked) : unpacked, root);
|
|
1972
|
+
if (writes > 0) refresh(added);
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
/** Reads the groups of scope records back into one record per scope. See `PackedState`. */
|
|
1976
|
+
function unpackState(state) {
|
|
1977
|
+
const taken = [];
|
|
1978
|
+
for (const group of state.g ?? []) {
|
|
1979
|
+
const [k, base, flags] = group;
|
|
1980
|
+
const count = group[3];
|
|
1981
|
+
let at = 4;
|
|
1982
|
+
const m = flags & 16 ? group[at++] : void 0;
|
|
1983
|
+
const source = flags & 32 ? [] : void 0;
|
|
1984
|
+
if (source) taken.push([
|
|
1985
|
+
source,
|
|
1986
|
+
group[at++],
|
|
1987
|
+
group[at++],
|
|
1988
|
+
flags & 1
|
|
1989
|
+
]);
|
|
1990
|
+
const prefix = flags & 64 ? group[at++] : "";
|
|
1991
|
+
function id(n) {
|
|
1992
|
+
return prefix ? prefix + n : n;
|
|
1993
|
+
}
|
|
1994
|
+
const skip = source && flags & 1 ? 1 : 0;
|
|
1995
|
+
let n = flags & 72 ? 0 : base;
|
|
1996
|
+
while (at < group.length) {
|
|
1997
|
+
n += group[at++];
|
|
1998
|
+
const s = group.slice(at - skip, at += count - skip);
|
|
1999
|
+
const record = flags & 8 ? {
|
|
2000
|
+
k,
|
|
2001
|
+
o: id(n - base)
|
|
2002
|
+
} : {
|
|
2003
|
+
k,
|
|
2004
|
+
p: base
|
|
2005
|
+
};
|
|
2006
|
+
record.s = s;
|
|
2007
|
+
if (m !== void 0) record.m = m;
|
|
2008
|
+
if (source) source.push(record);
|
|
2009
|
+
else if (flags & 1) record.i = s[0];
|
|
2010
|
+
else if (flags & 2) record.i = group[at++];
|
|
2011
|
+
if (flags & 4) record.c = group[at++];
|
|
2012
|
+
state[id(n)] = record;
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
delete state.g;
|
|
2016
|
+
for (const [records, source, slot, first] of taken) {
|
|
2017
|
+
const values = Array.from(state[source].s[slot]);
|
|
2018
|
+
for (let n = 0; n < records.length; n++) {
|
|
2019
|
+
const record = records[n];
|
|
2020
|
+
record.i = values[n];
|
|
2021
|
+
if (first) record.s[0] = values[n];
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
return state;
|
|
2025
|
+
}
|
|
2026
|
+
function addScopes(state, root) {
|
|
2027
|
+
const added = [];
|
|
2028
|
+
for (const [key, record] of Object.entries(state)) {
|
|
2029
|
+
const id = parseId(key);
|
|
2030
|
+
const scope = {
|
|
2031
|
+
id,
|
|
2032
|
+
k: record.k ?? 0,
|
|
2033
|
+
m: record.m,
|
|
2034
|
+
s: record.s ?? [],
|
|
2035
|
+
i: record.i,
|
|
2036
|
+
c: record.c,
|
|
2037
|
+
v: record.v,
|
|
2038
|
+
x: record.x,
|
|
2039
|
+
b: record.b,
|
|
2040
|
+
r: root
|
|
2041
|
+
};
|
|
2042
|
+
scopes.set(id, scope);
|
|
2043
|
+
added.push(scope);
|
|
2044
|
+
}
|
|
2045
|
+
for (const scope of added) {
|
|
2046
|
+
const record = state[scope.id];
|
|
2047
|
+
if (record.p !== void 0) {
|
|
2048
|
+
scope.p = scopes.get(record.p);
|
|
2049
|
+
if (scope.p) (scope.p.children ??= /* @__PURE__ */ new Set()).add(scope);
|
|
2050
|
+
treeChanged();
|
|
2051
|
+
}
|
|
2052
|
+
if (record.o !== void 0) {
|
|
2053
|
+
scope.o = scopes.get(record.o);
|
|
2054
|
+
if (scope.o) (scope.o.owned ??= /* @__PURE__ */ new Set()).add(scope);
|
|
2055
|
+
}
|
|
2056
|
+
if (record.t !== void 0) scope.t = scopes.get(record.t);
|
|
2057
|
+
if (record.f !== void 0) scope.f = scopes.get(record.f);
|
|
2058
|
+
}
|
|
2059
|
+
for (const scope of added) if (scope.m) options.loadBundle?.(scope.m)?.catch(() => {});
|
|
2060
|
+
for (const scope of added) if (scope.m) loadComponent(scope.m);
|
|
2061
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2062
|
+
for (const scope of added) {
|
|
2063
|
+
for (const key of Object.keys(scope.v ?? {})) scope.v[key] = callable(scope.v[key], seen);
|
|
2064
|
+
for (let i = 0; i < scope.s.length; i++) {
|
|
2065
|
+
const value = callable(scope.s[i], seen);
|
|
2066
|
+
scope.s[i] = value;
|
|
2067
|
+
if (isPropWrapper(value)) {
|
|
2068
|
+
scope.s[i] = value.promise;
|
|
2069
|
+
(scope.plain ??= /* @__PURE__ */ new Set()).add(i);
|
|
2070
|
+
value.promise.catch(() => {});
|
|
2071
|
+
continue;
|
|
2072
|
+
}
|
|
2073
|
+
if (value instanceof Promise) {
|
|
2074
|
+
loadingValues.add(value);
|
|
2075
|
+
value.then((result) => {
|
|
2076
|
+
settled.set(value, result);
|
|
2077
|
+
if (scope.s[i] === value && !loads.has(`${scope.id}:${i}`)) set(scope, i, result);
|
|
2078
|
+
});
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
if (HAS_BINDINGS) for (const scope of added) for (const [slot, bound] of Object.entries(scope.b ?? {})) {
|
|
2083
|
+
const value = scope.s[+slot];
|
|
2084
|
+
const element = value instanceof ElementReference ? findElement(value.scope, value.anchor) : null;
|
|
2085
|
+
if (element && !isSlotReference(bound)) send(bound, element);
|
|
2086
|
+
}
|
|
2087
|
+
hooks.added?.(added);
|
|
2088
|
+
return added;
|
|
2089
|
+
}
|
|
2090
|
+
/** Stops reading state records and markers that arrive later. Later records wait for `init`. */
|
|
2091
|
+
function dispose() {
|
|
2092
|
+
const queue = stateQueue();
|
|
2093
|
+
if (queue.push === pushRecords) delete queue.push;
|
|
2094
|
+
stopObserving();
|
|
2095
|
+
}
|
|
2096
|
+
/**
|
|
2097
|
+
* Stops reading, before client navigation replaces only some routes. The
|
|
2098
|
+
* server renders the new routes with its current values, so only the writes
|
|
2099
|
+
* that come after count for the records that stream in later.
|
|
2100
|
+
*/
|
|
2101
|
+
function leaveRoutes() {
|
|
2102
|
+
dispose();
|
|
2103
|
+
resetWrites();
|
|
2104
|
+
}
|
|
2105
|
+
/**
|
|
2106
|
+
* Removes the scopes of a render, by its `b-root`, with the scopes inside
|
|
2107
|
+
* them. Their cleanups run, and their memos no longer load. Its markup
|
|
2108
|
+
* stays. `brumate/testing` calls it when a mounted component unmounts.
|
|
2109
|
+
*/
|
|
2110
|
+
function removeRoot(root) {
|
|
2111
|
+
function inside(scope) {
|
|
2112
|
+
for (let current = scope; current; current = current.p ?? current.o) if (current.r === root) return true;
|
|
2113
|
+
return false;
|
|
2114
|
+
}
|
|
2115
|
+
for (const scope of scopes.values()) if (inside(scope)) removeScope(scope);
|
|
2116
|
+
refs.delete(root);
|
|
2117
|
+
for (const cache of [comments, elements]) for (const [key, node] of cache) if (!node.isConnected) cache.delete(key);
|
|
2118
|
+
}
|
|
2119
|
+
/**
|
|
2120
|
+
* Forgets the code of components that changed on the dev server, so the next
|
|
2121
|
+
* event or update loads the new code. Their state stays. `brumate/hot` calls it.
|
|
2122
|
+
*/
|
|
2123
|
+
function forget(ids) {
|
|
2124
|
+
for (const id of ids) {
|
|
2125
|
+
modules.delete(id);
|
|
2126
|
+
loading.delete(id);
|
|
2127
|
+
pageComponents.delete(id);
|
|
2128
|
+
for (const key of parts.keys()) if (key.startsWith(`${id}:`)) {
|
|
2129
|
+
parts.delete(key);
|
|
2130
|
+
loadedParts.delete(key);
|
|
2131
|
+
}
|
|
2132
|
+
}
|
|
2133
|
+
}
|
|
2134
|
+
/** The values of the signals of components whose markup changed, for when the page renders again. */
|
|
2135
|
+
const remounts = /* @__PURE__ */ new Map();
|
|
2136
|
+
/**
|
|
2137
|
+
* Keeps the values of the signals of each instance of a component, by
|
|
2138
|
+
* name, before the page renders again with its new markup. `brumate/hot` calls it.
|
|
2139
|
+
*/
|
|
2140
|
+
function remount(detail) {
|
|
2141
|
+
hooks.added = restoreRemounts;
|
|
2142
|
+
for (const { id, before, after, signals } of detail) {
|
|
2143
|
+
const names = new Set(signals);
|
|
2144
|
+
const kept = [];
|
|
2145
|
+
for (const root of scopes.values()) if (root.m === id) {
|
|
2146
|
+
const values = {};
|
|
2147
|
+
for (const [slot, name] of (before[root.k] ?? []).entries()) if (names.has(name)) values[name] = root.s[slot];
|
|
2148
|
+
kept.push(values);
|
|
2149
|
+
}
|
|
2150
|
+
remounts.set(id, {
|
|
2151
|
+
after,
|
|
2152
|
+
values: kept
|
|
2153
|
+
});
|
|
2154
|
+
}
|
|
2155
|
+
}
|
|
2156
|
+
/** Writes back the values of signals that `onRemount` kept, into the instances of the new page. */
|
|
2157
|
+
function restoreRemounts(added) {
|
|
2158
|
+
for (const [id, { after, values }] of remounts) {
|
|
2159
|
+
const instances = [];
|
|
2160
|
+
for (const scope of added) if (scope.m === id) instances.push(scope);
|
|
2161
|
+
if (instances.length === 0) continue;
|
|
2162
|
+
remounts.delete(id);
|
|
2163
|
+
for (const [index, root] of instances.entries()) {
|
|
2164
|
+
const saved = values[index];
|
|
2165
|
+
for (const [slot, name] of (after[root.k] ?? []).entries()) if (saved && name in saved) set(root, slot, saved[name]);
|
|
2166
|
+
}
|
|
2167
|
+
}
|
|
2168
|
+
}
|
|
2169
|
+
/**
|
|
2170
|
+
* Forgets the page, before another page replaces it. Cleanups run. Loaded
|
|
2171
|
+
* modules and parts stay, so the next page does not load them again. Only
|
|
2172
|
+
* client navigation calls it.
|
|
2173
|
+
*/
|
|
2174
|
+
function leavePage() {
|
|
2175
|
+
dispose();
|
|
2176
|
+
for (const scope of scopes.values()) for (const cleanup of scope.cleanups?.values() ?? []) cleanup();
|
|
2177
|
+
scopes.clear();
|
|
2178
|
+
comments.clear();
|
|
2179
|
+
elements.clear();
|
|
2180
|
+
unindexed.length = 0;
|
|
2181
|
+
rendered.length = 0;
|
|
2182
|
+
pendingEffects.clear();
|
|
2183
|
+
setHiding(void 0);
|
|
2184
|
+
loads.clear();
|
|
2185
|
+
loadingSlots.clear();
|
|
2186
|
+
for (const memoController of memoControllers.values()) memoController.abort();
|
|
2187
|
+
memoControllers.clear();
|
|
2188
|
+
renderedLoads.clear();
|
|
2189
|
+
for (const { timer } of timers.values()) clearTimeout(timer);
|
|
2190
|
+
timers.clear();
|
|
2191
|
+
guesses?.map.clear();
|
|
2192
|
+
resetIds();
|
|
2193
|
+
refs.clear();
|
|
2194
|
+
resetWrites();
|
|
2195
|
+
leaveGuesses();
|
|
2196
|
+
resetHandler();
|
|
2197
|
+
}
|
|
2198
|
+
/**
|
|
2199
|
+
* Whether the user changed a field since the server rendered it. The server's value is in
|
|
2200
|
+
* `defaultValue`, `defaultChecked` and `defaultSelected`. This runs only when the core starts,
|
|
2201
|
+
* before any update wrote the field. A value that the browser cleans, such as a number or a
|
|
2202
|
+
* range input, can differ from `defaultValue` without the user, and counts as typed.
|
|
2203
|
+
*/
|
|
2204
|
+
function typedInto(element) {
|
|
2205
|
+
const field = element;
|
|
2206
|
+
if (element instanceof HTMLSelectElement) {
|
|
2207
|
+
let expected = -1;
|
|
2208
|
+
let first = -1;
|
|
2209
|
+
let changed = false;
|
|
2210
|
+
for (let i = 0; i < element.options.length; i++) {
|
|
2211
|
+
const option = element.options[i];
|
|
2212
|
+
const rendered = option.hasAttribute("selected");
|
|
2213
|
+
if (rendered) expected = i;
|
|
2214
|
+
if (first < 0 && !option.matches(":disabled")) first = i;
|
|
2215
|
+
changed ||= option.selected !== rendered;
|
|
2216
|
+
}
|
|
2217
|
+
if (element.multiple) return changed;
|
|
2218
|
+
const shown = Number(element.getAttribute("size")) > 1 ? -1 : first;
|
|
2219
|
+
return element.selectedIndex !== (expected >= 0 ? expected : shown);
|
|
2220
|
+
}
|
|
2221
|
+
return isBox(field) ? field.checked !== field.defaultChecked : field.value !== field.defaultValue;
|
|
2222
|
+
}
|
|
2223
|
+
/**
|
|
2224
|
+
* Writes the signal of each bound field that the user changed before the code loaded, as an
|
|
2225
|
+
* `input` event would. The text is in the DOM, and the first update would write the old value
|
|
2226
|
+
* back. A field that still has the value the server rendered is not read.
|
|
2227
|
+
*/
|
|
2228
|
+
function readFields() {
|
|
2229
|
+
for (const element of document.querySelectorAll("[b-on*=\"=$bind#\"],[b-on*=\"=$set:\"]")) {
|
|
2230
|
+
const custom = hooks.field?.(element);
|
|
2231
|
+
if (custom === false || !custom && !typedInto(element)) continue;
|
|
2232
|
+
for (const entry of element.getAttribute("b-on").split(" ")) {
|
|
2233
|
+
const ref = entry.slice(entry.indexOf("=") + 1);
|
|
2234
|
+
if (/^\$(bind#|set:)/.test(ref)) handle(element, ref, void 0, custom);
|
|
2235
|
+
}
|
|
2236
|
+
}
|
|
2237
|
+
}
|
|
2238
|
+
/** Reads the page state. The loader calls it once, before the first handler. */
|
|
2239
|
+
function init(value) {
|
|
2240
|
+
configure(value);
|
|
2241
|
+
if (value.serial) useSerialEntry(value.serial);
|
|
2242
|
+
const queue = stateQueue();
|
|
2243
|
+
queue.push = pushRecords;
|
|
2244
|
+
for (const record of queue.splice(0)) readRecord(record);
|
|
2245
|
+
if (HAS_LATE_MARKUP) observeMarkers();
|
|
2246
|
+
else if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", () => indexMarkers(document.documentElement), { once: true });
|
|
2247
|
+
indexMarkers(document.documentElement);
|
|
2248
|
+
readFields();
|
|
2249
|
+
watchTitles(document);
|
|
2250
|
+
if (DEV) devtools()?.attach?.({
|
|
2251
|
+
scopes,
|
|
2252
|
+
set,
|
|
2253
|
+
handle
|
|
2254
|
+
});
|
|
2255
|
+
}
|
|
2256
|
+
//#endregion
|
|
2257
|
+
//#region src/runtime/client/branches.ts
|
|
2258
|
+
/** Shows the branch at `index` of `#if` or `#switch`, or nothing for -1. */
|
|
2259
|
+
function branch(scope, anchor, index, renders, kinds, holderKind, holderAnchor) {
|
|
2260
|
+
const start = blockStart(scope, anchor);
|
|
2261
|
+
if (!start) return;
|
|
2262
|
+
if (Number(start.data.split(":")[2]) === index) return;
|
|
2263
|
+
const end = rangeEnd(start);
|
|
2264
|
+
removeBlock(scope, kinds, start, end);
|
|
2265
|
+
start.data = `b${scope.id}:${anchor}:${index}`;
|
|
2266
|
+
const render = inPlace(scope, renders[index], holderKind, holderAnchor);
|
|
2267
|
+
if (render) insertHTML(end, render(scope));
|
|
2268
|
+
}
|
|
2269
|
+
/** Renders the body of `#key` again when its value changes. */
|
|
2270
|
+
function rekey(scope, anchor, value, render, kinds, holderKind, holderAnchor) {
|
|
2271
|
+
scope.keys ??= /* @__PURE__ */ new Map();
|
|
2272
|
+
if (scope.keys.has(anchor) && Object.is(scope.keys.get(anchor), value)) return;
|
|
2273
|
+
scope.keys.set(anchor, value);
|
|
2274
|
+
const start = blockStart(scope, anchor);
|
|
2275
|
+
if (!start) return;
|
|
2276
|
+
const end = rangeEnd(start);
|
|
2277
|
+
removeBlock(scope, kinds, start, end);
|
|
2278
|
+
insertHTML(end, inPlace(scope, render, holderKind, holderAnchor)(scope));
|
|
2279
|
+
}
|
|
2280
|
+
/**
|
|
2281
|
+
* `rekey` for the block around a dynamic tag. The first update keeps the element that the server
|
|
2282
|
+
* rendered, when it has the tag already, such as an update of a scope that streams in after an
|
|
2283
|
+
* event.
|
|
2284
|
+
*/
|
|
2285
|
+
function retag(scope, anchor, value, render, kinds, holderKind, holderAnchor) {
|
|
2286
|
+
scope.keys ??= /* @__PURE__ */ new Map();
|
|
2287
|
+
if (!scope.keys.has(anchor) && typeof value === "string") {
|
|
2288
|
+
let node = blockStart(scope, anchor)?.nextSibling;
|
|
2289
|
+
while (node && node.nodeType !== 1 && node.nodeType !== 8) node = node.nextSibling;
|
|
2290
|
+
if (node?.nodeType === 1 && node.localName === value.toLowerCase()) {
|
|
2291
|
+
scope.keys.set(anchor, value);
|
|
2292
|
+
return;
|
|
2293
|
+
}
|
|
2294
|
+
}
|
|
2295
|
+
if (!scope.keys.has(anchor) && typeof value === "function") {
|
|
2296
|
+
for (const block of scopes.values()) if (block.p === scope && kinds.includes(block.k)) {
|
|
2297
|
+
for (const id of Object.values(block.c ?? {})) if (scopes.get(id)?.m === value.brumateId) {
|
|
2298
|
+
scope.keys.set(anchor, value);
|
|
2299
|
+
return;
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
}
|
|
2303
|
+
rekey(scope, anchor, value, render, kinds, holderKind, holderAnchor);
|
|
2304
|
+
}
|
|
2305
|
+
/**
|
|
2306
|
+
* Renders the body of a `#try` block again, for its `reset` function. If the
|
|
2307
|
+
* body throws, the fallback renders again with the error.
|
|
2308
|
+
*/
|
|
2309
|
+
async function reset(scope, anchor, id, body, fallback, kinds, holderKind, holderAnchor) {
|
|
2310
|
+
const updates = await loadPart(await loadModule(id), "update");
|
|
2311
|
+
const start = blockStart(scope, anchor);
|
|
2312
|
+
if (!start) return;
|
|
2313
|
+
const end = rangeEnd(start);
|
|
2314
|
+
removeBlock(scope, kinds, start, end);
|
|
2315
|
+
let html;
|
|
2316
|
+
try {
|
|
2317
|
+
html = inPlace(scope, updates[body], holderKind, holderAnchor)(scope);
|
|
2318
|
+
} catch (error) {
|
|
2319
|
+
disposeKinds(scope, kinds);
|
|
2320
|
+
afterInsert.length = 0;
|
|
2321
|
+
html = inPlace(scope, updates[fallback], holderKind, holderAnchor)(scope, error);
|
|
2322
|
+
}
|
|
2323
|
+
insertHTML(end, html);
|
|
2324
|
+
}
|
|
2325
|
+
/**
|
|
2326
|
+
* Renders the body of a `#client` block once, in place of its fallback.
|
|
2327
|
+
* `kinds` are the scope IDs inside the fallback, whose instances go.
|
|
2328
|
+
*/
|
|
2329
|
+
function fill(scope, anchor, render, kinds = [], holderKind, holderAnchor) {
|
|
2330
|
+
const start = blockStart(scope, anchor);
|
|
2331
|
+
if (!start || start.data.split(":").length > 2) return;
|
|
2332
|
+
start.data += ":1";
|
|
2333
|
+
const end = rangeEnd(start);
|
|
2334
|
+
removeBlock(scope, kinds, start, end);
|
|
2335
|
+
insertHTML(end, inPlace(scope, render, holderKind, holderAnchor)(scope));
|
|
2336
|
+
}
|
|
2337
|
+
/**
|
|
2338
|
+
* Shows or hides the body of an `#offscreen` block. A body that the page
|
|
2339
|
+
* does not have yet renders the first time it shows. `kind` is the body's
|
|
2340
|
+
* scope ID in the compiled component.
|
|
2341
|
+
*/
|
|
2342
|
+
function offscreen(scope, anchor, value, render, kind, holderKind, holderAnchor) {
|
|
2343
|
+
const start = blockStart(scope, anchor);
|
|
2344
|
+
if (!start) return;
|
|
2345
|
+
const visible = Boolean(value);
|
|
2346
|
+
const state = start.data.split(":")[2];
|
|
2347
|
+
if (visible ? state === "1" : state !== "1") return;
|
|
2348
|
+
start.data = `b${scope.id}:${anchor}:${visible ? 1 : 0}`;
|
|
2349
|
+
const end = rangeEnd(start);
|
|
2350
|
+
if (state === "-1") {
|
|
2351
|
+
insertHTML(end, inPlace(scope, render, holderKind, holderAnchor)(scope));
|
|
2352
|
+
return;
|
|
2353
|
+
}
|
|
2354
|
+
let body;
|
|
2355
|
+
for (const child of scope.children ?? []) if (child.k === kind) body = child;
|
|
2356
|
+
if (body) {
|
|
2357
|
+
const holder = hiding ?? setHiding(createHiding());
|
|
2358
|
+
if (visible) holder.show(body);
|
|
2359
|
+
else holder.hide(body, start, end);
|
|
2360
|
+
}
|
|
2361
|
+
}
|
|
2362
|
+
/** True for a scope that is `ancestor` or inside it, also through the owner of a component. */
|
|
2363
|
+
function within(scope, ancestor) {
|
|
2364
|
+
for (let current = scope; current; current = current.p ?? current.o) if (current === ancestor) return true;
|
|
2365
|
+
return false;
|
|
2366
|
+
}
|
|
2367
|
+
/** True for a scope in a hidden `#offscreen` body. */
|
|
2368
|
+
function hidden(scope) {
|
|
2369
|
+
for (let current = scope; current; current = current.p ?? current.o) if (current.h) return true;
|
|
2370
|
+
return false;
|
|
2371
|
+
}
|
|
2372
|
+
/** The nodes between two markers. */
|
|
2373
|
+
function between([start, end]) {
|
|
2374
|
+
const result = [];
|
|
2375
|
+
for (let node = start.nextSibling; node && node !== end; node = node.nextSibling) result.push(node);
|
|
2376
|
+
return result;
|
|
2377
|
+
}
|
|
2378
|
+
/** The inline style of an element, which an element that is not HTML or SVG may not have. */
|
|
2379
|
+
function display(element) {
|
|
2380
|
+
return element.style;
|
|
2381
|
+
}
|
|
2382
|
+
function createHiding() {
|
|
2383
|
+
const bodies = /* @__PURE__ */ new Set();
|
|
2384
|
+
const nodes = /* @__PURE__ */ new WeakMap();
|
|
2385
|
+
function hideNode(node) {
|
|
2386
|
+
const saved = nodes.get(node);
|
|
2387
|
+
if (saved) {
|
|
2388
|
+
saved.n++;
|
|
2389
|
+
return;
|
|
2390
|
+
}
|
|
2391
|
+
if (node.nodeType === 3) {
|
|
2392
|
+
nodes.set(node, {
|
|
2393
|
+
n: 1,
|
|
2394
|
+
v: node.data
|
|
2395
|
+
});
|
|
2396
|
+
node.data = "";
|
|
2397
|
+
return;
|
|
2398
|
+
}
|
|
2399
|
+
const style = node.nodeType === 1 ? display(node) : void 0;
|
|
2400
|
+
if (style) {
|
|
2401
|
+
nodes.set(node, {
|
|
2402
|
+
n: 1,
|
|
2403
|
+
v: style.getPropertyValue("display"),
|
|
2404
|
+
p: style.getPropertyPriority("display"),
|
|
2405
|
+
a: node.hasAttribute("style")
|
|
2406
|
+
});
|
|
2407
|
+
style.setProperty("display", "none", "important");
|
|
2408
|
+
}
|
|
2409
|
+
}
|
|
2410
|
+
function showNode(node) {
|
|
2411
|
+
const saved = nodes.get(node);
|
|
2412
|
+
if (!saved || --saved.n > 0) return;
|
|
2413
|
+
nodes.delete(node);
|
|
2414
|
+
if (node.nodeType === 3) {
|
|
2415
|
+
node.data = saved.v;
|
|
2416
|
+
return;
|
|
2417
|
+
}
|
|
2418
|
+
const element = node;
|
|
2419
|
+
const style = display(element);
|
|
2420
|
+
if (saved.v) style.setProperty("display", saved.v, saved.p);
|
|
2421
|
+
else style.removeProperty("display");
|
|
2422
|
+
if (!saved.a && element.getAttribute("style") === "") element.removeAttribute("style");
|
|
2423
|
+
}
|
|
2424
|
+
return {
|
|
2425
|
+
bodies,
|
|
2426
|
+
nodes,
|
|
2427
|
+
hidden,
|
|
2428
|
+
hide(body, start, end) {
|
|
2429
|
+
const record = {
|
|
2430
|
+
scope: body,
|
|
2431
|
+
ranges: [[start, end]]
|
|
2432
|
+
};
|
|
2433
|
+
body.h = record;
|
|
2434
|
+
bodies.add(record);
|
|
2435
|
+
const inside = [];
|
|
2436
|
+
for (const scope of scopes.values()) if (within(scope, body)) inside.push(scope);
|
|
2437
|
+
for (const scope of inside) {
|
|
2438
|
+
const portal = comments.get(`P${scope.id}`);
|
|
2439
|
+
if (portal?.isConnected) record.ranges.push([portal, rangeEnd(portal)]);
|
|
2440
|
+
}
|
|
2441
|
+
for (const range of record.ranges) for (const node of between(range)) hideNode(node);
|
|
2442
|
+
for (const scope of inside) {
|
|
2443
|
+
for (const [name, cleanup] of scope.cleanups ?? []) {
|
|
2444
|
+
cleanup();
|
|
2445
|
+
(scope.w ??= /* @__PURE__ */ new Set()).add(name);
|
|
2446
|
+
}
|
|
2447
|
+
scope.cleanups?.clear();
|
|
2448
|
+
}
|
|
2449
|
+
},
|
|
2450
|
+
show(body) {
|
|
2451
|
+
const record = body.h;
|
|
2452
|
+
if (!record) return;
|
|
2453
|
+
body.h = void 0;
|
|
2454
|
+
bodies.delete(record);
|
|
2455
|
+
for (const range of record.ranges) if (range[0].isConnected) for (const node of between(range)) showNode(node);
|
|
2456
|
+
for (const scope of scopes.values()) if (scope.w && within(scope, body) && !hidden(scope)) {
|
|
2457
|
+
const ref = `${findRoot(scope).m}#`;
|
|
2458
|
+
for (const name of scope.w) call(ref + name, scope, []);
|
|
2459
|
+
scope.w = void 0;
|
|
2460
|
+
}
|
|
2461
|
+
},
|
|
2462
|
+
paused(scope, name) {
|
|
2463
|
+
if (bodies.size === 0 || !hidden(scope)) return false;
|
|
2464
|
+
(scope.w ??= /* @__PURE__ */ new Set()).add(name);
|
|
2465
|
+
return true;
|
|
2466
|
+
},
|
|
2467
|
+
added(added) {
|
|
2468
|
+
for (const node of added) for (const record of bodies) {
|
|
2469
|
+
for (const [start, end] of record.ranges) if (node.parentNode === start.parentNode && start.compareDocumentPosition(node) & 4 && node.compareDocumentPosition(end) & 4) hideNode(node);
|
|
2470
|
+
const data = node.nodeType === 8 ? node.data : "";
|
|
2471
|
+
const scope = /^P-?[\d.]+$/.test(data) ? scopes.get(parseId(data.slice(1))) : void 0;
|
|
2472
|
+
if (scope && within(scope, record.scope)) record.ranges.push([node, rangeEnd(node)]);
|
|
2473
|
+
}
|
|
2474
|
+
},
|
|
2475
|
+
styled(element) {
|
|
2476
|
+
const saved = nodes.get(element);
|
|
2477
|
+
const style = display(element);
|
|
2478
|
+
if (saved && style) {
|
|
2479
|
+
saved.v = style.getPropertyValue("display");
|
|
2480
|
+
saved.p = style.getPropertyPriority("display");
|
|
2481
|
+
saved.a = element.hasAttribute("style");
|
|
2482
|
+
style.setProperty("display", "none", "important");
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
};
|
|
2486
|
+
}
|
|
2487
|
+
//#endregion
|
|
2488
|
+
//#region src/runtime/client/templates.ts
|
|
2489
|
+
/** A template of the markup of a render, which `clone` parses once. */
|
|
2490
|
+
function template(html, count, foreign) {
|
|
2491
|
+
return {
|
|
2492
|
+
html,
|
|
2493
|
+
count,
|
|
2494
|
+
foreign
|
|
2495
|
+
};
|
|
2496
|
+
}
|
|
2497
|
+
/**
|
|
2498
|
+
* Parses a template, and finds the path to each element that it marks with `b-x`, as the parser
|
|
2499
|
+
* placed it: after the parser's own changes, such as a `<tbody>` that it adds.
|
|
2500
|
+
*/
|
|
2501
|
+
function parseTemplate(source) {
|
|
2502
|
+
const element = document.createElement("template");
|
|
2503
|
+
element.innerHTML = trustedHTML(source.foreign ? `<${source.foreign}>${source.html}</${source.foreign}>` : source.html);
|
|
2504
|
+
const root = element.content;
|
|
2505
|
+
if (source.foreign) root.replaceChildren(...root.firstChild.childNodes);
|
|
2506
|
+
const paths = Array.from({ length: source.count });
|
|
2507
|
+
function findPath(marked) {
|
|
2508
|
+
const path = [];
|
|
2509
|
+
for (let node = marked; node !== root; node = node.parentNode) {
|
|
2510
|
+
let index = 0;
|
|
2511
|
+
for (let before = node.previousSibling; before; before = before.previousSibling) index++;
|
|
2512
|
+
path.unshift(index);
|
|
2513
|
+
}
|
|
2514
|
+
return path;
|
|
2515
|
+
}
|
|
2516
|
+
for (const marked of root.querySelectorAll("[b-x]")) {
|
|
2517
|
+
paths[Number(marked.getAttribute("b-x"))] = findPath(marked);
|
|
2518
|
+
marked.removeAttribute("b-x");
|
|
2519
|
+
}
|
|
2520
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_COMMENT);
|
|
2521
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
2522
|
+
const mark = /^x(\d+)$/.exec(node.data);
|
|
2523
|
+
if (mark) paths[Number(mark[1])] = findPath(node);
|
|
2524
|
+
}
|
|
2525
|
+
options?.listen(root);
|
|
2526
|
+
source.t = element;
|
|
2527
|
+
return paths;
|
|
2528
|
+
}
|
|
2529
|
+
const FIRST_CHILD = -1;
|
|
2530
|
+
const PARENT = -2;
|
|
2531
|
+
const NEXT = -2;
|
|
2532
|
+
/**
|
|
2533
|
+
* The moves that reach the marked nodes of a template, in the order of the markup, from one to
|
|
2534
|
+
* the next: up to the ancestor that they share, along its children, and down. A clone makes
|
|
2535
|
+
* them once for its template, as Marko encodes a walk, so the clones of a render do not walk
|
|
2536
|
+
* from the top for each node or make an array of paths.
|
|
2537
|
+
*/
|
|
2538
|
+
function encodeWalk(paths) {
|
|
2539
|
+
const marked = [];
|
|
2540
|
+
for (const [mark, path] of paths.entries()) marked.push({
|
|
2541
|
+
path,
|
|
2542
|
+
mark
|
|
2543
|
+
});
|
|
2544
|
+
const order = marked.sort((a, b) => {
|
|
2545
|
+
for (let depth = 0; depth < Math.min(a.path.length, b.path.length); depth++) if (a.path[depth] !== b.path[depth]) return a.path[depth] - b.path[depth];
|
|
2546
|
+
return a.path.length - b.path.length;
|
|
2547
|
+
});
|
|
2548
|
+
const moves = [];
|
|
2549
|
+
function along(count) {
|
|
2550
|
+
if (count > 0) moves.push(NEXT - count);
|
|
2551
|
+
}
|
|
2552
|
+
let current = [];
|
|
2553
|
+
for (const { path, mark } of order) {
|
|
2554
|
+
let shared = 0;
|
|
2555
|
+
while (shared < current.length && current[shared] === path[shared]) shared++;
|
|
2556
|
+
let depth = shared;
|
|
2557
|
+
if (shared < current.length) {
|
|
2558
|
+
for (let up = current.length - 1; up > shared; up--) moves.push(PARENT);
|
|
2559
|
+
along(path[shared] - current[shared]);
|
|
2560
|
+
depth++;
|
|
2561
|
+
}
|
|
2562
|
+
for (; depth < path.length; depth++) {
|
|
2563
|
+
moves.push(FIRST_CHILD);
|
|
2564
|
+
along(path[depth]);
|
|
2565
|
+
}
|
|
2566
|
+
moves.push(mark);
|
|
2567
|
+
current = path;
|
|
2568
|
+
}
|
|
2569
|
+
return moves;
|
|
2570
|
+
}
|
|
2571
|
+
/**
|
|
2572
|
+
* Clones a template. Returns the nodes that the template marks, by their index, and the fragment
|
|
2573
|
+
* after them, in one array.
|
|
2574
|
+
*/
|
|
2575
|
+
function clone(source) {
|
|
2576
|
+
const walk = source.walk ??= encodeWalk(parseTemplate(source));
|
|
2577
|
+
const fragment = source.t.content.cloneNode(true);
|
|
2578
|
+
const cloned = new Array(source.count + 1);
|
|
2579
|
+
cloned[source.count] = fragment;
|
|
2580
|
+
let node = fragment;
|
|
2581
|
+
for (let index = 0; index < walk.length; index++) {
|
|
2582
|
+
const move = walk[index];
|
|
2583
|
+
if (move >= 0) cloned[move] = node;
|
|
2584
|
+
else if (move === FIRST_CHILD) node = node.firstChild;
|
|
2585
|
+
else if (move === PARENT) node = node.parentNode;
|
|
2586
|
+
else for (let step = NEXT - move; step > 0; step--) node = node.nextSibling;
|
|
2587
|
+
}
|
|
2588
|
+
return cloned;
|
|
2589
|
+
}
|
|
2590
|
+
/**
|
|
2591
|
+
* Keeps an element of cloned markup on its scope, where updates find it. An element at the top
|
|
2592
|
+
* of the clone, or one with handlers, gets its `b-b` too, which `list` and the handlers read to
|
|
2593
|
+
* find the scope.
|
|
2594
|
+
*/
|
|
2595
|
+
function keep(scope, anchor, element, named) {
|
|
2596
|
+
const found = scopes.get(scope);
|
|
2597
|
+
if (found) (found.n ??= [])[anchor] = element;
|
|
2598
|
+
if (named || element.parentNode?.nodeType === 11 || element.hasAttribute("b-on")) element.setAttribute("b-b", `${scope}:${anchor}`);
|
|
2599
|
+
}
|
|
2600
|
+
/**
|
|
2601
|
+
* Puts the value of a text insert of cloned markup in the place of its comment. A text value is
|
|
2602
|
+
* a text node, which the scope keeps, as Solid keeps it, so an update sets its data. Other
|
|
2603
|
+
* values render between the markers of an insert, as in HTML.
|
|
2604
|
+
*/
|
|
2605
|
+
function keepText(scope, anchor, place, value, at = scope) {
|
|
2606
|
+
const found = scopes.get(scope);
|
|
2607
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
2608
|
+
const node = document.createTextNode(String(value));
|
|
2609
|
+
place.replaceWith(node);
|
|
2610
|
+
if (found) (found.n ??= [])[anchor] = node;
|
|
2611
|
+
return;
|
|
2612
|
+
}
|
|
2613
|
+
keepMarkers(scope, anchor, place, value, at);
|
|
2614
|
+
}
|
|
2615
|
+
/** Puts a text insert at the end of an element of cloned markup, as `keepText` does. */
|
|
2616
|
+
function appendText(scope, anchor, parent, value, at = scope) {
|
|
2617
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
2618
|
+
const node = document.createTextNode(String(value));
|
|
2619
|
+
parent.append(node);
|
|
2620
|
+
const found = scopes.get(scope);
|
|
2621
|
+
if (found) (found.n ??= [])[anchor] = node;
|
|
2622
|
+
return;
|
|
2623
|
+
}
|
|
2624
|
+
const place = document.createComment("");
|
|
2625
|
+
parent.append(place);
|
|
2626
|
+
keepMarkers(scope, anchor, place, value, at);
|
|
2627
|
+
}
|
|
2628
|
+
/** Gives a value that is not text the markers of an insert, and renders it between them. */
|
|
2629
|
+
function keepMarkers(scope, anchor, place, value, at) {
|
|
2630
|
+
const found = scopes.get(scope);
|
|
2631
|
+
place.data = `t${scope}:${anchor}`;
|
|
2632
|
+
place.after(document.createComment("/t"));
|
|
2633
|
+
if (found) (found.n ??= [])[anchor] = place;
|
|
2634
|
+
if (value !== null && value !== void 0 && typeof value !== "boolean") place.after(insertNodes(value, at, place));
|
|
2635
|
+
}
|
|
2636
|
+
/** Names the start marker of a nested block of cloned markup, and keeps it on its scope. */
|
|
2637
|
+
function keepBlock(place, scope, data, anchor) {
|
|
2638
|
+
place.data = data;
|
|
2639
|
+
const found = scopes.get(scope);
|
|
2640
|
+
if (found) (found.n ??= [])[anchor] = place;
|
|
2641
|
+
return found;
|
|
2642
|
+
}
|
|
2643
|
+
/** Puts what renders gave into the range that a marker of cloned markup opens. */
|
|
2644
|
+
function fillRange(place, html, inserted) {
|
|
2645
|
+
if (html) {
|
|
2646
|
+
place.after(parseHTML(html, place));
|
|
2647
|
+
markCloned();
|
|
2648
|
+
} else if (inserted) place.after(inserted);
|
|
2649
|
+
}
|
|
2650
|
+
/** Names the start marker of a block whose markup is in the clone, and keeps it on its scope. */
|
|
2651
|
+
function markBlock(place, scope, anchor, suffix) {
|
|
2652
|
+
keepBlock(place, scope, `b${scope}:${anchor}${suffix}`, anchor);
|
|
2653
|
+
}
|
|
2654
|
+
/**
|
|
2655
|
+
* Fills a nested `#if` of cloned markup with the branch at `index`, or nothing for -1, before
|
|
2656
|
+
* the clone is in the page. `branch` updates it later.
|
|
2657
|
+
*/
|
|
2658
|
+
function fillBranch(place, scope, anchor, index, renders) {
|
|
2659
|
+
const found = keepBlock(place, scope, `b${scope}:${anchor}:${index}`, anchor);
|
|
2660
|
+
const render = renders[index];
|
|
2661
|
+
if (found && render) {
|
|
2662
|
+
const out = render(found);
|
|
2663
|
+
fillRange(place, typeof out === "string" ? out : "", typeof out === "string" ? void 0 : out);
|
|
2664
|
+
}
|
|
2665
|
+
}
|
|
2666
|
+
/** Fills a nested `#for` of cloned markup with its items, before the clone is in the page. */
|
|
2667
|
+
function fillList(place, scope, anchor, items, render) {
|
|
2668
|
+
const found = keepBlock(place, scope, `b${scope}:${anchor}`, anchor);
|
|
2669
|
+
if (!found) return;
|
|
2670
|
+
let html = "";
|
|
2671
|
+
let parts;
|
|
2672
|
+
for (let index = 0; index < items.length; index++) {
|
|
2673
|
+
const out = render(found, items[index], index);
|
|
2674
|
+
if (typeof out === "string") html += out;
|
|
2675
|
+
else (parts ??= document.createDocumentFragment()).append(out);
|
|
2676
|
+
}
|
|
2677
|
+
fillRange(place, html, parts);
|
|
2678
|
+
}
|
|
2679
|
+
/** Writes an attribute of an element of cloned markup, as `attribute` writes it in HTML. */
|
|
2680
|
+
function setAttr(element, name, value) {
|
|
2681
|
+
const written = attributeText(name, value);
|
|
2682
|
+
if (written !== void 0) element.setAttribute(name, written);
|
|
2683
|
+
}
|
|
2684
|
+
/** The nodes of a block that renders where it is, in HTML: a place, which the parse fills. */
|
|
2685
|
+
function nodes(fragment) {
|
|
2686
|
+
return holdPlace(fragment);
|
|
2687
|
+
}
|
|
2688
|
+
/** What a body gave, as nodes: its clone, or its HTML, which the insert of the clone indexes. */
|
|
2689
|
+
function bodyNodes(out) {
|
|
2690
|
+
if (typeof out !== "string") return out;
|
|
2691
|
+
markCloned();
|
|
2692
|
+
return parseHTML(out);
|
|
2693
|
+
}
|
|
2694
|
+
/** The markers of a block that renders where it is, around its nodes, with the start kept. */
|
|
2695
|
+
function blockNodes(scope, anchor, data, content) {
|
|
2696
|
+
if (scope === void 0 || anchor === void 0) return content;
|
|
2697
|
+
const start = document.createComment(data);
|
|
2698
|
+
keepBlock(start, scope, data, anchor);
|
|
2699
|
+
content.prepend(start);
|
|
2700
|
+
content.append(document.createComment("/b"));
|
|
2701
|
+
return content;
|
|
2702
|
+
}
|
|
2703
|
+
/**
|
|
2704
|
+
* Renders an `#if` or a `#switch` where it is: the branch at `index`, or nothing for -1, between
|
|
2705
|
+
* its markers when it has them, which `branch` updates later.
|
|
2706
|
+
*/
|
|
2707
|
+
function branchNodes(scope, anchor, index, bodies) {
|
|
2708
|
+
const body = bodies[index];
|
|
2709
|
+
const content = body ? bodyNodes(body()) : document.createDocumentFragment();
|
|
2710
|
+
return blockNodes(scope, anchor, `b${scope}:${anchor}:${index}`, content);
|
|
2711
|
+
}
|
|
2712
|
+
/**
|
|
2713
|
+
* Renders a `#for` or a `#repeat` where it is: its items, or its fallback for none, between its
|
|
2714
|
+
* markers when it has them, which `list` updates later.
|
|
2715
|
+
*/
|
|
2716
|
+
function listNodes(scope, anchor, items, body, fallback) {
|
|
2717
|
+
const content = document.createDocumentFragment();
|
|
2718
|
+
for (let index = 0; index < items.length; index++) content.append(bodyNodes(body(items[index], index)));
|
|
2719
|
+
if (items.length === 0 && fallback) content.append(bodyNodes(fallback()));
|
|
2720
|
+
return blockNodes(scope, anchor, `b${scope}:${anchor}`, content);
|
|
2721
|
+
}
|
|
2722
|
+
/** Writes attributes that code of a start tag gave as their text, as HTML has them. */
|
|
2723
|
+
function setAttrText(element, text) {
|
|
2724
|
+
if (text) {
|
|
2725
|
+
const foreign = findForeign(element);
|
|
2726
|
+
const parsed = parseHTML(foreign ? `<${foreign === "math" ? "mi" : "g"}${text}/>` : `<b${text}>`, void 0, foreign).firstChild;
|
|
2727
|
+
for (const attribute of Array.from(parsed.attributes)) {
|
|
2728
|
+
parsed.removeAttributeNode(attribute);
|
|
2729
|
+
element.setAttributeNode(attribute);
|
|
2730
|
+
}
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
/** Puts a value with no markers in the place of a comment of cloned markup, as `insert` does. */
|
|
2734
|
+
function putText(place, value, at) {
|
|
2735
|
+
place.replaceWith(typeof value === "string" || typeof value === "number" ? document.createTextNode(String(value)) : insertNodes(value, at, place));
|
|
2736
|
+
}
|
|
2737
|
+
/** Writes the spreads of an element of cloned markup, as `spread` writes them in HTML. */
|
|
2738
|
+
function setSpread(element, attributes, on, names) {
|
|
2739
|
+
setAttrText(element, spread(attributes, on, names));
|
|
2740
|
+
}
|
|
2741
|
+
/** Writes the `b-on` of an element of cloned markup with spreads, as `onAttribute` does. */
|
|
2742
|
+
function setOn(element, on, own) {
|
|
2743
|
+
const value = onValue(on, own);
|
|
2744
|
+
if (value) {
|
|
2745
|
+
element.setAttribute("b-on", value);
|
|
2746
|
+
options?.listen(element);
|
|
2747
|
+
}
|
|
2748
|
+
}
|
|
2749
|
+
/** Writes the content of an element of cloned markup, as `insert` writes it in HTML. */
|
|
2750
|
+
function setContent(element, value, at) {
|
|
2751
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
2752
|
+
element.textContent = String(value);
|
|
2753
|
+
return;
|
|
2754
|
+
}
|
|
2755
|
+
element.replaceChildren(insertNodes(value, at, element));
|
|
2756
|
+
}
|
|
2757
|
+
/**
|
|
2758
|
+
* The nodes of children, a template or raw HTML in cloned markup: the clone of children whose
|
|
2759
|
+
* markup clones, or the markup, which the insert of the clone indexes, as it would in HTML.
|
|
2760
|
+
*/
|
|
2761
|
+
function insertNodes(value, at, near) {
|
|
2762
|
+
const out = HAS_CHILDREN && value instanceof Children && !(value instanceof TemplateCall) ? value.render(resolveScope(at)) : insert(value, void 0, at);
|
|
2763
|
+
if (typeof out !== "string") return out;
|
|
2764
|
+
markCloned();
|
|
2765
|
+
return parseHTML(out, near);
|
|
2766
|
+
}
|
|
2767
|
+
//#endregion
|
|
2768
|
+
//#region src/runtime/client/components.ts
|
|
2769
|
+
/** Puts a child component that a clone renders in the place of its comment. */
|
|
2770
|
+
function place(at, out) {
|
|
2771
|
+
if (typeof out === "string") {
|
|
2772
|
+
at.replaceWith(parseHTML(out, at));
|
|
2773
|
+
markCloned();
|
|
2774
|
+
} else at.replaceWith(out);
|
|
2775
|
+
}
|
|
2776
|
+
/**
|
|
2777
|
+
* Renders a child component on the client. With `scope` and `anchor`,
|
|
2778
|
+
* records the child's scope so the parent can update its props.
|
|
2779
|
+
*/
|
|
2780
|
+
function component(Child, props, _ctx, scope, anchor) {
|
|
2781
|
+
const parent = resolveScope(scope);
|
|
2782
|
+
if (parent && anchor !== void 0) {
|
|
2783
|
+
parent.c ??= {};
|
|
2784
|
+
parent.c[anchor] = nextId;
|
|
2785
|
+
}
|
|
2786
|
+
if (parent) setOwner(parent);
|
|
2787
|
+
return holdPlace(Child(props));
|
|
2788
|
+
}
|
|
2789
|
+
/** Renders a child component for a clone: its markup as HTML, or the clone that it made. */
|
|
2790
|
+
function componentNodes(Child, props, _ctx, scope, anchor) {
|
|
2791
|
+
const parent = resolveScope(scope);
|
|
2792
|
+
if (parent && anchor !== void 0) {
|
|
2793
|
+
parent.c ??= {};
|
|
2794
|
+
parent.c[anchor] = nextId;
|
|
2795
|
+
}
|
|
2796
|
+
if (parent) setOwner(parent);
|
|
2797
|
+
return Child(props);
|
|
2798
|
+
}
|
|
2799
|
+
/** The attributes of a dynamic element, with one `b-on` for its own handlers and those of its props. */
|
|
2800
|
+
function startAttributes(attributes) {
|
|
2801
|
+
const on = [];
|
|
2802
|
+
return spread(attributes, on) + onAttribute(on, "");
|
|
2803
|
+
}
|
|
2804
|
+
/**
|
|
2805
|
+
* Renders a dynamic tag for a clone: the element, with its attributes and content, or what a
|
|
2806
|
+
* component renders.
|
|
2807
|
+
*/
|
|
2808
|
+
function dynamicNodes(tag, attributes, inner, _ctx, scope, ref, foreign) {
|
|
2809
|
+
if (typeof tag === "function") {
|
|
2810
|
+
const { "b-b": bound, "b-on": entries, ...props } = ref === void 0 ? attributes : {
|
|
2811
|
+
...attributes,
|
|
2812
|
+
ref
|
|
2813
|
+
};
|
|
2814
|
+
addEntryHandlers(props, entries, typeof scope === "object" ? scope.id : scope, (reference) => callable(reference, /* @__PURE__ */ new Set()));
|
|
2815
|
+
if (inner) props.children = inner;
|
|
2816
|
+
return componentNodes(valueRenders[tag.brumateId] ?? tag, props, void 0, scope, bound ? Number(ownValue("b-b", bound).split(":")[1]) : void 0);
|
|
2817
|
+
}
|
|
2818
|
+
const name = String(tag);
|
|
2819
|
+
const element = parseHTML(`<${name}${startAttributes(attributes)}>`, void 0, foreign).firstChild;
|
|
2820
|
+
const bound = element.getAttribute("b-b");
|
|
2821
|
+
if (bound) {
|
|
2822
|
+
const [id, anchor] = bound.split(":");
|
|
2823
|
+
const kept = scopes.get(Number(id));
|
|
2824
|
+
if (kept) (kept.n ??= [])[Number(anchor)] = element;
|
|
2825
|
+
}
|
|
2826
|
+
if (element.hasAttribute("b-on")) markCloned();
|
|
2827
|
+
if (inner && !VOID_ELEMENTS.has(name)) setContent(element, inner, scope);
|
|
2828
|
+
return element;
|
|
2829
|
+
}
|
|
2830
|
+
/** `dynamicNodes` for a dynamic tag inside SVG or MathML, `svg` or `math`. */
|
|
2831
|
+
function foreignNodes(foreign, tag, attributes, inner, ctx, scope, ref) {
|
|
2832
|
+
return dynamicNodes(tag, attributes, inner, ctx, scope, ref, foreign);
|
|
2833
|
+
}
|
|
2834
|
+
function dynamic(tag, attributes, inner, _ctx, scope, ref) {
|
|
2835
|
+
if (typeof tag === "function") return holdPlace(dynamicNodes(tag, attributes, inner, _ctx, scope, ref));
|
|
2836
|
+
const name = String(tag);
|
|
2837
|
+
const open = `<${name}${startAttributes(attributes)}>`;
|
|
2838
|
+
return VOID_ELEMENTS.has(name) ? open : `${open}${inner ? insert(inner, void 0, scope) : ""}</${name}>`;
|
|
2839
|
+
}
|
|
2840
|
+
function setSlots(id, values) {
|
|
2841
|
+
scopes.get(id).s = values;
|
|
2842
|
+
for (let slot = 0; slot < values.length; slot++) {
|
|
2843
|
+
const value = values[slot];
|
|
2844
|
+
if (value instanceof Promise && scopes.get(id).props?.includes(value) && values.indexOf(value) === slot) {
|
|
2845
|
+
(scopes.get(id).plain ??= /* @__PURE__ */ new Set()).add(slot);
|
|
2846
|
+
value.catch(noop);
|
|
2847
|
+
} else if (isThenable(value)) {
|
|
2848
|
+
const key = `${id}:${slot}`;
|
|
2849
|
+
renderedLoads.add(key);
|
|
2850
|
+
loadingValues.add(value);
|
|
2851
|
+
Promise.resolve(value).then((result) => {
|
|
2852
|
+
const scope = scopes.get(id);
|
|
2853
|
+
if (scope?.s[slot] === value && !loads.has(key)) {
|
|
2854
|
+
renderedLoads.delete(key);
|
|
2855
|
+
set(scope, slot, result);
|
|
2856
|
+
}
|
|
2857
|
+
}, noop);
|
|
2858
|
+
}
|
|
2859
|
+
}
|
|
2860
|
+
scopes.get(id).props = void 0;
|
|
2861
|
+
}
|
|
2862
|
+
function setItem(id, value) {
|
|
2863
|
+
scopes.get(id).i = value;
|
|
2864
|
+
}
|
|
2865
|
+
function elementAt(scope, anchor) {
|
|
2866
|
+
return new ElementReference(scope, anchor);
|
|
2867
|
+
}
|
|
2868
|
+
//#endregion
|
|
2869
|
+
//#region src/runtime/client/props.ts
|
|
2870
|
+
/** Writes a prop of a child component. */
|
|
2871
|
+
const spreadKeys = /* @__PURE__ */ new WeakMap();
|
|
2872
|
+
/**
|
|
2873
|
+
* Updates the props of a child component from a spread. A key that the
|
|
2874
|
+
* spread passed before and no longer has becomes undefined. `after` names
|
|
2875
|
+
* the props after the spread, which win over it.
|
|
2876
|
+
*/
|
|
2877
|
+
function spreadProps(scope, anchor, value, after) {
|
|
2878
|
+
const byAnchor = spreadKeys.get(scope) ?? /* @__PURE__ */ new Map();
|
|
2879
|
+
spreadKeys.set(scope, byAnchor);
|
|
2880
|
+
const before = byAnchor.get(anchor) ?? /* @__PURE__ */ new Set();
|
|
2881
|
+
const keys = /* @__PURE__ */ new Set();
|
|
2882
|
+
for (const key of Object.keys(value ?? {})) if (!after.includes(key) && !key.startsWith("b-")) keys.add(key);
|
|
2883
|
+
byAnchor.set(anchor, keys);
|
|
2884
|
+
for (const key of keys) prop(scope, anchor, key, value[key]);
|
|
2885
|
+
for (const key of before) if (!keys.has(key)) prop(scope, anchor, key, void 0);
|
|
2886
|
+
}
|
|
2887
|
+
function prop(scope, anchor, name, value) {
|
|
2888
|
+
const bound = value;
|
|
2889
|
+
if (HAS_BINDINGS && isBinding(value)) value = value.value;
|
|
2890
|
+
const id = scope.c?.[anchor];
|
|
2891
|
+
const child = id === void 0 ? void 0 : scopes.get(id);
|
|
2892
|
+
if (!child?.m) return;
|
|
2893
|
+
const apply = (module) => {
|
|
2894
|
+
const slot = module.props[name];
|
|
2895
|
+
if (slot !== void 0) {
|
|
2896
|
+
if (value instanceof Promise) (child.plain ??= /* @__PURE__ */ new Set()).add(slot);
|
|
2897
|
+
else child.plain?.delete(slot);
|
|
2898
|
+
writeSlot(child, slot, value);
|
|
2899
|
+
return;
|
|
2900
|
+
}
|
|
2901
|
+
const all = module.props["*"];
|
|
2902
|
+
if (all !== void 0) set(child, all, {
|
|
2903
|
+
...get(child, all),
|
|
2904
|
+
[name]: value
|
|
2905
|
+
});
|
|
2906
|
+
const rest = module.props["..."];
|
|
2907
|
+
if (rest !== void 0) {
|
|
2908
|
+
const next = { ...get(child, rest) };
|
|
2909
|
+
if (value === void 0) delete next[name];
|
|
2910
|
+
else next[name] = bound;
|
|
2911
|
+
set(child, rest, next);
|
|
2912
|
+
}
|
|
2913
|
+
};
|
|
2914
|
+
const module = modules.get(child.m);
|
|
2915
|
+
if (module) apply(module);
|
|
2916
|
+
else loadModule(child.m).then(apply);
|
|
2917
|
+
}
|
|
2918
|
+
//#endregion
|
|
2919
|
+
//#region src/runtime/client/inserts.ts
|
|
2920
|
+
/**
|
|
2921
|
+
* Renders a `#portal` body into its target, between markers with its scope
|
|
2922
|
+
* ID. `kinds` names the body, so a new target first removes it from the old one.
|
|
2923
|
+
*/
|
|
2924
|
+
function portalTo(scope, target, render, kinds = [], holderKind, holderAnchor) {
|
|
2925
|
+
disposeKinds(scope, kinds);
|
|
2926
|
+
const element = typeof target === "string" ? document.querySelector(target) : target;
|
|
2927
|
+
if (!(element instanceof Element)) return;
|
|
2928
|
+
const id = nextId;
|
|
2929
|
+
const body = inPlace(scope, render, holderKind, holderAnchor);
|
|
2930
|
+
insertHTML({ parent: element }, `<!--P${id}-->${body(scope)}<!--/P-->`);
|
|
2931
|
+
}
|
|
2932
|
+
/** Writes an inserted value between `<!--t…-->` and `<!--/t-->`. */
|
|
2933
|
+
function text(scope, anchor, value) {
|
|
2934
|
+
let kept = scope.n?.[anchor];
|
|
2935
|
+
if (kept?.nodeType === 3 && kept.isConnected) {
|
|
2936
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
2937
|
+
const data = String(value);
|
|
2938
|
+
if (kept.data !== data) kept.data = data;
|
|
2939
|
+
return;
|
|
2940
|
+
}
|
|
2941
|
+
const start = document.createComment(`t${scope.id}:${anchor}`);
|
|
2942
|
+
kept.before(start);
|
|
2943
|
+
kept.after(document.createComment("/t"));
|
|
2944
|
+
scope.n[anchor] = kept = start;
|
|
2945
|
+
}
|
|
2946
|
+
const start = kept?.isConnected ? kept : findComment(`t${scope.id}:${anchor}`);
|
|
2947
|
+
if (start) write(start.parentNode, start.nextSibling, rangeEnd(start), value, scope, start, anchor);
|
|
2948
|
+
}
|
|
2949
|
+
/** Writes an inserted value that is the whole content of the element with the anchor. */
|
|
2950
|
+
function content(scope, anchor, value) {
|
|
2951
|
+
const element = findElement(scope.id, anchor);
|
|
2952
|
+
if (element) write(element, element.firstChild, null, value, scope, element, anchor);
|
|
2953
|
+
}
|
|
2954
|
+
/**
|
|
2955
|
+
* Writes the text of an element whose content is text, such as a `<textarea>`
|
|
2956
|
+
* or a `<title>`. A `<textarea>` gets the value too, as `value` does on an
|
|
2957
|
+
* `<input>`, so a change replaces what the user typed.
|
|
2958
|
+
*/
|
|
2959
|
+
function writeText(scope, anchor, value) {
|
|
2960
|
+
const element = findElement(scope.id, anchor);
|
|
2961
|
+
if (element) {
|
|
2962
|
+
const written = textValue(value);
|
|
2963
|
+
if (element.textContent !== written) element.textContent = written;
|
|
2964
|
+
if (element instanceof HTMLTextAreaElement && element.value !== written) element.value = written;
|
|
2965
|
+
}
|
|
2966
|
+
}
|
|
2967
|
+
/**
|
|
2968
|
+
* Replaces the nodes of `parent` from `first` up to `end`, or to its last
|
|
2969
|
+
* child, with a value. A text node that stays a text node keeps its node.
|
|
2970
|
+
*/
|
|
2971
|
+
function write(parent, first, end, value, at, key, anchor) {
|
|
2972
|
+
const firstWrite = key !== void 0 && !writtenInserts.has(key);
|
|
2973
|
+
if (HAS_CHILDREN && refreshing && firstWrite && value instanceof RenderedChildren && value.at !== void 0 && value.at === at?.id && value.anchor === anchor) return;
|
|
2974
|
+
if (key) writtenInserts.add(key);
|
|
2975
|
+
if (HAS_TEMPLATES && key && value instanceof TemplateCall) {
|
|
2976
|
+
const known = templateInserts.get(key) ?? (firstWrite ? renderedTemplate(value, at, loadedFunction(`${value.ref}k`)) : void 0);
|
|
2977
|
+
const getSlots = loadedFunction(`${value.ref}a`);
|
|
2978
|
+
if (known && getSlots && known.ref === value.ref && scopes.has(known.scope.id)) {
|
|
2979
|
+
templateInserts.set(key, known);
|
|
2980
|
+
for (const [slot, next] of getSlots(...value.args)) set(known.scope, slot, next);
|
|
2981
|
+
return;
|
|
2982
|
+
}
|
|
2983
|
+
}
|
|
2984
|
+
if ((typeof value === "string" || typeof value === "number") && first?.nodeType === 3 && first.nextSibling === end) {
|
|
2985
|
+
const saved = hiding?.nodes.get(first);
|
|
2986
|
+
const data = String(value);
|
|
2987
|
+
if (saved) saved.v = data;
|
|
2988
|
+
else if (first.data !== data) first.data = data;
|
|
2989
|
+
return;
|
|
2990
|
+
}
|
|
2991
|
+
while (first && first !== end) {
|
|
2992
|
+
const next = first.nextSibling;
|
|
2993
|
+
first.remove();
|
|
2994
|
+
first = next;
|
|
2995
|
+
}
|
|
2996
|
+
const made = nextId;
|
|
2997
|
+
insertHTML(end ?? { parent }, (HAS_CHILDREN || HAS_TEMPLATES) && value instanceof Children ? value.render(at) : insertValue(value));
|
|
2998
|
+
if (HAS_TEMPLATES && key) {
|
|
2999
|
+
const scope = value instanceof TemplateCall ? scopes.get(made) : void 0;
|
|
3000
|
+
if (scope) templateInserts.set(key, {
|
|
3001
|
+
ref: value.ref,
|
|
3002
|
+
scope
|
|
3003
|
+
});
|
|
3004
|
+
else templateInserts.delete(key);
|
|
3005
|
+
}
|
|
3006
|
+
}
|
|
3007
|
+
/** The scope of the template that each insert shows, by the node that marks the insert. */
|
|
3008
|
+
const templateInserts = /* @__PURE__ */ new WeakMap();
|
|
3009
|
+
/** The inserts that the browser wrote, which no longer show what the server rendered. */
|
|
3010
|
+
const writtenInserts = /* @__PURE__ */ new WeakSet();
|
|
3011
|
+
/**
|
|
3012
|
+
* The scope of a template that the server rendered at an insert: the scope that the template's
|
|
3013
|
+
* component made, and that finds its contexts from the insert.
|
|
3014
|
+
*/
|
|
3015
|
+
function renderedTemplate(call, at, k) {
|
|
3016
|
+
if (k === void 0) return;
|
|
3017
|
+
let found;
|
|
3018
|
+
for (const scope of scopes.values()) if (call.owner && scope.p === call.owner && scope.t === at && scope.k === k) {
|
|
3019
|
+
if (found) return;
|
|
3020
|
+
found = scope;
|
|
3021
|
+
}
|
|
3022
|
+
return found && {
|
|
3023
|
+
ref: call.ref,
|
|
3024
|
+
scope: found
|
|
3025
|
+
};
|
|
3026
|
+
}
|
|
3027
|
+
/** The `value` and `checked` that updates last wrote to each field. */
|
|
3028
|
+
const fieldValues = /* @__PURE__ */ new WeakMap();
|
|
3029
|
+
function setAttribute(element, name, value, spread = false) {
|
|
3030
|
+
if (HAS_BINDINGS && isBinding(value)) value = value.value;
|
|
3031
|
+
if (name === "value" || name === "checked") {
|
|
3032
|
+
const field = element;
|
|
3033
|
+
const next = name === "checked" ? Boolean(value) : String(value ?? "");
|
|
3034
|
+
const written = fieldValues.get(element) ?? /* @__PURE__ */ new Map();
|
|
3035
|
+
fieldValues.set(element, written);
|
|
3036
|
+
let rendered;
|
|
3037
|
+
if (refreshing || spread) rendered = name === "checked" ? field.defaultChecked : field.defaultValue;
|
|
3038
|
+
const before = written.has(name) ? written.get(name) : rendered ?? field[name];
|
|
3039
|
+
written.set(name, next);
|
|
3040
|
+
if (!Object.is(before, next) && field[name] !== next) field[name] = next;
|
|
3041
|
+
}
|
|
3042
|
+
let written = attributeText(name, value);
|
|
3043
|
+
if (name === "style" && element.hasAttribute("b-v")) written = withVariables(element, written);
|
|
3044
|
+
if (element.getAttribute(name) === (written ?? null)) return;
|
|
3045
|
+
if (written === void 0) element.removeAttribute(name);
|
|
3046
|
+
else element.setAttribute(name, written);
|
|
3047
|
+
if (name === "style") hiding?.styled(element);
|
|
3048
|
+
}
|
|
3049
|
+
/**
|
|
3050
|
+
* A new style for a top-level element of a component, with the style variables of the component
|
|
3051
|
+
* that the element has now, so a style that a spread passes on does not remove them.
|
|
3052
|
+
*/
|
|
3053
|
+
function withVariables(element, style) {
|
|
3054
|
+
const variables = [];
|
|
3055
|
+
for (const property of Array.from(element.style)) if (/^--s-[\w-]+-\d+$/.test(property)) variables.push(`${property}:${element.style.getPropertyValue(property)}`);
|
|
3056
|
+
if (variables.length === 0) return style;
|
|
3057
|
+
return style ? `${style};${variables.join(";")}` : variables.join(";");
|
|
3058
|
+
}
|
|
3059
|
+
function attr(scope, anchor, name, value) {
|
|
3060
|
+
if (scope.c?.[anchor] !== void 0) {
|
|
3061
|
+
prop(scope, anchor, name, value);
|
|
3062
|
+
return;
|
|
3063
|
+
}
|
|
3064
|
+
const element = findElement(scope.id, anchor);
|
|
3065
|
+
if (element) setAttribute(element, name, value);
|
|
3066
|
+
}
|
|
3067
|
+
/** The attributes that the last update of a spread wrote, so the next one removes those it drops. */
|
|
3068
|
+
const spreadNames = /* @__PURE__ */ new WeakMap();
|
|
3069
|
+
/** The setters that were given each element, so an update of a spread does not call them again. */
|
|
3070
|
+
const given = /* @__PURE__ */ new WeakMap();
|
|
3071
|
+
/**
|
|
3072
|
+
* Gives the element of a spread to the signal that a `ref` in its attributes is bound to, or to
|
|
3073
|
+
* the setter of the binding.
|
|
3074
|
+
*/
|
|
3075
|
+
function forwardRef(element, values) {
|
|
3076
|
+
const bound = values.ref;
|
|
3077
|
+
if (HAS_BINDINGS && element && isBinding(bound)) {
|
|
3078
|
+
const { target } = bound;
|
|
3079
|
+
if (isSlotReference(target)) {
|
|
3080
|
+
const owner = scopes.get(target.scope);
|
|
3081
|
+
if (owner) set(owner, target.slot, element);
|
|
3082
|
+
} else {
|
|
3083
|
+
const key = target instanceof FunctionReference ? `${target.scope}#${target.ref}` : target;
|
|
3084
|
+
const seen = given.get(element) ?? /* @__PURE__ */ new Set();
|
|
3085
|
+
if (!seen.has(key)) {
|
|
3086
|
+
given.set(element, seen.add(key));
|
|
3087
|
+
send(target, element);
|
|
3088
|
+
}
|
|
3089
|
+
}
|
|
3090
|
+
}
|
|
3091
|
+
}
|
|
3092
|
+
/** The element of a spread in markup that the browser renders goes to the `ref` of its attributes. */
|
|
3093
|
+
function restRef(values, scope, anchor, tag) {
|
|
3094
|
+
if (typeof tag !== "function") afterInsert.push(() => forwardRef(findElement(scope, anchor), values));
|
|
3095
|
+
return values;
|
|
3096
|
+
}
|
|
3097
|
+
function attrs(scope, anchor, values, own = []) {
|
|
3098
|
+
if (scope.c?.[anchor] !== void 0) {
|
|
3099
|
+
spreadProps(scope, anchor, values, []);
|
|
3100
|
+
return;
|
|
3101
|
+
}
|
|
3102
|
+
const element = findElement(scope.id, anchor);
|
|
3103
|
+
if (element) {
|
|
3104
|
+
if (!spreadNames.has(element)) {
|
|
3105
|
+
const written = element.getAttribute("b-s");
|
|
3106
|
+
const known = /* @__PURE__ */ new Set();
|
|
3107
|
+
for (const name of written === null ? element.getAttributeNames() : written.split(" ")) if (name !== "" && !own.includes(name) && !name.startsWith("b-")) known.add(name);
|
|
3108
|
+
spreadNames.set(element, known);
|
|
3109
|
+
}
|
|
3110
|
+
const names = /* @__PURE__ */ new Set();
|
|
3111
|
+
for (const [name, value] of Object.entries(values)) if (name !== "children" && !/^on[A-Z]/.test(name) && !name.startsWith("b-") && !(name === "ref" && isBinding(value))) {
|
|
3112
|
+
names.add(name);
|
|
3113
|
+
setAttribute(element, name, value, true);
|
|
3114
|
+
}
|
|
3115
|
+
for (const name of spreadNames.get(element) ?? []) if (!names.has(name)) setAttribute(element, name, void 0);
|
|
3116
|
+
spreadNames.set(element, names);
|
|
3117
|
+
updateSpreadEntries(element, values);
|
|
3118
|
+
forwardRef(element, values);
|
|
3119
|
+
}
|
|
3120
|
+
}
|
|
3121
|
+
/**
|
|
3122
|
+
* Writes the handlers and bindings of a spread into `b-on` again, as they are now. The entries of
|
|
3123
|
+
* a spread name their scope after `@`, and the element's own entries do not, so those stay.
|
|
3124
|
+
*/
|
|
3125
|
+
function updateSpreadEntries(element, values) {
|
|
3126
|
+
const before = element.getAttribute("b-on") ?? "";
|
|
3127
|
+
const kept = [];
|
|
3128
|
+
for (const entry of before.split(" ")) if (entry && !entry.includes("@")) kept.push(entry);
|
|
3129
|
+
const own = kept.join(" ");
|
|
3130
|
+
const after = onValue(spreadEntries(values), own);
|
|
3131
|
+
if (after === before) return;
|
|
3132
|
+
if (after) element.setAttribute("b-on", after);
|
|
3133
|
+
else element.removeAttribute("b-on");
|
|
3134
|
+
options.listen(element);
|
|
3135
|
+
}
|
|
3136
|
+
//#endregion
|
|
3137
|
+
//#region src/runtime/client/lists.ts
|
|
3138
|
+
const ITEM_MARKER = /^i(-?[\d.]+)$/;
|
|
3139
|
+
/** The items of a `#for` list: an array as it is, since `list` only reads it, or any iterable as an array. */
|
|
3140
|
+
function toList(value) {
|
|
3141
|
+
return Array.isArray(value) ? value : Array.from(value);
|
|
3142
|
+
}
|
|
3143
|
+
/** Updates the items of `#for` or `#repeat`, keeping the elements of items whose keys stay. */
|
|
3144
|
+
function list(scope, anchor, items, key, given, assign, givenFallback, fallbackKinds, holderKind, holderAnchor, park) {
|
|
3145
|
+
const start = blockStart(scope, anchor);
|
|
3146
|
+
if (!start) return;
|
|
3147
|
+
const render = inPlace(scope, given, holderKind, holderAnchor);
|
|
3148
|
+
const fallback = inPlace(scope, givenFallback, holderKind, holderAnchor);
|
|
3149
|
+
const end = rangeEnd(start);
|
|
3150
|
+
const previous = /* @__PURE__ */ new Map();
|
|
3151
|
+
let others;
|
|
3152
|
+
let position = 0;
|
|
3153
|
+
for (let node = start.nextSibling; node && node !== end;) {
|
|
3154
|
+
let data;
|
|
3155
|
+
let id;
|
|
3156
|
+
if (node.nodeType === 8) {
|
|
3157
|
+
data = node.data;
|
|
3158
|
+
id = ITEM_MARKER.exec(data)?.[1];
|
|
3159
|
+
} else if (node.nodeType === 1) {
|
|
3160
|
+
const bound = node.getAttribute("b-b");
|
|
3161
|
+
id = bound === null ? void 0 : bound.slice(0, bound.indexOf(":"));
|
|
3162
|
+
}
|
|
3163
|
+
const item = id === void 0 ? void 0 : scopes.get(parseId(id));
|
|
3164
|
+
let last = node;
|
|
3165
|
+
if (item && (data !== void 0 || item.p === scope && !fallbackKinds.includes(item.k))) {
|
|
3166
|
+
if (data !== void 0) last = rangeEnd(node);
|
|
3167
|
+
previous.set(key(item.i, position + (park?.was ?? 0)), {
|
|
3168
|
+
scope: item,
|
|
3169
|
+
first: node,
|
|
3170
|
+
last,
|
|
3171
|
+
position: position++,
|
|
3172
|
+
kept: false,
|
|
3173
|
+
still: false
|
|
3174
|
+
});
|
|
3175
|
+
} else (others ??= []).push(node);
|
|
3176
|
+
node = last.nextSibling;
|
|
3177
|
+
}
|
|
3178
|
+
const count = items.length;
|
|
3179
|
+
function leave(id, item) {
|
|
3180
|
+
if (park?.stays(id)) {
|
|
3181
|
+
const nodes = document.createDocumentFragment();
|
|
3182
|
+
for (let node = item.first; node;) {
|
|
3183
|
+
const next = node === item.last ? null : node.nextSibling;
|
|
3184
|
+
nodes.append(node);
|
|
3185
|
+
node = next;
|
|
3186
|
+
}
|
|
3187
|
+
park.items.set(id, {
|
|
3188
|
+
scope: item.scope,
|
|
3189
|
+
nodes
|
|
3190
|
+
});
|
|
3191
|
+
return;
|
|
3192
|
+
}
|
|
3193
|
+
destroy(item.scope);
|
|
3194
|
+
for (let node = item.first; node;) {
|
|
3195
|
+
const next = node === item.last ? null : node.nextSibling;
|
|
3196
|
+
node.parentNode?.removeChild(node);
|
|
3197
|
+
node = next;
|
|
3198
|
+
}
|
|
3199
|
+
}
|
|
3200
|
+
if (count === 0 && !(park && park.total > 0)) {
|
|
3201
|
+
if (previous.size === 0 && others) return;
|
|
3202
|
+
for (const [id, item] of previous) leave(id, item);
|
|
3203
|
+
clear(start, end);
|
|
3204
|
+
if (fallback) insertHTML(end, fallback(scope));
|
|
3205
|
+
return;
|
|
3206
|
+
}
|
|
3207
|
+
disposeKinds(scope, fallbackKinds);
|
|
3208
|
+
if (others) for (const node of others) node.parentNode?.removeChild(node);
|
|
3209
|
+
const parent = end.parentNode;
|
|
3210
|
+
const keys = Array.from({ length: count });
|
|
3211
|
+
const kept = [];
|
|
3212
|
+
for (let index = 0; index < count; index++) {
|
|
3213
|
+
const id = keys[index] = key(items[index], index + (park?.from ?? 0));
|
|
3214
|
+
const item = previous.get(id);
|
|
3215
|
+
if (item && !item.kept) {
|
|
3216
|
+
item.kept = true;
|
|
3217
|
+
kept.push(item);
|
|
3218
|
+
}
|
|
3219
|
+
}
|
|
3220
|
+
markInPlace(kept);
|
|
3221
|
+
if (kept.length === 0 && previous.size > 0 && !park) {
|
|
3222
|
+
for (const item of previous.values()) destroy(item.scope);
|
|
3223
|
+
previous.clear();
|
|
3224
|
+
clear(start, end);
|
|
3225
|
+
}
|
|
3226
|
+
let cursor = start.nextSibling;
|
|
3227
|
+
let added = "";
|
|
3228
|
+
let addedNodes;
|
|
3229
|
+
function flushAdded() {
|
|
3230
|
+
if (added) {
|
|
3231
|
+
insertHTML(cursor, added);
|
|
3232
|
+
added = "";
|
|
3233
|
+
} else if (addedNodes) insertHTML(cursor, addedNodes);
|
|
3234
|
+
addedNodes = void 0;
|
|
3235
|
+
}
|
|
3236
|
+
for (let index = 0; index < count; index++) {
|
|
3237
|
+
const id = keys[index];
|
|
3238
|
+
const item = previous.get(id);
|
|
3239
|
+
const parked = item ? void 0 : park?.items.get(id);
|
|
3240
|
+
if (parked) {
|
|
3241
|
+
flushAdded();
|
|
3242
|
+
park.items.delete(id);
|
|
3243
|
+
parent.insertBefore(parked.nodes, cursor);
|
|
3244
|
+
assign(parked.scope, items[index], index);
|
|
3245
|
+
(park.back ??= []).push(parked.scope);
|
|
3246
|
+
continue;
|
|
3247
|
+
}
|
|
3248
|
+
if (!item) {
|
|
3249
|
+
const out = render(scope, items[index], index);
|
|
3250
|
+
if (typeof out === "string") added += out;
|
|
3251
|
+
else (addedNodes ??= []).push(out);
|
|
3252
|
+
continue;
|
|
3253
|
+
}
|
|
3254
|
+
flushAdded();
|
|
3255
|
+
previous.delete(id);
|
|
3256
|
+
assign(item.scope, items[index], index);
|
|
3257
|
+
if (item.still) cursor = item.last.nextSibling;
|
|
3258
|
+
else for (let node = item.first;;) {
|
|
3259
|
+
const next = node.nextSibling;
|
|
3260
|
+
parent.insertBefore(node, cursor);
|
|
3261
|
+
if (node === item.last) break;
|
|
3262
|
+
node = next;
|
|
3263
|
+
}
|
|
3264
|
+
}
|
|
3265
|
+
flushAdded();
|
|
3266
|
+
for (const [id, item] of previous) leave(id, item);
|
|
3267
|
+
if (park?.back) {
|
|
3268
|
+
const back = park.back.splice(0);
|
|
3269
|
+
refresh(back);
|
|
3270
|
+
for (const item of back) redraw(item);
|
|
3271
|
+
}
|
|
3272
|
+
}
|
|
3273
|
+
/** Draws the windowed lists of a scope and of the scopes inside it. */
|
|
3274
|
+
function redraw(scope) {
|
|
3275
|
+
for (const state of windows.get(scope)?.values() ?? []) state.draw?.();
|
|
3276
|
+
for (const child of scope.children ?? []) redraw(child);
|
|
3277
|
+
}
|
|
3278
|
+
const windows = /* @__PURE__ */ new WeakMap();
|
|
3279
|
+
/**
|
|
3280
|
+
* The element that scrolls a windowed list, or none for the page. An element scrolls the list
|
|
3281
|
+
* when it can scroll vertically: its overflow allows it, and its content is taller than it.
|
|
3282
|
+
* A wrapper without a height, such as one that scrolls sideways, grows with the list and has
|
|
3283
|
+
* nothing to scroll, so the list looks further up. The server renders enough items to fill
|
|
3284
|
+
* the box, so its content is taller from the first draw on.
|
|
3285
|
+
*/
|
|
3286
|
+
function findScroller(node) {
|
|
3287
|
+
for (let element = node.parentElement; element; element = element.parentElement) {
|
|
3288
|
+
const style = getComputedStyle(element);
|
|
3289
|
+
if (/^(auto|scroll|overlay)$/.test(style.overflowY || style.overflow) && element.scrollHeight > element.clientHeight) return element;
|
|
3290
|
+
}
|
|
3291
|
+
}
|
|
3292
|
+
/**
|
|
3293
|
+
* Renders the items of a windowed list that are in view, from `#for … window(height, count)`,
|
|
3294
|
+
* with `list`, between the spacers that the server wrote before and after the block. Items that
|
|
3295
|
+
* leave the view keep their scopes and nodes while they are in the list.
|
|
3296
|
+
*/
|
|
3297
|
+
function windowList(scope, anchor, items, key, render, assign, fallback, fallbackKinds, height, holderKind, holderAnchor) {
|
|
3298
|
+
const byAnchor = windows.get(scope) ?? /* @__PURE__ */ new Map();
|
|
3299
|
+
windows.set(scope, byAnchor);
|
|
3300
|
+
let state = byAnchor.get(anchor);
|
|
3301
|
+
if (!state) {
|
|
3302
|
+
state = {
|
|
3303
|
+
parked: /* @__PURE__ */ new Map(),
|
|
3304
|
+
from: 0
|
|
3305
|
+
};
|
|
3306
|
+
byAnchor.set(anchor, state);
|
|
3307
|
+
}
|
|
3308
|
+
const window = state;
|
|
3309
|
+
window.draw = () => {
|
|
3310
|
+
const start = blockStart(scope, anchor);
|
|
3311
|
+
if (!start) return;
|
|
3312
|
+
const end = rangeEnd(start);
|
|
3313
|
+
const before = start.previousSibling;
|
|
3314
|
+
const after = end.nextSibling;
|
|
3315
|
+
if (!before?.hasAttribute?.("data-window") || !after?.hasAttribute?.("data-window")) return;
|
|
3316
|
+
const size = window.measured ?? height;
|
|
3317
|
+
const top = before.getBoundingClientRect().top;
|
|
3318
|
+
const scroller = findScroller(start);
|
|
3319
|
+
const view = scroller ? scroller.getBoundingClientRect() : {
|
|
3320
|
+
top: 0,
|
|
3321
|
+
bottom: document.documentElement.clientHeight
|
|
3322
|
+
};
|
|
3323
|
+
const extra = 3;
|
|
3324
|
+
const count = items.length;
|
|
3325
|
+
const from = Math.min(count, Math.max(0, Math.floor((view.top - top) / size) - extra));
|
|
3326
|
+
const to = Math.min(count, Math.max(from, Math.ceil((view.bottom - top) / size) + extra));
|
|
3327
|
+
const keys = /* @__PURE__ */ new Set();
|
|
3328
|
+
for (const [index, item] of items.entries()) keys.add(key(item, index));
|
|
3329
|
+
const was = window.from;
|
|
3330
|
+
window.from = from;
|
|
3331
|
+
list(scope, anchor, items.slice(from, to), key, (parent, item, index) => render(parent, item, index === void 0 ? index : index + from), (item, value, index) => assign(item, value, index + from), fallback, fallbackKinds, holderKind, holderAnchor, {
|
|
3332
|
+
items: window.parked,
|
|
3333
|
+
stays: (id) => keys.has(id),
|
|
3334
|
+
was,
|
|
3335
|
+
from,
|
|
3336
|
+
total: count
|
|
3337
|
+
});
|
|
3338
|
+
for (const [id, parked] of window.parked) if (!keys.has(id)) {
|
|
3339
|
+
window.parked.delete(id);
|
|
3340
|
+
destroy(parked.scope);
|
|
3341
|
+
}
|
|
3342
|
+
before.style.height = `${from * size}px`;
|
|
3343
|
+
after.style.height = `${(count - to) * size}px`;
|
|
3344
|
+
if (to > from) {
|
|
3345
|
+
const rendered = after.getBoundingClientRect().top - before.getBoundingClientRect().bottom;
|
|
3346
|
+
if (rendered > 0) window.measured = rendered / (to - from);
|
|
3347
|
+
}
|
|
3348
|
+
};
|
|
3349
|
+
window.draw();
|
|
3350
|
+
}
|
|
3351
|
+
/**
|
|
3352
|
+
* Starts the window of a windowed list that the server rendered: it draws now, and again as
|
|
3353
|
+
* the page or the element that scrolls the list scrolls or resizes.
|
|
3354
|
+
*/
|
|
3355
|
+
function windowStart(scope, anchor, draw) {
|
|
3356
|
+
draw();
|
|
3357
|
+
const window = windows.get(scope)?.get(anchor);
|
|
3358
|
+
const start = blockStart(scope, anchor);
|
|
3359
|
+
if (!window || !start) return;
|
|
3360
|
+
const scroller = findScroller(start);
|
|
3361
|
+
const target = scroller ?? globalThis;
|
|
3362
|
+
let observer;
|
|
3363
|
+
const schedule = () => {
|
|
3364
|
+
if (!scopes.has(scope.id)) {
|
|
3365
|
+
target.removeEventListener("scroll", schedule);
|
|
3366
|
+
globalThis.removeEventListener("resize", schedule);
|
|
3367
|
+
observer?.disconnect();
|
|
3368
|
+
return;
|
|
3369
|
+
}
|
|
3370
|
+
if (!start.isConnected) return;
|
|
3371
|
+
if (window.frame === void 0) window.frame = requestAnimationFrame(() => {
|
|
3372
|
+
window.frame = void 0;
|
|
3373
|
+
window.draw?.();
|
|
3374
|
+
});
|
|
3375
|
+
};
|
|
3376
|
+
target.addEventListener("scroll", schedule, { passive: true });
|
|
3377
|
+
globalThis.addEventListener("resize", schedule, { passive: true });
|
|
3378
|
+
if (scroller && typeof ResizeObserver !== "undefined") {
|
|
3379
|
+
observer = new ResizeObserver(schedule);
|
|
3380
|
+
observer.observe(scroller);
|
|
3381
|
+
}
|
|
3382
|
+
}
|
|
3383
|
+
/**
|
|
3384
|
+
* Marks the items of a list that keep their places: the longest run of `kept`, the items that
|
|
3385
|
+
* stay in their new order, whose old positions go up.
|
|
3386
|
+
*/
|
|
3387
|
+
function markInPlace(kept) {
|
|
3388
|
+
let sorted = true;
|
|
3389
|
+
for (let index = 1; index < kept.length; index++) if (kept[index - 1].position > kept[index].position) {
|
|
3390
|
+
sorted = false;
|
|
3391
|
+
break;
|
|
3392
|
+
}
|
|
3393
|
+
if (sorted) {
|
|
3394
|
+
for (const item of kept) item.still = true;
|
|
3395
|
+
return;
|
|
3396
|
+
}
|
|
3397
|
+
const tails = [];
|
|
3398
|
+
const before = [];
|
|
3399
|
+
for (let index = 0; index < kept.length; index++) {
|
|
3400
|
+
const position = kept[index].position;
|
|
3401
|
+
let low = 0;
|
|
3402
|
+
let high = tails.length;
|
|
3403
|
+
while (low < high) {
|
|
3404
|
+
const middle = low + high >> 1;
|
|
3405
|
+
if (kept[tails[middle]].position < position) low = middle + 1;
|
|
3406
|
+
else high = middle;
|
|
3407
|
+
}
|
|
3408
|
+
before[index] = low > 0 ? tails[low - 1] : -1;
|
|
3409
|
+
tails[low] = index;
|
|
3410
|
+
}
|
|
3411
|
+
for (let index = tails.at(-1) ?? -1; index >= 0; index = before[index]) kept[index].still = true;
|
|
3412
|
+
}
|
|
3413
|
+
//#endregion
|
|
3414
|
+
//#region src/runtime/client/page.ts
|
|
3415
|
+
/** Sets a style variable on the top-level elements of a component instance. */
|
|
3416
|
+
function setVar(scope, name, value) {
|
|
3417
|
+
const css = value === null || value === void 0 || value === false ? "" : String(value);
|
|
3418
|
+
for (const element of document.querySelectorAll(`[b-v="${scope.id}"]`)) if (element.style.getPropertyValue(name) !== css) element.style.setProperty(name, css);
|
|
3419
|
+
}
|
|
3420
|
+
/**
|
|
3421
|
+
* Warns in development that the browser renders a `#defer` block of a
|
|
3422
|
+
* component, such as inside a `#client` body of its parent. Only the server
|
|
3423
|
+
* renders the body, so the fallback stays.
|
|
3424
|
+
*/
|
|
3425
|
+
function deferInBrowser(id) {
|
|
3426
|
+
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.`);
|
|
3427
|
+
}
|
|
3428
|
+
/** Adds the markup of a `#head` block to the document's head. */
|
|
3429
|
+
function addHead(html) {
|
|
3430
|
+
afterInsert.push(() => insertHTML({ parent: document.head }, html));
|
|
3431
|
+
}
|
|
3432
|
+
/** Adds the markup of a `#portal` block at the end of the page, after the markup around it. */
|
|
3433
|
+
function addPortal(html) {
|
|
3434
|
+
afterInsert.push(() => insertHTML({ parent: document.body }, html));
|
|
3435
|
+
}
|
|
3436
|
+
/** Adds a component's stylesheet to the page, if the server did not. */
|
|
3437
|
+
function addStyle(id, css) {
|
|
3438
|
+
if (!document.querySelector(`style[b-style="${id}"]`)) {
|
|
3439
|
+
const sheet = document.createElement("style");
|
|
3440
|
+
sheet.setAttribute("b-style", id);
|
|
3441
|
+
sheet.textContent = css;
|
|
3442
|
+
document.head.append(sheet);
|
|
3443
|
+
}
|
|
3444
|
+
return "";
|
|
3445
|
+
}
|
|
3446
|
+
//#endregion
|
|
3447
|
+
//#region src/runtime/client.ts
|
|
3448
|
+
/**
|
|
3449
|
+
* What the loader calls. It is one export, so the loader's import of the core
|
|
3450
|
+
* names one export, and a build keeps the rest only when components call them.
|
|
3451
|
+
*/
|
|
3452
|
+
const loader = {
|
|
3453
|
+
init,
|
|
3454
|
+
handle,
|
|
3455
|
+
dispose,
|
|
3456
|
+
submitAction,
|
|
3457
|
+
setGlobal,
|
|
3458
|
+
updateRoute
|
|
3459
|
+
};
|
|
3460
|
+
//#endregion
|
|
3461
|
+
export { forget as $, branchNodes as A, update as At, setAttr as B, makeId as Bt, dynamicNodes as C, get as Ct, setItem as D, resolve as Dt, place as E, pending as Et, keepText as F, title as Ft, template as G, loadPart as Gt, setContent as H, setOwner as Ht, listNodes as I, afterInsert as It, offscreen as J, build as Jt, branch as K, nameComment as Kt, markBlock as L, createScope as Lt, fillBranch as M, fail as Mt, fillList as N, children as Nt, setSlots as O, set as Ot, keep as P, insert as Pt, dispose as Q, skew as Qt, nodes as R, descendants as Rt, dynamic as S, effect as St, foreignNodes as T, memo as Tt, setOn as U, sink as Ut, setAttrText as V, removeScope as Vt, setSpread as W, loadModule as Wt, reset as X, rpcPrefix as Xt, rekey as Y, hooks as Yt, retag as Z, scopes as Zt, writeText as _, updateRoute as _t, deferInBrowser as a, action as at, component as b, bindings as bt, toList as c, rpc as ct, attr as d, guess as dt, init as et, attrs as f, track as ft, text as g, updateContext as gt, restRef as h, setGlobal as ht, addStyle as i, removeRoot as it, clone as j, updated as jt, appendText as k, timed as kt, windowList as l, spread as lt, portalTo as m, provideValue as mt, addHead as n, leaveRoutes as nt, setVar as o, handle as ot, content as p, consumeContext as pt, fill as q, DEVTOOLS as qt, addPortal as r, remount as rt, list as s, handlerProp as st, loader as t, leavePage as tt, windowStart as u, submitAction as ut, prop as v, asyncMemo as vt, elementAt as w, isBound as wt, componentNodes as x, cells as xt, spreadProps as y, binding as yt, putText as z, getScope as zt };
|
|
3462
|
+
|
|
3463
|
+
//# sourceMappingURL=client-BvDsC6Qr.js.map
|