@bquery/bquery 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +61 -7
  2. package/dist/component/index.d.ts +8 -0
  3. package/dist/component/index.d.ts.map +1 -1
  4. package/dist/component.es.mjs +80 -53
  5. package/dist/component.es.mjs.map +1 -1
  6. package/dist/core/collection.d.ts +46 -0
  7. package/dist/core/collection.d.ts.map +1 -1
  8. package/dist/core/element.d.ts +124 -22
  9. package/dist/core/element.d.ts.map +1 -1
  10. package/dist/core/utils.d.ts +13 -0
  11. package/dist/core/utils.d.ts.map +1 -1
  12. package/dist/core.es.mjs +298 -55
  13. package/dist/core.es.mjs.map +1 -1
  14. package/dist/full.d.ts +2 -2
  15. package/dist/full.d.ts.map +1 -1
  16. package/dist/full.es.mjs +38 -33
  17. package/dist/full.iife.js +1 -1
  18. package/dist/full.iife.js.map +1 -1
  19. package/dist/full.umd.js +1 -1
  20. package/dist/full.umd.js.map +1 -1
  21. package/dist/index.d.ts +1 -1
  22. package/dist/index.es.mjs +38 -33
  23. package/dist/reactive/index.d.ts +2 -2
  24. package/dist/reactive/index.d.ts.map +1 -1
  25. package/dist/reactive/signal.d.ts +107 -0
  26. package/dist/reactive/signal.d.ts.map +1 -1
  27. package/dist/reactive.es.mjs +92 -55
  28. package/dist/reactive.es.mjs.map +1 -1
  29. package/dist/security/sanitize.d.ts.map +1 -1
  30. package/dist/security.es.mjs +136 -66
  31. package/dist/security.es.mjs.map +1 -1
  32. package/package.json +120 -120
  33. package/src/component/index.ts +414 -360
  34. package/src/core/collection.ts +454 -339
  35. package/src/core/element.ts +740 -493
  36. package/src/core/utils.ts +444 -425
  37. package/src/full.ts +106 -101
  38. package/src/index.ts +27 -27
  39. package/src/reactive/index.ts +22 -9
  40. package/src/reactive/signal.ts +506 -347
  41. package/src/security/sanitize.ts +553 -446
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * and build-step frameworks with modern features.
6
6
  *
7
7
  * @module bquery
8
- * @see https://github.com/your-org/bquery
8
+ * @see https://github.com/bquery/bquery
9
9
  */
10
10
  export * from './core/index';
11
11
  export * from './reactive/index';
package/dist/index.es.mjs CHANGED
@@ -1,43 +1,48 @@
1
- import { $ as i, $$ as r, BQueryCollection as s, BQueryElement as o, utils as p } from "./core.es.mjs";
2
- import { Computed as n, Signal as c, batch as l, computed as m, effect as f, persistedSignal as u, signal as g } from "./reactive.es.mjs";
3
- import { component as x, html as T, safeHtml as y } from "./component.es.mjs";
4
- import { capturePosition as H, flip as P, flipList as S, spring as z, springPresets as C, transition as $ } from "./motion.es.mjs";
5
- import { createTrustedHtml as B, escapeHtml as Q, generateNonce as k, getTrustedTypesPolicy as v, hasCSPDirective as D, isTrustedTypesSupported as E, sanitize as L, sanitize as N, stripTags as j } from "./security.es.mjs";
6
- import { buckets as w, cache as A, notifications as F, storage as G } from "./platform.es.mjs";
1
+ import { $ as i, $$ as r, BQueryCollection as s, BQueryElement as o, utils as a } from "./core.es.mjs";
2
+ import { Computed as p, Signal as c, batch as l, computed as m, effect as u, isComputed as f, isSignal as g, persistedSignal as d, readonly as x, signal as y, untrack as T, watch as h } from "./reactive.es.mjs";
3
+ import { component as C, html as H, safeHtml as P } from "./component.es.mjs";
4
+ import { capturePosition as $, flip as b, flipList as k, spring as B, springPresets as Q, transition as v } from "./motion.es.mjs";
5
+ import { createTrustedHtml as D, escapeHtml as E, generateNonce as L, getTrustedTypesPolicy as N, hasCSPDirective as j, isTrustedTypesSupported as q, sanitize as A, sanitize as F, stripTags as G } from "./security.es.mjs";
6
+ import { buckets as J, cache as K, notifications as M, storage as O } from "./platform.es.mjs";
7
7
  export {
8
8
  i as $,
9
9
  r as $$,
10
10
  s as BQueryCollection,
11
11
  o as BQueryElement,
12
- n as Computed,
12
+ p as Computed,
13
13
  c as Signal,
14
14
  l as batch,
15
- w as buckets,
16
- A as cache,
17
- H as capturePosition,
18
- x as component,
15
+ J as buckets,
16
+ K as cache,
17
+ $ as capturePosition,
18
+ C as component,
19
19
  m as computed,
20
- B as createTrustedHtml,
21
- f as effect,
22
- Q as escapeHtml,
23
- P as flip,
24
- S as flipList,
25
- k as generateNonce,
26
- v as getTrustedTypesPolicy,
27
- D as hasCSPDirective,
28
- T as html,
29
- E as isTrustedTypesSupported,
30
- F as notifications,
31
- u as persistedSignal,
32
- y as safeHtml,
33
- L as sanitize,
34
- N as sanitizeHtml,
35
- g as signal,
36
- z as spring,
37
- C as springPresets,
38
- G as storage,
39
- j as stripTags,
40
- $ as transition,
41
- p as utils
20
+ D as createTrustedHtml,
21
+ u as effect,
22
+ E as escapeHtml,
23
+ b as flip,
24
+ k as flipList,
25
+ L as generateNonce,
26
+ N as getTrustedTypesPolicy,
27
+ j as hasCSPDirective,
28
+ H as html,
29
+ f as isComputed,
30
+ g as isSignal,
31
+ q as isTrustedTypesSupported,
32
+ M as notifications,
33
+ d as persistedSignal,
34
+ x as readonly,
35
+ P as safeHtml,
36
+ A as sanitize,
37
+ F as sanitizeHtml,
38
+ y as signal,
39
+ B as spring,
40
+ Q as springPresets,
41
+ O as storage,
42
+ G as stripTags,
43
+ v as transition,
44
+ T as untrack,
45
+ a as utils,
46
+ h as watch
42
47
  };
