@vielzeug/stateit 2.0.0 → 2.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 (60) hide show
  1. package/README.md +54 -295
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.ts +1 -18
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +2 -9
  6. package/dist/stateit.cjs +1 -1
  7. package/dist/stateit.cjs.map +1 -1
  8. package/dist/stateit.d.ts +17 -0
  9. package/dist/stateit.d.ts.map +1 -0
  10. package/dist/stateit.js +180 -236
  11. package/dist/stateit.js.map +1 -1
  12. package/dist/types.d.ts +19 -20
  13. package/dist/types.d.ts.map +1 -1
  14. package/package.json +4 -3
  15. package/dist/batch.cjs +0 -2
  16. package/dist/batch.cjs.map +0 -1
  17. package/dist/batch.d.ts +0 -5
  18. package/dist/batch.d.ts.map +0 -1
  19. package/dist/batch.js +0 -28
  20. package/dist/batch.js.map +0 -1
  21. package/dist/computed.cjs +0 -2
  22. package/dist/computed.cjs.map +0 -1
  23. package/dist/computed.d.ts +0 -58
  24. package/dist/computed.d.ts.map +0 -1
  25. package/dist/computed.js +0 -65
  26. package/dist/computed.js.map +0 -1
  27. package/dist/effect.cjs +0 -2
  28. package/dist/effect.cjs.map +0 -1
  29. package/dist/effect.d.ts +0 -31
  30. package/dist/effect.d.ts.map +0 -1
  31. package/dist/effect.js +0 -53
  32. package/dist/effect.js.map +0 -1
  33. package/dist/runtime.cjs +0 -2
  34. package/dist/runtime.cjs.map +0 -1
  35. package/dist/runtime.d.ts +0 -40
  36. package/dist/runtime.d.ts.map +0 -1
  37. package/dist/runtime.js +0 -47
  38. package/dist/runtime.js.map +0 -1
  39. package/dist/signal.cjs +0 -2
  40. package/dist/signal.cjs.map +0 -1
  41. package/dist/signal.d.ts +0 -26
  42. package/dist/signal.d.ts.map +0 -1
  43. package/dist/signal.js +0 -40
  44. package/dist/signal.js.map +0 -1
  45. package/dist/store.cjs +0 -2
  46. package/dist/store.cjs.map +0 -1
  47. package/dist/store.d.ts +0 -32
  48. package/dist/store.d.ts.map +0 -1
  49. package/dist/store.js +0 -51
  50. package/dist/store.js.map +0 -1
  51. package/dist/types.cjs +0 -2
  52. package/dist/types.cjs.map +0 -1
  53. package/dist/types.js +0 -6
  54. package/dist/types.js.map +0 -1
  55. package/dist/watch.cjs +0 -2
  56. package/dist/watch.cjs.map +0 -1
  57. package/dist/watch.d.ts +0 -36
  58. package/dist/watch.d.ts.map +0 -1
  59. package/dist/watch.js +0 -32
  60. package/dist/watch.js.map +0 -1
