brumate 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/check.d.ts +3 -3
  2. package/dist/{boot-DkbcqdUz.d.ts → boot-Cs_P8cDu.d.ts} +2 -2
  3. package/dist/cli.js +77 -6
  4. package/dist/cli.js.map +1 -1
  5. package/dist/{client-CqGDbWd_.d.ts → client-BDPsTd4q.d.ts} +40 -14
  6. package/dist/{client-C3vRNuLi.js → client-DYIZ7BYK.js} +271 -112
  7. package/dist/client-DYIZ7BYK.js.map +1 -0
  8. package/dist/{client-routes-BtGpYCef.js → client-routes-BmPvNr4s.js} +10 -1
  9. package/dist/client-routes-BmPvNr4s.js.map +1 -0
  10. package/dist/{context-btHuB01Z.d.ts → context-BNgBXVUQ.d.ts} +7 -5
  11. package/dist/{context-CTiSzQRc.js → context-CU4Fpn5a.js} +10 -13
  12. package/dist/context-CU4Fpn5a.js.map +1 -0
  13. package/dist/{defer-M18UKyfG.js → defer-7JDhFxQq.js} +2 -2
  14. package/dist/{defer-M18UKyfG.js.map → defer-7JDhFxQq.js.map} +1 -1
  15. package/dist/{devtools-YnvrZ6D8.js → devtools-CzSp1GGq.js} +34 -24
  16. package/dist/devtools-CzSp1GGq.js.map +1 -0
  17. package/dist/{devtools-panel-DRcKclEG.js → devtools-panel-DugGrz7L.js} +32 -9
  18. package/dist/devtools-panel-DugGrz7L.js.map +1 -0
  19. package/dist/{features-CbgII-o7.js → features-DwMYKBIC.js} +6 -2
  20. package/dist/features-DwMYKBIC.js.map +1 -0
  21. package/dist/{files-HzPDUyTY.js → files-XX5yW3KL.js} +94 -3
  22. package/dist/files-XX5yW3KL.js.map +1 -0
  23. package/dist/index.d.ts +49 -0
  24. package/dist/index.js +3 -3
  25. package/dist/index.js.map +1 -1
  26. package/dist/{inspect-Q5W0A66e.js → inspect-B9cRF6AM.js} +2767 -2077
  27. package/dist/inspect-B9cRF6AM.js.map +1 -0
  28. package/dist/{loader-C-S3yzep.js → loader-BGSXt_Eb.js} +5 -5
  29. package/dist/{loader-C-S3yzep.js.map → loader-BGSXt_Eb.js.map} +1 -1
  30. package/dist/{rpc-C_znDZ_w.js → rpc-B9EkaPdU.js} +3 -3
  31. package/dist/{rpc-C_znDZ_w.js.map → rpc-B9EkaPdU.js.map} +1 -1
  32. package/dist/runtime/bundles.d.ts +1 -1
  33. package/dist/runtime/bundles.js +2 -0
  34. package/dist/runtime/bundles.js.map +1 -1
  35. package/dist/runtime/client.d.ts +4 -4
  36. package/dist/runtime/client.js +4 -4
  37. package/dist/runtime/context.d.ts +2 -2
  38. package/dist/runtime/context.js +1 -1
  39. package/dist/runtime/devtools.d.ts +15 -2
  40. package/dist/runtime/devtools.js +2 -2
  41. package/dist/runtime/hot.d.ts +1 -1
  42. package/dist/runtime/html.d.ts +1 -1
  43. package/dist/runtime/loader.d.ts +2 -2
  44. package/dist/runtime/loader.js +2 -2
  45. package/dist/runtime/server.d.ts +4 -4
  46. package/dist/runtime/server.js +4 -4
  47. package/dist/runtime/signals.d.ts +2 -2
  48. package/dist/runtime/signals.js +43 -5
  49. package/dist/runtime/signals.js.map +1 -1
  50. package/dist/runtime/static-loader.d.ts +2 -2
  51. package/dist/runtime/static-loader.js +1 -1
  52. package/dist/runtime/store.d.ts +2 -0
  53. package/dist/runtime/store.js +1 -0
  54. package/dist/runtime/testing.d.ts +2 -2
  55. package/dist/runtime/testing.js +2 -2
  56. package/dist/{search-params-CM1RWR2P.js → search-params-Cy3uRphR.js} +33 -8
  57. package/dist/search-params-Cy3uRphR.js.map +1 -0
  58. package/dist/{server-BcT9icQ4.js → server-C1OTFH4r.js} +127 -14
  59. package/dist/server-C1OTFH4r.js.map +1 -0
  60. package/dist/{server-dP_ucRiJ.d.ts → server-CavhuoFI.d.ts} +32 -8
  61. package/dist/{shared-CzbnWkVA.d.ts → shared-CRduzyi3.d.ts} +15 -2
  62. package/dist/{signals-BwD4u22K.d.ts → signals-DBnq9QRW.d.ts} +46 -6
  63. package/dist/{static-loader-DysAET-w.d.ts → static-loader-D5hvLQK7.d.ts} +2 -2
  64. package/dist/{static-loader-Bt2WDkY1.js → static-loader-XGXMzfJR.js} +3 -3
  65. package/dist/{static-loader-Bt2WDkY1.js.map → static-loader-XGXMzfJR.js.map} +1 -1
  66. package/dist/store-BIBAYZo-.js +293 -0
  67. package/dist/store-BIBAYZo-.js.map +1 -0
  68. package/dist/store-xlTzAM_r.d.ts +171 -0
  69. package/dist/{stream-DRjx0keF.js → stream-BBnArf_5.js} +89 -3
  70. package/dist/stream-BBnArf_5.js.map +1 -0
  71. package/dist/vite.d.ts +1 -1
  72. package/dist/vite.js +128 -13
  73. package/dist/vite.js.map +1 -1
  74. package/elements.d.ts +268 -21
  75. package/package.json +5 -1
  76. package/skills/brumate/SKILL.md +1 -1
  77. package/skills/brumate/references/errors.md +127 -9
  78. package/skills/brumate/references/signals.md +37 -30
  79. package/skills/brumate/references/state.md +3 -2
  80. package/dist/client-C3vRNuLi.js.map +0 -1
  81. package/dist/client-routes-BtGpYCef.js.map +0 -1
  82. package/dist/context-CTiSzQRc.js.map +0 -1
  83. package/dist/devtools-YnvrZ6D8.js.map +0 -1
  84. package/dist/devtools-panel-DRcKclEG.js.map +0 -1
  85. package/dist/features-CbgII-o7.js.map +0 -1
  86. package/dist/files-HzPDUyTY.js.map +0 -1
  87. package/dist/inspect-Q5W0A66e.js.map +0 -1
  88. package/dist/search-params-CM1RWR2P.js.map +0 -1
  89. package/dist/server-BcT9icQ4.js.map +0 -1
  90. package/dist/stream-DRjx0keF.js.map +0 -1
