@silvery/signals 0.5.0 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Bjørn Stabell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # @silvery/signals
2
+
3
+ Reactive signals for silvery -- thin wrapper around [alien-signals](https://github.com/nicepkg/alien-signals).
4
+
5
+ Provides fine-grained reactivity: signals hold values, computeds derive from them, and effects re-run when dependencies change.
6
+
7
+ Part of the [Silvery](https://silvery.dev) ecosystem.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install @silvery/signals
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```ts
18
+ import { signal, computed, effect, batch } from "@silvery/signals"
19
+
20
+ const count = signal(0)
21
+ const doubled = computed(() => count() * 2)
22
+ effect(() => console.log(`doubled: ${doubled()}`)) // logs "doubled: 0"
23
+
24
+ batch(() => {
25
+ count(1)
26
+ count(2)
27
+ }) // logs "doubled: 4" (once, not twice)
28
+ ```
29
+
30
+ ## API
31
+
32
+ ### Core
33
+
34
+ - **`signal(value)`** -- Create a reactive value (call to read, call with arg to write)
35
+ - **`computed(fn)`** -- Derived value that recomputes when dependencies change
36
+ - **`effect(fn)`** -- Side effect that re-runs when dependencies change
37
+ - **`batch(fn)`** -- Batch multiple updates into one notification
38
+
39
+ ### Advanced
40
+
41
+ - **`effectScope(fn)`** -- Group effects for collective disposal
42
+ - **`startBatch()` / `endBatch()`** -- Manual batch control
43
+ - **`trigger(signal)`** -- Force subscribers to re-run
44
+ - **`isSignal(v)`**, **`isComputed(v)`**, **`isEffect(v)`**, **`isEffectScope(v)`** -- Type guards
45
+
46
+ ## License
47
+
48
+ MIT
@@ -0,0 +1,56 @@
1
+ import { computed, effect, effectScope, endBatch, getActiveSub, getBatchDepth, isComputed, isEffect, isEffectScope, isSignal, setActiveSub, signal, startBatch, trigger } from "alien-signals";
2
+
3
+ //#region src/index.d.ts
4
+ /**
5
+ * A reactive value — callable getter/setter.
6
+ *
7
+ * - `sig()` reads the current value (and subscribes the active effect/computed).
8
+ * - `sig(next)` writes a new value; subscribers re-run only if `next !== current`.
9
+ *
10
+ * Matches the return shape of `signal<T>(initial)` from alien-signals, so any
11
+ * `signal()` result is assignable to `Signal<T>`.
12
+ */
13
+ type Signal<T> = {
14
+ (): T;
15
+ (value: T): void;
16
+ };
17
+ /**
18
+ * A read-only reactive value — callable getter that subscribes the active
19
+ * effect/computed but cannot be written to. Matches the return shape of
20
+ * `computed<T>(fn)` from alien-signals.
21
+ */
22
+ type ReadSignal<T> = () => T;
23
+ /** Batch multiple signal updates, notifying subscribers once at the end. */
24
+ declare function batch(fn: () => void): void;
25
+ /**
26
+ * Options for {@link watch}.
27
+ */
28
+ interface WatchOptions<T> {
29
+ /** If true, fire `onChange` on the seed read with `(next, next)`. Default false (change-only). */
30
+ immediate?: boolean;
31
+ /** Equality predicate — handler fires only when `equals(next, prev)` is false. Default `Object.is`. */
32
+ equals?: (a: T, b: T) => boolean;
33
+ }
34
+ /**
35
+ * Subscribe to signal changes via an imperative callback.
36
+ *
37
+ * alien-signals' `effect()` runs once eagerly to establish dependencies,
38
+ * which means an `effect(() => { handler(sig()) })` fires with the seed
39
+ * value. `watch()` swallows that first fire and only calls `onChange` when
40
+ * the value actually changes — matching the old `.subscribe(handler)`
41
+ * semantic consumers often want.
42
+ *
43
+ * ```ts
44
+ * const stop = watch(
45
+ * () => size.snapshot(),
46
+ * (next, prev) => { emit({ cols: next.cols, rows: next.rows }) },
47
+ * )
48
+ * // stop() to unsubscribe
49
+ * ```
50
+ *
51
+ * Returns the effect's stop function.
52
+ */
53
+ declare function watch<T>(read: () => T, onChange: (next: T, prev: T) => void, opts?: WatchOptions<T>): () => void;
54
+ //#endregion
55
+ export { ReadSignal, Signal, WatchOptions, batch, computed, effect, effectScope, endBatch, getActiveSub, getBatchDepth, isComputed, isEffect, isEffectScope, isSignal, setActiveSub, signal, startBatch, trigger, watch };
56
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;;AAuDA;;;;;KAbY,MAAA;EAAA,IACN,CAAA;EAAA,CACH,KAAA,EAAO,CAAA;AAAA;;;;;;KAQE,UAAA,YAAsB,CAAA;;iBAGlB,KAAA,CAAM,EAAA;;AAsCtB;;UA1BiB,YAAA;EA2BH;EAzBZ,SAAA;EA0B0B;EAxB1B,MAAA,IAAU,CAAA,EAAG,CAAA,EAAG,CAAA,EAAG,CAAA;AAAA;;;;;;;;;;;;;;;;;;;;iBAsBL,KAAA,GAAA,CACd,IAAA,QAAY,CAAA,EACZ,QAAA,GAAW,IAAA,EAAM,CAAA,EAAG,IAAA,EAAM,CAAA,WAC1B,IAAA,GAAM,YAAA,CAAa,CAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,52 @@
1
+ import { computed, effect, effect as effect$1, effectScope, endBatch, endBatch as endBatch$1, getActiveSub, getBatchDepth, isComputed, isEffect, isEffectScope, isSignal, setActiveSub, signal, startBatch, startBatch as startBatch$1, trigger } from "alien-signals";
2
+ //#region src/index.ts
3
+ /** Batch multiple signal updates, notifying subscribers once at the end. */
4
+ function batch(fn) {
5
+ startBatch$1();
6
+ try {
7
+ fn();
8
+ } finally {
9
+ endBatch$1();
10
+ }
11
+ }
12
+ /**
13
+ * Subscribe to signal changes via an imperative callback.
14
+ *
15
+ * alien-signals' `effect()` runs once eagerly to establish dependencies,
16
+ * which means an `effect(() => { handler(sig()) })` fires with the seed
17
+ * value. `watch()` swallows that first fire and only calls `onChange` when
18
+ * the value actually changes — matching the old `.subscribe(handler)`
19
+ * semantic consumers often want.
20
+ *
21
+ * ```ts
22
+ * const stop = watch(
23
+ * () => size.snapshot(),
24
+ * (next, prev) => { emit({ cols: next.cols, rows: next.rows }) },
25
+ * )
26
+ * // stop() to unsubscribe
27
+ * ```
28
+ *
29
+ * Returns the effect's stop function.
30
+ */
31
+ function watch(read, onChange, opts = {}) {
32
+ const equals = opts.equals ?? Object.is;
33
+ let seeded = false;
34
+ let prev;
35
+ return effect$1(() => {
36
+ const next = read();
37
+ if (!seeded) {
38
+ seeded = true;
39
+ prev = next;
40
+ if (opts.immediate) onChange(next, next);
41
+ return;
42
+ }
43
+ if (equals(next, prev)) return;
44
+ const old = prev;
45
+ prev = next;
46
+ onChange(next, old);
47
+ });
48
+ }
49
+ //#endregion
50
+ export { batch, computed, effect, effectScope, endBatch, getActiveSub, getBatchDepth, isComputed, isEffect, isEffectScope, isSignal, setActiveSub, signal, startBatch, trigger, watch };
51
+
52
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":["effect"],"sources":["../src/index.ts"],"sourcesContent":["/**\n * @silvery/signals — Reactive signals for silvery.\n *\n * Thin wrapper around alien-signals providing:\n * - signal(value) — create a reactive value (call to read, call with arg to write)\n * - computed(fn) — derived reactive value that recomputes on dependency changes\n * - effect(fn) — side effect that re-runs when dependencies change\n * - effectScope(fn) — group effects for collective disposal\n * - batch(fn) — batch multiple signal updates into one notification\n * - watch(read, onChange) — change-only subscription (skips the seed fire)\n *\n * @packageDocumentation\n */\n\nexport {\n signal,\n computed,\n effect,\n effectScope,\n startBatch,\n endBatch,\n trigger,\n isSignal,\n isComputed,\n isEffect,\n isEffectScope,\n getActiveSub,\n setActiveSub,\n getBatchDepth,\n} from \"alien-signals\"\n\nimport { effect, startBatch, endBatch } from \"alien-signals\"\n\n/**\n * A reactive value — callable getter/setter.\n *\n * - `sig()` reads the current value (and subscribes the active effect/computed).\n * - `sig(next)` writes a new value; subscribers re-run only if `next !== current`.\n *\n * Matches the return shape of `signal<T>(initial)` from alien-signals, so any\n * `signal()` result is assignable to `Signal<T>`.\n */\nexport type Signal<T> = {\n (): T\n (value: T): void\n}\n\n/**\n * A read-only reactive value — callable getter that subscribes the active\n * effect/computed but cannot be written to. Matches the return shape of\n * `computed<T>(fn)` from alien-signals.\n */\nexport type ReadSignal<T> = () => T\n\n/** Batch multiple signal updates, notifying subscribers once at the end. */\nexport function batch(fn: () => void): void {\n startBatch()\n try {\n fn()\n } finally {\n endBatch()\n }\n}\n\n/**\n * Options for {@link watch}.\n */\nexport interface WatchOptions<T> {\n /** If true, fire `onChange` on the seed read with `(next, next)`. Default false (change-only). */\n immediate?: boolean\n /** Equality predicate — handler fires only when `equals(next, prev)` is false. Default `Object.is`. */\n equals?: (a: T, b: T) => boolean\n}\n\n/**\n * Subscribe to signal changes via an imperative callback.\n *\n * alien-signals' `effect()` runs once eagerly to establish dependencies,\n * which means an `effect(() => { handler(sig()) })` fires with the seed\n * value. `watch()` swallows that first fire and only calls `onChange` when\n * the value actually changes — matching the old `.subscribe(handler)`\n * semantic consumers often want.\n *\n * ```ts\n * const stop = watch(\n * () => size.snapshot(),\n * (next, prev) => { emit({ cols: next.cols, rows: next.rows }) },\n * )\n * // stop() to unsubscribe\n * ```\n *\n * Returns the effect's stop function.\n */\nexport function watch<T>(\n read: () => T,\n onChange: (next: T, prev: T) => void,\n opts: WatchOptions<T> = {},\n): () => void {\n const equals = opts.equals ?? Object.is\n let seeded = false\n let prev!: T\n return effect(() => {\n const next = read()\n if (!seeded) {\n seeded = true\n prev = next\n if (opts.immediate) onChange(next, next)\n return\n }\n if (equals(next, prev)) return\n const old = prev\n prev = next\n onChange(next, old)\n })\n}\n"],"mappings":";;;AAuDA,SAAgB,MAAM,IAAsB;AAC1C,eAAY;AACZ,KAAI;AACF,MAAI;WACI;AACR,cAAU;;;;;;;;;;;;;;;;;;;;;;AAiCd,SAAgB,MACd,MACA,UACA,OAAwB,EAAE,EACd;CACZ,MAAM,SAAS,KAAK,UAAU,OAAO;CACrC,IAAI,SAAS;CACb,IAAI;AACJ,QAAOA,eAAa;EAClB,MAAM,OAAO,MAAM;AACnB,MAAI,CAAC,QAAQ;AACX,YAAS;AACT,UAAO;AACP,OAAI,KAAK,UAAW,UAAS,MAAM,KAAK;AACxC;;AAEF,MAAI,OAAO,MAAM,KAAK,CAAE;EACxB,MAAM,MAAM;AACZ,SAAO;AACP,WAAS,MAAM,IAAI;GACnB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@silvery/signals",
3
- "version": "0.5.0",
3
+ "version": "0.23.0",
4
4
  "description": "Reactive signals for silvery — thin wrapper around alien-signals",
5
5
  "license": "MIT",
6
6
  "author": "Bjørn Stabell <bjorn@stabell.org>",
@@ -10,21 +10,28 @@
10
10
  "directory": "packages/signals"
11
11
  },