@@ -1 +0,0 @@
1
- {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAY,MAAM,YAAY,CAAC;AAG3D,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,eAAe,EAAE,KAAK,MAAM,EAAgB,MAAM,SAAS,CAAC;AAE3F;;;;GAIG;AACH,eAAO,MAAM,YAAY,EAAE,UAAU,CAAC,OAAO,CAiB5C,CAAC;AAEF,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,IAAI;IAC3C,4EAA4E;IAC5E,MAAM,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC;CACxB,CAAC;AAEF,oGAAoG;AACpG,MAAM,WAAW,KAAK,CAAC,CAAC,SAAS,MAAM,CAAE,SAAQ,MAAM,CAAC,CAAC,CAAC;IACxD,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,6DAA6D;IAC7D,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IACjC;4FACwF;IACxF,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;IAC9B,2CAA2C;IAC3C,KAAK,IAAI,IAAI,CAAC;IACd,oFAAoF;IACpF,MAAM,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC;IAClF,oFAAoF;IACpF,MAAM,IAAI,IAAI,CAAC;CAChB;AAqDD,qDAAqD;AACrD,eAAO,MAAM,KAAK,GAAI,CAAC,SAAS,MAAM,EAAE,SAAS,CAAC,EAAE,UAAU,YAAY,CAAC,CAAC,CAAC,KAAG,KAAK,CAAC,CAAC,CACtD,CAAC;AAElC,gDAAgD;AAChD,eAAO,MAAM,OAAO,GAAI,CAAC,SAAS,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,OAAO,KAAG,KAAK,IAAI,KAAK,CAAC,CAAC,CAC/B,CAAC"}
package/dist/store.js DELETED
@@ -1,51 +0,0 @@
1
- import { _STORE_BRAND as e } from "./types.js";
2
- import { computed as t } from "./computed.js";
3
- import { SignalImpl as n } from "./signal.js";
4
- //#region src/store.ts
5
- var r = (e, t) => {
6
- if (e === t) return !0;
7
- if (e == null || t == null) return e === t;
8
- if (typeof e != "object" || typeof t != "object") return !1;
9
- let n = Object.keys(e), r = Object.keys(t);
10
- if (n.length !== r.length) return !1;
11
- for (let r of n) if (!Object.is(e[r], t[r])) return !1;
12
- return !0;
13
- }, i = class extends n {
14
- [e] = !0;
15
- #e;
16
- #t = !1;
17
- constructor(e, t) {
18
- super(e, { equals: t?.equals ?? r }), this.#e = { ...e };
19
- }
20
- get frozen() {
21
- return this.#t;
22
- }
23
- get value() {
24
- return super.value;
25
- }
26
- set value(e) {
27
- this.#t || (super.value = e);
28
- }
29
- patch(e) {
30
- this.value = {
31
- ...this.peek(),
32
- ...e
33
- };
34
- }
35
- update(e) {
36
- this.value = e({ ...this.peek() });
37
- }
38
- reset() {
39
- this.value = { ...this.#e };
40
- }
41
- select(e, n) {
42
- return t(() => e(this.value), n);
43
- }
44
- freeze() {
45
- this.#t = !0;
46
- }
47
- }, a = (e, t) => new i(e, t), o = (t) => typeof t == "object" && !!t && e in t;
48
- //#endregion
49
- export { o as isStore, r as shallowEqual, a as store };
50
-
51
- //# sourceMappingURL=store.js.map
package/dist/store.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"store.js","names":["#initial","#frozen"],"sources":["../src/store.ts"],"sourcesContent":["import { type ComputedSignal, computed } from './computed';\nimport { _DEV } from './runtime';\nimport { SignalImpl } from './signal';\nimport { type EqualityFn, type ReactiveOptions, type Signal, _STORE_BRAND } from './types';\n\n/**\n * Shallow structural equality — compares own enumerable keys by reference.\n * This is the default equality function used by `store()`. Export it to avoid\n * reimplementation when composing custom `StoreOptions.equals`.\n */\nexport const shallowEqual: EqualityFn<unknown> = (a, b) => {\n if (a === b) return true;\n\n if (a == null || b == null) return a === b;\n\n if (typeof a !== 'object' || typeof b !== 'object') return false;\n\n const keysA = Object.keys(a as object);\n const keysB = Object.keys(b as object);\n\n if (keysA.length !== keysB.length) return false;\n\n for (const key of keysA) {\n if (!Object.is((a as Record<string, unknown>)[key], (b as Record<string, unknown>)[key])) return false;\n }\n\n return true;\n};\n\nexport type StoreOptions<T extends object> = {\n /** Custom equality for top-level change detection. Default: shallowEqual */\n equals?: EqualityFn<T>;\n};\n\n/** Reactive store for object state. Implements Signal<T> so all signal primitives work natively. */\nexport interface Store<T extends object> extends Signal<T> {\n readonly frozen: boolean;\n /** Shallow-merge a partial object into the current state. */\n patch(partial: Partial<T>): void;\n /** Derive the next state from the current state via an updater function.\n * Receives a shallow copy of the current state — mutations in the updater are safe. */\n update(fn: (s: T) => T): void;\n /** Reset to the original initial state. */\n reset(): void;\n /** Create a lazily recomputed derived signal from a slice of this store's state. */\n select<U>(selector: (s: T) => U, options?: ReactiveOptions<U>): ComputedSignal<U>;\n /** Freeze the store. Further writes via patch/update/reset are silently ignored. */\n freeze(): void;\n}\n\n/** @internal */\nclass StoreImpl<T extends object> extends SignalImpl<T> implements Store<T> {\n readonly [_STORE_BRAND] = true as const;\n\n readonly #initial: T;\n #frozen = false;\n\n constructor(initial: T, options?: StoreOptions<T>) {\n super(initial, { equals: options?.equals ?? (shallowEqual as EqualityFn<T>) });\n this.#initial = { ...initial }; // defensive copy — external mutation cannot corrupt reset()\n }\n\n get frozen(): boolean {\n return this.#frozen;\n }\n\n override get value(): T {\n return super.value;\n }\n\n override set value(next: T) {\n if (this.#frozen) {\n if (_DEV) console.warn('[stateit] store is frozen — write ignored.');\n\n return;\n }\n\n super.value = next;\n }\n\n patch(partial: Partial<T>): void {\n this.value = { ...this.peek(), ...partial };\n }\n\n update(fn: (s: T) => T): void {\n this.value = fn({ ...this.peek() });\n }\n\n reset(): void {\n this.value = { ...this.#initial };\n }\n\n select<U>(selector: (s: T) => U, options?: ReactiveOptions<U>): ComputedSignal<U> {\n return computed(() => selector(this.value), options);\n }\n\n freeze(): void {\n this.#frozen = true;\n }\n}\n\n/** Creates a reactive store for the object state. */\nexport const store = <T extends object>(initial: T, options?: StoreOptions<T>): Store<T> =>\n new StoreImpl(initial, options);\n\n/** Type guard -- identifies Store instances. */\nexport const isStore = <T extends object = Record<string, unknown>>(value: unknown): value is Store<T> =>\n typeof value === 'object' && value !== null && _STORE_BRAND in value;\n"],"mappings":";;;;AAUA,IAAa,KAAqC,GAAG,MAAM;AACzD,KAAI,MAAM,EAAG,QAAO;AAEpB,KAAI,KAAK,QAAQ,KAAK,KAAM,QAAO,MAAM;AAEzC,KAAI,OAAO,KAAM,YAAY,OAAO,KAAM,SAAU,QAAO;CAE3D,IAAM,IAAQ,OAAO,KAAK,EAAY,EAChC,IAAQ,OAAO,KAAK,EAAY;AAEtC,KAAI,EAAM,WAAW,EAAM,OAAQ,QAAO;AAE1C,MAAK,IAAM,KAAO,EAChB,KAAI,CAAC,OAAO,GAAI,EAA8B,IAAO,EAA8B,GAAK,CAAE,QAAO;AAGnG,QAAO;GAyBH,IAAN,cAA0C,EAAkC;CAC1E,CAAU,KAAgB;CAE1B;CACA,KAAU;CAEV,YAAY,GAAY,GAA2B;AAEjD,EADA,MAAM,GAAS,EAAE,QAAQ,GAAS,UAAW,GAAgC,CAAC,EAC9E,MAAA,IAAgB,EAAE,GAAG,GAAS;;CAGhC,IAAI,SAAkB;AACpB,SAAO,MAAA;;CAGT,IAAa,QAAW;AACtB,SAAO,MAAM;;CAGf,IAAa,MAAM,GAAS;AACtB,QAAA,MAMJ,MAAM,QAAQ;;CAGhB,MAAM,GAA2B;AAC/B,OAAK,QAAQ;GAAE,GAAG,KAAK,MAAM;GAAE,GAAG;GAAS;;CAG7C,OAAO,GAAuB;AAC5B,OAAK,QAAQ,EAAG,EAAE,GAAG,KAAK,MAAM,EAAE,CAAC;;CAGrC,QAAc;AACZ,OAAK,QAAQ,EAAE,GAAG,MAAA,GAAe;;CAGnC,OAAU,GAAuB,GAAiD;AAChF,SAAO,QAAe,EAAS,KAAK,MAAM,EAAE,EAAQ;;CAGtD,SAAe;AACb,QAAA,IAAe;;GAKN,KAA2B,GAAY,MAClD,IAAI,EAAU,GAAS,EAAQ,EAGpB,KAAuD,MAClE,OAAO,KAAU,cAAY,KAAkB,KAAgB"}
package/dist/types.cjs DELETED
@@ -1,2 +0,0 @@
1
- var e=Symbol(`stateit.signal`),t=Symbol(`stateit.store`);exports._SIGNAL_BRAND=e,exports._STORE_BRAND=t;
2
- //# sourceMappingURL=types.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.cjs","names":[],"sources":["../src/types.ts"],"sourcesContent":["/** A function that tears down a subscription or effect. */\nexport type CleanupFn = () => void;\n\n/** The shape of a function passed to `effect()`. Can return an optional cleanup. */\nexport type EffectCallback = () => CleanupFn | void;\n\nexport type EqualityFn<T> = (a: T, b: T) => boolean;\n\n/** Options accepted by signal(), computed(), writable(), and store(). */\nexport type ReactiveOptions<T> = { equals?: EqualityFn<T> };\n\n/** Shared interface for disposable reactive values (computed, writable). Supports the TC39 `using` declaration. */\nexport interface Disposable {\n dispose(): void;\n [Symbol.dispose](): void;\n}\n\n/**\n * A callable subscription handle returned by `effect()` and `watch()`.\n * Can be called directly or via `.dispose()` to stop the subscription.\n * Supports the TC39 `using` declaration via `[Symbol.dispose]`.\n */\nexport interface Subscription {\n (): void;\n dispose(): void;\n [Symbol.dispose](): void;\n}\n\nexport const _SIGNAL_BRAND = Symbol('stateit.signal');\nexport const _STORE_BRAND = Symbol('stateit.store');\n\n/** Public read-only signal interface. */\nexport interface ReadonlySignal<T> {\n readonly [_SIGNAL_BRAND]: true;\n readonly value: T;\n peek(): T;\n}\n\n/** Public read/write signal interface. */\nexport interface Signal<T> extends ReadonlySignal<T> {\n value: T;\n /** Update the value by applying a function to the current value. */\n update(fn: (current: T) => T): void;\n}\n"],"mappings":"AA4BA,IAAa,EAAgB,OAAO,iBAAiB,CACxC,EAAe,OAAO,gBAAgB"}
package/dist/types.js DELETED
@@ -1,6 +0,0 @@
1
- //#region src/types.ts
2
- var e = Symbol("stateit.signal"), t = Symbol("stateit.store");
3
- //#endregion
4
- export { e as _SIGNAL_BRAND, t as _STORE_BRAND };
5
-
6
- //# sourceMappingURL=types.js.map
package/dist/types.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","names":[],"sources":["../src/types.ts"],"sourcesContent":["/** A function that tears down a subscription or effect. */\nexport type CleanupFn = () => void;\n\n/** The shape of a function passed to `effect()`. Can return an optional cleanup. */\nexport type EffectCallback = () => CleanupFn | void;\n\nexport type EqualityFn<T> = (a: T, b: T) => boolean;\n\n/** Options accepted by signal(), computed(), writable(), and store(). */\nexport type ReactiveOptions<T> = { equals?: EqualityFn<T> };\n\n/** Shared interface for disposable reactive values (computed, writable). Supports the TC39 `using` declaration. */\nexport interface Disposable {\n dispose(): void;\n [Symbol.dispose](): void;\n}\n\n/**\n * A callable subscription handle returned by `effect()` and `watch()`.\n * Can be called directly or via `.dispose()` to stop the subscription.\n * Supports the TC39 `using` declaration via `[Symbol.dispose]`.\n */\nexport interface Subscription {\n (): void;\n dispose(): void;\n [Symbol.dispose](): void;\n}\n\nexport const _SIGNAL_BRAND = Symbol('stateit.signal');\nexport const _STORE_BRAND = Symbol('stateit.store');\n\n/** Public read-only signal interface. */\nexport interface ReadonlySignal<T> {\n readonly [_SIGNAL_BRAND]: true;\n readonly value: T;\n peek(): T;\n}\n\n/** Public read/write signal interface. */\nexport interface Signal<T> extends ReadonlySignal<T> {\n value: T;\n /** Update the value by applying a function to the current value. */\n update(fn: (current: T) => T): void;\n}\n"],"mappings":";AA4BA,IAAa,IAAgB,OAAO,iBAAiB,EACxC,IAAe,OAAO,gBAAgB"}
package/dist/watch.cjs DELETED
@@ -1,2 +0,0 @@
1
- const e=require(`./effect.cjs`);var t=(t,n,r)=>{let i=r?.equals??Object.is,a=t.peek();r?.immediate&&n(a,a);let o;return o=e.effect(()=>{let e=t.value;if(!i(a,e)){let t=a;a=e,n(e,t),r?.once&&o()}}),o},n=(e,n,r)=>new Promise((i,a)=>{let o=r?.signal;try{o?.throwIfAborted()}catch(e){a(e);return}let s=t(e,e=>{(!n||n(e))&&(s(),o?.removeEventListener(`abort`,c),i(e))}),c=()=>{s(),a(o.reason)};o?.addEventListener(`abort`,c,{once:!0})});exports.nextValue=n,exports.watch=t;
2
- //# sourceMappingURL=watch.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"watch.cjs","names":[],"sources":["../src/watch.ts"],"sourcesContent":["import { effect } from './effect';\nimport { type EqualityFn, type ReadonlySignal, type Subscription } from './types';\n\nexport type WatchOptions<T> = {\n /** Custom equality; suppresses the callback when old and new values are equal. Default: Object.is */\n equals?: EqualityFn<T>;\n /** Fire the callback immediately with the current value on subscribe. */\n immediate?: boolean;\n /**\n * Auto-unsubscribe after the first *change* invocation.\n * When combined with `immediate`, the immediate call does not count against this quota —\n * the callback may fire up to twice total.\n */\n once?: boolean;\n};\n\n/**\n * Watches a Signal and calls cb when its value changes. Returns a dispose handle.\n * Does not fire on initial subscription unless `{ immediate: true }` is passed.\n * To watch a derived slice, compose with `store.select()` or `computed()`:\n *\n * @example\n * const stop = watch(count, (next, prev) => console.log(prev, '->', next));\n * const stop = watch(userStore.select(s => s.name), (name) => ...);\n */\nexport const watch = <T>(\n source: ReadonlySignal<T>,\n cb: (value: T, prev: T) => void,\n options?: WatchOptions<T>,\n): Subscription => {\n const eq: EqualityFn<T> = options?.equals ?? Object.is;\n let prev = source.peek();\n\n if (options?.immediate) cb(prev, prev);\n\n // Use `let` so the stop reference is readable inside the callback without TDZ risk.\n let stop: Subscription;\n\n // eslint-disable-next-line prefer-const\n stop = effect(() => {\n const next = source.value;\n\n if (!eq(prev, next)) {\n const old = prev;\n\n prev = next;\n cb(next, old);\n\n if (options?.once) stop();\n }\n });\n\n return stop;\n};\n\n/**\n * Returns a Promise that resolves with the next value of `source` that satisfies the optional predicate.\n * Disposes automatically after one emission — no cleanup needed.\n * Pass `{ signal: AbortSignal }` to cancel early; the promise rejects with the abort reason.\n *\n * @example\n * const name = await nextValue(userStore.select(s => s.name));\n * const nonZero = await nextValue(count, v => v > 0, { signal: AbortSignal.timeout(5000) });\n */\nexport const nextValue = <T>(\n source: ReadonlySignal<T>,\n predicate?: (v: T) => boolean,\n options?: { signal?: AbortSignal },\n): Promise<T> =>\n new Promise<T>((resolve, reject) => {\n const signal = options?.signal;\n\n try {\n signal?.throwIfAborted();\n } catch (err) {\n reject(err);\n\n return;\n }\n\n const stop = watch(source, (v) => {\n if (!predicate || predicate(v)) {\n stop();\n signal?.removeEventListener('abort', onAbort);\n resolve(v);\n }\n });\n\n const onAbort = () => {\n stop();\n reject(signal!.reason);\n };\n\n signal?.addEventListener('abort', onAbort, { once: true });\n });\n"],"mappings":"gCAyBA,IAAa,GACX,EACA,EACA,IACiB,CACjB,IAAM,EAAoB,GAAS,QAAU,OAAO,GAChD,EAAO,EAAO,MAAM,CAEpB,GAAS,WAAW,EAAG,EAAM,EAAK,CAGtC,IAAI,EAgBJ,MAbA,GAAO,EAAA,WAAa,CAClB,IAAM,EAAO,EAAO,MAEpB,GAAI,CAAC,EAAG,EAAM,EAAK,CAAE,CACnB,IAAM,EAAM,EAEZ,EAAO,EACP,EAAG,EAAM,EAAI,CAET,GAAS,MAAM,GAAM,GAE3B,CAEK,GAYI,GACX,EACA,EACA,IAEA,IAAI,SAAY,EAAS,IAAW,CAClC,IAAM,EAAS,GAAS,OAExB,GAAI,CACF,GAAQ,gBAAgB,OACjB,EAAK,CACZ,EAAO,EAAI,CAEX,OAGF,IAAM,EAAO,EAAM,EAAS,GAAM,EAC5B,CAAC,GAAa,EAAU,EAAE,IAC5B,GAAM,CACN,GAAQ,oBAAoB,QAAS,EAAQ,CAC7C,EAAQ,EAAE,GAEZ,CAEI,MAAgB,CACpB,GAAM,CACN,EAAO,EAAQ,OAAO,EAGxB,GAAQ,iBAAiB,QAAS,EAAS,CAAE,KAAM,GAAM,CAAC,EAC1D"}
package/dist/watch.d.ts DELETED
@@ -1,36 +0,0 @@
1
- import { type EqualityFn, type ReadonlySignal, type Subscription } from './types';
2
- export type WatchOptions<T> = {
3
- /** Custom equality; suppresses the callback when old and new values are equal. Default: Object.is */
4
- equals?: EqualityFn<T>;
5
- /** Fire the callback immediately with the current value on subscribe. */
6
- immediate?: boolean;
7
- /**
8
- * Auto-unsubscribe after the first *change* invocation.
9
- * When combined with `immediate`, the immediate call does not count against this quota —
10
- * the callback may fire up to twice total.
11
- */
12
- once?: boolean;
13
- };
14
- /**
15
- * Watches a Signal and calls cb when its value changes. Returns a dispose handle.
16
- * Does not fire on initial subscription unless `{ immediate: true }` is passed.
17
- * To watch a derived slice, compose with `store.select()` or `computed()`:
18
- *
19
- * @example
20
- * const stop = watch(count, (next, prev) => console.log(prev, '->', next));
21
- * const stop = watch(userStore.select(s => s.name), (name) => ...);
22
- */
23
- export declare const watch: <T>(source: ReadonlySignal<T>, cb: (value: T, prev: T) => void, options?: WatchOptions<T>) => Subscription;
24
- /**
25
- * Returns a Promise that resolves with the next value of `source` that satisfies the optional predicate.
26
- * Disposes automatically after one emission — no cleanup needed.
27
- * Pass `{ signal: AbortSignal }` to cancel early; the promise rejects with the abort reason.
28
- *
29
- * @example
30
- * const name = await nextValue(userStore.select(s => s.name));
31
- * const nonZero = await nextValue(count, v => v > 0, { signal: AbortSignal.timeout(5000) });
32
- */
33
- export declare const nextValue: <T>(source: ReadonlySignal<T>, predicate?: (v: T) => boolean, options?: {
34
- signal?: AbortSignal;
35
- }) => Promise<T>;
36
- //# sourceMappingURL=watch.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"watch.d.ts","sourceRoot":"","sources":["../src/watch.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,cAAc,EAAE,KAAK,YAAY,EAAE,MAAM,SAAS,CAAC;AAElF,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI;IAC5B,qGAAqG;IACrG,MAAM,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC;IACvB,yEAAyE;IACzE,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;;;OAIG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,KAAK,GAAI,CAAC,EACrB,QAAQ,cAAc,CAAC,CAAC,CAAC,EACzB,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,KAAK,IAAI,EAC/B,UAAU,YAAY,CAAC,CAAC,CAAC,KACxB,YAwBF,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,EACzB,QAAQ,cAAc,CAAC,CAAC,CAAC,EACzB,YAAY,CAAC,CAAC,EAAE,CAAC,KAAK,OAAO,EAC7B,UAAU;IAAE,MAAM,CAAC,EAAE,WAAW,CAAA;CAAE,KACjC,OAAO,CAAC,CAAC,CA0BR,CAAC"}
package/dist/watch.js DELETED
@@ -1,32 +0,0 @@
1
- import { effect as e } from "./effect.js";
2
- //#region src/watch.ts
3
- var t = (t, n, r) => {
4
- let i = r?.equals ?? Object.is, a = t.peek();
5
- r?.immediate && n(a, a);
6
- let o;
7
- return o = e(() => {
8
- let e = t.value;
9
- if (!i(a, e)) {
10
- let t = a;
11
- a = e, n(e, t), r?.once && o();
12
- }
13
- }), o;
14
- }, n = (e, n, r) => new Promise((i, a) => {
15
- let o = r?.signal;
16
- try {
17
- o?.throwIfAborted();
18
- } catch (e) {
19
- a(e);
20
- return;
21
- }
22
- let s = t(e, (e) => {
23
- (!n || n(e)) && (s(), o?.removeEventListener("abort", c), i(e));
24
- }), c = () => {
25
- s(), a(o.reason);
26
- };
27
- o?.addEventListener("abort", c, { once: !0 });
28
- });
29
- //#endregion
30
- export { n as nextValue, t as watch };
31
-
32
- //# sourceMappingURL=watch.js.map
package/dist/watch.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"watch.js","names":[],"sources":["../src/watch.ts"],"sourcesContent":["import { effect } from './effect';\nimport { type EqualityFn, type ReadonlySignal, type Subscription } from './types';\n\nexport type WatchOptions<T> = {\n /** Custom equality; suppresses the callback when old and new values are equal. Default: Object.is */\n equals?: EqualityFn<T>;\n /** Fire the callback immediately with the current value on subscribe. */\n immediate?: boolean;\n /**\n * Auto-unsubscribe after the first *change* invocation.\n * When combined with `immediate`, the immediate call does not count against this quota —\n * the callback may fire up to twice total.\n */\n once?: boolean;\n};\n\n/**\n * Watches a Signal and calls cb when its value changes. Returns a dispose handle.\n * Does not fire on initial subscription unless `{ immediate: true }` is passed.\n * To watch a derived slice, compose with `store.select()` or `computed()`:\n *\n * @example\n * const stop = watch(count, (next, prev) => console.log(prev, '->', next));\n * const stop = watch(userStore.select(s => s.name), (name) => ...);\n */\nexport const watch = <T>(\n source: ReadonlySignal<T>,\n cb: (value: T, prev: T) => void,\n options?: WatchOptions<T>,\n): Subscription => {\n const eq: EqualityFn<T> = options?.equals ?? Object.is;\n let prev = source.peek();\n\n if (options?.immediate) cb(prev, prev);\n\n // Use `let` so the stop reference is readable inside the callback without TDZ risk.\n let stop: Subscription;\n\n // eslint-disable-next-line prefer-const\n stop = effect(() => {\n const next = source.value;\n\n if (!eq(prev, next)) {\n const old = prev;\n\n prev = next;\n cb(next, old);\n\n if (options?.once) stop();\n }\n });\n\n return stop;\n};\n\n/**\n * Returns a Promise that resolves with the next value of `source` that satisfies the optional predicate.\n * Disposes automatically after one emission — no cleanup needed.\n * Pass `{ signal: AbortSignal }` to cancel early; the promise rejects with the abort reason.\n *\n * @example\n * const name = await nextValue(userStore.select(s => s.name));\n * const nonZero = await nextValue(count, v => v > 0, { signal: AbortSignal.timeout(5000) });\n */\nexport const nextValue = <T>(\n source: ReadonlySignal<T>,\n predicate?: (v: T) => boolean,\n options?: { signal?: AbortSignal },\n): Promise<T> =>\n new Promise<T>((resolve, reject) => {\n const signal = options?.signal;\n\n try {\n signal?.throwIfAborted();\n } catch (err) {\n reject(err);\n\n return;\n }\n\n const stop = watch(source, (v) => {\n if (!predicate || predicate(v)) {\n stop();\n signal?.removeEventListener('abort', onAbort);\n resolve(v);\n }\n });\n\n const onAbort = () => {\n stop();\n reject(signal!.reason);\n };\n\n signal?.addEventListener('abort', onAbort, { once: true });\n });\n"],"mappings":";;AAyBA,IAAa,KACX,GACA,GACA,MACiB;CACjB,IAAM,IAAoB,GAAS,UAAU,OAAO,IAChD,IAAO,EAAO,MAAM;AAExB,CAAI,GAAS,aAAW,EAAG,GAAM,EAAK;CAGtC,IAAI;AAgBJ,QAbA,IAAO,QAAa;EAClB,IAAM,IAAO,EAAO;AAEpB,MAAI,CAAC,EAAG,GAAM,EAAK,EAAE;GACnB,IAAM,IAAM;AAKZ,GAHA,IAAO,GACP,EAAG,GAAM,EAAI,EAET,GAAS,QAAM,GAAM;;GAE3B,EAEK;GAYI,KACX,GACA,GACA,MAEA,IAAI,SAAY,GAAS,MAAW;CAClC,IAAM,IAAS,GAAS;AAExB,KAAI;AACF,KAAQ,gBAAgB;UACjB,GAAK;AACZ,IAAO,EAAI;AAEX;;CAGF,IAAM,IAAO,EAAM,IAAS,MAAM;AAChC,GAAI,CAAC,KAAa,EAAU,EAAE,MAC5B,GAAM,EACN,GAAQ,oBAAoB,SAAS,EAAQ,EAC7C,EAAQ,EAAE;GAEZ,EAEI,UAAgB;AAEpB,EADA,GAAM,EACN,EAAO,EAAQ,OAAO;;AAGxB,IAAQ,iBAAiB,SAAS,GAAS,EAAE,MAAM,IAAM,CAAC;EAC1D"}