@@ -0,0 +1,293 @@
1
+ import { i as HAS_BRIDGES } from "./features-DwMYKBIC.js";
2
+ //#region src/runtime/store.ts
3
+ /** The pass of the core, which the browser sets, so a store's readers update in one pass. */
4
+ const PASS = Symbol.for("brumate.pass");
5
+ /** True for a context, which `createContext` made. */
6
+ function isContext(value) {
7
+ return typeof value === "object" && value !== null && typeof value.key === "string" && Object.keys(value).length === 1 && !isContract(value);
8
+ }
9
+ /**
10
+ * The store that `$store` reads: `value` is the store, or for `path` an object that holds it there.
11
+ * `key` names the context that gave `value`, for the message. Throws a `TypeError` for a value that
12
+ * is no store, such as `undefined` or a context, and for `write` on a store that has no `set`.
13
+ */
14
+ function findStored(value, path, write, key) {
15
+ const found = path === void 0 ? value : value?.[path];
16
+ if (!isContract(found)) {
17
+ if (isContext(found)) throw new TypeError(`$store reads the context "${found.key}", which is no store. Read the store in it with $store($getContext(Context))`);
18
+ throw new TypeError(`$store reads ${key === void 0 ? "a value" : `the context "${key}"`}${path === void 0 ? "" : ` at "${path}"`}, which gives no store: a store has the functions get and subscribe`);
19
+ }
20
+ if (write && !canSet(found)) throw new TypeError(`let $store(${key ?? "…"}) writes the store, but the store has no set. Read it with const`);
21
+ return found;
22
+ }
23
+ /**
24
+ * The error of `$store` for a store of another library in a value that the page sends, such as a
25
+ * prop: only the value of a store of `brumate/store` crosses. The fix is said here only.
26
+ */
27
+ function sentForeignStore(component, value) {
28
+ return /* @__PURE__ */ new Error(`$store in ${component} reads a store of another library from ${value}, which the page sends, and only a store of brumate/store crosses. Wrap the library's store with bridge() in a module, or read it from an import, <script module> or a call in this component`);
29
+ }
30
+ /** A value that changes, with the functions that read it and write it. */
31
+ var Store = class {
32
+ /** The value. */
33
+ v;
34
+ /** The functions that `subscribe` added. */
35
+ l = /* @__PURE__ */ new Set();
36
+ /** For a store of `$toStore(signal)`, the write of the signal, which the browser sets. */
37
+ w;
38
+ /** True for a store of `$toStore(memo)`, which only its provider writes. */
39
+ r;
40
+ /** Where `$toStore` made it, for the page state. */
41
+ o;
42
+ /** True for a store of `$toStore` in the browser until its first update. */
43
+ f;
44
+ /**
45
+ * The mark of a store, which `isStore` reads in every copy of the runtime. A field, so a build
46
+ * without stores drops the class.
47
+ */
48
+ k = "brumate.store";
49
+ constructor(value) {
50
+ this.v = value;
51
+ }
52
+ /** The current value. */
53
+ get() {
54
+ return this.v;
55
+ }
56
+ /** Calls `listener` with each new value. Returns the function that stops it. */
57
+ subscribe(listener) {
58
+ this.l.add(listener);
59
+ return () => {
60
+ this.l.delete(listener);
61
+ };
62
+ }
63
+ /**
64
+ * Writes the value, and calls the listeners when it changed. A store of `$toStore(signal)`
65
+ * writes the signal, and a store of a memo throws.
66
+ */
67
+ set(value) {
68
+ if (this.r) throw new Error("This store follows a memo, so only its provider writes it");
69
+ if (Object.is(this.v, value)) return;
70
+ if (this.w) {
71
+ this.v = value;
72
+ this.w(value);
73
+ return;
74
+ }
75
+ sendStore(this, value);
76
+ }
77
+ };
78
+ /** True for a store of `brumate/store`, of any copy of the runtime. */
79
+ function isStore(value) {
80
+ return typeof value === "object" && value !== null && value.k === "brumate.store";
81
+ }
82
+ /** True for a value with the functions `get` and `subscribe` of the store contract. */
83
+ function isContract(value) {
84
+ return typeof value === "object" && value !== null && typeof value.get === "function" && typeof value.subscribe === "function";
85
+ }
86
+ /** True for a value of the store contract that is not a store of brumate. */
87
+ function isForeignStore(value) {
88
+ return isContract(value) && !isStore(value) && !(HAS_BRIDGES && isBridged(value));
89
+ }
90
+ /** True when `$store` can write the store: it has `set`, and is not the store of a memo. */
91
+ function canSet(value) {
92
+ return typeof value.set === "function" && !value.r;
93
+ }
94
+ /** Makes a store, such as one that a module holds for the whole app. */
95
+ function store(value) {
96
+ return new Store(value);
97
+ }
98
+ /** Writes the value of a store and calls its listeners, in one pass of the page. */
99
+ function sendStore(target, value, force) {
100
+ if (!force && Object.is(target.v, value)) return;
101
+ target.v = value;
102
+ const listeners = () => {
103
+ for (const listener of target.l) listener(value);
104
+ };
105
+ const pass = globalThis[PASS];
106
+ if (pass) pass(listeners);
107
+ else listeners();
108
+ }
109
+ const BRIDGES = Symbol.for("brumate.bridges");
110
+ const BRIDGE_LOAD = Symbol.for("brumate.bridge-load");
111
+ /** The bridges by name, which any copy of the runtime shares. */
112
+ function bridges() {
113
+ return globalThis[BRIDGES] ??= /* @__PURE__ */ new Map();
114
+ }
115
+ /**
116
+ * Sets how the browser loads the module of a bridge, and where a failed load is reported. The
117
+ * core calls it when it starts.
118
+ */
119
+ function setBridgeLoader(...loader) {
120
+ globalThis[BRIDGE_LOAD] = loader;
121
+ }
122
+ /** The module of a bridge, from its name. */
123
+ function bridgeModule(id) {
124
+ return id.slice(0, id.lastIndexOf("#"));
125
+ }
126
+ /**
127
+ * Makes a bridge. Keep its value in a module-level export, so the page state can name it:
128
+ * `export const bears = bridge({ … })`. The compiler gives it its name.
129
+ */
130
+ function bridge(options) {
131
+ const made = Object.assign((snapshot) => new Bridged(made, options.create(snapshot)), { b: options });
132
+ return made;
133
+ }
134
+ /** Gives a bridge its name, which the compiler writes at the end of its module. */
135
+ function nameBridge(made, id) {
136
+ made.i = id;
137
+ bridges().set(id, made);
138
+ }
139
+ /** True for a store of `bridge()`, of any copy of the runtime. */
140
+ function isBridged(value) {
141
+ return typeof value === "object" && value !== null && value.k === "brumate.bridge";
142
+ }
143
+ /**
144
+ * Whether two values hold the same data: the same value, or arrays and plain objects whose items
145
+ * hold the same data. A store that the browser makes again holds a new object with the content
146
+ * the server sent, which needs no repair.
147
+ */
148
+ function sameData(a, b, open) {
149
+ if (Object.is(a, b)) return true;
150
+ const array = Array.isArray(a);
151
+ if (array ? !Array.isArray(b) || a.length !== b.length : !isPlain(a) || !isPlain(b)) return false;
152
+ const left = a;
153
+ const right = b;
154
+ open ??= /* @__PURE__ */ new Map();
155
+ let pairs = open.get(left);
156
+ if (pairs?.has(right)) return true;
157
+ if (!pairs) {
158
+ pairs = /* @__PURE__ */ new Set();
159
+ open.set(left, pairs);
160
+ }
161
+ pairs.add(right);
162
+ const keys = Object.keys(left);
163
+ if (!array && keys.length !== Object.keys(right).length) return false;
164
+ for (const key of keys) if (!(key in right) || !sameData(left[key], right[key], open)) return false;
165
+ return true;
166
+ }
167
+ /** Whether a value is an object of `Object` or of no prototype, as page state holds. */
168
+ function isPlain(value) {
169
+ if (typeof value !== "object" || value === null) return false;
170
+ const proto = Object.getPrototypeOf(value);
171
+ return proto === Object.prototype || proto === null;
172
+ }
173
+ /**
174
+ * The store that a bridge makes. On the server it holds the library's store from the start. In
175
+ * the browser the page state makes it with the name of the bridge and the data of the server, and
176
+ * it makes the library's store at its first subscriber.
177
+ */
178
+ var Bridged = class {
179
+ /** The mark of a bridged store, which `isBridged` reads in every copy of the runtime. */
180
+ k = "brumate.bridge";
181
+ /** The bridge. Undefined for a store of the page state until the module of the bridge loads. */
182
+ f;
183
+ /** The name of the bridge, for a store of the page state. */
184
+ i;
185
+ /** The library's store. */
186
+ s;
187
+ /** True once the library's store exists. */
188
+ m = false;
189
+ /** The data that the server sent, until the library's store exists. */
190
+ v;
191
+ /** Whether the bridge has `set`, as the server sent it, until the module loads. */
192
+ w;
193
+ constructor(made, source, id, snapshot, writable) {
194
+ this.f = made;
195
+ this.i = id;
196
+ this.v = snapshot;
197
+ this.w = writable;
198
+ if (made) {
199
+ this.s = source;
200
+ this.m = true;
201
+ }
202
+ }
203
+ /**
204
+ * The library's store. In the browser it is undefined until the store has a subscriber and the
205
+ * module of the bridge has loaded.
206
+ */
207
+ get source() {
208
+ return this.s;
209
+ }
210
+ /** The name of the bridge. */
211
+ get id() {
212
+ return this.f?.i ?? this.i;
213
+ }
214
+ /** The data of the store: what `get` reads, or what the server sent before the first subscriber. */
215
+ get() {
216
+ return this.m ? this.f.b.get(this.s) : this.v;
217
+ }
218
+ /**
219
+ * Calls `listener` with the data when it changes. The first subscriber makes the library's
220
+ * store in the browser. When the module of the bridge has not loaded, the subscription starts
221
+ * when it has.
222
+ */
223
+ subscribe(listener, ...seen) {
224
+ let stopped = false;
225
+ let stop;
226
+ let last = seen.length > 0 ? seen[0] : this.get();
227
+ const start = () => {
228
+ if (stopped) return;
229
+ const options = this.f.b;
230
+ const source = this.make();
231
+ stop = options.subscribe(source, () => {
232
+ const next = options.get(source);
233
+ if (!sameData(next, last)) {
234
+ last = next;
235
+ listener(next);
236
+ }
237
+ });
238
+ const first = options.get(source);
239
+ if (!sameData(first, last)) {
240
+ last = first;
241
+ listener(first);
242
+ }
243
+ };
244
+ if (this.find()) start();
245
+ else this.load().then((loaded) => loaded && start());
246
+ return () => {
247
+ stopped = true;
248
+ stop?.();
249
+ };
250
+ }
251
+ /** Writes the library's store, when the bridge has `set`. */
252
+ get set() {
253
+ return (this.find() ? !!this.f.b.set : this.w !== false) ? (value) => this.write(value) : void 0;
254
+ }
255
+ /** Writes the library's store, after the module of the bridge has loaded. */
256
+ write(value) {
257
+ if (this.find()) this.f.b.set(this.make(), value);
258
+ else this.load().then((loaded) => loaded && this.write(value));
259
+ }
260
+ /** The bridge, once its module has loaded. */
261
+ find() {
262
+ if (!this.f && this.i !== void 0) this.f = bridges().get(this.i);
263
+ return this.f;
264
+ }
265
+ /** Loads the module of the bridge. A failure goes to the core's error path; it gives false. */
266
+ load() {
267
+ const [load, report] = globalThis[BRIDGE_LOAD] ?? [];
268
+ return Promise.resolve(load?.(bridgeModule(this.i))).then(() => {
269
+ if (this.find()) return true;
270
+ this.fail(report, /* @__PURE__ */ new Error(`The bridge "${this.i}" is not in its module. Keep bridge() in a module-level export, as "export const name = bridge({ … })"`));
271
+ return false;
272
+ }, (error) => {
273
+ this.fail(report, error);
274
+ return false;
275
+ });
276
+ }
277
+ fail(report, error) {
278
+ if (report) report(error);
279
+ else console.error(error);
280
+ }
281
+ /** Makes the library's store once, with the data of the server. */
282
+ make() {
283
+ if (!this.m) {
284
+ this.s = this.f.b.create(this.v);
285
+ this.m = true;
286
+ }
287
+ return this.s;
288
+ }
289
+ };
290
+ //#endregion
291
+ export { findStored as a, isContract as c, nameBridge as d, sameData as f, store as g, setBridgeLoader as h, canSet as i, isForeignStore as l, sentForeignStore as m, Store as n, isBridged as o, sendStore as p, bridge as r, isContext as s, Bridged as t, isStore as u };
292
+
293
+ //# sourceMappingURL=store-BIBAYZo-.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store-BIBAYZo-.js","names":[],"sources":["../src/runtime/store.ts"],"sourcesContent":["// Stores: values that change, which a context can hold while the context itself stays the same.\n// `$toStore` makes the store of a provider, and `$store` reads one in a component. The page state\n// sends only the value of a store; the browser makes a new store for it.\n\nimport type { Context } from './context.ts';\nimport { HAS_BRIDGES } from './features.ts';\n\n/** The pass of the core, which the browser sets, so a store's readers update in one pass. */\nconst PASS = Symbol.for('brumate.pass');\n\n/** True for a context, which `createContext` made. */\nexport function isContext(value: unknown): value is Context<unknown> {\n return (\n typeof value === 'object' &&\n value !== null &&\n typeof (value as { key?: unknown }).key === 'string' &&\n Object.keys(value).length === 1 &&\n !isContract(value)\n );\n}\n\n/**\n * The store that `$store` reads: `value` is the store, or for `path` an object that holds it there.\n * `key` names the context that gave `value`, for the message. Throws a `TypeError` for a value that\n * is no store, such as `undefined` or a context, and for `write` on a store that has no `set`.\n */\nexport function findStored(\n value: unknown,\n path: string | undefined,\n write: boolean | undefined,\n key?: string,\n): StoreLike<unknown> {\n const found = path === undefined ? value : (value as Record<string, unknown> | undefined)?.[path];\n if (!isContract(found)) {\n // A context is the most likely mistake, so its message names the read that gives its store.\n if (isContext(found)) {\n throw new TypeError(\n `$store reads the context \"${found.key}\", which is no store. Read the store in it with $store($getContext(Context))`,\n );\n }\n throw new TypeError(\n `$store reads ${key === undefined ? 'a value' : `the context \"${key}\"`}${path === undefined ? '' : ` at \"${path}\"`}, which gives no store: a store has the functions get and subscribe`,\n );\n }\n if (write && !canSet(found)) {\n throw new TypeError(\n `let $store(${key ?? '…'}) writes the store, but the store has no set. Read it with const`,\n );\n }\n return found;\n}\n\n/**\n * The error of `$store` for a store of another library in a value that the page sends, such as a\n * prop: only the value of a store of `brumate/store` crosses. The fix is said here only.\n */\nexport function sentForeignStore(component: string, value: string): Error {\n return new Error(\n `$store in ${component} reads a store of another library from ${value}, which the page sends, and only a store of brumate/store crosses. Wrap the library's store with bridge() in a module, or read it from an import, <script module> or a call in this component`,\n );\n}\n\n/** Where `$toStore` made a store: its scope, the ID of its site, and the slot of its signal. */\nexport type StoreOrigin = [scope: number | string, site: number, slot?: number];\n\n/**\n * The contract of a store, which `$store` reads: `get`, `subscribe` (which returns the function\n * that stops it, and does not call the listener at once) and, for a two-way binding, `set`.\n */\nexport interface StoreLike<T> {\n get(): T;\n subscribe(listener: (value: T) => void): () => void;\n set?(value: T): void;\n}\n\n/** A value that changes, with the functions that read it and write it. */\nexport class Store<T> implements StoreLike<T> {\n /** The value. */\n v: T;\n /** The functions that `subscribe` added. */\n l: Set<(value: T) => void> = new Set();\n /** For a store of `$toStore(signal)`, the write of the signal, which the browser sets. */\n w?: (value: T) => void;\n /** True for a store of `$toStore(memo)`, which only its provider writes. */\n r?: boolean;\n /** Where `$toStore` made it, for the page state. */\n o?: StoreOrigin;\n /** True for a store of `$toStore` in the browser until its first update. */\n f?: boolean;\n /**\n * The mark of a store, which `isStore` reads in every copy of the runtime. A field, so a build\n * without stores drops the class.\n */\n k = 'brumate.store' as const;\n\n constructor(value: T) {\n this.v = value;\n }\n\n /** The current value. */\n get(): T {\n return this.v;\n }\n\n /** Calls `listener` with each new value. Returns the function that stops it. */\n subscribe(listener: (value: T) => void): () => void {\n this.l.add(listener);\n return () => {\n this.l.delete(listener);\n };\n }\n\n /**\n * Writes the value, and calls the listeners when it changed. A store of `$toStore(signal)`\n * writes the signal, and a store of a memo throws.\n */\n set(value: T): void {\n if (this.r) {\n throw new Error('This store follows a memo, so only its provider writes it');\n }\n if (Object.is(this.v, value)) {\n return;\n }\n if (this.w) {\n this.v = value;\n this.w(value);\n return;\n }\n sendStore(this, value);\n }\n}\n\n/** True for a store of `brumate/store`, of any copy of the runtime. */\nexport function isStore(value: unknown): value is Store<unknown> {\n return (\n typeof value === 'object' && value !== null && (value as { k?: unknown }).k === 'brumate.store'\n );\n}\n\n/** True for a value with the functions `get` and `subscribe` of the store contract. */\nexport function isContract(value: unknown): value is StoreLike<unknown> {\n return (\n typeof value === 'object' &&\n value !== null &&\n typeof (value as { get?: unknown }).get === 'function' &&\n typeof (value as { subscribe?: unknown }).subscribe === 'function'\n );\n}\n\n/** True for a value of the store contract that is not a store of brumate. */\nexport function isForeignStore(value: unknown): boolean {\n return isContract(value) && !isStore(value) && !(HAS_BRIDGES && isBridged(value));\n}\n\n/** True when `$store` can write the store: it has `set`, and is not the store of a memo. */\nexport function canSet(value: StoreLike<unknown>): boolean {\n return typeof value.set === 'function' && !(value as Store<unknown>).r;\n}\n\n/** Makes a store, such as one that a module holds for the whole app. */\nexport function store<T>(value: T): Store<T> {\n return new Store(value);\n}\n\n/** Writes the value of a store and calls its listeners, in one pass of the page. */\nexport function sendStore<T>(target: Store<T>, value: T, force?: boolean): void {\n // The listeners hear of a change only. A provider's update forces it, since a `set` of\n // `$toStore(signal)` wrote the value before the signal's update came.\n if (!force && Object.is(target.v, value)) {\n return;\n }\n target.v = value;\n const listeners = () => {\n for (const listener of target.l) {\n listener(value);\n }\n };\n const pass = (globalThis as { [key: symbol]: (<R>(run: () => R) => R) | undefined })[PASS];\n if (pass) {\n pass(listeners);\n } else {\n listeners();\n }\n}\n\n// Bridges: a store of another library that can cross the page state. `bridge()` names how to make\n// the library's store, read it and listen to it, and the page state sends the data that `get`\n// reads, with the name of the bridge. The browser makes the library's store again, once, from\n// that data.\n\n/** What `bridge` takes. */\nexport interface BridgeOptions<S, T> {\n /**\n * Makes the library's store. On the server it runs once for each call of the bridge. In the\n * browser it runs once, when the store gets its first subscriber, with the data that the server\n * sent. It must be in a module that the browser can load.\n */\n create: (snapshot: T | undefined) => S;\n /** The data of the store: plain data that the page state can send. */\n get: (source: S) => T;\n /** Calls `listener` when the store changes. Returns the function that stops it. */\n subscribe: (source: S, listener: () => void) => () => void;\n /** Writes the store. The browser never calls it when the page resumes. */\n set?: (source: S, value: T) => void;\n}\n\n/** A store of `bridge()`: the contract of a store, and the store of the library. */\nexport interface BridgedStore<S, T> extends StoreLike<T> {\n /**\n * The store of the library. In the browser it is undefined until the store has a subscriber,\n * since the browser makes it at the first subscriber.\n */\n readonly source: S | undefined;\n}\n\n/** What `bridge` returns: makes a store, from the data of the server in the browser. */\nexport interface Bridge<S, T> {\n (snapshot?: T): BridgedStore<S, T>;\n /** The options. */\n b: BridgeOptions<S, T>;\n /** The name that the compiler gave, `bridge:<file>#<export>`. */\n i?: string;\n}\n\nconst BRIDGES = Symbol.for('brumate.bridges');\nconst BRIDGE_LOAD = Symbol.for('brumate.bridge-load');\n\ntype Global = { [BRIDGES]?: Map<string, Bridge<never, never>>; [BRIDGE_LOAD]?: Loader };\ntype Loader = [load: (module: string) => Promise<unknown>, report: (error: unknown) => void];\n\n/** The bridges by name, which any copy of the runtime shares. */\nfunction bridges(): Map<string, Bridge<never, never>> {\n return ((globalThis as Global)[BRIDGES] ??= new Map());\n}\n\n/**\n * Sets how the browser loads the module of a bridge, and where a failed load is reported. The\n * core calls it when it starts.\n */\nexport function setBridgeLoader(...loader: Loader): void {\n (globalThis as Global)[BRIDGE_LOAD] = loader;\n}\n\n/** The module of a bridge, from its name. */\nfunction bridgeModule(id: string): string {\n return id.slice(0, id.lastIndexOf('#'));\n}\n\n/**\n * Makes a bridge. Keep its value in a module-level export, so the page state can name it:\n * `export const bears = bridge({ … })`. The compiler gives it its name.\n */\nexport function bridge<S, T>(options: BridgeOptions<S, T>): Bridge<S, T> {\n const made = Object.assign(\n (snapshot?: T): BridgedStore<S, T> => new Bridged(made, options.create(snapshot)),\n { b: options },\n ) as Bridge<S, T>;\n return made;\n}\n\n/** Gives a bridge its name, which the compiler writes at the end of its module. */\nexport function nameBridge(made: Bridge<never, never>, id: string): void {\n made.i = id;\n bridges().set(id, made);\n}\n\n/** True for a store of `bridge()`, of any copy of the runtime. */\nexport function isBridged(value: unknown): value is Bridged<unknown, unknown> {\n return (\n typeof value === 'object' && value !== null && (value as { k?: unknown }).k === 'brumate.bridge'\n );\n}\n\n/**\n * Whether two values hold the same data: the same value, or arrays and plain objects whose items\n * hold the same data. A store that the browser makes again holds a new object with the content\n * the server sent, which needs no repair.\n */\nexport function sameData(a: unknown, b: unknown, open?: Map<object, Set<unknown>>): boolean {\n if (Object.is(a, b)) {\n return true;\n }\n const array = Array.isArray(a);\n if (array ? !Array.isArray(b) || a.length !== b.length : !isPlain(a) || !isPlain(b)) {\n return false;\n }\n const left = a as Record<string, unknown>;\n const right = b as Record<string, unknown>;\n // A pair already under compare is a cycle: it counts as the same, so each pair is compared once.\n open ??= new Map();\n let pairs = open.get(left);\n if (pairs?.has(right)) {\n return true;\n }\n if (!pairs) {\n pairs = new Set();\n open.set(left, pairs);\n }\n pairs.add(right);\n const keys = Object.keys(left);\n if (!array && keys.length !== Object.keys(right).length) {\n return false;\n }\n for (const key of keys) {\n if (!(key in right) || !sameData(left[key], right[key], open)) {\n return false;\n }\n }\n return true;\n}\n\n/** Whether a value is an object of `Object` or of no prototype, as page state holds. */\nfunction isPlain(value: unknown): value is Record<string, unknown> {\n if (typeof value !== 'object' || value === null) {\n return false;\n }\n const proto = Object.getPrototypeOf(value);\n return proto === Object.prototype || proto === null;\n}\n\n/**\n * The store that a bridge makes. On the server it holds the library's store from the start. In\n * the browser the page state makes it with the name of the bridge and the data of the server, and\n * it makes the library's store at its first subscriber.\n */\nexport class Bridged<S, T> implements BridgedStore<S, T> {\n /** The mark of a bridged store, which `isBridged` reads in every copy of the runtime. */\n k = 'brumate.bridge' as const;\n /** The bridge. Undefined for a store of the page state until the module of the bridge loads. */\n f?: Bridge<S, T>;\n /** The name of the bridge, for a store of the page state. */\n i?: string;\n /** The library's store. */\n s?: S;\n /** True once the library's store exists. */\n m = false;\n /** The data that the server sent, until the library's store exists. */\n v?: T;\n /** Whether the bridge has `set`, as the server sent it, until the module loads. */\n w?: boolean;\n\n constructor(\n made: Bridge<S, T> | undefined,\n source: S | undefined,\n id?: string,\n snapshot?: T,\n writable?: boolean,\n ) {\n this.f = made;\n this.i = id;\n this.v = snapshot;\n this.w = writable;\n if (made) {\n this.s = source;\n this.m = true;\n }\n }\n\n /**\n * The library's store. In the browser it is undefined until the store has a subscriber and the\n * module of the bridge has loaded.\n */\n get source(): S | undefined {\n return this.s;\n }\n\n /** The name of the bridge. */\n get id(): string | undefined {\n return this.f?.i ?? this.i;\n }\n\n /** The data of the store: what `get` reads, or what the server sent before the first subscriber. */\n get(): T {\n return this.m ? this.f!.b.get(this.s as S) : (this.v as T);\n }\n\n /**\n * Calls `listener` with the data when it changes. The first subscriber makes the library's\n * store in the browser. When the module of the bridge has not loaded, the subscription starts\n * when it has.\n */\n subscribe(listener: (value: T) => void, ...seen: [value?: T]): () => void {\n let stopped = false;\n let stop: (() => void) | undefined;\n // What this subscriber has seen, which `seen` can name: a source that starts later compares\n // with it.\n let last = seen.length > 0 ? (seen[0] as T) : this.get();\n const start = (): void => {\n if (stopped) {\n return;\n }\n const options = this.f!.b;\n const source = this.make();\n stop = options.subscribe(source, () => {\n const next = options.get(source);\n if (!sameData(next, last)) {\n last = next;\n listener(next);\n }\n });\n // A source that starts from other data than this subscriber saw, such as one that keeps its\n // own state, is written once.\n const first = options.get(source);\n if (!sameData(first, last)) {\n last = first;\n listener(first);\n }\n };\n if (this.find()) {\n start();\n } else {\n void this.load().then((loaded) => loaded && start());\n }\n return () => {\n stopped = true;\n stop?.();\n };\n }\n\n /** Writes the library's store, when the bridge has `set`. */\n get set(): ((value: T) => void) | undefined {\n // Before the module loads, the server's word is all there is, so `let` checks the same.\n const writable = this.find() ? !!this.f!.b.set : this.w !== false;\n return writable ? (value) => this.write(value) : undefined;\n }\n\n /** Writes the library's store, after the module of the bridge has loaded. */\n private write(value: T): void {\n if (this.find()) {\n this.f!.b.set!(this.make(), value);\n } else {\n void this.load().then((loaded) => loaded && this.write(value));\n }\n }\n\n /** The bridge, once its module has loaded. */\n private find(): Bridge<S, T> | undefined {\n if (!this.f && this.i !== undefined) {\n this.f = bridges().get(this.i) as Bridge<S, T> | undefined;\n }\n return this.f;\n }\n\n /** Loads the module of the bridge. A failure goes to the core's error path; it gives false. */\n private load(): Promise<boolean> {\n const [load, report] = (globalThis as Global)[BRIDGE_LOAD] ?? [];\n return Promise.resolve(load?.(bridgeModule(this.i!))).then(\n () => {\n if (this.find()) {\n return true;\n }\n this.fail(\n report,\n new Error(\n `The bridge \"${this.i}\" is not in its module. Keep bridge() in a module-level export, as \"export const name = bridge({ … })\"`,\n ),\n );\n return false;\n },\n (error: unknown) => {\n this.fail(report, error);\n return false;\n },\n );\n }\n\n private fail(report: ((error: unknown) => void) | undefined, error: unknown): void {\n if (report) {\n report(error);\n } else {\n console.error(error);\n }\n }\n\n /** Makes the library's store once, with the data of the server. */\n private make(): S {\n if (!this.m) {\n this.s = this.f!.b.create(this.v);\n this.m = true;\n }\n return this.s as S;\n }\n}\n"],"mappings":";;;AAQA,MAAM,OAAO,OAAO,IAAI,cAAc;;AAGtC,SAAgB,UAAU,OAA2C;CACnE,OACE,OAAO,UAAU,YACjB,UAAU,QACV,OAAQ,MAA4B,QAAQ,YAC5C,OAAO,KAAK,KAAK,CAAC,CAAC,WAAW,KAC9B,CAAC,WAAW,KAAK;AAErB;;;;;;AAOA,SAAgB,WACd,OACA,MACA,OACA,KACoB;CACpB,MAAM,QAAQ,SAAS,KAAA,IAAY,QAAS,QAAgD;CAC5F,IAAI,CAAC,WAAW,KAAK,GAAG;EAEtB,IAAI,UAAU,KAAK,GACjB,MAAM,IAAI,UACR,6BAA6B,MAAM,IAAI,6EACzC;EAEF,MAAM,IAAI,UACR,gBAAgB,QAAQ,KAAA,IAAY,YAAY,gBAAgB,IAAI,KAAK,SAAS,KAAA,IAAY,KAAK,QAAQ,KAAK,GAAG,oEACrH;CACF;CACA,IAAI,SAAS,CAAC,OAAO,KAAK,GACxB,MAAM,IAAI,UACR,cAAc,OAAO,IAAI,iEAC3B;CAEF,OAAO;AACT;;;;;AAMA,SAAgB,iBAAiB,WAAmB,OAAsB;CACxE,uBAAO,IAAI,MACT,aAAa,UAAU,yCAAyC,MAAM,8LACxE;AACF;;AAgBA,IAAa,QAAb,MAA8C;;CAE5C;;CAEA,oBAA6B,IAAI,IAAI;;CAErC;;CAEA;;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BNgBXVUQ.js";
2
+ //#region src/runtime/store.d.ts
3
+ /** True for a context, which `createContext` made. */
4
+ declare function isContext(value: unknown): value is Context<unknown>;
5
+ /**
6
+ * The store that `$store` reads: `value` is the store, or for `path` an object that holds it there.
7
+ * `key` names the context that gave `value`, for the message. Throws a `TypeError` for a value that
8
+ * is no store, such as `undefined` or a context, and for `write` on a store that has no `set`.
9
+ */
10
+ declare function findStored(value: unknown, path: string | undefined, write: boolean | undefined, key?: string): StoreLike<unknown>;
11
+ /**
12
+ * The error of `$store` for a store of another library in a value that the page sends, such as a
13
+ * prop: only the value of a store of `brumate/store` crosses. The fix is said here only.
14
+ */
15
+ declare function sentForeignStore(component: string, value: string): Error;
16
+ /** Where `$toStore` made a store: its scope, the ID of its site, and the slot of its signal. */
17
+ type StoreOrigin = [scope: number | string, site: number, slot?: number];
18
+ /**
19
+ * The contract of a store, which `$store` reads: `get`, `subscribe` (which returns the function
20
+ * that stops it, and does not call the listener at once) and, for a two-way binding, `set`.
21
+ */
22
+ interface StoreLike<T> {
23
+ get(): T;
24
+ subscribe(listener: (value: T) => void): () => void;
25
+ set?(value: T): void;
26
+ }
27
+ /** A value that changes, with the functions that read it and write it. */
28
+ declare class Store<T> implements StoreLike<T> {
29
+ /** The value. */
30
+ v: T;
31
+ /** The functions that `subscribe` added. */
32
+ l: Set<(value: T) => void>;
33
+ /** For a store of `$toStore(signal)`, the write of the signal, which the browser sets. */
34
+ w?: (value: T) => void;
35
+ /** True for a store of `$toStore(memo)`, which only its provider writes. */
36
+ r?: boolean;
37
+ /** Where `$toStore` made it, for the page state. */
38
+ o?: StoreOrigin;
39
+ /** True for a store of `$toStore` in the browser until its first update. */
40
+ f?: boolean;
41
+ /**
42
+ * The mark of a store, which `isStore` reads in every copy of the runtime. A field, so a build
43
+ * without stores drops the class.
44
+ */
45
+ k: "brumate.store";
46
+ constructor(value: T);
47
+ /** The current value. */
48
+ get(): T;
49
+ /** Calls `listener` with each new value. Returns the function that stops it. */
50
+ subscribe(listener: (value: T) => void): () => void;
51
+ /**
52
+ * Writes the value, and calls the listeners when it changed. A store of `$toStore(signal)`
53
+ * writes the signal, and a store of a memo throws.
54
+ */
55
+ set(value: T): void;
56
+ }
57
+ /** True for a store of `brumate/store`, of any copy of the runtime. */
58
+ declare function isStore(value: unknown): value is Store<unknown>;
59
+ /** True for a value with the functions `get` and `subscribe` of the store contract. */
60
+ declare function isContract(value: unknown): value is StoreLike<unknown>;
61
+ /** True for a value of the store contract that is not a store of brumate. */
62
+ declare function isForeignStore(value: unknown): boolean;
63
+ /** True when `$store` can write the store: it has `set`, and is not the store of a memo. */
64
+ declare function canSet(value: StoreLike<unknown>): boolean;
65
+ /** Makes a store, such as one that a module holds for the whole app. */
66
+ declare function store<T>(value: T): Store<T>;
67
+ /** Writes the value of a store and calls its listeners, in one pass of the page. */
68
+ declare function sendStore<T>(target: Store<T>, value: T, force?: boolean): void;
69
+ /** What `bridge` takes. */
70
+ interface BridgeOptions<S, T> {
71
+ /**
72
+ * Makes the library's store. On the server it runs once for each call of the bridge. In the
73
+ * browser it runs once, when the store gets its first subscriber, with the data that the server
74
+ * sent. It must be in a module that the browser can load.
75
+ */
76
+ create: (snapshot: T | undefined) => S;
77
+ /** The data of the store: plain data that the page state can send. */
78
+ get: (source: S) => T;
79
+ /** Calls `listener` when the store changes. Returns the function that stops it. */
80
+ subscribe: (source: S, listener: () => void) => () => void;
81
+ /** Writes the store. The browser never calls it when the page resumes. */
82
+ set?: (source: S, value: T) => void;
83
+ }
84
+ /** A store of `bridge()`: the contract of a store, and the store of the library. */
85
+ interface BridgedStore<S, T> extends StoreLike<T> {
86
+ /**
87
+ * The store of the library. In the browser it is undefined until the store has a subscriber,
88
+ * since the browser makes it at the first subscriber.
89
+ */
90
+ readonly source: S | undefined;
91
+ }
92
+ /** What `bridge` returns: makes a store, from the data of the server in the browser. */
93
+ interface Bridge<S, T> {
94
+ (snapshot?: T): BridgedStore<S, T>;
95
+ /** The options. */
96
+ b: BridgeOptions<S, T>;
97
+ /** The name that the compiler gave, `bridge:<file>#<export>`. */
98
+ i?: string;
99
+ }
100
+ type Loader = [load: (module: string) => Promise<unknown>, report: (error: unknown) => void];
101
+ /**
102
+ * Sets how the browser loads the module of a bridge, and where a failed load is reported. The
103
+ * core calls it when it starts.
104
+ */
105
+ declare function setBridgeLoader(...loader: Loader): void;
106
+ /**
107
+ * Makes a bridge. Keep its value in a module-level export, so the page state can name it:
108
+ * `export const bears = bridge({ … })`. The compiler gives it its name.
109
+ */
110
+ declare function bridge<S, T>(options: BridgeOptions<S, T>): Bridge<S, T>;
111
+ /** Gives a bridge its name, which the compiler writes at the end of its module. */
112
+ declare function nameBridge(made: Bridge<never, never>, id: string): void;
113
+ /** True for a store of `bridge()`, of any copy of the runtime. */
114
+ declare function isBridged(value: unknown): value is Bridged<unknown, unknown>;
115
+ /**
116
+ * Whether two values hold the same data: the same value, or arrays and plain objects whose items
117
+ * hold the same data. A store that the browser makes again holds a new object with the content
118
+ * the server sent, which needs no repair.
119
+ */
120
+ declare function sameData(a: unknown, b: unknown, open?: Map<object, Set<unknown>>): boolean;
121
+ /**
122
+ * The store that a bridge makes. On the server it holds the library's store from the start. In
123
+ * the browser the page state makes it with the name of the bridge and the data of the server, and
124
+ * it makes the library's store at its first subscriber.
125
+ */
126
+ declare class Bridged<S, T> implements BridgedStore<S, T> {
127
+ /** The mark of a bridged store, which `isBridged` reads in every copy of the runtime. */
128
+ k: "brumate.bridge";
129
+ /** The bridge. Undefined for a store of the page state until the module of the bridge loads. */
130
+ f?: Bridge<S, T>;
131
+ /** The name of the bridge, for a store of the page state. */
132
+ i?: string;
133
+ /** The library's store. */
134
+ s?: S;
135
+ /** True once the library's store exists. */
136
+ m: boolean;
137
+ /** The data that the server sent, until the library's store exists. */
138
+ v?: T;
139
+ /** Whether the bridge has `set`, as the server sent it, until the module loads. */
140
+ w?: boolean;
141
+ constructor(made: Bridge<S, T> | undefined, source: S | undefined, id?: string, snapshot?: T, writable?: boolean);
142
+ /**
143
+ * The library's store. In the browser it is undefined until the store has a subscriber and the
144
+ * module of the bridge has loaded.
145
+ */
146
+ get source(): S | undefined;
147
+ /** The name of the bridge. */
148
+ get id(): string | undefined;
149
+ /** The data of the store: what `get` reads, or what the server sent before the first subscriber. */
150
+ get(): T;
151
+ /**
152
+ * Calls `listener` with the data when it changes. The first subscriber makes the library's
153
+ * store in the browser. When the module of the bridge has not loaded, the subscription starts
154
+ * when it has.
155
+ */
156
+ subscribe(listener: (value: T) => void, ...seen: [value?: T]): () => void;
157
+ /** Writes the library's store, when the bridge has `set`. */
158
+ get set(): ((value: T) => void) | undefined;
159
+ /** Writes the library's store, after the module of the bridge has loaded. */
160
+ private write;
161
+ /** The bridge, once its module has loaded. */
162
+ private find;
163
+ /** Loads the module of the bridge. A failure goes to the core's error path; it gives false. */
164
+ private load;
165
+ private fail;
166
+ /** Makes the library's store once, with the data of the server. */
167
+ private make;
168
+ }
169
+ //#endregion
170
+ export { sameData as _, Store as a, setBridgeLoader as b, bridge as c, isBridged as d, isContext as f, nameBridge as g, isStore as h, BridgedStore as i, canSet as l, isForeignStore as m, BridgeOptions as n, StoreLike as o, isContract as p, Bridged as r, StoreOrigin as s, Bridge as t, findStored as u, sendStore as v, store as x, sentForeignStore as y };
171
+ //# sourceMappingURL=store-xlTzAM_r.d.ts.map
@@ -1,6 +1,8 @@
1
1
  import { n as MODIFIERS } from "./events-LgLdp1R7.js";