43
48
  //# sourceMappingURL=index.es.mjs.map
@@ -3,6 +3,6 @@
3
3
  *
4
4
  * @module bquery/reactive
5
5
  */
6
- export { batch, Computed, computed, effect, persistedSignal, Signal, signal } from './signal';
7
- export type { CleanupFn, Observer } from './signal';
6
+ export { Computed, Signal, batch, computed, effect, isComputed, isSignal, persistedSignal, readonly, signal, untrack, watch, } from './signal';
7
+ export type { CleanupFn, Observer, ReadonlySignal } from './signal';
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/reactive/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAE9F,YAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/reactive/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,QAAQ,EACR,MAAM,EACN,KAAK,EACL,QAAQ,EACR,MAAM,EACN,UAAU,EACV,QAAQ,EACR,eAAe,EACf,QAAQ,EACR,MAAM,EACN,OAAO,EACP,KAAK,GACN,MAAM,UAAU,CAAC;AAElB,YAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC"}
@@ -61,6 +61,7 @@ export declare class Signal<T> {
61
61
  constructor(_value: T);
62
62
  /**
63
63
  * Gets the current value and tracks the read if inside an observer.
64
+ * Respects the global tracking state (disabled during untrack calls).
64
65
  */
65
66
  get value(): T;
66
67
  /**
@@ -201,4 +202,110 @@ export declare const batch: (fn: () => void) => void;
201
202
  * ```
202
203
  */
203
204
  export declare const persistedSignal: <T>(key: string, initialValue: T) => Signal<T>;
205
+ /**
206
+ * A readonly wrapper around a signal that prevents writes.
207
+ * Provides read-only access to a signal's value while maintaining reactivity.
208
+ *
209
+ * @template T - The type of the wrapped value
210
+ */
211
+ export interface ReadonlySignal<T> {
212
+ /** Gets the current value with dependency tracking. */
213
+ readonly value: T;
214
+ /** Gets the current value without dependency tracking. */
215
+ peek(): T;
216
+ }
217
+ /**
218
+ * Creates a read-only view of a signal.
219
+ * Useful for exposing reactive state without allowing modifications.
220
+ *
221
+ * @template T - The type of the signal value
222
+ * @param sig - The signal to wrap
223
+ * @returns A readonly signal wrapper
224
+ *
225
+ * @example
226
+ * ```ts
227
+ * const _count = signal(0);
228
+ * const count = readonly(_count); // Expose read-only version
229
+ *
230
+ * console.log(count.value); // 0
231
+ * count.value = 1; // TypeScript error: Cannot assign to 'value'
232
+ * ```
233
+ */
234
+ export declare const readonly: <T>(sig: Signal<T>) => ReadonlySignal<T>;
235
+ /**
236
+ * Watches a signal or computed value and calls a callback with old and new values.
237
+ * Unlike effect, watch provides access to the previous value.
238
+ *
239
+ * @template T - The type of the watched value
240
+ * @param source - The signal or computed to watch
241
+ * @param callback - Function called with (newValue, oldValue) on changes
242
+ * @param options - Watch options
243
+ * @returns A cleanup function to stop watching
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * const count = signal(0);
248
+ *
249
+ * const cleanup = watch(count, (newVal, oldVal) => {
250
+ * console.log(`Changed from ${oldVal} to ${newVal}`);
251
+ * });
252
+ *
253
+ * count.value = 5; // Logs: "Changed from 0 to 5"
254
+ * cleanup();
255
+ * ```
256
+ */
257
+ export declare const watch: <T>(source: Signal<T> | Computed<T>, callback: (newValue: T, oldValue: T | undefined) => void, options?: {
258
+ immediate?: boolean;
259
+ }) => CleanupFn;
260
+ /**
261
+ * Executes a function without tracking any signal dependencies.
262
+ * Useful when reading a signal value without creating a reactive dependency.
263
+ *
264
+ * @template T - The return type of the function
265
+ * @param fn - The function to execute without tracking
266
+ * @returns The result of the function
267
+ *
268
+ * @example
269
+ * ```ts
270
+ * const count = signal(0);
271
+ *
272
+ * effect(() => {
273
+ * // This creates a dependency
274
+ * console.log('Tracked:', count.value);
275
+ *
276
+ * // This does NOT create a dependency
277
+ * const untracked = untrack(() => otherSignal.value);
278
+ * });
279
+ * ```
280
+ */
281
+ export declare const untrack: <T>(fn: () => T) => T;
282
+ /**
283
+ * Type guard to check if a value is a Signal instance.
284
+ *
285
+ * @param value - The value to check
286
+ * @returns True if the value is a Signal
287
+ *
288
+ * @example
289
+ * ```ts
290
+ * const count = signal(0);
291
+ * const num = 42;
292
+ *
293
+ * isSignal(count); // true
294
+ * isSignal(num); // false
295
+ * ```
296
+ */
297
+ export declare const isSignal: (value: unknown) => value is Signal<unknown>;
298
+ /**
299
+ * Type guard to check if a value is a Computed instance.
300
+ *
301
+ * @param value - The value to check
302
+ * @returns True if the value is a Computed
303
+ *
304
+ * @example
305
+ * ```ts
306
+ * const doubled = computed(() => count.value * 2);
307
+ * isComputed(doubled); // true
308
+ * ```
309
+ */
310
+ export declare const isComputed: (value: unknown) => value is Computed<unknown>;
204
311
  //# sourceMappingURL=signal.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"signal.d.ts","sourceRoot":"","sources":["../../src/reactive/signal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC;AAElC;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC;AA2CnC;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,MAAM,CAAC,CAAC;IAOP,OAAO,CAAC,MAAM;IAN1B,OAAO,CAAC,WAAW,CAAuB;IAE1C;;;OAGG;gBACiB,MAAM,EAAE,CAAC;IAE7B;;OAEG;IACH,IAAI,KAAK,IAAI,CAAC,CAMb;IAED;;;OAGG;IACH,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,EAMhB;IAED;;;;;OAKG;IACH,IAAI,IAAI,CAAC;IAIT;;;;;OAKG;IACH,MAAM,CAAC,OAAO,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,GAAG,IAAI;CAGzC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,QAAQ,CAAC,CAAC;IAeT,OAAO,CAAC,QAAQ,CAAC,OAAO;IAdpC,OAAO,CAAC,WAAW,CAAK;IACxB,OAAO,CAAC,KAAK,CAAQ;IACrB,OAAO,CAAC,WAAW,CAAuB;IAC1C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAKxB;IAEF;;;OAGG;gBAC0B,OAAO,EAAE,MAAM,CAAC;IAE7C;;OAEG;IACH,IAAI,KAAK,IAAI,CAAC,CAUb;CACF;AAED;;;;;;;;;;;;GAYG;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;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,KAAK,GAAI,CAAC,EACrB,QAAQ,MAAM,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,EAC/B,UAAU,CAAC,QAAQ,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,SAAS,KAAK,IAAI,EACxD,UAAS;IAAE,SAAS,CAAC,EAAE,OAAO,CAAA;CAAO,KACpC,SAmBF,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,OAAO,GAAI,CAAC,EAAE,IAAI,MAAM,CAAC,KAAG,CAQxC,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,MAAM,CAAC,OAAO,CAA4B,CAAC;AAE9F;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,UAAU,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,QAAQ,CAAC,OAAO,CAA8B,CAAC"}
@@ -1,45 +1,52 @@
1
- let r = [], c = 0;
2
- const n = /* @__PURE__ */ new Set(), a = (e, t) => {
3
- r = [...r, e];
1
+ const i = [];
2
+ let u = 0;
3
+ const o = /* @__PURE__ */ new Set();
4
+ let a = !0;
5
+ const l = (t, e) => {
6
+ i.push(t);
4
7
  try {
5
- return t();
8
+ return e();
6
9
  } finally {
7
- r = r.slice(0, -1);
10
+ i.pop();
8
11
  }
9
- }, l = (e) => {
10
- if (c > 0) {
11
- n.add(e);
12
+ }, h = (t) => {
13
+ if (u > 0) {
14
+ o.add(t);
12
15
  return;
13
16
  }
14
- e();
15
- }, o = () => {
16
- for (const e of Array.from(n))
17
- n.delete(e), e();
17
+ t();
18
+ }, v = () => {
19
+ for (const t of Array.from(o))
20
+ o.delete(t), t();
18
21
  };
19
- class h {
22
+ class f {
20
23
  /**
21
24
  * Creates a new signal with an initial value.
22
25
  * @param _value - The initial value
23
26
  */
24
- constructor(t) {
25
- this._value = t, this.subscribers = /* @__PURE__ */ new Set();
27
+ constructor(e) {
28
+ this._value = e, this.subscribers = /* @__PURE__ */ new Set();
26
29
  }
27
30
  /**
28
31
  * Gets the current value and tracks the read if inside an observer.
32
+ * Respects the global tracking state (disabled during untrack calls).
29
33
  */
30
34
  get value() {
31
- const t = r[r.length - 1];
32
- return t && this.subscribers.add(t), this._value;
35
+ if (a) {
36
+ const e = i[i.length - 1];
37
+ e && this.subscribers.add(e);
38
+ }
39
+ return this._value;
33
40
  }
34
41
  /**
35
42
  * Sets a new value and notifies all subscribers if the value changed.
36
43
  * Uses Object.is for equality comparison.
37
44
  */
38
- set value(t) {
39
- if (!Object.is(this._value, t)) {
40
- this._value = t;
45
+ set value(e) {
46
+ if (!Object.is(this._value, e)) {
47
+ this._value = e;
41
48
  for (const s of this.subscribers)
42
- l(s);
49
+ h(s);
43
50
  }
44
51
  }
45
52
  /**
@@ -57,67 +64,97 @@ class h {
57
64
  *
58
65
  * @param updater - Function that receives current value and returns new value
59
66
  */
60
- update(t) {
61
- this.value = t(this._value);
67
+ update(e) {
68
+ this.value = e(this._value);
62
69
  }
63
70
  }
64
- class f {
71
+ class d {
65
72
  /**
66
73
  * Creates a new computed value.
67
74
  * @param compute - Function that computes the value
68
75
  */
69
- constructor(t) {
70
- this.compute = t, this.dirty = !0, this.subscribers = /* @__PURE__ */ new Set(), this.markDirty = () => {
76
+ constructor(e) {
77
+ this.compute = e, this.dirty = !0, this.subscribers = /* @__PURE__ */ new Set(), this.markDirty = () => {
71
78
  this.dirty = !0;
72
79
  for (const s of this.subscribers)
73
- l(s);
80
+ h(s);
74
81
  };
75
82
  }
76
83
  /**
77
84
  * Gets the computed value, recomputing if dependencies changed.
78
85
  */
79
86
  get value() {
80
- const t = r[r.length - 1];
81
- return t && this.subscribers.add(t), this.dirty && (this.dirty = !1, this.cachedValue = a(this.markDirty, this.compute)), this.cachedValue;
87
+ const e = i[i.length - 1];
88
+ return e && this.subscribers.add(e), this.dirty && (this.dirty = !1, this.cachedValue = l(this.markDirty, this.compute)), this.cachedValue;
82
89
  }
83
90
  }
84
- const b = (e) => new h(e), v = (e) => new f(e), d = (e) => {
85
- let t, s = !1;
86
- const i = () => {
87
- s || (t && t(), t = a(i, e));
91
+ const p = (t) => new f(t), g = (t) => new d(t), b = (t) => {
92
+ let e, s = !1;
93
+ const r = () => {
94
+ s || (e && e(), e = l(r, t));
88
95
  };
89
- return i(), () => {
90
- s = !0, t && t();
96
+ return r(), () => {
97
+ s = !0, e && e();
91
98
  };
92
- }, g = (e) => {
93
- c += 1;
99
+ }, y = (t) => {
100
+ u += 1;
94
101
  try {
95
- e();
102
+ t();
96
103
  } finally {
97
- c -= 1, c === 0 && o();
104
+ u -= 1, u === 0 && v();
98
105
  }
99
- }, p = (e, t) => {
100
- let s = t;
106
+ }, m = (t, e) => {
107
+ let s = e;
101
108
  try {
102
- const u = localStorage.getItem(e);
103
- u !== null && (s = JSON.parse(u));
109
+ const n = localStorage.getItem(t);
110
+ n !== null && (s = JSON.parse(n));
104
111
  } catch {
105
112
  }
106
- const i = b(s);
107
- return d(() => {
113
+ const r = p(s);
114
+ return b(() => {
108
115
  try {
109
- localStorage.setItem(e, JSON.stringify(i.value));
116
+ localStorage.setItem(t, JSON.stringify(r.value));
110
117
  } catch {
111
118
  }
112
- }), i;
113
- };
119
+ }), r;
120
+ }, S = (t) => ({
121
+ get value() {
122
+ return t.value;
123
+ },
124
+ peek() {
125
+ return t.peek();
126
+ }
127
+ }), k = (t, e, s = {}) => {
128
+ let r, n = !0;
129
+ return b(() => {
130
+ const c = t.value;
131
+ if (n) {
132
+ n = !1, r = c, s.immediate && e(c, void 0);
133
+ return;
134
+ }
135
+ e(c, r), r = c;
136
+ });
137
+ }, w = (t) => {
138
+ const e = a;
139
+ a = !1;
140
+ try {
141
+ return t();
142
+ } finally {
143
+ a = e;
144
+ }
145
+ }, O = (t) => t instanceof f, _ = (t) => t instanceof d;
114
146
  export {
115
- f as Computed,
116
- h as Signal,
117
- g as batch,
118
- v as computed,
119
- d as effect,
120
- p as persistedSignal,
121
- b as signal
147
+ d as Computed,
148
+ f as Signal,
149
+ y as batch,
150
+ g as computed,
151
+ b as effect,
152
+ _ as isComputed,
153
+ O as isSignal,
154
+ m as persistedSignal,
155
+ S as readonly,
156
+ p as signal,
157
+ w as untrack,
158
+ k as watch
122
159
  };
123
160
  //# sourceMappingURL=reactive.es.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"reactive.es.mjs","sources":["../src/reactive/signal.ts"],"sourcesContent":["/**\n * Reactive primitives inspired by fine-grained reactivity.\n *\n * This module provides a minimal but powerful reactive system:\n * - Signal: A reactive value that notifies subscribers when changed\n * - Computed: A derived value that automatically updates when dependencies change\n * - Effect: A side effect that re-runs when its dependencies change\n * - Batch: Group multiple updates to prevent intermediate re-renders\n *\n * @module bquery/reactive\n *\n * @example\n * ```ts\n * const count = signal(0);\n * const doubled = computed(() => count.value * 2);\n *\n * effect(() => {\n * console.log(`Count: ${count.value}, Doubled: ${doubled.value}`);\n * });\n *\n * batch(() => {\n * count.value = 1;\n * count.value = 2;\n * });\n * // Logs: \"Count: 2, Doubled: 4\" (only once due to batching)\n * ```\n */\n\n/**\n * Observer function type used internally for tracking reactivity.\n */\nexport type Observer = () => void;\n\n/**\n * Cleanup function returned by effects for disposal.\n */\nexport type CleanupFn = () => void;\n\n// Internal state for tracking the current observer context\nlet observerStack: Observer[] = [];\nlet batchDepth = 0;\nconst pendingObservers = new Set<Observer>();\n\n/**\n * Tracks dependencies during a function execution.\n * @internal\n */\nconst track = <T>(observer: Observer, fn: () => T): T => {\n observerStack = [...observerStack, observer];\n try {\n return fn();\n } finally {\n observerStack = observerStack.slice(0, -1);\n }\n};\n\n/**\n * Schedules an observer to run, respecting batch mode.\n * @internal\n */\nconst scheduleObserver = (observer: Observer) => {\n if (batchDepth > 0) {\n pendingObservers.add(observer);\n return;\n }\n observer();\n};\n\n/**\n * Flushes all pending observers after a batch completes.\n * @internal\n */\nconst flushObservers = () => {\n for (const observer of Array.from(pendingObservers)) {\n pendingObservers.delete(observer);\n observer();\n }\n};\n\n/**\n * A reactive value container that notifies subscribers on change.\n *\n * Signals are the foundational primitive of the reactive system.\n * Reading a signal's value inside an effect or computed automatically\n * establishes a reactive dependency.\n *\n * @template T - The type of the stored value\n *\n * @example\n * ```ts\n * const name = signal('World');\n * console.log(name.value); // 'World'\n *\n * name.value = 'bQuery';\n * console.log(name.value); // 'bQuery'\n * ```\n */\nexport class Signal<T> {\n private subscribers = new Set<Observer>();\n\n /**\n * Creates a new signal with an initial value.\n * @param _value - The initial value\n */\n constructor(private _value: T) {}\n\n /**\n * Gets the current value and tracks the read if inside an observer.\n */\n get value(): T {\n const current = observerStack[observerStack.length - 1];\n if (current) {\n this.subscribers.add(current);\n }\n return this._value;\n }\n\n /**\n * Sets a new value and notifies all subscribers if the value changed.\n * Uses Object.is for equality comparison.\n */\n set value(next: T) {\n if (Object.is(this._value, next)) return;\n this._value = next;\n for (const subscriber of this.subscribers) {\n scheduleObserver(subscriber);\n }\n }\n\n /**\n * Reads the current value without tracking.\n * Useful when you need the value but don't want to create a dependency.\n *\n * @returns The current value\n */\n peek(): T {\n return this._value;\n }\n\n /**\n * Updates the value using a function.\n * Useful for updates based on the current value.\n *\n * @param updater - Function that receives current value and returns new value\n */\n update(updater: (current: T) => T): void {\n this.value = updater(this._value);\n }\n}\n\n/**\n * A computed value that derives from other reactive sources.\n *\n * Computed values are lazily evaluated and cached. They only\n * recompute when their dependencies change.\n *\n * @template T - The type of the computed value\n *\n * @example\n * ```ts\n * const price = signal(100);\n * const quantity = signal(2);\n * const total = computed(() => price.value * quantity.value);\n *\n * console.log(total.value); // 200\n * price.value = 150;\n * console.log(total.value); // 300\n * ```\n */\nexport class Computed<T> {\n private cachedValue!: T;\n private dirty = true;\n private subscribers = new Set<Observer>();\n private readonly markDirty = () => {\n this.dirty = true;\n for (const subscriber of this.subscribers) {\n scheduleObserver(subscriber);\n }\n };\n\n /**\n * Creates a new computed value.\n * @param compute - Function that computes the value\n */\n constructor(private readonly compute: () => T) {}\n\n /**\n * Gets the computed value, recomputing if dependencies changed.\n */\n get value(): T {\n const current = observerStack[observerStack.length - 1];\n if (current) {\n this.subscribers.add(current);\n }\n if (this.dirty) {\n this.dirty = false;\n this.cachedValue = track(this.markDirty, this.compute);\n }\n return this.cachedValue;\n }\n}\n\n/**\n * Creates a new reactive signal.\n *\n * @template T - The type of the signal value\n * @param value - The initial value\n * @returns A new Signal instance\n *\n * @example\n * ```ts\n * const count = signal(0);\n * count.value++; // Triggers subscribers\n * ```\n */\nexport const signal = <T>(value: T): Signal<T> => new Signal(value);\n\n/**\n * Creates a new computed value.\n *\n * @template T - The type of the computed value\n * @param fn - Function that computes the value from reactive sources\n * @returns A new Computed instance\n *\n * @example\n * ```ts\n * const doubled = computed(() => count.value * 2);\n * ```\n */\nexport const computed = <T>(fn: () => T): Computed<T> => new Computed(fn);\n\n/**\n * Creates a side effect that automatically re-runs when dependencies change.\n *\n * The effect runs immediately upon creation and then re-runs whenever\n * any signal or computed value read inside it changes.\n *\n * @param fn - The effect function to run\n * @returns A cleanup function to stop the effect\n *\n * @example\n * ```ts\n * const count = signal(0);\n *\n * const cleanup = effect(() => {\n * document.title = `Count: ${count.value}`;\n * });\n *\n * // Later, to stop the effect:\n * cleanup();\n * ```\n */\nexport const effect = (fn: () => void | CleanupFn): CleanupFn => {\n let cleanupFn: CleanupFn | void;\n let isDisposed = false;\n\n const observer: Observer = () => {\n if (isDisposed) return;\n\n // Run previous cleanup if exists\n if (cleanupFn) {\n cleanupFn();\n }\n\n // Run effect and capture cleanup\n cleanupFn = track(observer, fn);\n };\n\n observer();\n\n return () => {\n isDisposed = true;\n if (cleanupFn) {\n cleanupFn();\n }\n };\n};\n\n/**\n * Batches multiple signal updates into a single notification cycle.\n *\n * Updates made inside the batch function are deferred until the batch\n * completes, preventing intermediate re-renders and improving performance.\n *\n * @param fn - Function containing multiple signal updates\n *\n * @example\n * ```ts\n * batch(() => {\n * firstName.value = 'John';\n * lastName.value = 'Doe';\n * age.value = 30;\n * });\n * // Effects only run once with all three updates\n * ```\n */\nexport const batch = (fn: () => void): void => {\n batchDepth += 1;\n try {\n fn();\n } finally {\n batchDepth -= 1;\n if (batchDepth === 0) {\n flushObservers();\n }\n }\n};\n\n/**\n * Creates a signal that persists to localStorage.\n *\n * @template T - The type of the signal value\n * @param key - The localStorage key\n * @param initialValue - The initial value if not found in storage\n * @returns A Signal that syncs with localStorage\n *\n * @example\n * ```ts\n * const theme = persistedSignal('theme', 'light');\n * theme.value = 'dark'; // Automatically saved to localStorage\n * ```\n */\nexport const persistedSignal = <T>(key: string, initialValue: T): Signal<T> => {\n let stored: T = initialValue;\n\n try {\n const raw = localStorage.getItem(key);\n if (raw !== null) {\n stored = JSON.parse(raw) as T;\n }\n } catch {\n // Use initial value on parse error\n }\n\n const sig = signal(stored);\n\n // Create an effect to persist changes\n effect(() => {\n try {\n localStorage.setItem(key, JSON.stringify(sig.value));\n } catch {\n // Ignore storage errors\n }\n });\n\n return sig;\n};\n"],"names":["observerStack","batchDepth","pendingObservers","track","observer","fn","scheduleObserver","flushObservers","Signal","_value","current","next","subscriber","updater","Computed","compute","signal","value","computed","effect","cleanupFn","isDisposed","batch","persistedSignal","key","initialValue","stored","raw","sig"],"mappings":"AAuCA,IAAIA,IAA4B,CAAA,GAC5BC,IAAa;AACjB,MAAMC,wBAAuB,IAAA,GAMvBC,IAAQ,CAAIC,GAAoBC,MAAmB;AACvD,EAAAL,IAAgB,CAAC,GAAGA,GAAeI,CAAQ;AAC3C,MAAI;AACF,WAAOC,EAAA;AAAA,EACT,UAAA;AACE,IAAAL,IAAgBA,EAAc,MAAM,GAAG,EAAE;AAAA,EAC3C;AACF,GAMMM,IAAmB,CAACF,MAAuB;AAC/C,MAAIH,IAAa,GAAG;AAClB,IAAAC,EAAiB,IAAIE,CAAQ;AAC7B;AAAA,EACF;AACA,EAAAA,EAAA;AACF,GAMMG,IAAiB,M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,EAAM,KAAK,WAAW,KAAK,OAAO,IAEhD,KAAK;AAAA,EACd;AACF;AAeO,MAAMa,IAAS,CAAIC,MAAwB,IAAIT,EAAOS,CAAK,GAcrDC,IAAW,CAAIb,MAA6B,IAAIS,EAAST,CAAE,GAuB3Dc,IAAS,CAACd,MAA0C;AAC/D,MAAIe,GACAC,IAAa;AAEjB,QAAMjB,IAAqB,MAAM;AAC/B,IAAIiB,MAGAD,KACFA,EAAA,GAIFA,IAAYjB,EAAMC,GAAUC,CAAE;AAAA,EAChC;AAEA,SAAAD,EAAA,GAEO,MAAM;AACX,IAAAiB,IAAa,IACTD,KACFA,EAAA;AAAA,EAEJ;AACF,GAoBaE,IAAQ,CAACjB,MAAyB;AAC7C,EAAAJ,KAAc;AACd,MAAI;AACF,IAAAI,EAAA;AAAA,EACF,UAAA;AACE,IAAAJ,KAAc,GACVA,MAAe,KACjBM,EAAA;AAAA,EAEJ;AACF,GAgBagB,IAAkB,CAAIC,GAAaC,MAA+B;AAC7E,MAAIC,IAAYD;AAEhB,MAAI;AACF,UAAME,IAAM,aAAa,QAAQH,CAAG;AACpC,IAAIG,MAAQ,SACVD,IAAS,KAAK,MAAMC,CAAG;AAAA,EAE3B,QAAQ;AAAA,EAER;AAEA,QAAMC,IAAMZ,EAAOU,CAAM;AAGzB,SAAAP,EAAO,MAAM;AACX,QAAI;AACF,mBAAa,QAAQK,GAAK,KAAK,UAAUI,EAAI,KAAK,CAAC;AAAA,IACrD,QAAQ;AAAA,IAER;AAAA,EACF,CAAC,GAEMA;AACT;"}
1
+ {"version":3,"file":"reactive.es.mjs","sources":["../src/reactive/signal.ts"],"sourcesContent":["/**\r\n * Reactive primitives inspired by fine-grained reactivity.\r\n *\r\n * This module provides a minimal but powerful reactive system:\r\n * - Signal: A reactive value that notifies subscribers when changed\r\n * - Computed: A derived value that automatically updates when dependencies change\r\n * - Effect: A side effect that re-runs when its dependencies change\r\n * - Batch: Group multiple updates to prevent intermediate re-renders\r\n *\r\n * @module bquery/reactive\r\n *\r\n * @example\r\n * ```ts\r\n * const count = signal(0);\r\n * const doubled = computed(() => count.value * 2);\r\n *\r\n * effect(() => {\r\n * console.log(`Count: ${count.value}, Doubled: ${doubled.value}`);\r\n * });\r\n *\r\n * batch(() => {\r\n * count.value = 1;\r\n * count.value = 2;\r\n * });\r\n * // Logs: \"Count: 2, Doubled: 4\" (only once due to batching)\r\n * ```\r\n */\r\n\r\n/**\r\n * Observer function type used internally for tracking reactivity.\r\n */\r\nexport type Observer = () => void;\r\n\r\n/**\r\n * Cleanup function returned by effects for disposal.\r\n */\r\nexport type CleanupFn = () => void;\r\n\r\n// Internal state for tracking the current observer context\r\nconst observerStack: Observer[] = [];\r\nlet batchDepth = 0;\r\nconst pendingObservers = new Set<Observer>();\r\n\r\n// Flag to disable tracking temporarily (for untrack)\r\nlet trackingEnabled = true;\r\n\r\n/**\r\n * Tracks dependencies during a function execution.\r\n * Uses direct push/pop for O(1) operations instead of array copying.\r\n * @internal\r\n */\r\nconst track = <T>(observer: Observer, fn: () => T): T => {\r\n observerStack.push(observer);\r\n try {\r\n return fn();\r\n } finally {\r\n observerStack.pop();\r\n }\r\n};\r\n\r\n/**\r\n * Schedules an observer to run, respecting batch mode.\r\n * @internal\r\n */\r\nconst scheduleObserver = (observer: Observer) => {\r\n if (batchDepth > 0) {\r\n pendingObservers.add(observer);\r\n return;\r\n }\r\n observer();\r\n};\r\n\r\n/**\r\n * Flushes all pending observers after a batch completes.\r\n * @internal\r\n */\r\nconst flushObservers = () => {\r\n for (const observer of Array.from(pendingObservers)) {\r\n pendingObservers.delete(observer);\r\n observer();\r\n }\r\n};\r\n\r\n/**\r\n * A reactive value container that notifies subscribers on change.\r\n *\r\n * Signals are the foundational primitive of the reactive system.\r\n * Reading a signal's value inside an effect or computed automatically\r\n * establishes a reactive dependency.\r\n *\r\n * @template T - The type of the stored value\r\n *\r\n * @example\r\n * ```ts\r\n * const name = signal('World');\r\n * console.log(name.value); // 'World'\r\n *\r\n * name.value = 'bQuery';\r\n * console.log(name.value); // 'bQuery'\r\n * ```\r\n */\r\nexport class Signal<T> {\r\n private subscribers = new Set<Observer>();\r\n\r\n /**\r\n * Creates a new signal with an initial value.\r\n * @param _value - The initial value\r\n */\r\n constructor(private _value: T) {}\r\n\r\n /**\r\n * Gets the current value and tracks the read if inside an observer.\r\n * Respects the global tracking state (disabled during untrack calls).\r\n */\r\n get value(): T {\r\n if (trackingEnabled) {\r\n const current = observerStack[observerStack.length - 1];\r\n if (current) {\r\n this.subscribers.add(current);\r\n }\r\n }\r\n return this._value;\r\n }\r\n\r\n /**\r\n * Sets a new value and notifies all subscribers if the value changed.\r\n * Uses Object.is for equality comparison.\r\n */\r\n set value(next: T) {\r\n if (Object.is(this._value, next)) return;\r\n this._value = next;\r\n for (const subscriber of this.subscribers) {\r\n scheduleObserver(subscriber);\r\n }\r\n }\r\n\r\n /**\r\n * Reads the current value without tracking.\r\n * Useful when you need the value but don't want to create a dependency.\r\n *\r\n * @returns The current value\r\n */\r\n peek(): T {\r\n return this._value;\r\n }\r\n\r\n /**\r\n * Updates the value using a function.\r\n * Useful for updates based on the current value.\r\n *\r\n * @param updater - Function that receives current value and returns new value\r\n */\r\n update(updater: (current: T) => T): void {\r\n this.value = updater(this._value);\r\n }\r\n}\r\n\r\n/**\r\n * A computed value that derives from other reactive sources.\r\n *\r\n * Computed values are lazily evaluated and cached. They only\r\n * recompute when their dependencies change.\r\n *\r\n * @template T - The type of the computed value\r\n *\r\n * @example\r\n * ```ts\r\n * const price = signal(100);\r\n * const quantity = signal(2);\r\n * const total = computed(() => price.value * quantity.value);\r\n *\r\n * console.log(total.value); // 200\r\n * price.value = 150;\r\n * console.log(total.value); // 300\r\n * ```\r\n */\r\nexport class Computed<T> {\r\n private cachedValue!: T;\r\n private dirty = true;\r\n private subscribers = new Set<Observer>();\r\n private readonly markDirty = () => {\r\n this.dirty = true;\r\n for (const subscriber of this.subscribers) {\r\n scheduleObserver(subscriber);\r\n }\r\n };\r\n\r\n /**\r\n * Creates a new computed value.\r\n * @param compute - Function that computes the value\r\n */\r\n constructor(private readonly compute: () => T) {}\r\n\r\n /**\r\n * Gets the computed value, recomputing if dependencies changed.\r\n */\r\n get value(): T {\r\n const current = observerStack[observerStack.length - 1];\r\n if (current) {\r\n this.subscribers.add(current);\r\n }\r\n if (this.dirty) {\r\n this.dirty = false;\r\n this.cachedValue = track(this.markDirty, this.compute);\r\n }\r\n return this.cachedValue;\r\n }\r\n}\r\n\r\n/**\r\n * Creates a new reactive signal.\r\n *\r\n * @template T - The type of the signal value\r\n * @param value - The initial value\r\n * @returns A new Signal instance\r\n *\r\n * @example\r\n * ```ts\r\n * const count = signal(0);\r\n * count.value++; // Triggers subscribers\r\n * ```\r\n */\r\nexport const signal = <T>(value: T): Signal<T> => new Signal(value);\r\n\r\n/**\r\n * Creates a new computed value.\r\n *\r\n * @template T - The type of the computed value\r\n * @param fn - Function that computes the value from reactive sources\r\n * @returns A new Computed instance\r\n *\r\n * @example\r\n * ```ts\r\n * const doubled = computed(() => count.value * 2);\r\n * ```\r\n */\r\nexport const computed = <T>(fn: () => T): Computed<T> => new Computed(fn);\r\n\r\n/**\r\n * Creates a side effect that automatically re-runs when dependencies change.\r\n *\r\n * The effect runs immediately upon creation and then re-runs whenever\r\n * any signal or computed value read inside it changes.\r\n *\r\n * @param fn - The effect function to run\r\n * @returns A cleanup function to stop the effect\r\n *\r\n * @example\r\n * ```ts\r\n * const count = signal(0);\r\n *\r\n * const cleanup = effect(() => {\r\n * document.title = `Count: ${count.value}`;\r\n * });\r\n *\r\n * // Later, to stop the effect:\r\n * cleanup();\r\n * ```\r\n */\r\nexport const effect = (fn: () => void | CleanupFn): CleanupFn => {\r\n let cleanupFn: CleanupFn | void;\r\n let isDisposed = false;\r\n\r\n const observer: Observer = () => {\r\n if (isDisposed) return;\r\n\r\n // Run previous cleanup if exists\r\n if (cleanupFn) {\r\n cleanupFn();\r\n }\r\n\r\n // Run effect and capture cleanup\r\n cleanupFn = track(observer, fn);\r\n };\r\n\r\n observer();\r\n\r\n return () => {\r\n isDisposed = true;\r\n if (cleanupFn) {\r\n cleanupFn();\r\n }\r\n };\r\n};\r\n\r\n/**\r\n * Batches multiple signal updates into a single notification cycle.\r\n *\r\n * Updates made inside the batch function are deferred until the batch\r\n * completes, preventing intermediate re-renders and improving performance.\r\n *\r\n * @param fn - Function containing multiple signal updates\r\n *\r\n * @example\r\n * ```ts\r\n * batch(() => {\r\n * firstName.value = 'John';\r\n * lastName.value = 'Doe';\r\n * age.value = 30;\r\n * });\r\n * // Effects only run once with all three updates\r\n * ```\r\n */\r\nexport const batch = (fn: () => void): void => {\r\n batchDepth += 1;\r\n try {\r\n fn();\r\n } finally {\r\n batchDepth -= 1;\r\n if (batchDepth === 0) {\r\n flushObservers();\r\n }\r\n }\r\n};\r\n\r\n/**\r\n * Creates a signal that persists to localStorage.\r\n *\r\n * @template T - The type of the signal value\r\n * @param key - The localStorage key\r\n * @param initialValue - The initial value if not found in storage\r\n * @returns A Signal that syncs with localStorage\r\n *\r\n * @example\r\n * ```ts\r\n * const theme = persistedSignal('theme', 'light');\r\n * theme.value = 'dark'; // Automatically saved to localStorage\r\n * ```\r\n */\r\nexport const persistedSignal = <T>(key: string, initialValue: T): Signal<T> => {\r\n let stored: T = initialValue;\r\n\r\n try {\r\n const raw = localStorage.getItem(key);\r\n if (raw !== null) {\r\n stored = JSON.parse(raw) as T;\r\n }\r\n } catch {\r\n // Use initial value on parse error\r\n }\r\n\r\n const sig = signal(stored);\r\n\r\n // Create an effect to persist changes\r\n effect(() => {\r\n try {\r\n localStorage.setItem(key, JSON.stringify(sig.value));\r\n } catch {\r\n // Ignore storage errors\r\n }\r\n });\r\n\r\n return sig;\r\n};\r\n\r\n// ============================================================================\r\n// Extended Reactive Utilities\r\n// ============================================================================\r\n\r\n/**\r\n * A readonly wrapper around a signal that prevents writes.\r\n * Provides read-only access to a signal's value while maintaining reactivity.\r\n *\r\n * @template T - The type of the wrapped value\r\n */\r\nexport interface ReadonlySignal<T> {\r\n /** Gets the current value with dependency tracking. */\r\n readonly value: T;\r\n /** Gets the current value without dependency tracking. */\r\n peek(): T;\r\n}\r\n\r\n/**\r\n * Creates a read-only view of a signal.\r\n * Useful for exposing reactive state without allowing modifications.\r\n *\r\n * @template T - The type of the signal value\r\n * @param sig - The signal to wrap\r\n * @returns A readonly signal wrapper\r\n *\r\n * @example\r\n * ```ts\r\n * const _count = signal(0);\r\n * const count = readonly(_count); // Expose read-only version\r\n *\r\n * console.log(count.value); // 0\r\n * count.value = 1; // TypeScript error: Cannot assign to 'value'\r\n * ```\r\n */\r\nexport const readonly = <T>(sig: Signal<T>): ReadonlySignal<T> => ({\r\n get value(): T {\r\n return sig.value;\r\n },\r\n peek(): T {\r\n return sig.peek();\r\n },\r\n});\r\n\r\n/**\r\n * Watches a signal or computed value and calls a callback with old and new values.\r\n * Unlike effect, watch provides access to the previous value.\r\n *\r\n * @template T - The type of the watched value\r\n * @param source - The signal or computed to watch\r\n * @param callback - Function called with (newValue, oldValue) on changes\r\n * @param options - Watch options\r\n * @returns A cleanup function to stop watching\r\n *\r\n * @example\r\n * ```ts\r\n * const count = signal(0);\r\n *\r\n * const cleanup = watch(count, (newVal, oldVal) => {\r\n * console.log(`Changed from ${oldVal} to ${newVal}`);\r\n * });\r\n *\r\n * count.value = 5; // Logs: \"Changed from 0 to 5\"\r\n * cleanup();\r\n * ```\r\n */\r\nexport const watch = <T>(\r\n source: Signal<T> | Computed<T>,\r\n callback: (newValue: T, oldValue: T | undefined) => void,\r\n options: { immediate?: boolean } = {}\r\n): CleanupFn => {\r\n let oldValue: T | undefined;\r\n let isFirst = true;\r\n\r\n return effect(() => {\r\n const newValue = source.value;\r\n\r\n if (isFirst) {\r\n isFirst = false;\r\n oldValue = newValue;\r\n if (options.immediate) {\r\n callback(newValue, undefined);\r\n }\r\n return;\r\n }\r\n\r\n callback(newValue, oldValue);\r\n oldValue = newValue;\r\n });\r\n};\r\n\r\n/**\r\n * Executes a function without tracking any signal dependencies.\r\n * Useful when reading a signal value without creating a reactive dependency.\r\n *\r\n * @template T - The return type of the function\r\n * @param fn - The function to execute without tracking\r\n * @returns The result of the function\r\n *\r\n * @example\r\n * ```ts\r\n * const count = signal(0);\r\n *\r\n * effect(() => {\r\n * // This creates a dependency\r\n * console.log('Tracked:', count.value);\r\n *\r\n * // This does NOT create a dependency\r\n * const untracked = untrack(() => otherSignal.value);\r\n * });\r\n * ```\r\n */\r\nexport const untrack = <T>(fn: () => T): T => {\r\n const prevTracking = trackingEnabled;\r\n trackingEnabled = false;\r\n try {\r\n return fn();\r\n } finally {\r\n trackingEnabled = prevTracking;\r\n }\r\n};\r\n\r\n/**\r\n * Type guard to check if a value is a Signal instance.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is a Signal\r\n *\r\n * @example\r\n * ```ts\r\n * const count = signal(0);\r\n * const num = 42;\r\n *\r\n * isSignal(count); // true\r\n * isSignal(num); // false\r\n * ```\r\n */\r\nexport const isSignal = (value: unknown): value is Signal<unknown> => value instanceof Signal;\r\n\r\n/**\r\n * Type guard to check if a value is a Computed instance.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is a Computed\r\n *\r\n * @example\r\n * ```ts\r\n * const doubled = computed(() => count.value * 2);\r\n * isComputed(doubled); // true\r\n * ```\r\n */\r\nexport const isComputed = (value: unknown): value is Computed<unknown> => value instanceof Computed;\r\n"],"names":["observerStack","batchDepth","pendingObservers","trackingEnabled","track","observer","fn","scheduleObserver","flushObservers","Signal","_value","current","next","subscriber","updater","Computed","compute","signal","value","computed","effect","cleanupFn","isDisposed","batch","persistedSignal","key","initialValue","stored","raw","sig","readonly","watch","source","callback","options","oldValue","isFirst","newValue","untrack","prevTracking","isSignal","isComputed"],"mappings":"AAuCA,MAAMA,IAA4B,CAAA;AAClC,IAAIC,IAAa;AACjB,MAAMC,wBAAuB,IAAA;AAG7B,IAAIC,IAAkB;AAOtB,MAAMC,IAAQ,CAAIC,GAAoBC,MAAmB;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A,IAER;AAAA,EACF,CAAC,GAEMA;AACT,GAoCaC,IAAW,CAAID,OAAuC;AAAA,EACjE,IAAI,QAAW;AACb,WAAOA,EAAI;AAAA,EACb;AAAA,EACA,OAAU;AACR,WAAOA,EAAI,KAAA;AAAA,EACb;AACF,IAwBaE,IAAQ,CACnBC,GACAC,GACAC,IAAmC,CAAA,MACrB;AACd,MAAIC,GACAC,IAAU;AAEd,SAAOhB,EAAO,MAAM;AAClB,UAAMiB,IAAWL,EAAO;AAExB,QAAII,GAAS;AACX,MAAAA,IAAU,IACVD,IAAWE,GACPH,EAAQ,aACVD,EAASI,GAAU,MAAS;AAE9B;AAAA,IACF;AAEA,IAAAJ,EAASI,GAAUF,CAAQ,GAC3BA,IAAWE;AAAA,EACb,CAAC;AACH,GAuBaC,IAAU,CAAIhC,MAAmB;AAC5C,QAAMiC,IAAepC;AACrB,EAAAA,IAAkB;AAClB,MAAI;AACF,WAAOG,EAAA;AAAA,EACT,UAAA;AACE,IAAAH,IAAkBoC;AAAA,EACpB;AACF,GAiBaC,IAAW,CAACtB,MAA6CA,aAAiBT,GAc1EgC,IAAa,CAACvB,MAA+CA,aAAiBH;"}
@@ -1 +1 @@
1
- {"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../../src/security/sanitize.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,kDAAkD;IAClD,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,wDAAwD;IACxD,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B,8CAA8C;IAC9C,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,4DAA4D;IAC5D,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAsBD,qCAAqC;AACrC,UAAU,iBAAiB;IACzB,UAAU,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,WAAW,CAAC;CAC5C;AAED,2EAA2E;AAC3E,UAAU,WAAW;IAC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