12
12
  "files": [
13
- "src"
13
+ "dist"
14
14
  ],
15
15
  "type": "module",
16
- "main": "src/index.ts",
17
- "types": "src/index.ts",
18
16
  "exports": {
19
17
  ".": {
20
- "types": "./src/index.ts",
21
- "import": "./src/index.ts"
18
+ "types": "./dist/index.d.mts",
19
+ "import": "./dist/index.mjs"
22
20
  }
23
21
  },
24
22
  "publishConfig": {
25
23
  "access": "public"
26
24
  },
27
25
  "dependencies": {
28
- "alien-signals": "^3.1.2"
26
+ "alien-signals": "~3.2.1"
27
+ },
28
+ "tsdown": {
29
+ "clean": true,
30
+ "dts": true,
31
+ "entry": "src/index.ts",
32
+ "format": "esm"
33
+ },
34
+ "scripts": {
35
+ "build": "tsdown"
29
36
  }
30
- }
37
+ }
package/src/index.ts DELETED
@@ -1,41 +0,0 @@
1
- /**
2
- * @silvery/signals — Reactive signals for silvery.
3
- *
4
- * Thin wrapper around alien-signals providing:
5
- * - signal(value) — create a reactive value (call to read, call with arg to write)
6
- * - computed(fn) — derived reactive value that recomputes on dependency changes
7
- * - effect(fn) — side effect that re-runs when dependencies change
8
- * - effectScope(fn) — group effects for collective disposal
9
- * - batch(fn) — batch multiple signal updates into one notification
10
- *
11
- * @packageDocumentation
12
- */
13
-
14
- export {
15
- signal,
16
- computed,
17
- effect,
18
- effectScope,
19
- startBatch,
20
- endBatch,
21
- trigger,
22
- isSignal,
23
- isComputed,
24
- isEffect,
25
- isEffectScope,
26
- getActiveSub,
27
- setActiveSub,
28
- getBatchDepth,
29
- } from "alien-signals"
30
-
31
- import { startBatch, endBatch } from "alien-signals"
32
-
33
- /** Batch multiple signal updates, notifying subscribers once at the end. */
34
- export function batch(fn: () => void): void {
35
- startBatch()
36
- try {
37
- fn()
38
- } finally {
39
- endBatch()
40
- }
41
- }