2
2
  import { S as rawHTML, g as isRaw, h as isInternal } from "./html-t1JpkODI.js";
3
- import { S as onValuePlugin, _ as isBinding, c as ElementReference, d as PARTS, o as CHILDREN_REF, v as isPropWrapper, w as stateValues, x as onSerialOptions, y as isSlotReference } from "./search-params-CM1RWR2P.js";
3
+ import { i as HAS_BRIDGES } from "./features-DwMYKBIC.js";
4
+ import { o as isBridged, u as isStore } from "./store-BIBAYZo-.js";
5
+ import { C as onSerialOptions, E as stateValues, b as isSlotReference, c as ElementReference, d as PARTS, o as CHILDREN_REF, v as isBinding, w as onValuePlugin, y as isPropWrapper } from "./search-params-Cy3uRphR.js";
4
6
  import { Feature, Stream, createPlugin, fromCrossJSON, toCrossJSONStream } from "seroval";
5
7
  import { AbortSignalPlugin, CustomEventPlugin, DOMExceptionPlugin, EventPlugin, FormDataPlugin, HeadersPlugin, ImageDataPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLPlugin, URLSearchParamsPlugin } from "seroval-plugins/web";
6
8
  //#region src/runtime/calls.ts
@@ -205,6 +207,31 @@ const ComponentPlugin = createPlugin({
205
207
  serialize: (node, ctx) => `$b.c(${ctx.serialize(node.c)})`,
206
208
  deserialize: (node, ctx) => stateValues.c(ctx.deserialize(node.c))
207
209
  });
