@manyducks.co/dolla 2.0.0-alpha.3 → 2.0.0-alpha.31

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 (71) hide show
  1. package/README.md +31 -951
  2. package/dist/core/batch.d.ts +17 -0
  3. package/dist/core/context.d.ts +94 -0
  4. package/dist/core/dolla.d.ts +161 -0
  5. package/dist/core/markup.d.ts +91 -0
  6. package/dist/core/nodes/dom.d.ts +13 -0
  7. package/dist/core/nodes/html.d.ts +39 -0
  8. package/dist/core/nodes/observer.d.ts +30 -0
  9. package/dist/core/nodes/outlet.d.ts +19 -0
  10. package/dist/core/nodes/portal.d.ts +22 -0
  11. package/dist/core/nodes/repeat.d.ts +36 -0
  12. package/dist/core/nodes/view.d.ts +92 -0
  13. package/dist/core/ref.d.ts +29 -0
  14. package/dist/core/state.d.ts +126 -0
  15. package/dist/core/stats.d.ts +31 -0
  16. package/dist/core/store.d.ts +62 -0
  17. package/dist/core/symbols.d.ts +7 -0
  18. package/dist/index.d.ts +18 -11
  19. package/dist/index.js +1157 -1159
  20. package/dist/index.js.map +1 -1
  21. package/dist/jsx-dev-runtime.d.ts +2 -2
  22. package/dist/jsx-dev-runtime.js +2 -2
  23. package/dist/jsx-dev-runtime.js.map +1 -1
  24. package/dist/jsx-runtime.d.ts +3 -3
  25. package/dist/jsx-runtime.js +2 -2
  26. package/dist/jsx-runtime.js.map +1 -1
  27. package/dist/markup-D1i09ddt.js +1563 -0
  28. package/dist/markup-D1i09ddt.js.map +1 -0
  29. package/dist/modules/http.d.ts +5 -5
  30. package/dist/modules/i18n.d.ts +129 -0
  31. package/dist/modules/router.d.ts +37 -48
  32. package/dist/typeChecking.d.ts +2 -2
  33. package/dist/types.d.ts +12 -13
  34. package/dist/utils.d.ts +14 -2
  35. package/dist/views/default-crash-view.d.ts +1 -1
  36. package/dist/views/passthrough.d.ts +2 -2
  37. package/docs/http.md +29 -0
  38. package/docs/i18n.md +38 -0
  39. package/docs/index.md +10 -0
  40. package/docs/router.md +77 -0
  41. package/docs/setup.md +31 -0
  42. package/docs/state.md +141 -0
  43. package/docs/stores.md +62 -0
  44. package/docs/views.md +308 -0
  45. package/index.d.ts +2 -2
  46. package/notes/TODO.md +6 -0
  47. package/notes/context-vars.md +21 -0
  48. package/notes/readme-scratch.md +222 -0
  49. package/notes/route-middleware.md +42 -0
  50. package/notes/scratch.md +195 -7
  51. package/notes/stores.md +73 -0
  52. package/package.json +12 -14
  53. package/tests/{signals.test.js → state.test.js} +6 -6
  54. package/vite.config.js +0 -10
  55. package/dist/markup.d.ts +0 -100
  56. package/dist/modules/dolla.d.ts +0 -111
  57. package/dist/modules/language.d.ts +0 -41
  58. package/dist/modules/render.d.ts +0 -17
  59. package/dist/nodes/cond.d.ts +0 -26
  60. package/dist/nodes/html.d.ts +0 -26
  61. package/dist/nodes/observer.d.ts +0 -29
  62. package/dist/nodes/outlet.d.ts +0 -22
  63. package/dist/nodes/portal.d.ts +0 -19
  64. package/dist/nodes/repeat.d.ts +0 -34
  65. package/dist/nodes/text.d.ts +0 -19
  66. package/dist/passthrough-DrtCifRF.js +0 -1228
  67. package/dist/passthrough-DrtCifRF.js.map +0 -1
  68. package/dist/signals.d.ts +0 -101
  69. package/dist/view.d.ts +0 -50
  70. /package/dist/{routing.d.ts → modules/router.utils.d.ts} +0 -0
  71. /package/dist/{routing.test.d.ts → modules/router.utils.test.d.ts} +0 -0