210
+ /**
211
+ * A store of `brumate/store`: only its value crosses, and where `$toStore` made it, so the
212
+ * browser makes a new store that the provider writes. Readers subscribe as their scopes resume.
213
+ */
214
+ const StorePlugin = createPlugin({
215
+ tag: "brumate/store",
216
+ skipPlainObjects: true,
217
+ test: isStore,
218
+ parse: parsers((value) => {
219
+ const node = { a: value.v };
220
+ if (value.o) {
221
+ node.b = value.o[0];
222
+ node.c = value.o[1];
223
+ if (value.o[2] !== void 0) node.d = value.o[2];
224
+ }
225
+ return node;
226
+ }),
227
+ serialize: (node, ctx) => `$b.s(${serializeParts([
228
+ node.a,
229
+ node.b,
230
+ node.c,
231
+ node.d
232
+ ], ctx)})`,
233
+ deserialize: (node, ctx) => stateValues.s(ctx.deserialize(node.a), node.b === void 0 ? void 0 : ctx.deserialize(node.b), node.c === void 0 ? void 0 : ctx.deserialize(node.c), node.d === void 0 ? void 0 : ctx.deserialize(node.d))
234
+ });
208
235
  function isFile(value) {
209
236
  const file = value;
210
237
  return typeof file.name === "string" && typeof file.lastModified === "number";
@@ -282,6 +309,63 @@ const BodyRequestPlugin = {
282
309
  duplex: "half"
283
310
  })
284
311
  };
312
+ /**
313
+ * The error of a store of `bridge()` whose data the page state cannot send. It holds the store as
314
+ * `value`, so the server finds the context or the prop that sends it.
315
+ */
316
+ var BridgeDataError = class extends Error {
317
+ value;
318
+ constructor(value, cause) {
319
+ super(`get of the bridge "${value.id}" gives data that cannot be sent`, { cause });
320
+ this.value = value;
321
+ }
322
+ };
323
+ /** Reads the data of a bridged store for the page state. */
324
+ function bridgeData(value) {
325
+ if (value.id === void 0) throw new BridgeDataError(value, /* @__PURE__ */ new Error("The bridge has no name. Keep bridge() in a module-level export of a script file, as \"export const name = bridge({ … })\", so the compiler names it"));
326
+ return value.get();
327
+ }
328
+ /** The node of a bridged store: its data and its name. An error of the data names the store. */
329
+ function bridgeNode(value, parse) {
330
+ try {
331
+ return {
332
+ a: parse(bridgeData(value)),
333
+ b: parse(value.id),
334
+ c: parse(value.set ? 1 : 0)
335
+ };
336
+ } catch (error) {
337
+ throw error instanceof BridgeDataError ? error : new BridgeDataError(value, error);
338
+ }
339
+ }
340
+ /**
341
+ * A store of `bridge()`: only the data that `get` reads crosses, and the name of the bridge, so
342
+ * the browser makes the library's store again from the data at the first subscriber.
343
+ */
344
+ function makeBridgePlugin() {
345
+ return createPlugin({
346
+ tag: "brumate/bridge",
347
+ skipPlainObjects: true,
348
+ test: isBridged,
349
+ parse: {
350
+ sync: (value, ctx) => bridgeNode(value, (data) => ctx.parse(data)),
351
+ stream: (value, ctx) => bridgeNode(value, (data) => ctx.parse(data)),
352
+ async: async (value, ctx) => {
353
+ try {
354
+ const data = bridgeData(value);
355
+ return {
356
+ a: await ctx.parse(data),
357
+ b: await ctx.parse(value.id),
358
+ c: await ctx.parse(value.set ? 1 : 0)
359
+ };
360
+ } catch (error) {
361
+ throw error instanceof BridgeDataError ? error : new BridgeDataError(value, error);
362
+ }
363
+ }
364
+ },
365
+ serialize: (node, ctx) => `$b.g(${ctx.serialize(node.a)},${ctx.serialize(node.b)},${ctx.serialize(node.c)})`,
366
+ deserialize: (node, ctx) => stateValues.g(ctx.deserialize(node.a), ctx.deserialize(node.b), ctx.deserialize(node.c))
367
+ });
368
+ }
285
369
  /** The plugins after the plugin of uploads, which a call makes for its files. */
286
370
  const valuePlugins = [
287
371
  ElementPlugin,
@@ -295,6 +379,8 @@ const valuePlugins = [
295
379
  ChildrenPlugin,
296
380
  RawPlugin,
297
381
  ComponentPlugin,
382
+ StorePlugin,
383
+ ...HAS_BRIDGES ? [makeBridgePlugin()] : [],
298
384
  SignalPlugin,
299
385
  CustomEventPlugin,
300
386
  DOMExceptionPlugin,
@@ -529,6 +615,6 @@ async function fromChunkStream(stream) {
529
615
  return value;
530
616
  }
531
617
  //#endregion
532
- export { plugins as a, functionReferences as c, callPlugins as i, fromChunkStream as n, serialOptions as o, toChunkStream as r, syncPlugins as s, STREAM_TYPE as t };
618
+ export { callPlugins as a, syncPlugins as c, BridgeDataError as i, functionReferences as l, fromChunkStream as n, plugins as o, toChunkStream as r, serialOptions as s, STREAM_TYPE as t };
533
619
 
534
- //# sourceMappingURL=stream-DRjx0keF.js.map
620
+ //# sourceMappingURL=stream-BBnArf_5.js.map