package/dist/index.js CHANGED
@@ -1,1268 +1,1252 @@
1
- var pt = Object.defineProperty;
2
- var Ze = (n) => {
3
- throw TypeError(n);
1
+ var ue = Object.defineProperty;
2
+ var Gt = (o) => {
3
+ throw TypeError(o);
4
4
  };
5
- var mt = (n, e, t) => e in n ? pt(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
6
- var k = (n, e, t) => mt(n, typeof e != "symbol" ? e + "" : e, t), Fe = (n, e, t) => e.has(n) || Ze("Cannot " + t);
7
- var a = (n, e, t) => (Fe(n, e, "read from private field"), t ? t.call(n) : e.get(n)), u = (n, e, t) => e.has(n) ? Ze("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(n) : e.set(n, t), m = (n, e, t, r) => (Fe(n, e, "write to private field"), r ? r.call(n, t) : e.set(n, t), t), P = (n, e, t) => (Fe(n, e, "access private method"), t);
8
- var et = (n, e, t, r) => ({
9
- set _(s) {
10
- m(n, e, s, t);
5
+ var fe = (o, t, e) => t in o ? ue(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
6
+ var d = (o, t, e) => fe(o, typeof t != "symbol" ? t + "" : t, e), Pt = (o, t, e) => t.has(o) || Gt("Cannot " + e);
7
+ var i = (o, t, e) => (Pt(o, t, "read from private field"), e ? e.call(o) : t.get(o)), l = (o, t, e) => t.has(o) ? Gt("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(o) : t.set(o, e), m = (o, t, e, s) => (Pt(o, t, "write to private field"), s ? s.call(o, e) : t.set(o, e), e), f = (o, t, e) => (Pt(o, t, "access private method"), e);
8
+ var et = (o, t, e, s) => ({
9
+ set _(n) {
10
+ m(o, t, n, e);
11
11
  },
12
12
  get _() {
13
- return a(n, e, r);
13
+ return i(o, t, s);
14
14
  }
15
15
  });
16
- import { h as tt, c as yt, i as Ie, s as Ge, a as wt, d as Be, b as bt, e as Xe, f as D, g as K, j as lt, k as vt, w as ht, P as ut, l as rt, m as $t, t as Et, n as kt, o as Lt, p as Pt, q as nt, r as at, u as St, v as Oe, x as Te, y as Rt, z as xt, A as Mt } from "./passthrough-DrtCifRF.js";
17
- import { B as rr, C as nr, D as ar } from "./passthrough-DrtCifRF.js";
18
- function Nt(n) {
19
- return tt`
20
- <div
21
- style=${{
22
- backgroundColor: "#880000",
23
- color: "#fff",
24
- padding: "2rem",
25
- position: "fixed",
26
- inset: 0,
27
- fontSize: "20px"
28
- }}
29
- >
30
- <h1 style=${{ marginBottom: "0.5rem" }}>The app has crashed</h1>
31
- <p style=${{ marginBottom: "0.25rem" }}>
32
- <span style=${{ fontFamily: "monospace" }}>${n.loggerName}</span>
33
- ${yt(n.uid, tt`<span style=${{ fontFamily: "monospace", opacity: 0.5 }}> [uid: ${n.uid}]</span>`)}
34
- ${" "}says:
35
- </p>
36
- <blockquote
37
- style=${{
38
- backgroundColor: "#991111",
39
- padding: "0.25em",
40
- borderRadius: "6px",
41
- fontFamily: "monospace",
42
- marginBottom: "1rem"
43
- }}
44
- >
45
- <span
46
- style=${{
47
- display: "inline-block",
48
- backgroundColor: "red",
49
- padding: "0.1em 0.4em",
50
- marginRight: "0.5em",
51
- borderRadius: "4px",
52
- fontSize: "0.9em",
53
- fontWeight: "bold"
54
- }}
55
- >
56
- ${n.error.name}
57
- </span>
58
- ${n.error.message}
59
- </blockquote>
60
-
61
- <p>Please see the browser console for details.</p>
62
- </div>
63
- `;
64
- }
65
- var J, _e, N, q;
66
- class Ot {
67
- constructor() {
68
- u(this, N);
69
- u(this, J, []);
70
- u(this, _e, Tt());
71
- }
72
- /**
73
- * Adds a new middleware that will apply to subsequent requests.
74
- * Returns a function to remove this middleware.
75
- *
76
- * @param middleware - A middleware function that will intercept requests.
77
- */
78
- use(e) {
79
- return a(this, J).push(e), () => {
80
- a(this, J).splice(a(this, J).indexOf(e), 1);
81
- };
82
- }
83
- async get(e, t) {
84
- return P(this, N, q).call(this, "get", e, t);
85
- }
86
- async put(e, t) {
87
- return P(this, N, q).call(this, "put", e, t);
88
- }
89
- async patch(e, t) {
90
- return P(this, N, q).call(this, "patch", e, t);
91
- }
92
- async post(e, t) {
93
- return P(this, N, q).call(this, "post", e, t);
94
- }
95
- async delete(e, t) {
96
- return P(this, N, q).call(this, "delete", e, t);
97
- }
98
- async head(e, t) {
99
- return P(this, N, q).call(this, "head", e, t);
100
- }
101
- async options(e, t) {
102
- return P(this, N, q).call(this, "options", e, t);
103
- }
104
- async trace(e, t) {
105
- return P(this, N, q).call(this, "trace", e, t);
106
- }
107
- }
108
- J = new WeakMap(), _e = new WeakMap(), N = new WeakSet(), q = async function(e, t, r) {
109
- return jt({
110
- ...r,
111
- method: e,
112
- uri: t,
113
- middleware: a(this, J),
114
- fetch: a(this, _e)
115
- });
116
- };
117
- function Tt() {
118
- if (typeof window < "u" && window.fetch)
119
- return window.fetch.bind(window);
120
- if (typeof global < "u" && global.fetch)
121
- return global.fetch.bind(global);
122
- throw new Error("Running in neither browser nor node. Please run this app in one of the supported environments.");
123
- }
124
- class At extends Error {
125
- constructor(t) {
126
- const { status: r, statusText: s, method: i, uri: o } = t, f = `${r} ${s}: Request failed (${i.toUpperCase()} ${o})`;
127
- super(f);
128
- k(this, "response");
129
- this.response = t;
130
- }
131
- }
132
- async function jt(n) {
133
- const { headers: e, query: t, fetch: r, middleware: s } = n, i = {
134
- method: n.method,
135
- uri: n.uri,
136
- get sameOrigin() {
137
- return !i.uri.startsWith("http");
138
- },
139
- query: new URLSearchParams(),
140
- headers: new Headers(),
141
- body: n.body
142
- };
143
- if (e)
144
- if (e instanceof Map || e instanceof Headers)
145
- e.forEach((c, y) => {
146
- i.headers.set(y, c);
147
- });
148
- else if (e != null && typeof e == "object" && !Array.isArray(e))
149
- for (const c in e) {
150
- const y = e[c];
151
- y instanceof Date ? i.headers.set(c, y.toISOString()) : y != null && i.headers.set(c, String(y));
152
- }
153
- else
154
- throw new TypeError(`Unknown headers type. Got: ${e}`);
155
- if (t)
156
- if (t instanceof Map || t instanceof URLSearchParams)
157
- t.forEach((c, y) => {
158
- i.query.set(y, c);
159
- });
160
- else if (t != null && typeof t == "object" && !Array.isArray(t))
161
- for (const c in t) {
162
- const y = t[c];
163
- y instanceof Date ? i.query.set(c, y.toISOString()) : y != null && i.query.set(c, String(y));
164
- }
165
- else
166
- throw new TypeError(`Unknown query params type. Got: ${t}`);
167
- let o;
168
- const f = async () => {
169
- const c = i.query.toString(), y = c.length > 0 ? i.uri + "?" + c : i.uri;
170
- let w;
171
- !i.headers.has("content-type") && Ie(i.body) ? (i.headers.set("content-type", "application/json"), w = JSON.stringify(i.body)) : w = i.body;
172
- const p = await r(y, {
173
- method: i.method,
174
- headers: i.headers,
175
- body: w
176
- }), d = Object.fromEntries(p.headers.entries()), b = d["content-type"];
177
- let $;
178
- b != null && b.includes("application/json") ? $ = await p.json() : b != null && b.includes("application/x-www-form-urlencoded") ? $ = await p.formData() : $ = await p.text(), o = {
179
- method: i.method,
180
- uri: i.uri,
181
- status: p.status,
182
- statusText: p.statusText,
183
- headers: d,
184
- body: $
185
- };
186
- };
187
- if (s.length > 0) {
188
- const c = (y = 0) => {
189
- const w = s[y], p = s[y + 1] ? c(y + 1) : f;
190
- return async () => w(i, async () => (await p(), o));
191
- };
192
- await c()();
193
- } else
194
- await f();
195
- if (o.status < 200 || o.status >= 400)
196
- throw new At(o);
197
- return o;
198
- }
199
- class Ct {
200
- constructor(e, t) {
201
- k(this, "dolla");
202
- k(this, "config");
203
- k(this, "strings");
204
- this.config = e, this.dolla = t;
205
- }
206
- async load() {
207
- if (this.strings == null) {
208
- if (Xe(this.config.fetch)) {
209
- const e = await this.config.fetch();
210
- if (Ie(e))
211
- this.strings = e;
212
- else
213
- throw new Error(`Fetch function did not return an object of language strings: ${e}`);
214
- } else if (D(this.config.path)) {
215
- const e = await this.dolla.http.get(this.config.path);
216
- if (e.status >= 200 && e.status < 300)
217
- if (Ie(e.body))
218
- this.strings = e.body;
219
- else
220
- throw new Error(
221
- `Language path '${this.config.path}' did not return an object of language strings: ${e.body}`
222
- );
223
- else
224
- throw new Error("HTTP request failed.");
225
- }
226
- }
227
- if (this.strings == null)
228
- throw new Error("Language could not be loaded.");
229
- return this.strings;
230
- }
231
- }
232
- var ye, Y, x, ie, we, be, se, ve, We, ft;
233
- class qt {
234
- constructor(e) {
235
- u(this, We);
236
- u(this, ye);
237
- u(this, Y);
238
- u(this, x, /* @__PURE__ */ new Map());
239
- u(this, ie, []);
240
- u(this, we, "auto");
241
- k(this, "$current");
242
- u(this, be);
243
- u(this, se);
244
- u(this, ve);
245
- m(this, ye, e), m(this, Y, e.createLogger("dolla/language"));
246
- const [t, r] = K(), [s, i] = K();
247
- this.$current = t, m(this, be, r), m(this, se, s), m(this, ve, i), e.beforeMount(async () => {
248
- a(this, x).size > 0 && await this.setLanguage(a(this, we));
249
- });
250
- }
251
- get supportedLanguages() {
252
- return [...a(this, x).keys()];
253
- }
254
- setup(e) {
255
- if (e.languages.forEach((t) => {
256
- a(this, x).set(t.name, new Ct(t, a(this, ye)));
257
- }), e.initialLanguage && e.initialLanguage !== "auto") {
258
- if (!e.languages.some((r) => r.name === e.initialLanguage))
259
- throw new Error(`Initial language '${e.initialLanguage}' has no registered translation.`);
260
- m(this, we, e.initialLanguage);
261
- }
262
- a(this, Y).info(
263
- `${a(this, x).size} language${a(this, x).size === 1 ? "" : "s"} supported: '${[...a(this, x).keys()].join("', '")}'`
264
- );
265
- }
266
- async setLanguage(e) {
267
- var s;
268
- let t;
269
- if (e === "auto") {
270
- let i = [];
271
- if (typeof navigator == "object") {
272
- const o = navigator;
273
- ((s = o.languages) == null ? void 0 : s.length) > 0 ? i.push(...o.languages) : o.language ? i.push(o.language) : o.browserLanguage ? i.push(o.browserLanguage) : o.userLanguage && i.push(o.userLanguage);
274
- }
275
- for (const o of i)
276
- a(this, x).has(o) && (t = o);
277
- } else
278
- a(this, x).has(e) && (t = e);
279
- if (t == null) {
280
- const i = a(this, x).keys().next().value;
281
- i && (t = i);
282
- }
283
- if (!t || !a(this, x).has(t))
284
- throw new Error(`Language '${e}' is not configured for this app.`);
285
- const r = a(this, x).get(t);
286
- try {
287
- const i = await r.load();
288
- a(this, ve).call(this, i), a(this, be).call(this, t), a(this, Y).info("set language to " + t);
289
- } catch (i) {
290
- i instanceof Error && a(this, Y).crash(i);
291
- }
292
- }
293
- /**
294
- * Returns a Signal containing the value at `key`.
295
-
296
- * @param key - Key to the translated value.
297
- * @param values - A map of {{placeholder}} names and the values to replace them with.
298
- */
299
- t(e, t) {
300
- if (this === void 0)
301
- throw new Error(
302
- `The 't' function cannot be destructured. If you need a standalone version you can import it like so: 'import { t } from "@manyducks.co/dolla"'`
303
- );
304
- if (!this.$current.get())
305
- return Ut;
306
- const r = P(this, We, ft).call(this, e, t);
307
- if (r)
308
- return r;
309
- if (t) {
310
- const i = {};
311
- for (const [f, c] of Object.entries(t))
312
- wt(c) && (i[f] = c);
313
- const o = Object.entries(i);
314
- if (o.length > 0) {
315
- const f = o.map((y) => y[1]), c = Be([a(this, se), ...f], (y, ...w) => {
316
- const p = w.map(($, j) => o[j]), d = {
317
- ...t
318
- };
319
- for (let $ = 0; $ < p.length; $++) {
320
- const j = p[$][0];
321
- d[j] = w[$];
322
- }
323
- const b = st(y, e) || `[NO TRANSLATION: ${e}]`;
324
- return it(b, d);
325
- });
326
- return a(this, ie).push([e, t, c]), c;
327
- }
328
- }
329
- const s = Be([a(this, se)], (i) => {
330
- let o = st(i, e) || `[NO TRANSLATION: ${e}]`;
331
- return t && (o = it(o, t)), o;
332
- });
333
- return a(this, ie).push([e, t, s]), s;
334
- }
335
- }
336
- ye = new WeakMap(), Y = new WeakMap(), x = new WeakMap(), ie = new WeakMap(), we = new WeakMap(), be = new WeakMap(), se = new WeakMap(), ve = new WeakMap(), We = new WeakSet(), ft = function(e, t) {
337
- for (const r of a(this, ie))
338
- if (r[0] === e && bt(r[1], t))
339
- return r[2];
340
- };
341
- const Ut = Ge("[NO LANGUAGE SET]");
342
- function it(n, e) {
343
- for (const t in e)
344
- n = n.replace(`{{${t}}}`, String(e[t]));
345
- return n;
16
+ import { a as Zt, b as de, i as nt, I as te, c as me, d as At, e as x, s as Ht, f as v, g as tt, P as ee, h as we, t as Bt, j as zt, k as pe, l as Kt, p as ge, m as Qt, S as ye, n as be, o as $e, q as Ee, r as ve, u as ke, C as Re, v as Se, w as Tt, x as _e, y as Jt, z as Le, A as vt, B as kt, V as Me, D as xe, E as Fe } from "./markup-D1i09ddt.js";
17
+ import { F as hs, K as cs, H as us, J as fs, G as ds } from "./markup-D1i09ddt.js";
18
+ function rt(o) {
19
+ return Zt(o, "Expected `path` to be a string. Got type: %t, value: %v"), o.split("/").map((t) => t.trim()).filter((t) => t !== "");
346
20
  }
347
- function st(n, e) {
348
- const t = String(e).split(/[\.\[\]]/).filter((s) => s.trim() !== "");
349
- let r = n;
350
- for (; t.length > 0; ) {
351
- const s = t.shift();
352
- r != null ? r = r[s] : r = void 0;
353
- }
354
- return r;
355
- }
356
- var X, $e, H, _, oe, U, ce, Ae;
357
- class Vt {
358
- constructor(e) {
359
- u(this, ce);
360
- u(this, X);
361
- u(this, $e);
362
- // Keyed updates ensure only the most recent callback queued with a certain key
363
- // will be called, keeping DOM operations to a minimum.
364
- u(this, H, /* @__PURE__ */ new Map());
365
- // All unkeyed updates are run on every batch.
366
- u(this, _, []);
367
- u(this, oe, []);
368
- u(this, U, !1);
369
- m(this, X, e), m(this, $e, e.createLogger("dolla/render"));
370
- }
371
- /**
372
- * Queues a callback to run in the next render batch.
373
- * Running your DOM mutations in update callbacks reduces layout thrashing.
374
- * Returns a Promise that resolves once the callback has run.
375
- */
376
- update(e, t) {
377
- return new Promise((r) => {
378
- t ? a(this, H).set(t, () => {
379
- e(), r();
380
- }) : a(this, _).push(() => {
381
- e(), r();
382
- }), !a(this, U) && a(this, X).isMounted && (m(this, U, !0), P(this, ce, Ae).call(this));
383
- });
384
- }
385
- /**
386
- * Queues a callback that reads DOM information to run after the next render batch,
387
- * ensuring all writes have been performed before reading.
388
- * Returns a Promise that resolves once the callback has run.
389
- */
390
- async read(e) {
391
- return new Promise((t) => {
392
- a(this, oe).push(() => {
393
- e(), t();
394
- }), !a(this, U) && a(this, X).isMounted && (m(this, U, !0), P(this, ce, Ae).call(this));
395
- });
396
- }
397
- }
398
- X = new WeakMap(), $e = new WeakMap(), H = new WeakMap(), _ = new WeakMap(), oe = new WeakMap(), U = new WeakMap(), ce = new WeakSet(), Ae = function() {
399
- const e = a(this, H).size + a(this, _).length;
400
- if ((!a(this, X).isMounted || e === 0) && m(this, U, !1), !a(this, U)) {
401
- for (const t of a(this, oe))
402
- t();
403
- m(this, oe, []);
404
- return;
405
- }
406
- requestAnimationFrame(() => {
407
- a(this, $e).info(`Batching ${a(this, H).size + a(this, _).length} queued DOM update(s).`);
408
- for (const t of a(this, H).values())
409
- t();
410
- a(this, H).clear();
411
- for (const t of a(this, _))
412
- t();
413
- m(this, _, []), P(this, ce, Ae).call(this);
414
- });
415
- };
416
- function fe() {
417
- return fe = Object.assign || function(n) {
418
- for (var e = 1; e < arguments.length; e++) {
419
- var t = arguments[e];
420
- for (var r in t)
421
- Object.prototype.hasOwnProperty.call(t, r) && (n[r] = t[r]);
422
- }
423
- return n;
424
- }, fe.apply(this, arguments);
425
- }
426
- var A;
427
- (function(n) {
428
- n.Pop = "POP", n.Push = "PUSH", n.Replace = "REPLACE";
429
- })(A || (A = {}));
430
- var Ue = process.env.NODE_ENV !== "production" ? function(n) {
431
- return Object.freeze(n);
432
- } : function(n) {
433
- return n;
434
- };
435
- function je(n, e) {
436
- if (!n) {
437
- typeof console < "u" && console.warn(e);
438
- try {
439
- throw new Error(e);
440
- } catch {
441
- }
442
- }
443
- }
444
- var Ve = "beforeunload", Dt = "hashchange", dt = "popstate";
445
- function Ht(n) {
446
- n === void 0 && (n = {});
447
- var e = n, t = e.window, r = t === void 0 ? document.defaultView : t, s = r.history;
448
- function i() {
449
- var g = r.location, l = g.pathname, h = g.search, v = g.hash, E = s.state || {};
450
- return [E.idx, Ue({
451
- pathname: l,
452
- search: h,
453
- hash: v,
454
- state: E.usr || null,
455
- key: E.key || "default"
456
- })];
457
- }
458
- var o = null;
459
- function f() {
460
- if (o)
461
- b.call(o), o = null;
462
- else {
463
- var g = A.Pop, l = i(), h = l[0], v = l[1];
464
- if (b.length)
465
- if (h != null) {
466
- var E = w - h;
467
- E && (o = {
468
- action: g,
469
- location: v,
470
- retry: function() {
471
- V(E * -1);
472
- }
473
- }, V(E));
474
- } else
475
- process.env.NODE_ENV !== "production" && je(
476
- !1,
477
- // TODO: Write up a doc that explains our blocking strategy in
478
- // detail and link to it here so people can understand better what
479
- // is going on and how to avoid it.
480
- "You are trying to block a POP navigation to a location that was not created by the history library. The block will fail silently in production, but in general you should do all navigation with the history library (instead of using window.history.pushState directly) to avoid this situation."
481
- );
482
- else
483
- re(g);
484
- }
485
- }
486
- r.addEventListener(dt, f);
487
- var c = A.Pop, y = i(), w = y[0], p = y[1], d = He(), b = He();
488
- w == null && (w = 0, s.replaceState(fe({}, s.state, {
489
- idx: w
490
- }), ""));
491
- function $(g) {
492
- return typeof g == "string" ? g : Ce(g);
493
- }
494
- function j(g, l) {
495
- return l === void 0 && (l = null), Ue(fe({
496
- pathname: p.pathname,
497
- hash: "",
498
- search: ""
499
- }, typeof g == "string" ? Qe(g) : g, {
500
- state: l,
501
- key: gt()
502
- }));
503
- }
504
- function de(g, l) {
505
- return [{
506
- usr: g.state,
507
- key: g.key,
508
- idx: l
509
- }, $(g)];
510
- }
511
- function ge(g, l, h) {
512
- return !b.length || (b.call({
513
- action: g,
514
- location: l,
515
- retry: h
516
- }), !1);
517
- }
518
- function re(g) {
519
- c = g;
520
- var l = i();
521
- w = l[0], p = l[1], d.call({
522
- action: c,
523
- location: p
524
- });
525
- }
526
- function ne(g, l) {
527
- var h = A.Push, v = j(g, l);
528
- function E() {
529
- ne(g, l);
530
- }
531
- if (ge(h, v, E)) {
532
- var S = de(v, w + 1), M = S[0], O = S[1];
533
- try {
534
- s.pushState(M, "", O);
535
- } catch {
536
- r.location.assign(O);
537
- }
538
- re(h);
539
- }
540
- }
541
- function pe(g, l) {
542
- var h = A.Replace, v = j(g, l);
543
- function E() {
544
- pe(g, l);
545
- }
546
- if (ge(h, v, E)) {
547
- var S = de(v, w), M = S[0], O = S[1];
548
- s.replaceState(M, "", O), re(h);
549
- }
550
- }
551
- function V(g) {
552
- s.go(g);
553
- }
554
- var Q = {
555
- get action() {
556
- return c;
557
- },
558
- get location() {
559
- return p;
560
- },
561
- createHref: $,
562
- push: ne,
563
- replace: pe,
564
- go: V,
565
- back: function() {
566
- V(-1);
567
- },
568
- forward: function() {
569
- V(1);
570
- },
571
- listen: function(l) {
572
- return d.push(l);
573
- },
574
- block: function(l) {
575
- var h = b.push(l);
576
- return b.length === 1 && r.addEventListener(Ve, De), function() {
577
- h(), b.length || r.removeEventListener(Ve, De);
578
- };
579
- }
580
- };
581
- return Q;
582
- }
583
- function _t(n) {
584
- n === void 0 && (n = {});
585
- var e = n, t = e.window, r = t === void 0 ? document.defaultView : t, s = r.history;
586
- function i() {
587
- var l = Qe(r.location.hash.substr(1)), h = l.pathname, v = h === void 0 ? "/" : h, E = l.search, S = E === void 0 ? "" : E, M = l.hash, O = M === void 0 ? "" : M, C = s.state || {};
588
- return [C.idx, Ue({
589
- pathname: v,
590
- search: S,
591
- hash: O,
592
- state: C.usr || null,
593
- key: C.key || "default"
594
- })];
595
- }
596
- var o = null;
597
- function f() {
598
- if (o)
599
- b.call(o), o = null;
600
- else {
601
- var l = A.Pop, h = i(), v = h[0], E = h[1];
602
- if (b.length)
603
- if (v != null) {
604
- var S = w - v;
605
- S && (o = {
606
- action: l,
607
- location: E,
608
- retry: function() {
609
- Q(S * -1);
610
- }
611
- }, Q(S));
612
- } else
613
- process.env.NODE_ENV !== "production" && je(
614
- !1,
615
- // TODO: Write up a doc that explains our blocking strategy in
616
- // detail and link to it here so people can understand better
617
- // what is going on and how to avoid it.
618
- "You are trying to block a POP navigation to a location that was not created by the history library. The block will fail silently in production, but in general you should do all navigation with the history library (instead of using window.history.pushState directly) to avoid this situation."
619
- );
620
- else
621
- ne(l);
622
- }
623
- }
624
- r.addEventListener(dt, f), r.addEventListener(Dt, function() {
625
- var l = i(), h = l[1];
626
- Ce(h) !== Ce(p) && f();
627
- });
628
- var c = A.Pop, y = i(), w = y[0], p = y[1], d = He(), b = He();
629
- w == null && (w = 0, s.replaceState(fe({}, s.state, {
630
- idx: w
631
- }), ""));
632
- function $() {
633
- var l = document.querySelector("base"), h = "";
634
- if (l && l.getAttribute("href")) {
635
- var v = r.location.href, E = v.indexOf("#");
636
- h = E === -1 ? v : v.slice(0, E);
637
- }
638
- return h;
639
- }
640
- function j(l) {
641
- return $() + "#" + (typeof l == "string" ? l : Ce(l));
642
- }
643
- function de(l, h) {
644
- return h === void 0 && (h = null), Ue(fe({
645
- pathname: p.pathname,
646
- hash: "",
647
- search: ""
648
- }, typeof l == "string" ? Qe(l) : l, {
649
- state: h,
650
- key: gt()
651
- }));
652
- }
653
- function ge(l, h) {
654
- return [{
655
- usr: l.state,
656
- key: l.key,
657
- idx: h
658
- }, j(l)];
659
- }
660
- function re(l, h, v) {
661
- return !b.length || (b.call({
662
- action: l,
663
- location: h,
664
- retry: v
665
- }), !1);
666
- }
667
- function ne(l) {
668
- c = l;
669
- var h = i();
670
- w = h[0], p = h[1], d.call({
671
- action: c,
672
- location: p
673
- });
674
- }
675
- function pe(l, h) {
676
- var v = A.Push, E = de(l, h);
677
- function S() {
678
- pe(l, h);
679
- }
680
- if (process.env.NODE_ENV !== "production" && je(E.pathname.charAt(0) === "/", "Relative pathnames are not supported in hash history.push(" + JSON.stringify(l) + ")"), re(v, E, S)) {
681
- var M = ge(E, w + 1), O = M[0], C = M[1];
682
- try {
683
- s.pushState(O, "", C);
684
- } catch {
685
- r.location.assign(C);
686
- }
687
- ne(v);
688
- }
689
- }
690
- function V(l, h) {
691
- var v = A.Replace, E = de(l, h);
692
- function S() {
693
- V(l, h);
694
- }
695
- if (process.env.NODE_ENV !== "production" && je(E.pathname.charAt(0) === "/", "Relative pathnames are not supported in hash history.replace(" + JSON.stringify(l) + ")"), re(v, E, S)) {
696
- var M = ge(E, w), O = M[0], C = M[1];
697
- s.replaceState(O, "", C), ne(v);
698
- }
699
- }
700
- function Q(l) {
701
- s.go(l);
702
- }
703
- var g = {
704
- get action() {
705
- return c;
706
- },
707
- get location() {
708
- return p;
709
- },
710
- createHref: j,
711
- push: pe,
712
- replace: V,
713
- go: Q,
714
- back: function() {
715
- Q(-1);
716
- },
717
- forward: function() {
718
- Q(1);
719
- },
720
- listen: function(h) {
721
- return d.push(h);
722
- },
723
- block: function(h) {
724
- var v = b.push(h);
725
- return b.length === 1 && r.addEventListener(Ve, De), function() {
726
- v(), b.length || r.removeEventListener(Ve, De);
727
- };
728
- }
729
- };
730
- return g;
731
- }
732
- function De(n) {
733
- n.preventDefault(), n.returnValue = "";
734
- }
735
- function He() {
736
- var n = [];
737
- return {
738
- get length() {
739
- return n.length;
740
- },
741
- push: function(t) {
742
- return n.push(t), function() {
743
- n = n.filter(function(r) {
744
- return r !== t;
745
- });
746
- };
747
- },
748
- call: function(t) {
749
- n.forEach(function(r) {
750
- return r && r(t);
751
- });
752
- }
753
- };
754
- }
755
- function gt() {
756
- return Math.random().toString(36).substr(2, 8);
757
- }
758
- function Ce(n) {
759
- var e = n.pathname, t = e === void 0 ? "/" : e, r = n.search, s = r === void 0 ? "" : r, i = n.hash, o = i === void 0 ? "" : i;
760
- return s && s !== "?" && (t += s.charAt(0) === "?" ? s : "?" + s), o && o !== "#" && (t += o.charAt(0) === "#" ? o : "#" + o), t;
761
- }
762
- function Qe(n) {
763
- var e = {};
764
- if (n) {
765
- var t = n.indexOf("#");
766
- t >= 0 && (e.hash = n.substr(t), n = n.substr(0, t));
767
- var r = n.indexOf("?");
768
- r >= 0 && (e.search = n.substr(r), n = n.substr(0, r)), n && (e.pathname = n);
769
- }
770
- return e;
771
- }
772
- function me(n) {
773
- return lt(n, "Expected `path` to be a string. Got type: %t, value: %v"), n.split("/").map((e) => e.trim()).filter((e) => e !== "");
774
- }
775
- function ae(n) {
776
- var t;
777
- vt(
778
- (r) => Xe(r == null ? void 0 : r.toString),
779
- n,
21
+ function z(o) {
22
+ var e;
23
+ de(
24
+ (s) => nt(s == null ? void 0 : s.toString),
25
+ o,
780
26
  "Expected `parts` to be an array of objects with a .toString() method. Got type: %t, value: %v"
781
- ), n = n.filter((r) => r).flatMap(String);
782
- let e = (t = n.shift()) == null ? void 0 : t.toString();
783
- if (e) {
784
- for (const r of n.map((s) => s.toString()))
785
- r.startsWith(".") ? e = qe(e, r) : e[e.length - 1] !== "/" ? r[0] !== "/" ? e += "/" + r : e += r : r[0] === "/" ? e += r.slice(1) : e += r;
786
- e && e !== "/" && e.endsWith("/") && (e = e.slice(0, e.length - 1));
787
- }
788
- return e ?? "";
27
+ ), o = o.filter((s) => s).flatMap(String);
28
+ let t = (e = o.shift()) == null ? void 0 : e.toString();
29
+ if (t) {
30
+ for (const s of o.map((n) => n.toString()))
31
+ s.startsWith(".") ? t = Rt(t, s) : t[t.length - 1] !== "/" ? s[0] !== "/" ? t += "/" + s : t += s : s[0] === "/" ? t += s.slice(1) : t += s;
32
+ t && t !== "/" && t.endsWith("/") && (t = t.slice(0, t.length - 1));
33
+ }
34
+ return t ?? "";
789
35
  }
790
- function qe(n, e) {
791
- if (lt(n, "Expected `base` to be a string. Got type: %t, value: %v"), e == null && (e = n, n = ""), e.startsWith("/"))
792
- return e;
793
- let t = n;
36
+ function Rt(o, t) {
37
+ if (Zt(o, "Expected `base` to be a string. Got type: %t, value: %v"), t == null && (t = o, o = ""), t.startsWith("/"))
38
+ return t;
39
+ let e = o;
794
40
  for (; ; )
795
- if (e.startsWith("..")) {
796
- for (let r = t.length; r > 0; --r)
797
- if (t[r] === "/" || r === 0) {
798
- t = t.slice(0, r), e = e.replace(/^\.\.\/?/, "");
41
+ if (t.startsWith("..")) {
42
+ for (let s = e.length; s > 0; --s)
43
+ if (e[s] === "/" || s === 0) {
44
+ e = e.slice(0, s), t = t.replace(/^\.\.\/?/, "");
799
45
  break;
800
46
  }
801
- } else if (e.startsWith("."))
802
- e = e.replace(/^\.\/?/, "");
47
+ } else if (t.startsWith("."))
48
+ t = t.replace(/^\.\/?/, "");
803
49
  else
804
50
  break;
805
- return ae([t, e]);
51
+ return z([e, t]);
806
52
  }
807
- function Ke(n) {
808
- if (!n) return {};
809
- n.startsWith("?") && (n = n.slice(1));
810
- const e = n.split("&").filter((t) => t.trim() !== "").map((t) => {
811
- const [r, s] = t.split("=").map((i) => i.trim());
812
- return s.toLowerCase() === "true" ? [r, !0] : s.toLowerCase() === "false" ? [r, !1] : isNaN(Number(s)) ? [r, s] : [r, Number(s)];
53
+ function De(o) {
54
+ if (!o) return {};
55
+ o.startsWith("?") && (o = o.slice(1));
56
+ const t = o.split("&").filter((e) => e.trim() !== "").map((e) => {
57
+ const [s, n] = e.split("=").map((r) => r.trim());
58
+ return n.toLowerCase() === "true" ? [s, !0] : n.toLowerCase() === "false" ? [s, !1] : isNaN(Number(n)) ? [s, n] : [s, Number(n)];
813
59
  });
814
- return Object.fromEntries(e);
60
+ return Object.fromEntries(t);
815
61
  }
816
- function ot(n, e, t = {}) {
817
- var o;
818
- const [r, s] = e.split("?"), i = me(r);
819
- e: for (const f of n) {
820
- const { fragments: c } = f;
821
- if (!(((o = c[c.length - 1]) == null ? void 0 : o.type) === 3) && c.length !== i.length || t.willMatch && !t.willMatch(f))
822
- continue e;
823
- const w = [];
824
- t: for (let d = 0; d < c.length; d++) {
825
- const b = i[d], $ = c[d];
826
- if (b == null && $.type !== 3)
827
- continue e;
828
- switch ($.type) {
62
+ function se(o, t, e = {}) {
63
+ var a;
64
+ const [s, n] = t.split("?"), r = rt(s);
65
+ t: for (const c of o) {
66
+ const { fragments: h } = c;
67
+ if (!(((a = h[h.length - 1]) == null ? void 0 : a.type) === 3) && h.length !== r.length || e.willMatch && !e.willMatch(c))
68
+ continue t;
69
+ const E = [];
70
+ e: for (let u = 0; u < h.length; u++) {
71
+ const D = r[u], R = h[u];
72
+ if (D == null && R.type !== 3)
73
+ continue t;
74
+ switch (R.type) {
829
75
  case 1:
830
- if ($.name.toLowerCase() === b.toLowerCase()) {
831
- w.push($);
76
+ if (R.name.toLowerCase() === D.toLowerCase()) {
77
+ E.push(R);
832
78
  break;
833
79
  } else
834
- continue e;
80
+ continue t;
835
81
  case 2:
836
- w.push({ ...$, value: b });
82
+ E.push({ ...R, value: D });
837
83
  break;
838
84
  case 3:
839
- w.push({ ...$, value: i.slice(d).join("/") });
840
- break t;
85
+ E.push({ ...R, value: r.slice(u).join("/") });
86
+ break e;
841
87
  case 4:
842
- if (isNaN(Number(b)))
843
- continue e;
844
- w.push({ ...$, value: Number(b) });
88
+ if (isNaN(Number(D)))
89
+ continue t;
90
+ E.push({ ...R, value: Number(D) });
845
91
  break;
846
92
  default:
847
- throw new Error(`Unknown fragment type: ${$.type}`);
93
+ throw new Error(`Unknown fragment type: ${R.type}`);
848
94
  }
849
95
  }
850
- const p = /* @__PURE__ */ Object.create(null);
851
- for (const d of w)
852
- d.type === 2 && (p[d.name] = decodeURIComponent(d.value)), d.type === 4 && (p[d.name] = d.value), d.type === 3 && (p.wildcard = "/" + decodeURIComponent(d.value));
96
+ const k = {};
97
+ for (const u of E)
98
+ u.type === 2 && (k[u.name] = decodeURIComponent(u.value)), u.type === 4 && (k[u.name] = u.value), u.type === 3 && (k.wildcard = "/" + decodeURIComponent(u.value));
853
99
  return {
854
- path: "/" + w.map((d) => d.value).join("/"),
855
- pattern: "/" + c.map((d) => d.type === 2 ? `{${d.name}}` : d.type === 4 ? `{#${d.name}}` : d.name).join("/"),
856
- params: p,
857
- query: Ke(s),
858
- meta: f.meta
100
+ path: "/" + E.map((u) => u.value).join("/"),
101
+ pattern: "/" + h.map((u) => u.type === 2 ? `{${u.name}}` : u.type === 4 ? `{#${u.name}}` : u.name).join("/"),
102
+ params: k,
103
+ query: De(n),
104
+ meta: c.meta
859
105
  };
860
106
  }
861
107
  }
862
- function Wt(n) {
863
- const e = [], t = [], r = [], s = [];
864
- for (const o of n) {
865
- const { fragments: f } = o;
866
- f.some(
867
- (c) => c.type === 3
108
+ function Pe(o) {
109
+ const t = [], e = [], s = [], n = [];
110
+ for (const a of o) {
111
+ const { fragments: c } = a;
112
+ c.some(
113
+ (h) => h.type === 3
868
114
  /* Wildcard */
869
- ) ? s.push(o) : f.some(
870
- (c) => c.type === 4
115
+ ) ? n.push(a) : c.some(
116
+ (h) => h.type === 4
871
117
  /* NumericParam */
872
- ) ? t.push(o) : f.some(
873
- (c) => c.type === 2
118
+ ) ? e.push(a) : c.some(
119
+ (h) => h.type === 2
874
120
  /* Param */
875
- ) ? r.push(o) : e.push(o);
121
+ ) ? s.push(a) : t.push(a);
876
122
  }
877
- const i = (o, f) => o.fragments.length > f.fragments.length ? -1 : 1;
878
- return e.sort(i), t.sort(i), r.sort(i), s.sort(i), [...e, ...t, ...r, ...s];
123
+ const r = (a, c) => a.fragments.length > c.fragments.length ? -1 : 1;
124
+ return t.sort(r), e.sort(r), s.sort(r), n.sort(r), [...t, ...e, ...s, ...n];
879
125
  }
880
- function zt(n) {
881
- const e = me(n), t = [];
882
- for (let r = 0; r < e.length; r++) {
883
- const s = e[r];
884
- if (s === "*") {
885
- if (r !== e.length - 1)
886
- throw new Error(`Wildcard must be at the end of a pattern. Received: ${n}`);
887
- t.push({
126
+ function Te(o) {
127
+ const t = rt(o), e = [];
128
+ for (let s = 0; s < t.length; s++) {
129
+ const n = t[s];
130
+ if (n === "*") {
131
+ if (s !== t.length - 1)
132
+ throw new Error(`Wildcard must be at the end of a pattern. Received: ${o}`);
133
+ e.push({
888
134
  type: 3,
889
135
  name: "*",
890
136
  value: null
891
137
  });
892
- } else s.at(0) === "{" && s.at(-1) === "}" ? t.push({
893
- type: s[1] === "#" ? 4 : 2,
894
- name: s[1] === "#" ? s.slice(2, -1) : s.slice(1, -1),
138
+ } else n.at(0) === "{" && n.at(-1) === "}" ? e.push({
139
+ type: n[1] === "#" ? 4 : 2,
140
+ name: n[1] === "#" ? n.slice(2, -1) : n.slice(1, -1),
895
141
  value: null
896
- }) : t.push({
142
+ }) : e.push({
897
143
  type: 1,
898
- name: s,
899
- value: s
144
+ name: n,
145
+ value: n
900
146
  });
901
147
  }
902
- return t;
148
+ return e;
149
+ }
150
+ function rs(o) {
151
+ return new Ue(o);
903
152
  }
904
- var Ee, T, Z, L, ze, W, ke, ee, z, le, he, ue, Le, B, Je, Ye;
905
- class Ft {
906
- constructor(e, t) {
907
- u(this, B);
908
- u(this, Ee);
909
- u(this, T);
910
- u(this, Z);
911
- u(this, L, Ht());
912
- u(this, ze, 0);
913
- u(this, W, []);
914
- u(this, ke);
915
- u(this, ee, []);
153
+ const re = Symbol.for("DollaRouterMountMethod"), ne = Symbol.for("DollaRouterUnmountMethod");
154
+ function Ot(o) {
155
+ return (o == null ? void 0 : o[te]) === !0;
156
+ }
157
+ async function Oe(o, t) {
158
+ return o[re](t);
159
+ }
160
+ async function Ne(o) {
161
+ return o[ne]();
162
+ }
163
+ var Yt, K, L, _t, P, Q, it, I, q, T, at, p, Nt, ie, ae, st, Ut, oe, Ct;
164
+ class Ue {
165
+ constructor(t) {
166
+ l(this, p);
167
+ d(this, Yt, !0);
168
+ l(this, K);
169
+ l(this, L);
170
+ l(this, _t, 0);
171
+ l(this, P, []);
172
+ l(this, Q, []);
173
+ l(this, it, !1);
174
+ /**
175
+ * Use hash routing when true. Configured in router options.
176
+ */
177
+ l(this, I, !1);
916
178
  // Callbacks that need to be called on unmount.
917
- u(this, z, []);
179
+ l(this, q, []);
180
+ /**
181
+ * The current match object.
182
+ */
183
+ l(this, T);
184
+ l(this, at);
918
185
  /**
919
186
  * The currently matched route pattern, if any.
920
187
  */
921
- k(this, "$pattern");
922
- u(this, le);
188
+ d(this, "$pattern");
923
189
  /**
924
190
  * The current URL path.
925
191
  */
926
- k(this, "$path");
927
- u(this, he);
192
+ d(this, "$path");
928
193
  /**
929
194
  * The current named path params.
930
195
  */
931
- k(this, "$params");
932
- u(this, ue);
196
+ d(this, "$params");
933
197
  /**
934
198
  * The current query params. Changes to this object will be reflected in the URL.
935
199
  */
936
- k(this, "$query");
937
- u(this, Le);
938
- m(this, Ee, e), m(this, T, e.createLogger("dolla/router")), m(this, Z, t);
939
- const [r, s] = K(null), [i, o] = K(""), [f, c] = K({}), [y, w] = K(Ke(window.location.search));
940
- this.$pattern = r, m(this, le, s), this.$path = i, m(this, he, o), this.$params = f, m(this, ue, c), this.$query = y, m(this, Le, w), e.beforeMount(() => {
941
- a(this, z).push(
942
- ht([y], (p) => {
943
- const d = new URLSearchParams();
944
- for (const $ in p)
945
- d.set($, String(p[$]));
946
- const b = "?" + d.toString();
947
- b != a(this, L).location.search && a(this, L).replace({
948
- pathname: a(this, L).location.pathname,
949
- search: b
950
- });
951
- })
952
- ), a(this, z).push(a(this, L).listen(P(this, B, Ye).bind(this))), P(this, B, Ye).call(this, a(this, L)), a(this, z).push(
953
- Bt(a(this, Z).rootElement, (p) => {
954
- let d = p.getAttribute("href");
955
- a(this, T).info("Intercepted link click", p, d), /^https?:\/\/|^\//.test(d) || (d = ae([a(this, L).location.pathname, d])), a(this, L).push(d);
956
- })
957
- ), a(this, T).info("Intercepting <a> clicks within root element:", a(this, Z).rootElement);
958
- }), e.onUnmount(() => {
959
- for (; a(this, z).length > 0; )
960
- a(this, z).pop()();
961
- });
962
- }
963
- setup(e) {
964
- e.style === "hash" && m(this, L, _t()), m(this, ee, Wt(
965
- e.routes.flatMap((t) => P(this, B, Je).call(this, t)).map((t) => ({
966
- pattern: t.pattern,
967
- meta: t.meta,
968
- fragments: zt(t.pattern)
200
+ d(this, "$query");
201
+ me(t, "Options must be an object. Got: %t"), [et(this, T)._, et(this, at)._] = At(), this.$pattern = x([i(this, T)], (e) => e == null ? void 0 : e.pattern), this.$path = x([i(this, T)], (e) => (e == null ? void 0 : e.path) ?? window.location.pathname), this.$params = x([i(this, T)], (e) => (e == null ? void 0 : e.params) ?? {}, { equals: Ht }), this.$query = x([i(this, T)], (e) => (e == null ? void 0 : e.query) ?? {}, { equals: Ht }), t.hash && m(this, I, !0), m(this, Q, Pe(
202
+ t.routes.flatMap((e) => f(this, p, Ct).call(this, e)).map((e) => ({
203
+ pattern: e.pattern,
204
+ meta: e.meta,
205
+ fragments: Te(e.pattern)
969
206
  }))
970
- ));
971
- for (const t of a(this, ee))
972
- if (t.meta.redirect) {
973
- let r;
974
- if (!Xe(t.meta.redirect)) if (D(t.meta.redirect)) {
975
- if (r = t.meta.redirect, !ot(a(this, ee), r, {
976
- willMatch(i) {
977
- return i !== t;
978
- }
979
- }))
980
- throw new Error(
981
- `Found a redirect to an undefined URL. From '${t.pattern}' to '${t.meta.redirect}'`
982
- );
983
- } else
984
- throw new TypeError(`Expected a string or redirect function. Got: ${t.meta.redirect}`);
985
- }
207
+ )), qe(i(this, Q));
986
208
  }
987
- /**
988
- * Navigates to another route.
989
- *
990
- * @example
991
- * navigate("/login"); // navigate to `/login`
992
- * navigate(["/users", 215], { replace: true }); // replace current history entry with `/users/215`
993
- */
994
- go(e, t = {}) {
995
- let r;
996
- Array.isArray(e) ? r = ae(e) : r = e.toString(), r = qe(a(this, L).location.pathname, r), t.preserveQuery && (r += a(this, L).location.search), t.replace ? a(this, L).replace(r) : a(this, L).push(r);
209
+ async [(Yt = te, re)](t) {
210
+ m(this, K, t), m(this, L, t.createLogger("Dolla.router"));
211
+ const e = () => {
212
+ f(this, p, st).call(this);
213
+ };
214
+ window.addEventListener("popstate", e), i(this, q).push(() => window.removeEventListener("popstate", e));
215
+ const s = t.getRootElement();
216
+ i(this, q).push(
217
+ Ve(s, (n) => {
218
+ let r = n.getAttribute("href");
219
+ i(this, L).info("intercepted click on <a> tag", n), /^https?:\/\/|^\//.test(r) || (r = z([window.location.pathname, r])), f(this, p, Nt).call(this, r);
220
+ })
221
+ ), i(this, L).info("will intercept clicks on <a> tags within root element", s), m(this, it, !0), await f(this, p, st).call(this);
222
+ }
223
+ async [ne]() {
224
+ for (const t of i(this, q))
225
+ t();
226
+ m(this, q, []);
997
227
  }
998
228
  /**
999
229
  * Navigate backward. Pass a number of steps to hit the back button that many times.
1000
230
  */
1001
- back(e = 1) {
1002
- a(this, L).go(-e);
231
+ back(t = 1) {
232
+ window.history.go(-t);
1003
233
  }
1004
234
  /**
1005
235
  * Navigate forward. Pass a number of steps to hit the forward button that many times.
1006
236
  */
1007
- forward(e = 1) {
1008
- a(this, L).go(e);
237
+ forward(t = 1) {
238
+ window.history.go(t);
239
+ }
240
+ /**
241
+ * Navigates to another route.
242
+ *
243
+ * @example
244
+ * Dolla.router.go("/login"); // navigate to `/login`
245
+ * Dolla.router.go["/users", 215], { replace: true }); // replace current history entry with `/users/215`
246
+ */
247
+ go(t, e = {}) {
248
+ if (i(this, K) == null)
249
+ throw new Error("Routa methods won't work until you register it: Dolla.use(Routa, { /* ...options */ })");
250
+ let s;
251
+ Array.isArray(t) ? s = z(t) : s = t.toString(), s = Rt(window.location.pathname, s), e.preserveQuery && (s += window.location.search), e.replace ? f(this, p, ie).call(this, s) : f(this, p, Nt).call(this, s);
1009
252
  }
1010
253
  }
1011
- Ee = new WeakMap(), T = new WeakMap(), Z = new WeakMap(), L = new WeakMap(), ze = new WeakMap(), W = new WeakMap(), ke = new WeakMap(), ee = new WeakMap(), z = new WeakMap(), le = new WeakMap(), he = new WeakMap(), ue = new WeakMap(), Le = new WeakMap(), B = new WeakSet(), /**
254
+ K = new WeakMap(), L = new WeakMap(), _t = new WeakMap(), P = new WeakMap(), Q = new WeakMap(), it = new WeakMap(), I = new WeakMap(), q = new WeakMap(), T = new WeakMap(), at = new WeakMap(), p = new WeakSet(), Nt = function(t, e) {
255
+ var s;
256
+ (s = i(this, L)) == null || s.info("(push)", t), window.history.pushState(e, "", i(this, I) ? "/#" + t : t), f(this, p, st).call(this, t);
257
+ }, ie = function(t, e) {
258
+ var s;
259
+ (s = i(this, L)) == null || s.info("(replace)", t), window.history.replaceState(e, "", i(this, I) ? "/#" + t : t), f(this, p, st).call(this, t);
260
+ }, ae = function() {
261
+ return i(this, I) ? new URL(window.location.hash.slice(1), window.location.origin) : new URL(window.location.pathname, window.location.origin);
262
+ }, st = async function(t) {
263
+ var c;
264
+ const e = i(this, L), s = (c = i(this, K)) == null ? void 0 : c.getRootView(), n = t ? new URL(t, window.location.origin) : f(this, p, ae).call(this), { match: r, journey: a } = await f(this, p, Ut).call(this, n);
265
+ for (const h of a)
266
+ switch (h.kind) {
267
+ case "match":
268
+ e == null || e.info(`📍 ${h.message}`);
269
+ break;
270
+ case "redirect":
271
+ e == null || e.info(`↩️ ${h.message}`);
272
+ break;
273
+ case "miss":
274
+ e == null || e.info(`💀 ${h.message}`);
275
+ break;
276
+ }
277
+ if (r) {
278
+ const h = this.$pattern.get();
279
+ i(this, at).call(this, r), s && r.pattern !== h && f(this, p, oe).call(this, s, r);
280
+ } else
281
+ i(this, it) && e.crash(new We(`Failed to match route '${n.pathname}'`));
282
+ return { match: r, journey: a };
283
+ }, Ut = async function(t, e = []) {
284
+ const s = se(i(this, Q), t.pathname);
285
+ if (!s)
286
+ return {
287
+ match: null,
288
+ journey: [...e, { kind: "miss", message: `no match for '${t.pathname}'` }]
289
+ };
290
+ let n = s.meta.redirect;
291
+ if (s.meta.beforeMatch && await s.meta.beforeMatch({
292
+ // TODO: Allow setting context variables from here? Would apply to the context of the matched view.
293
+ redirect: (r) => {
294
+ n = r;
295
+ }
296
+ }), n != null) {
297
+ let r;
298
+ if (v(n))
299
+ r = Ie(n, s.params);
300
+ else if (nt(n)) {
301
+ const a = {
302
+ path: s.path,
303
+ pattern: s.pattern,
304
+ params: s.params,
305
+ query: s.query
306
+ };
307
+ if (r = await n(a), !v(r))
308
+ throw new Error("Redirect function must return a path to redirect to.");
309
+ r.startsWith("/") || (r = Rt(s.path, r));
310
+ } else
311
+ throw new TypeError("Redirect must either be a path string or a function.");
312
+ return f(this, p, Ut).call(this, new URL(r, window.location.origin), [
313
+ ...e,
314
+ { kind: "redirect", message: `redirecting '${s.path}' -> '${r}'` }
315
+ ]);
316
+ } else
317
+ return { match: s, journey: [...e, { kind: "match", message: `matched route '${s.path}'` }] };
318
+ }, /**
319
+ * Takes a matched route and mounts it.
320
+ */
321
+ oe = function(t, e) {
322
+ const s = e.meta.layers;
323
+ for (let n = 0; n < s.length; n++) {
324
+ const r = s[n], a = i(this, P)[n];
325
+ if ((a == null ? void 0 : a.id) !== r.id) {
326
+ m(this, P, i(this, P).slice(0, n)), a == null || a.view.unmount();
327
+ const c = i(this, P).at(-1), w = ((c == null ? void 0 : c.view) ?? t).setChildView(r.view);
328
+ i(this, P).push({ id: r.id, view: w });
329
+ }
330
+ }
331
+ }, /**
1012
332
  * Parses a route definition object into a set of matchable routes.
1013
333
  *
1014
334
  * @param route - Route config object.
1015
335
  * @param layers - Array of parent layers. Passed when this function calls itself on nested routes.
1016
336
  */
1017
- Je = function(e, t = [], r = []) {
1018
- if (!(typeof e == "object" && !Array.isArray(e)) || typeof e.path != "string")
1019
- throw new TypeError(`Route configs must be objects with a 'path' string property. Got: ${e}`);
1020
- if (e.redirect && e.routes)
337
+ Ct = function(t, e = [], s = []) {
338
+ if (!tt(t) || !v(t.path))
339
+ throw new TypeError(`Route configs must be objects with a 'path' string property. Got: ${t}`);
340
+ if (t.redirect && t.routes)
1021
341
  throw new Error("Route cannot have both a 'redirect' and nested 'routes'.");
1022
- if (e.redirect && e.view)
342
+ if (t.redirect && t.view)
1023
343
  throw new Error("Route cannot have both a 'redirect' and a 'view'.");
1024
- if (!e.view && !e.routes && !e.redirect)
344
+ if (!t.view && !t.routes && !t.redirect)
1025
345
  throw new Error("Route must have a 'view', a 'redirect', or a set of nested 'routes'.");
1026
- let s = [];
1027
- for (const c of t)
1028
- s.push(...me(c.path));
1029
- s.push(...me(e.path)), s[s.length - 1] === "*" && s.pop();
1030
- const i = [];
1031
- if (e.redirect) {
1032
- let c = e.redirect;
1033
- return D(c) && (c = qe(ae(s), c), c.startsWith("/") || (c = "/" + c)), i.push({
1034
- pattern: "/" + ae([...s, ...me(e.path)]),
346
+ let n = [];
347
+ for (const h of e)
348
+ n.push(...rt(h.path));
349
+ n.push(...rt(t.path)), n[n.length - 1] === "*" && n.pop();
350
+ const r = [];
351
+ if (t.redirect) {
352
+ let h = t.redirect;
353
+ return v(h) && (h = Rt(z(n), h), h.startsWith("/") || (h = "/" + h)), r.push({
354
+ pattern: "/" + z([...n, ...rt(t.path)]),
1035
355
  meta: {
1036
- redirect: c
356
+ redirect: h
1037
357
  }
1038
- }), i;
1039
- }
1040
- let o = ut;
1041
- if (typeof e.view == "function")
1042
- o = e.view;
1043
- else if (e.view)
1044
- throw new TypeError(`Route '${e.path}' expected a view function or undefined. Got: ${e.view}`);
1045
- const f = { id: et(this, ze)._++, view: o };
1046
- if (e.routes)
1047
- for (const c of e.routes)
1048
- i.push(...P(this, B, Je).call(this, c, [...t, e], [...r, f]));
358
+ }), r;
359
+ }
360
+ let a = ee;
361
+ if (nt(t.view))
362
+ a = t.view;
363
+ else if (t.view)
364
+ throw new TypeError(`Route '${t.path}' expected a view function or undefined. Got: ${t.view}`);
365
+ const c = { id: et(this, _t)._++, view: a };
366
+ if (t.routes)
367
+ for (const h of t.routes)
368
+ r.push(...f(this, p, Ct).call(this, h, [...e, t], [...s, c]));
1049
369
  else
1050
- i.push({
1051
- pattern: parent ? ae([...t.map((c) => c.path), e.path]) : e.path,
370
+ r.push({
371
+ pattern: parent ? z([...e.map((h) => h.path), t.path]) : t.path,
1052
372
  meta: {
1053
- pattern: e.path,
1054
- layers: [...r, f],
1055
- beforeMatch: e.beforeMatch
373
+ pattern: t.path,
374
+ layers: [...s, c],
375
+ beforeMatch: t.beforeMatch
1056
376
  }
1057
377
  });
1058
- return i;
1059
- }, Ye = async function({ location: e }) {
1060
- e.search !== a(this, ke) && (m(this, ke, e.search), a(this, Le).call(this, Ke(e.search)));
1061
- const t = ot(a(this, ee), e.pathname);
1062
- if (!t) {
1063
- a(this, le).call(this, null), a(this, he).call(this, e.pathname), a(this, ue).call(this, {
1064
- wildcard: e.pathname
1065
- });
1066
- return;
378
+ return r;
379
+ };
380
+ const Ce = /(noopener|noreferrer) (noopener|noreferrer)/, je = /^[\w-_]+:/;
381
+ function Ve(o, t, e = window) {
382
+ function s(r) {
383
+ return !r || r === o ? null : r.localName !== "a" || r.href === void 0 ? s(r.parentNode) : r;
1067
384
  }
1068
- if (t.meta.beforeMatch && await t.meta.beforeMatch({
1069
- redirect: (r) => {
1070
- throw new Error("Redirect not yet implemented.");
385
+ function n(r) {
386
+ if (r.button && r.button !== 0 || r.ctrlKey || r.metaKey || r.altKey || r.shiftKey || r.defaultPrevented)
387
+ return;
388
+ const a = s(r.target);
389
+ a && (e.location.protocol !== a.protocol || e.location.hostname !== a.hostname || e.location.port !== a.port || a.hasAttribute("data-router-ignore") || a.hasAttribute("download") || a.getAttribute("target") === "_blank" && Ce.test(a.getAttribute("rel")) || je.test(a.getAttribute("href")) || (r.preventDefault(), t(a)));
390
+ }
391
+ return o.addEventListener("click", n), function() {
392
+ o.removeEventListener("click", n);
393
+ };
394
+ }
395
+ function Ie(o, t) {
396
+ for (const e in t) {
397
+ const s = t[e].toString();
398
+ o = o.replace(`{${e}}`, s).replace(`{#${e}}`, s);
399
+ }
400
+ return o;
401
+ }
402
+ function qe(o) {
403
+ for (const t of o)
404
+ if (t.meta.redirect) {
405
+ let e;
406
+ if (!nt(t.meta.redirect)) if (v(t.meta.redirect)) {
407
+ if (e = t.meta.redirect, !se(o, e, {
408
+ willMatch(n) {
409
+ return n !== t;
410
+ }
411
+ }))
412
+ throw new Error(`Found a redirect to an undefined URL. From '${t.pattern}' to '${t.meta.redirect}'`);
413
+ } else
414
+ throw new TypeError(`Expected a string or redirect function. Got: ${t.meta.redirect}`);
1071
415
  }
1072
- }), a(this, T).info(`Matched route: '${t.pattern}' ('${t.path}')`), t.meta.redirect != null)
1073
- if (typeof t.meta.redirect == "string") {
1074
- const r = Qt(t.meta.redirect, t.params);
1075
- a(this, T).info(`Redirecting to: '${r}'`), a(this, L).replace(r);
1076
- } else if (typeof t.meta.redirect == "function") {
1077
- const r = {
1078
- path: t.path,
1079
- pattern: t.pattern,
1080
- params: t.params,
1081
- query: t.query
416
+ }
417
+ class We extends Error {
418
+ }
419
+ var W, Lt, Mt, ot, S, _;
420
+ class Ae {
421
+ constructor(t) {
422
+ l(this, S);
423
+ l(this, W, []);
424
+ l(this, Lt, Ge());
425
+ l(this, Mt);
426
+ l(this, ot);
427
+ m(this, Mt, t), m(this, ot, t.createLogger("Dolla.http"));
428
+ }
429
+ /**
430
+ * Adds a new middleware that will apply to subsequent requests.
431
+ * Returns a function to remove this middleware.
432
+ *
433
+ * @param middleware - A middleware function that will intercept requests.
434
+ */
435
+ use(t) {
436
+ return i(this, W).push(t), () => {
437
+ i(this, W).splice(i(this, W).indexOf(t), 1);
438
+ };
439
+ }
440
+ async get(t, e) {
441
+ return f(this, S, _).call(this, "get", t, e);
442
+ }
443
+ async put(t, e) {
444
+ return f(this, S, _).call(this, "put", t, e);
445
+ }
446
+ async patch(t, e) {
447
+ return f(this, S, _).call(this, "patch", t, e);
448
+ }
449
+ async post(t, e) {
450
+ return f(this, S, _).call(this, "post", t, e);
451
+ }
452
+ async delete(t, e) {
453
+ return f(this, S, _).call(this, "delete", t, e);
454
+ }
455
+ async head(t, e) {
456
+ return f(this, S, _).call(this, "head", t, e);
457
+ }
458
+ async options(t, e) {
459
+ return f(this, S, _).call(this, "options", t, e);
460
+ }
461
+ async trace(t, e) {
462
+ return f(this, S, _).call(this, "trace", t, e);
463
+ }
464
+ }
465
+ W = new WeakMap(), Lt = new WeakMap(), Mt = new WeakMap(), ot = new WeakMap(), S = new WeakSet(), _ = async function(t, e, s) {
466
+ return new ze({
467
+ ...s,
468
+ method: t,
469
+ uri: e,
470
+ middleware: i(this, W),
471
+ fetch: i(this, Lt),
472
+ logger: i(this, ot)
473
+ }).fetch();
474
+ };
475
+ function Ge() {
476
+ if (typeof window < "u" && window.fetch)
477
+ return window.fetch.bind(window);
478
+ if (typeof global < "u" && global.fetch)
479
+ return global.fetch.bind(global);
480
+ throw new Error("Running in neither browser nor node. Please run this app in one of the supported environments.");
481
+ }
482
+ class He extends Error {
483
+ constructor(e) {
484
+ const { status: s, statusText: n, method: r, url: a } = e, c = `${s} ${n}: Request failed (${r.toUpperCase()} ${a.toString()})`;
485
+ super(c);
486
+ d(this, "response");
487
+ this.response = e;
488
+ }
489
+ }
490
+ class Be {
491
+ constructor(t) {
492
+ d(this, "method");
493
+ d(this, "url");
494
+ d(this, "headers", new Headers());
495
+ d(this, "body");
496
+ this.method = t.method, this.body = t.body, t.uri.startsWith("http") ? this.url = new URL(t.uri) : this.url = new URL(t.uri, window.location.origin), this._applyHeaders(t.headers), this._applyQueryParams(t.query);
497
+ }
498
+ get isSameOrigin() {
499
+ return this.url.origin === window.location.origin;
500
+ }
501
+ _applyHeaders(t) {
502
+ if (t != null)
503
+ if (t instanceof Map || t instanceof Headers)
504
+ t.forEach((e, s) => {
505
+ this.headers.set(s, e);
506
+ });
507
+ else if (tt(t))
508
+ for (const e in t) {
509
+ const s = t[e];
510
+ s instanceof Date ? this.headers.set(e, s.toISOString()) : s != null && this.headers.set(e, String(s));
511
+ }
512
+ else
513
+ throw new TypeError(`Unknown headers type. Got: ${t}`);
514
+ }
515
+ _applyQueryParams(t) {
516
+ if (t != null)
517
+ if (t instanceof Map || t instanceof URLSearchParams)
518
+ t.forEach((e, s) => {
519
+ this.url.searchParams.set(s, e);
520
+ });
521
+ else if (tt(t))
522
+ for (const e in t) {
523
+ const s = t[e];
524
+ s instanceof Date ? this.url.searchParams.set(e, s.toISOString()) : s != null && this.url.searchParams.set(e, String(s));
525
+ }
526
+ else
527
+ throw new TypeError(`Unknown query params type. Got: ${t}`);
528
+ }
529
+ }
530
+ class ze {
531
+ constructor(t) {
532
+ d(this, "_middleware");
533
+ d(this, "_fetch");
534
+ d(this, "_request");
535
+ d(this, "_response");
536
+ this._middleware = t.middleware, this._fetch = t.fetch, this._request = new Be(t);
537
+ }
538
+ async fetch() {
539
+ if (this._middleware.length > 0) {
540
+ const t = (e = 0) => {
541
+ const s = this._middleware[e], n = this._middleware[e + 1] ? t(e + 1) : this._handler.bind(this);
542
+ return async () => s(this._request, async () => (await n(), this._response));
1082
543
  };
1083
- let s = await t.meta.redirect(r);
1084
- if (typeof s != "string")
1085
- throw new Error("Redirect function must return a path to redirect to.");
1086
- s.startsWith("/") || (s = qe(t.path, s)), a(this, T).info(`Redirecting to: '${s}'`), a(this, L).replace(s);
544
+ await t()();
1087
545
  } else
1088
- throw new TypeError("Redirect must either be a path string or a function.");
1089
- else if (a(this, he).call(this, t.path), a(this, ue).call(this, t.params), t.pattern !== this.$pattern.get()) {
1090
- a(this, le).call(this, t.pattern);
1091
- const r = t.meta.layers;
1092
- for (let s = 0; s < r.length; s++) {
1093
- const i = r[s], o = a(this, W)[s];
1094
- if ((o == null ? void 0 : o.id) !== i.id) {
1095
- a(this, T).info(`Replacing layer @${s} (active ID: ${o == null ? void 0 : o.id}, matched ID: ${i.id})`), m(this, W, a(this, W).slice(0, s));
1096
- const f = a(this, W).at(-1), c = a(this, Ee).constructView(i.view, {});
1097
- o && o.node.isMounted && o.node.unmount(), f ? f.node.setChildren([c]) : a(this, Z).rootView.setChildren([c]), a(this, W).push({ id: i.id, node: c });
546
+ await this._handler();
547
+ if (this._response.status < 200 || this._response.status >= 400)
548
+ throw new He(this._response);
549
+ return this._response;
550
+ }
551
+ // This is the function that performs the actual request after the final middleware.
552
+ async _handler() {
553
+ let t;
554
+ const e = this._request;
555
+ !e.headers.has("content-type") && tt(e.body) ? (e.headers.set("content-type", "application/json"), t = JSON.stringify(e.body)) : t = e.body;
556
+ const s = await this._fetch(e.url.toString(), {
557
+ method: e.method,
558
+ headers: e.headers,
559
+ body: t
560
+ }), n = s.headers.get("content-type");
561
+ let r;
562
+ n != null && n.includes("application/json") ? r = await s.json() : n != null && n.includes("application/x-www-form-urlencoded") ? r = await s.formData() : r = await s.text(), this._response = {
563
+ method: e.method,
564
+ url: e.url,
565
+ status: s.status,
566
+ statusText: s.statusText,
567
+ headers: s.headers,
568
+ body: r
569
+ };
570
+ }
571
+ }
572
+ var xt, J, B, jt, le;
573
+ class Ke {
574
+ constructor(t, e) {
575
+ l(this, B);
576
+ d(this, "dolla");
577
+ d(this, "config");
578
+ l(this, xt, !1);
579
+ l(this, J, /* @__PURE__ */ new Map());
580
+ this.config = t, this.dolla = e;
581
+ }
582
+ async load() {
583
+ let t;
584
+ if (!i(this, xt)) {
585
+ if (nt(this.config.fetch)) {
586
+ if (t = await this.config.fetch(), !tt(t))
587
+ throw new Error(`Fetch function did not return an object of language strings: ${t}`);
588
+ } else if (v(this.config.path)) {
589
+ const e = await fetch(this.config.path);
590
+ if (e.ok) {
591
+ const s = await e.json();
592
+ if (tt(s))
593
+ t = s;
594
+ else
595
+ throw new Error(
596
+ `Language path '${this.config.path}' did not return an object of language strings: ${s}`
597
+ );
598
+ } else
599
+ throw new Error("HTTP request failed.");
1098
600
  }
1099
601
  }
602
+ if (t) {
603
+ const e = f(this, B, jt).call(this, t);
604
+ for (const s of e)
605
+ i(this, J).set(s[0], s[1]);
606
+ } else
607
+ throw new Error("Language could not be loaded.");
1100
608
  }
1101
- };
1102
- const It = /(noopener|noreferrer) (noopener|noreferrer)/, Gt = /^[\w-_]+:/;
1103
- function Bt(n, e, t = window) {
1104
- function r(i) {
1105
- return !i || i === n ? null : i.localName !== "a" || i.href === void 0 ? r(i.parentNode) : i;
609
+ getTemplate(t) {
610
+ return i(this, J).get(t) ?? {
611
+ segments: [{ type: 0, text: `[MISSING: ${t}]` }]
612
+ };
1106
613
  }
1107
- function s(i) {
1108
- if (i.button && i.button !== 0 || i.ctrlKey || i.metaKey || i.altKey || i.shiftKey || i.defaultPrevented)
1109
- return;
1110
- const o = r(i.target);
1111
- o && (t.location.protocol !== o.protocol || t.location.hostname !== o.hostname || t.location.port !== o.port || o.hasAttribute("data-router-ignore") || o.hasAttribute("download") || o.getAttribute("target") === "_blank" && It.test(o.getAttribute("rel")) || Gt.test(o.getAttribute("href")) || (i.preventDefault(), e(o)));
614
+ hasTemplate(t) {
615
+ return i(this, J).has(t);
1112
616
  }
1113
- return n.addEventListener("click", s), function() {
1114
- n.removeEventListener("click", s);
617
+ }
618
+ xt = new WeakMap(), J = new WeakMap(), B = new WeakSet(), jt = function(t, e = []) {
619
+ const s = [];
620
+ for (const n in t)
621
+ switch (Bt(t[n])) {
622
+ case "string":
623
+ s.push([[...e, n].join("."), f(this, B, le).call(this, t[n])]);
624
+ break;
625
+ case "object":
626
+ s.push(...f(this, B, jt).call(this, t[n], [...e, n]));
627
+ break;
628
+ default:
629
+ throw new Error(
630
+ `Expected to find a string or object at ${[...e, n].join(".")}. Got: ${Bt(t[n])}`
631
+ );
632
+ }
633
+ return s;
634
+ }, le = function(t) {
635
+ let e;
636
+ ((u) => {
637
+ u[u.Static = 0] = "Static", u[u.ValueName = 1] = "ValueName", u[u.FormatName = 2] = "FormatName", u[u.FormatOptionName = 3] = "FormatOptionName", u[u.FormatOptionValue = 4] = "FormatOptionValue", u[u.FormatOptionEnd = 5] = "FormatOptionEnd";
638
+ })(e || (e = {}));
639
+ const s = {
640
+ segments: []
1115
641
  };
642
+ let n = "", r = 0, a = 0, c, h, w;
643
+ const E = () => {
644
+ c = {
645
+ type: 1,
646
+ name: "",
647
+ formats: []
648
+ };
649
+ }, k = () => {
650
+ h = {
651
+ name: "",
652
+ options: {}
653
+ };
654
+ };
655
+ for (; r < t.length; ) {
656
+ if (a !== 0 && t[r] === " ") {
657
+ r++;
658
+ continue;
659
+ }
660
+ switch (a) {
661
+ case 0:
662
+ t[r] === "{" && t[r + 1] === "{" ? (a = 1, r += 2, n.length > 0 && (s.segments.push({ type: 0, text: n }), n = ""), E()) : (n += t[r], r++);
663
+ break;
664
+ case 1:
665
+ t[r] === "|" ? (a = 2, r += 1, c.name = n, n = "", k()) : t[r] === "}" && t[r + 1] === "}" ? (a = 0, r += 2, c.name = n, n = "", s.segments.push(c)) : (n += t[r], r++);
666
+ break;
667
+ case 2:
668
+ t[r] === "(" ? (a = 3, r += 1, h.name = n, n = "") : t[r] === "}" && t[r + 1] === "}" ? (a = 0, r += 2, c.formats.push(h), s.segments.push(c)) : (n += t[r], r++);
669
+ break;
670
+ case 3:
671
+ t[r] === ")" || (t[r] === ":" ? (a = 4, r += 1, w = n, n = "") : t[r] === "}" && t[r + 1] === "}" || (n += t[r], r++));
672
+ break;
673
+ case 4:
674
+ t[r] === ")" ? (a = 5, r += 1, h.options[w] = n, n = "", c.formats.push(h)) : t[r] === "," ? (a = 3, r += 1, h.options[w] = n, n = "") : t[r] === "}" && t[r + 1] === "}" || (n += t[r], r++);
675
+ break;
676
+ case 5:
677
+ t[r] === "|" ? (a = 2, r += 1, k()) : t[r] === "}" && t[r + 1] === "}" && (a = 0, r += 2, s.segments.push(c));
678
+ break;
679
+ }
680
+ }
681
+ return a === 0 && n.length > 0 && s.segments.push({ type: 0, text: n }), s;
682
+ };
683
+ var lt, O, b, ht, ct, ut, ft, $, he, Vt, It, qt, ce;
684
+ class Qe {
685
+ constructor(t) {
686
+ l(this, $);
687
+ l(this, lt);
688
+ l(this, O);
689
+ l(this, b, /* @__PURE__ */ new Map());
690
+ l(this, ht, []);
691
+ l(this, ct, /* @__PURE__ */ new Map());
692
+ l(this, ut, "auto");
693
+ d(this, "$locale");
694
+ l(this, ft);
695
+ m(this, lt, t), m(this, O, t.createLogger("Dolla.i18n"));
696
+ const [e, s] = At();
697
+ this.$locale = e, m(this, ft, s), this.addFormat("number", (n, r, a) => f(this, $, Vt).call(this, Number(r), a)), this.addFormat("datetime", (n, r, a) => f(this, $, It).call(this, r, a)), this.addFormat("list", (n, r, a) => f(this, $, qt).call(this, r, a)), t.beforeMount(async () => {
698
+ i(this, b).size > 0 && await this.setLocale(i(this, ut));
699
+ });
700
+ }
701
+ get locales() {
702
+ return [...i(this, b).keys()];
703
+ }
704
+ setup(t) {
705
+ if (t.translations.forEach((e) => {
706
+ i(this, b).set(e.locale, new Ke(e, i(this, lt)));
707
+ }), t.locale && t.locale !== "auto") {
708
+ if (!t.translations.some((s) => s.locale === t.locale))
709
+ throw new Error(`Initial locale '${t.locale}' is not registered in the locales array.`);
710
+ m(this, ut, t.locale);
711
+ }
712
+ i(this, O).info(
713
+ `${i(this, b).size} language${i(this, b).size === 1 ? "" : "s"} supported: '${[...i(this, b).keys()].join("', '")}'`
714
+ );
715
+ }
716
+ async setLocale(t) {
717
+ var n;
718
+ let e;
719
+ if (t === "auto") {
720
+ let r = [];
721
+ if (typeof navigator < "u") {
722
+ const a = navigator;
723
+ ((n = a.languages) == null ? void 0 : n.length) > 0 ? r.push(...a.languages) : a.language ? r.push(a.language) : a.browserLanguage ? r.push(a.browserLanguage) : a.userLanguage && r.push(a.userLanguage);
724
+ }
725
+ for (const a of r)
726
+ i(this, b).has(a) && (e = a);
727
+ } else
728
+ i(this, b).has(t) && (e = t);
729
+ if (e == null) {
730
+ const r = i(this, b).keys().next().value;
731
+ r && (e = r);
732
+ }
733
+ if (!e || !i(this, b).has(e))
734
+ throw new Error(`Locale '${t}' has no translation.`);
735
+ const s = i(this, b).get(e);
736
+ try {
737
+ await s.load(), m(this, ht, []), i(this, ft).call(this, e), i(this, O).info("set language to " + e);
738
+ } catch (r) {
739
+ r instanceof Error && i(this, O).crash(r);
740
+ }
741
+ }
742
+ /**
743
+ * Returns a State containing the value at `key`.
744
+
745
+ * @param selector - Key to the translated value.
746
+ * @param options - A map of `{{placeholder}}` names and the values to replace them with.
747
+ *
748
+ * @example
749
+ * const $value = t("your.key.here", { count: 5 });
750
+ */
751
+ t(t, e) {
752
+ if (this === void 0)
753
+ throw new Error(
754
+ `The 't' function cannot be destructured. If you need a standalone version you can import it like so: 'import { t } from "@manyducks.co/dolla"'`
755
+ );
756
+ let s = [], n = [];
757
+ for (const r in e)
758
+ s.push(r), n.push(e[r]);
759
+ return x([this.$locale, ...n], (r, ...a) => {
760
+ if (r == null)
761
+ return "[NO LOCALE SET]";
762
+ const c = {};
763
+ for (let h = 0; h < a.length; h++)
764
+ c[s[h]] = a[h];
765
+ return f(this, $, he).call(this, r, t, c);
766
+ });
767
+ }
768
+ /**
769
+ * Add a custom format callback.
770
+ *
771
+ * @example
772
+ * Dolla.i18n.addFormat("uppercase", (locale, value, options) => {
773
+ * return value.toUpperCase();
774
+ * });
775
+ *
776
+ * {
777
+ * "greeting": "Hello, {{name|uppercase}}!"
778
+ * }
779
+ *
780
+ * t("greeting", {name: "world"}); // State<"Hello, WORLD!">
781
+ */
782
+ addFormat(t, e) {
783
+ i(this, ct).set(t, e);
784
+ }
785
+ /**
786
+ * Creates an `Intl.Collator` configured for the current locale.
787
+ * NOTE: The Collator remains bound to the locale it was created with, even when the app's locale changes.
788
+ *
789
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#options
790
+ */
791
+ collator(t) {
792
+ return new Intl.Collator(this.$locale.get(), t);
793
+ }
794
+ /**
795
+ * Returns a State containing the number formatted for the current locale. Uses `Intl.NumberFormat` under the hood.
796
+ *
797
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options
798
+ */
799
+ number(t, e) {
800
+ return x([this.$locale, t], (s, n) => f(this, $, Vt).call(this, n, e));
801
+ }
802
+ /**
803
+ * Returns a State containing the date formatted for the current locale. Uses `Intl.DateTimeFormat` under the hood.
804
+ *
805
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options
806
+ *
807
+ * @example
808
+ * const date = new Date();
809
+ * const $formatted = Dolla.i18n.dateTime(date, { dateFormat: "short" });
810
+ */
811
+ dateTime(t, e) {
812
+ return x([this.$locale, t], (s, n) => f(this, $, It).call(this, n, e));
813
+ }
814
+ /**
815
+ * Returns a State containing the date formatted for the current locale. Uses `Intl.DateTimeFormat` under the hood.
816
+ *
817
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options
818
+ *
819
+ * @example
820
+ * const list = new Date();
821
+ * const $formatted = Dolla.i18n.list(list, { });
822
+ */
823
+ list(t, e) {
824
+ return x([this.$locale, t], (s, n) => f(this, $, qt).call(this, n, e));
825
+ }
826
+ }
827
+ lt = new WeakMap(), O = new WeakMap(), b = new WeakMap(), ht = new WeakMap(), ct = new WeakMap(), ut = new WeakMap(), ft = new WeakMap(), $ = new WeakSet(), he = function(t, e, s) {
828
+ var h;
829
+ const n = f(this, $, ce).call(this, e, s);
830
+ if (n) return n;
831
+ const r = i(this, b).get(t);
832
+ if (s.context != null && (e += "_" + s.context), s.count != null)
833
+ if (s.ordinal) {
834
+ const w = `${e}_ordinal_(=${s.count})`;
835
+ r.hasTemplate(w) ? e = w : e += "_ordinal_" + new Intl.PluralRules(t, { type: "ordinal" }).select(s.count);
836
+ } else {
837
+ const w = `${e}_(=${s.count})`;
838
+ r.hasTemplate(w) ? e = w : e += "_" + new Intl.PluralRules(t).select(s.count);
839
+ }
840
+ const a = r.getTemplate(e);
841
+ let c = "";
842
+ for (const w of a.segments)
843
+ if (w.type === 0)
844
+ c += w.text;
845
+ else if (w.type === 1) {
846
+ let E = Je(s, w.name);
847
+ const k = ((h = s.formatOverrides) == null ? void 0 : h[w.name]) ?? [...w.formats];
848
+ w.name === "count" && k.length === 0 && k.push({ name: "number", options: {} });
849
+ for (const u of k) {
850
+ const D = i(this, ct).get(u.name);
851
+ if (D == null) {
852
+ const R = new Error(
853
+ `Failed to load format '${u.name}' when processing '${e}', template: ${a}`
854
+ );
855
+ throw i(this, O).crash(R), R;
856
+ }
857
+ E = D(t, E, u.options);
858
+ }
859
+ c += E;
860
+ }
861
+ return c;
862
+ }, Vt = function(t, e) {
863
+ return new Intl.NumberFormat(this.$locale.get(), e).format(t);
864
+ }, It = function(t, e) {
865
+ return new Intl.DateTimeFormat(this.$locale.get(), e).format(v(t) ? new Date(t) : t);
866
+ }, qt = function(t, e) {
867
+ return new Intl.ListFormat(this.$locale.get(), e).format(t);
868
+ }, // relativeTime(): State<string> {
869
+ // }
870
+ ce = function(t, e) {
871
+ for (const s of i(this, ht))
872
+ if (s[0] === t && we(s[1], e))
873
+ return s[2];
874
+ };
875
+ function Je(o, t) {
876
+ const e = String(t).split(/[\.\[\]]/).filter((n) => n.trim() !== "");
877
+ let s = o;
878
+ for (; e.length > 0; ) {
879
+ const n = e.shift();
880
+ s != null ? s = s[n] : s = void 0;
881
+ }
882
+ return s;
883
+ }
884
+ function Xe(o) {
885
+ return zt`
886
+ <div
887
+ style=${{
888
+ backgroundColor: "#880000",
889
+ color: "#fff",
890
+ padding: "2rem",
891
+ position: "fixed",
892
+ inset: 0,
893
+ fontSize: "20px"
894
+ }}
895
+ >
896
+ <h1 style=${{ marginBottom: "0.5rem" }}>The app has crashed</h1>
897
+ <p style=${{ marginBottom: "0.25rem" }}>
898
+ <span style=${{ fontFamily: "monospace" }}>${o.loggerName}</span>
899
+ ${pe(o.uid, zt`<span style=${{ fontFamily: "monospace", opacity: 0.5 }}> [uid: ${o.uid}]</span>`)}
900
+ ${" "}says:
901
+ </p>
902
+ <blockquote
903
+ style=${{
904
+ backgroundColor: "#991111",
905
+ padding: "0.25em",
906
+ borderRadius: "6px",
907
+ fontFamily: "monospace",
908
+ marginBottom: "1rem"
909
+ }}
910
+ >
911
+ <span
912
+ style=${{
913
+ display: "inline-block",
914
+ backgroundColor: "red",
915
+ padding: "0.1em 0.4em",
916
+ marginRight: "0.5em",
917
+ borderRadius: "4px",
918
+ fontSize: "0.9em",
919
+ fontWeight: "bold"
920
+ }}
921
+ >
922
+ ${o.error.name}
923
+ </span>
924
+ ${o.error.message}
925
+ </blockquote>
926
+
927
+ <p>Please see the browser console for details.</p>
928
+ </div>
929
+ `;
1116
930
  }
1117
- function Qt(n, e) {
1118
- for (const t in e) {
1119
- const r = e[t].toString();
1120
- n = n.replace(`{${t}}`, r).replace(`{#${t}}`, r);
931
+ var dt, X, N, A, G, Y, Ft, M, mt, F, Wt, St;
932
+ class Ye {
933
+ constructor(t) {
934
+ l(this, F);
935
+ l(this, dt);
936
+ l(this, X);
937
+ // Keys ensure only the most recent callback queued with a certain key
938
+ // will be called, keeping DOM operations to a minimum.
939
+ l(this, N, /* @__PURE__ */ new Map());
940
+ // All unkeyed writes are run on every batch.
941
+ l(this, A, []);
942
+ // All read callbacks are run before updates on every batch.
943
+ l(this, G, []);
944
+ l(this, Y, !1);
945
+ // When true, batches that would exceed 16ms will be split and deferred to a rAF.
946
+ // This may not be desirable, because while it does prevent hitching it sometimes leaves
947
+ // the state partially rendered for a brief second and certain elements can be seen to update after the fact.
948
+ // But the tradeoff here is snappier navigation with possibly slightly out of date DOM updates on heavy pages.
949
+ l(this, Ft, !0);
950
+ l(this, M, 0);
951
+ l(this, mt, new Intl.NumberFormat("en-US", {
952
+ maximumFractionDigits: 2,
953
+ minimumFractionDigits: 0,
954
+ style: "unit",
955
+ unit: "millisecond",
956
+ unitDisplay: "short"
957
+ }));
958
+ m(this, dt, t), m(this, X, t.createLogger("Dolla.batch"));
959
+ }
960
+ /**
961
+ * Queues a callback that runs before the next batch of writes.
962
+ */
963
+ read(t) {
964
+ i(this, G).push(t), f(this, F, Wt).call(this);
965
+ }
966
+ /**
967
+ * Queues a callback to run in the next render batch.
968
+ * Always put DOM mutations in a write callback when possible to help Dolla batch them efficiently.
969
+ */
970
+ write(t, e) {
971
+ e ? i(this, N).set(e, t) : i(this, A).push(t), f(this, F, Wt).call(this);
1121
972
  }
1122
- return n;
1123
973
  }
1124
- var F, Pe, I, te, Se, Re, xe, Me, Ne, R, G;
1125
- class Kt {
974
+ dt = new WeakMap(), X = new WeakMap(), N = new WeakMap(), A = new WeakMap(), G = new WeakMap(), Y = new WeakMap(), Ft = new WeakMap(), M = new WeakMap(), mt = new WeakMap(), F = new WeakSet(), Wt = function() {
975
+ if (!i(this, Y)) {
976
+ m(this, Y, !0);
977
+ const t = i(this, dt).getEnv() === "development";
978
+ queueMicrotask(() => {
979
+ f(this, F, St).call(this, t);
980
+ });
981
+ }
982
+ }, St = function(t = !1) {
983
+ const e = performance.now();
984
+ let s = 0;
985
+ const n = i(this, G).length + i(this, N).size + i(this, A).length;
986
+ let r = 0;
987
+ const a = () => (r++, s = performance.now() - e, i(this, Ft) && s > 12 && r < n ? (et(this, M)._++, t && i(this, X).warn(
988
+ `⚠️ Deferring batch to next frame. Performed ${r} of ${n} batched operation${r === 1 ? "" : "s"} in ${i(this, mt).format(s)} (deferral ${i(this, M)}).`
989
+ ), requestAnimationFrame(() => {
990
+ f(this, F, St).call(this, t);
991
+ }), !0) : !1), c = [...i(this, N).entries()];
992
+ let h, w;
993
+ for (; w = i(this, G).shift(); )
994
+ if (w(), a()) return;
995
+ for ([h, w] of c)
996
+ if (w(), i(this, N).delete(h), a()) return;
997
+ for (; w = i(this, A).shift(); )
998
+ if (w(), a()) return;
999
+ t && i(this, X)[s > 16 ? "warn" : "info"](
1000
+ `${s > 16 ? "⚠️ (>=16ms) " : ""}Executed ${r} operation${r === 1 ? "" : "s"} in ${i(this, mt).format(s)}${i(this, M) > 0 ? ` (after ${i(this, M)} deferral${i(this, M) === 1 ? "" : "s"})` : ""}.`
1001
+ ), m(this, M, 0), i(this, G).length || i(this, N).size || i(this, A).length ? queueMicrotask(() => {
1002
+ f(this, F, St).call(this, t);
1003
+ }) : m(this, Y, !1);
1004
+ };
1005
+ var U, wt, C, H, pt, gt, Z, yt, bt, $t, Et, g, y, j, V, Dt;
1006
+ class Ze {
1126
1007
  constructor() {
1127
- k(this, "http");
1128
- k(this, "language");
1129
- k(this, "render");
1130
- k(this, "router");
1131
- u(this, F, !1);
1132
- u(this, Pe, "production");
1133
- u(this, I);
1134
- u(this, te);
1135
- u(this, Se, Nt);
1136
- u(this, Re, []);
1137
- u(this, xe, []);
1138
- u(this, Me, []);
1139
- u(this, Ne, []);
1140
- u(this, R, {
1008
+ d(this, "batch");
1009
+ // Remove `private` when there are public methods to call.
1010
+ d(this, "stats");
1011
+ d(this, "http");
1012
+ d(this, "i18n");
1013
+ l(this, U, !1);
1014
+ l(this, wt, "production");
1015
+ l(this, C);
1016
+ l(this, H);
1017
+ l(this, pt, Xe);
1018
+ l(this, gt, Kt());
1019
+ l(this, Z);
1020
+ l(this, yt, []);
1021
+ l(this, bt, []);
1022
+ l(this, $t, []);
1023
+ l(this, Et, []);
1024
+ l(this, g, {
1025
+ root: this,
1026
+ data: {},
1027
+ emitter: new ge(),
1028
+ stores: /* @__PURE__ */ new Map(),
1029
+ viewName: "Dolla"
1030
+ });
1031
+ l(this, y, {
1141
1032
  info: "development",
1142
1033
  log: "development",
1143
1034
  warn: "development",
1144
1035
  error: !0
1145
1036
  });
1146
- u(this, G, rt("*,-dolla/*"));
1147
- k(this, "createSignal", K);
1148
- k(this, "createSettableSignal", $t);
1149
- k(this, "toSettableSignal", Et);
1150
- k(this, "signalify", Ge);
1151
- k(this, "designalify", kt);
1152
- k(this, "derive", Be);
1153
- k(this, "watch", ht);
1154
- k(this, "createRef", Lt);
1155
- k(this, "isRef", Pt);
1156
- const e = this;
1157
- this.http = new Ot(), this.language = new qt(this), this.render = new Vt(this), this.router = new Ft(this, {
1158
- get rootElement() {
1159
- return a(e, I);
1160
- },
1161
- get rootView() {
1162
- return a(e, te);
1163
- }
1164
- });
1037
+ l(this, j, Qt("*,-Dolla.*"));
1038
+ l(this, V, /* @__PURE__ */ new Map());
1039
+ // Registration functions for modules.
1040
+ // All modules will be registered before mount.
1041
+ l(this, Dt, []);
1042
+ d(this, "watch", i(this, gt).watch);
1043
+ d(this, "createState", At);
1044
+ d(this, "toState", be);
1045
+ d(this, "toValue", $e);
1046
+ d(this, "isState", Ee);
1047
+ d(this, "derive", x);
1048
+ d(this, "createWatcher", Kt);
1049
+ d(this, "createRef", ve);
1050
+ d(this, "isRef", ke);
1051
+ this.batch = new Ye(this), this.stats = new ye(this), this.http = new Ae(this), this.i18n = new Qe(this);
1165
1052
  }
1166
1053
  /**
1167
1054
  * True when the app is connected to a DOM node and displayed to the user.
1168
1055
  */
1169
1056
  get isMounted() {
1170
- return a(this, F);
1057
+ return i(this, U);
1171
1058
  }
1172
1059
  /**
1173
1060
  * Get the current environment that this app is running in.
1174
1061
  * Environment affects which log messages will print and how much debugging info is included in the DOM.
1175
1062
  */
1176
1063
  getEnv() {
1177
- return a(this, Pe);
1064
+ return i(this, wt);
1178
1065
  }
1179
1066
  /**
1180
1067
  * Sets the environment that this app is running in.
1181
1068
  * Environment affects which log messages will print and how much debugging info is included in the DOM.
1182
1069
  */
1183
- setEnv(e) {
1184
- m(this, Pe, e);
1070
+ setEnv(t) {
1071
+ m(this, wt, t);
1185
1072
  }
1186
1073
  /**
1187
1074
  * Sets the view that will be shown when the `crash` method is called on any logger.
1188
1075
  * When a crash is reported the app will be unmounted and replaced with this crash page.
1189
1076
  */
1190
- setCrashView(e) {
1191
- m(this, Se, e);
1077
+ setCrashView(t) {
1078
+ m(this, pt, t);
1079
+ }
1080
+ /**
1081
+ * Returns the HTMLElement Dolla is mounted to. This will return undefined until Dolla.mount() is called.
1082
+ */
1083
+ getRootElement() {
1084
+ return i(this, C);
1085
+ }
1086
+ /**
1087
+ * Returns the top level view Dolla is rendering inside the root element. This will return undefined until Dolla.mount() is called.
1088
+ */
1089
+ getRootView() {
1090
+ return i(this, H);
1091
+ }
1092
+ /**
1093
+ * Sets a context variable and returns its value. Context variables are accessible on the app and in child views.
1094
+ */
1095
+ set(t, e) {
1096
+ return i(this, g).data[t] = e, e;
1097
+ }
1098
+ /**
1099
+ * Gets the value of a context variable. Returns null if the variable is not set.
1100
+ */
1101
+ get(t) {
1102
+ return i(this, g).data[t] ?? null;
1103
+ }
1104
+ /**
1105
+ * Returns an object of all context variables stored at the app level.
1106
+ */
1107
+ // getAll(): Record<string | symbol, unknown> {
1108
+ // return { ...this.#rootElementContext.data };
1109
+ // }
1110
+ /**
1111
+ * Adds a listener to be called when `eventName` is emitted.
1112
+ */
1113
+ on(t, e) {
1114
+ if (t === "*") {
1115
+ const s = (n, r) => {
1116
+ e(r);
1117
+ };
1118
+ i(this, g).emitter.on(t, s), i(this, V).set(e, s);
1119
+ } else
1120
+ i(this, g).emitter.on(t, e);
1121
+ }
1122
+ /**
1123
+ * Removes a listener from the list to be called when `eventName` is emitted.
1124
+ */
1125
+ off(t, e) {
1126
+ if (t === "*") {
1127
+ const s = i(this, V).get(e);
1128
+ s && (i(this, g).emitter.off(t, s), i(this, V).delete(e));
1129
+ } else
1130
+ i(this, g).emitter.off(t, e);
1192
1131
  }
1193
- async mount(e, t) {
1194
- if (a(this, F))
1132
+ /**
1133
+ * Adds a listener to be called when `eventName` is emitted. The listener is immediately removed after being called once.
1134
+ */
1135
+ once(t, e) {
1136
+ if (t === "*") {
1137
+ const s = (n, r) => {
1138
+ i(this, V).delete(e), e(r);
1139
+ };
1140
+ i(this, g).emitter.once(t, s), i(this, V).set(e, s);
1141
+ } else
1142
+ i(this, g).emitter.once(t, e);
1143
+ }
1144
+ /**
1145
+ * Emits a new event to all listeners.
1146
+ */
1147
+ emit(t, e) {
1148
+ return i(this, g).emitter.emit(t, new Re(t, e));
1149
+ }
1150
+ /**
1151
+ * Attaches a new store to this context.
1152
+ */
1153
+ attachStore(t) {
1154
+ if (!t.attach(i(this, g))) {
1155
+ let s = t.name ? `'${t.name}'` : "this store";
1156
+ console.warn(`An instance of ${s} was already attached to this context.`);
1157
+ }
1158
+ }
1159
+ /**
1160
+ * Gets the nearest instance of a store. Throws an error if the store isn't provided higher in the tree.
1161
+ */
1162
+ useStore(t) {
1163
+ if (Se(t)) {
1164
+ const e = t.key, s = i(this, g).stores.get(e);
1165
+ if (s == null)
1166
+ throw new Tt("Store not found on this context.");
1167
+ return s.value;
1168
+ } else throw _e(t) ? new Tt(
1169
+ "Received a Store instance. Please pass the Store factory function to useStore without calling it."
1170
+ ) : new Tt("Invalid store.");
1171
+ }
1172
+ async mount(t, e) {
1173
+ if (i(this, U))
1195
1174
  throw new Error("Dolla is already mounted.");
1196
- if (D(e)) {
1197
- const s = document.querySelector(e);
1198
- nt(HTMLElement, s, `Selector '${e}' did not match any element.`), m(this, I, s);
1175
+ if (v(t)) {
1176
+ const r = document.querySelector(t);
1177
+ Jt(HTMLElement, r, `Selector '${t}' did not match any element.`), m(this, C, r);
1199
1178
  } else
1200
- nt(HTMLElement, e, "Expected an HTML element or a selector string. Got type: %t, value: %v"), m(this, I, e);
1201
- let r;
1202
- t ? r = at(t) : r = at(ut), m(this, te, this.constructView(r.type, r.props)), await Promise.all(a(this, Re).map((s) => s())), a(this, te).mount(a(this, I)), m(this, F, !0);
1203
- for (const s of a(this, xe))
1204
- s();
1179
+ Jt(HTMLElement, t, "Expected an HTML element or a selector string. Got type: %t, value: %v"), m(this, C, t);
1180
+ Ot(e) && m(this, Z, e);
1181
+ const s = Ot(e) ? ee : e, n = Le(s);
1182
+ m(this, H, this.constructView(n.type, n.props)), await Promise.all(i(this, Dt).map((r) => r())), Ot(e) && await Oe(e, this), await Promise.all(i(this, yt).map((r) => r())), i(this, H).mount(i(this, C)), m(this, U, !0);
1183
+ for (const r of i(this, g).stores.values())
1184
+ r.handleMount();
1185
+ for (const r of i(this, bt))
1186
+ r();
1205
1187
  }
1206
1188
  async unmount() {
1207
- var e;
1208
- if (a(this, F)) {
1209
- await Promise.all(a(this, Me).map((t) => t())), (e = a(this, te)) == null || e.unmount(), m(this, F, !1);
1210
- for (const t of a(this, Ne))
1211
- t();
1189
+ var t;
1190
+ if (i(this, U)) {
1191
+ await Promise.all(i(this, $t).map((e) => e())), (t = i(this, H)) == null || t.unmount(!1), i(this, gt).stopAll(), i(this, Z) && await Ne(i(this, Z)), m(this, U, !1);
1192
+ for (const e of i(this, Et))
1193
+ e();
1212
1194
  }
1213
1195
  }
1214
1196
  /**
1215
1197
  * Registers a `callback` to run after `Dolla.mount` is called, before the app is mounted. If `callback` returns a Promise,
1216
1198
  * it will be awaited before mounting finishes. Use this to perform initial setup before the app is displayed to the user.
1217
1199
  */
1218
- beforeMount(e) {
1219
- a(this, Re).push(e);
1200
+ beforeMount(t) {
1201
+ i(this, yt).push(t);
1220
1202
  }
1221
1203
  /**
1222
1204
  * Registers a `callback` to run after the app is mounted.
1223
1205
  */
1224
- onMount(e) {
1225
- a(this, xe).push(e);
1206
+ onMount(t) {
1207
+ i(this, bt).push(t);
1226
1208
  }
1227
1209
  /**
1228
1210
  * Registers a `callback` to run after `Dolla.unmount` is called, before the app is unmounted. If `callback` returns a Promise,
1229
1211
  * it will be awaited before unmounting finishes. Use this to perform cleanup.
1230
1212
  */
1231
- beforeUnmount(e) {
1232
- a(this, Me).push(e);
1213
+ beforeUnmount(t) {
1214
+ i(this, $t).push(t);
1233
1215
  }
1234
1216
  /**
1235
1217
  * Registers a `callback` to run after the app is unmounted.
1236
1218
  */
1237
- onUnmount(e) {
1238
- a(this, Ne).push(e);
1219
+ onUnmount(t) {
1220
+ i(this, Et).push(t);
1239
1221
  }
1240
1222
  /**
1241
1223
  * Update log type toggles. Values that are not passed will remain unchanged.
1242
1224
  */
1243
- setLoggles(e) {
1244
- for (const t in e) {
1245
- const r = e[t];
1246
- r && (a(this, R)[t] = r);
1225
+ setLoggles(t) {
1226
+ for (const e in t) {
1227
+ const s = t[e];
1228
+ s && (i(this, y)[e] = s);
1247
1229
  }
1248
1230
  }
1249
- setLogFilter(e) {
1250
- m(this, G, rt(e));
1231
+ setLogFilter(t) {
1232
+ m(this, j, Qt(t));
1251
1233
  }
1252
- createLogger(e, t) {
1253
- const r = Ge(e), s = (t == null ? void 0 : t.console) ?? St(), i = this;
1234
+ createLogger(t, e) {
1235
+ const s = (e == null ? void 0 : e.console) ?? ts(), n = this;
1254
1236
  return {
1237
+ setName(r) {
1238
+ return t = r, this;
1239
+ },
1255
1240
  get info() {
1256
- var f;
1257
- const o = r.get();
1258
- if (a(i, R).info === !1 || D(a(i, R).info) && a(i, R).info !== i.getEnv() || !a(f = i, G).call(f, o))
1259
- return Oe;
1241
+ var r;
1242
+ if (i(n, y).info === !1 || v(i(n, y).info) && i(n, y).info !== n.getEnv() || !i(r = n, j).call(r, t))
1243
+ return vt;
1260
1244
  {
1261
- let c = `%c${o}`;
1262
- return t != null && t.uid ? c += ` %c[uid: %c${t.uid}%c]` : c += "%c%c%c", s.info.bind(
1245
+ let a = `%c${t}`;
1246
+ return e != null && e.uid ? a += ` %c[uid: %c${e.uid}%c]` : a += "%c%c%c", s.info.bind(
1263
1247
  s,
1264
- c,
1265
- `color:${Te(c)};font-weight:bold`,
1248
+ a,
1249
+ `color:${kt(a)};font-weight:bold`,
1266
1250
  "color:#777",
1267
1251
  "color:#aaa",
1268
1252
  "color:#777"
@@ -1270,16 +1254,15 @@ class Kt {
1270
1254
  }
1271
1255
  },
1272
1256
  get log() {
1273
- var f;
1274
- const o = r.get();
1275
- if (a(i, R).log === !1 || D(a(i, R).log) && a(i, R).log !== i.getEnv() || !a(f = i, G).call(f, o))
1276
- return Oe;
1257
+ var r;
1258
+ if (i(n, y).log === !1 || v(i(n, y).log) && i(n, y).log !== n.getEnv() || !i(r = n, j).call(r, t))
1259
+ return vt;
1277
1260
  {
1278
- let c = `%c${o}`;
1279
- return t != null && t.uid ? c += ` %c[uid: %c${t.uid}%c]` : c += "%c%c%c", s.log.bind(
1261
+ let a = `%c${t}`;
1262
+ return e != null && e.uid ? a += ` %c[uid: %c${e.uid}%c]` : a += "%c%c%c", s.log.bind(
1280
1263
  s,
1281
- c,
1282
- `color:${Te(c)};font-weight:bold`,
1264
+ a,
1265
+ `color:${kt(a)};font-weight:bold`,
1283
1266
  "color:#777",
1284
1267
  "color:#aaa",
1285
1268
  "color:#777"
@@ -1287,16 +1270,15 @@ class Kt {
1287
1270
  }
1288
1271
  },
1289
1272
  get warn() {
1290
- var f;
1291
- const o = r.get();
1292
- if (a(i, R).warn === !1 || D(a(i, R).warn) && a(i, R).warn !== i.getEnv() || !a(f = i, G).call(f, o))
1293
- return Oe;
1273
+ var r;
1274
+ if (i(n, y).warn === !1 || v(i(n, y).warn) && i(n, y).warn !== n.getEnv() || !i(r = n, j).call(r, t))
1275
+ return vt;
1294
1276
  {
1295
- let c = `%c${o}`;
1296
- return t != null && t.uid ? c += ` %c[uid: %c${t.uid}%c]` : c += "%c%c%c", s.warn.bind(
1277
+ let a = `%c${t}`;
1278
+ return e != null && e.uid ? a += ` %c[uid: %c${e.uid}%c]` : a += "%c%c%c", s.warn.bind(
1297
1279
  s,
1298
- c,
1299
- `color:${Te(c)};font-weight:bold`,
1280
+ a,
1281
+ `color:${kt(a)};font-weight:bold`,
1300
1282
  "color:#777",
1301
1283
  "color:#aaa",
1302
1284
  "color:#777"
@@ -1304,63 +1286,79 @@ class Kt {
1304
1286
  }
1305
1287
  },
1306
1288
  get error() {
1307
- var f;
1308
- const o = r.get();
1309
- if (a(i, R).error === !1 || D(a(i, R).error) && a(i, R).error !== i.getEnv() || !a(f = i, G).call(f, o))
1310
- return Oe;
1289
+ var r;
1290
+ if (i(n, y).error === !1 || v(i(n, y).error) && i(n, y).error !== n.getEnv() || !i(r = n, j).call(r, t))
1291
+ return vt;
1311
1292
  {
1312
- let c = `%c${o}`;
1313
- return t != null && t.uid ? c += ` %c[uid: %c${t.uid}%c]` : c += "%c%c%c", s.error.bind(
1293
+ let a = `%c${t}`;
1294
+ return e != null && e.uid ? a += ` %c[uid: %c${e.uid}%c]` : a += "%c%c%c", s.error.bind(
1314
1295
  s,
1315
- c,
1316
- `color:${Te(c)};font-weight:bold`,
1296
+ a,
1297
+ `color:${kt(a)};font-weight:bold`,
1317
1298
  "color:#777",
1318
1299
  "color:#aaa",
1319
1300
  "color:#777"
1320
1301
  );
1321
1302
  }
1322
1303
  },
1323
- crash(o) {
1324
- throw i.isMounted && (i.unmount(), i.constructView(a(i, Se), {
1325
- error: o,
1326
- loggerName: r.get(),
1327
- uid: t == null ? void 0 : t.uid
1328
- }).mount(a(i, I))), o;
1304
+ crash(r) {
1305
+ throw n.isMounted && (n.unmount(), n.constructView(i(n, pt), {
1306
+ error: r,
1307
+ loggerName: t,
1308
+ uid: e == null ? void 0 : e.uid
1309
+ }).mount(i(n, C))), r;
1329
1310
  }
1330
1311
  };
1331
1312
  }
1332
1313
  /**
1333
1314
  *
1334
1315
  */
1335
- constructView(e, t, r = []) {
1336
- return Rt({ root: this }, e, t, r);
1316
+ constructView(t, e, s = []) {
1317
+ return new Me(i(this, g), t, e, s);
1337
1318
  }
1338
1319
  /**
1339
1320
  *
1340
1321
  */
1341
- constructMarkup(e) {
1342
- return xt(Mt({ root: this }, e));
1322
+ constructMarkup(t) {
1323
+ return xe(Fe(i(this, g), t));
1343
1324
  }
1344
1325
  }
1345
- F = new WeakMap(), Pe = new WeakMap(), I = new WeakMap(), te = new WeakMap(), Se = new WeakMap(), Re = new WeakMap(), xe = new WeakMap(), Me = new WeakMap(), Ne = new WeakMap(), R = new WeakMap(), G = new WeakMap();
1346
- const ct = new Kt(), Zt = ct.language.t.bind(ct.language);
1326
+ U = new WeakMap(), wt = new WeakMap(), C = new WeakMap(), H = new WeakMap(), pt = new WeakMap(), gt = new WeakMap(), Z = new WeakMap(), yt = new WeakMap(), bt = new WeakMap(), $t = new WeakMap(), Et = new WeakMap(), g = new WeakMap(), y = new WeakMap(), j = new WeakMap(), V = new WeakMap(), Dt = new WeakMap();
1327
+ function ts() {
1328
+ if (typeof window < "u" && window.console)
1329
+ return window.console;
1330
+ if (typeof global < "u" && global.console)
1331
+ return global.console;
1332
+ }
1333
+ const Xt = new Ze(), ns = Xt.i18n.t.bind(Xt.i18n);
1334
+ function is(o) {
1335
+ typeof window < "u" && (window.DOLLA_DEV_DEBUG = o);
1336
+ }
1337
+ function as() {
1338
+ return typeof window < "u" ? window.DOLLA_DEV_DEBUG === !0 : !1;
1339
+ }
1347
1340
  export {
1348
- yt as cond,
1349
- at as createMarkup,
1350
- Lt as createRef,
1351
- $t as createSettableSignal,
1352
- K as createSignal,
1353
- rr as createSignalSetter,
1354
- ct as default,
1355
- Be as derive,
1356
- kt as designalify,
1357
- tt as html,
1358
- Pt as isRef,
1359
- nr as portal,
1360
- ar as repeat,
1361
- Ge as signalify,
1362
- Zt as t,
1363
- Et as toSettableSignal,
1364
- ht as watch
1341
+ pe as cond,
1342
+ Le as createMarkup,
1343
+ ve as createRef,
1344
+ rs as createRouter,
1345
+ At as createState,
1346
+ hs as createStore,
1347
+ cs as createView,
1348
+ we as deepEqual,
1349
+ Xt as default,
1350
+ x as derive,
1351
+ as as getDevDebug,
1352
+ zt as html,
1353
+ ke as isRef,
1354
+ Ee as isState,
1355
+ us as portal,
1356
+ fs as repeat,
1357
+ is as setDevDebug,
1358
+ Ht as shallowEqual,
1359
+ ds as strictEqual,
1360
+ ns as t,
1361
+ be as toState,
1362
+ $e as toValue
1365
1363
  };
1366
1364
  //# sourceMappingURL=index.js.map