@xmachines/play-signals 1.0.0-beta.45 → 1.0.0-beta.47

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 (2) hide show
  1. package/README.md +105 -73
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,16 +1,10 @@
1
- # @xmachines/play-signals
2
-
3
- **Canonical Signals substrate for XMachines with Stage 1 API isolation**
1
+ <!-- generated-by: gsd-doc-writer -->
4
2
 
5
- `@xmachines/play-signals` re-exports `Signal` from `signal-polyfill` as the single import boundary for XMachines packages.
6
-
7
- ## Why This Package Exists
3
+ # @xmachines/play-signals
8
4
 
9
- - Keep the raw `Signal` API as the canonical substrate surface.
10
- - Isolate Stage 1 proposal churn behind one package boundary.
11
- - Preserve Play invariants: Signal-only reactivity, passive infrastructure, and event-only mutation paths.
5
+ TC39 Signals polyfill for XMachines — fine-grained reactive state primitives that enable glitch-free, subscription-free state propagation in the Play Architecture.
12
6
 
13
- This package does not add business behavior to signals. Adapters and renderers observe signals and forward events; they do not mutate business state directly.
7
+ Part of the [xmachines-js monorepo](../../README.md).
14
8
 
15
9
  ## Installation
16
10
 
@@ -18,13 +12,27 @@ This package does not add business behavior to signals. Adapters and renderers o
18
12
  npm install @xmachines/play-signals
19
13
  ```
20
14
 
21
- ## Current Exports
15
+ ## Overview
22
16
 
23
- - `Signal` (re-export from `signal-polyfill`)
24
- - `watchSignal(signal, onValue)`
25
- - Type exports from `src/types.ts`: `SignalState`, `SignalComputed`, `SignalWatcher`, `SignalOptions`, `ComputedOptions`, `WatcherNotify`
17
+ This package wraps the [`signal-polyfill`](https://github.com/nicolo-ribaudo/tc39-proposal-signals-polyfill) reference implementation of the [TC39 Signals proposal](https://github.com/tc39/proposal-signals) (Stage 1). It re-exports the full `Signal` namespace and adds a memory-safe `watchSignal` utility, isolating the rest of the codebase from potential Stage 1 API churn.
26
18
 
27
- ## Quick Start
19
+ **All signal imports in the XMachines ecosystem should come from this package**, not directly from `signal-polyfill`, so that polyfill version bumps or API adaptations can be made in one place.
20
+
21
+ ## Usage
22
+
23
+ ### `Signal.State` — writable reactive state
24
+
25
+ ```typescript
26
+ import { Signal } from "@xmachines/play-signals";
27
+
28
+ const count = new Signal.State(0);
29
+
30
+ console.log(count.get()); // 0
31
+ count.set(5);
32
+ console.log(count.get()); // 5
33
+ ```
34
+
35
+ ### `Signal.Computed` — lazy memoized derived values
28
36
 
29
37
  ```typescript
30
38
  import { Signal } from "@xmachines/play-signals";
@@ -32,93 +40,117 @@ import { Signal } from "@xmachines/play-signals";
32
40
  const count = new Signal.State(0);
33
41
  const doubled = new Signal.Computed(() => count.get() * 2);
34
42
 
35
- const watcher = new Signal.subtle.Watcher(() => {
36
- queueMicrotask(() => {
37
- const pending = watcher.getPending();
38
- for (const signal of pending) {
39
- signal.get();
40
- }
41
- watcher.watch(...pending);
42
- });
43
- });
43
+ console.log(doubled.get()); // 0 (computed on first access)
44
+ count.set(5);
45
+ console.log(doubled.get()); // 10 (recomputed because dependency changed)
46
+ console.log(doubled.get()); // 10 (memoized — no recomputation)
47
+ ```
44
48
 
45
- watcher.watch(doubled);
46
- doubled.get();
49
+ Computations automatically track every signal accessed inside them. Dynamic branching is fully supported — only signals read in the _current_ execution path are tracked as dependencies.
47
50
 
48
- count.set(2);
51
+ ### `watchSignal` — memory-safe one-shot effect
49
52
 
50
- const dispose = () => {
51
- watcher.unwatch(doubled);
52
- };
53
+ Use `watchSignal` to subscribe to a `Signal.State` or `Signal.Computed` and receive its value after each change. Updates are coalesced into a single microtask per synchronous batch.
53
54
 
54
- void dispose;
55
+ ```typescript
56
+ import { Signal, watchSignal } from "@xmachines/play-signals";
57
+
58
+ const count = new Signal.State(0);
59
+
60
+ const cleanup = watchSignal(count, (value) => {
61
+ console.log("count changed:", value);
62
+ });
63
+
64
+ count.set(1); // → logs "count changed: 1" (via microtask)
65
+ count.set(2); // coalesced with any rapid synchronous changes
66
+ count.set(3); // → logs "count changed: 3" once
67
+
68
+ // Stop watching
69
+ cleanup();
55
70
  ```
56
71
 
57
- ### `watchSignal()` helper
72
+ The returned cleanup function is idempotent — calling it multiple times is safe and will not throw.
73
+
74
+ ### `Signal.subtle.Watcher` — low-level multi-signal observation
58
75
 
59
- For the common case of subscribing to one signal with deterministic cleanup, use `watchSignal()`:
76
+ For advanced use cases such as framework integrations, the full `Signal.subtle.Watcher` API is available:
60
77
 
61
78
  ```typescript
62
- import { watchSignal } from "@xmachines/play-signals";
79
+ import { Signal } from "@xmachines/play-signals";
63
80
 
64
- const stopWatching = watchSignal(actor.currentRoute, (route) => {
65
- console.log("Route changed:", route);
81
+ const count = new Signal.State(0);
82
+ const doubled = new Signal.Computed(() => count.get() * 2);
83
+
84
+ const watcher = new Signal.subtle.Watcher(() => {
85
+ queueMicrotask(() => {
86
+ const pending = watcher.getPending();
87
+ console.log("signals changed:", pending.length);
88
+ watcher.watch(...pending); // re-arm for future changes
89
+ });
66
90
  });
67
91
 
68
- stopWatching();
92
+ watcher.watch(count);
93
+ watcher.watch(doubled);
94
+
95
+ count.set(5); // schedules microtask notification
69
96
  ```
70
97
 
71
- `watchSignal()` is memory-safe:
98
+ ### Custom equality
72
99
 
73
- - Calling the returned cleanup before a pending microtask fires will **not** invoke the callback — a `disposed` flag prevents use-after-free when components unmount with in-flight updates.
74
- - Rapid synchronous signal changes coalesce to **one** microtask (`needsEnqueue` guard) — the callback receives the latest value only.
75
- - Calling the cleanup multiple times is idempotent (no throw).
100
+ Both `Signal.State` and `Signal.Computed` accept an `equals` option to control when dependents are notified:
76
101
 
77
- `watchSignal()` preserves the same canonical watcher lifecycle described below. It is an optional helper for adapter and renderer code; raw `Signal` APIs remain the base substrate.
102
+ ```typescript
103
+ import { Signal } from "@xmachines/play-signals";
104
+ import type { SignalOptions } from "@xmachines/play-signals";
78
105
 
79
- ## Canonical Watcher Lifecycle
106
+ const options: SignalOptions<{ name: string; age: number }> = {
107
+ equals: (a, b) => a.name === b.name && a.age === b.age,
108
+ };
80
109
 
81
- Use one lifecycle pattern everywhere (React, Vue, Solid, router bridges, helper wrappers):
110
+ const person = new Signal.State({ name: "Alice", age: 30 }, options);
111
+ // Setting structurally identical value will not notify dependents
112
+ person.set({ name: "Alice", age: 30 });
113
+ ```
82
114
 
83
- 1. `notify` callback runs.
84
- 2. Schedule work with `queueMicrotask`.
85
- 3. Drain `watcher.getPending()`.
86
- 4. Perform reads/effects.
87
- 5. Re-arm watcher with `watch()` or `watch(...signals)`.
115
+ ## API Summary
88
116
 
89
- Watcher notifications are one-shot. If you do not re-arm, you will miss future updates.
117
+ | Export | Kind | Description |
118
+ | ------------------------------ | --------- | ------------------------------------------------------------------------------------------------------ |
119
+ | `Signal` | namespace | Full TC39 Signals namespace (`State`, `Computed`, `subtle.Watcher`) re-exported from `signal-polyfill` |
120
+ | `watchSignal(signal, onValue)` | function | Memory-safe subscription helper; returns a cleanup function |
121
+ | `SignalState<T>` | interface | Shape of `Signal.State<T>` (`.get()`, `.set()`) |
122
+ | `SignalComputed<T>` | interface | Shape of `Signal.Computed<T>` (`.get()`) |
123
+ | `SignalWatcher` | interface | Shape of `Signal.subtle.Watcher` (`.watch()`, `.unwatch()`, `.getPending()`) |
124
+ | `SignalOptions<T>` | interface | Options bag for `Signal.State` constructor (`equals?`) |
125
+ | `ComputedOptions<T>` | interface | Options bag for `Signal.Computed` constructor (`equals?`) |
126
+ | `WatcherNotify` | type | Callback signature for `Signal.subtle.Watcher` notify function |
90
127
 
91
- ## Cleanup Contract
128
+ ## Testing
92
129
 
93
- Always dispose explicitly. Do not rely on GC-only cleanup guidance.
130
+ Run tests for this package in isolation:
94
131
 
95
- - If you called `watch(...)`, call `unwatch(...)` in teardown.
96
- - Framework lifecycles (`useEffect` cleanup, `onUnmounted`, `onCleanup`) must unwatch.
97
- - Bridge lifecycles (`disconnect`, `dispose`) must unwatch and unsubscribe.
132
+ ```bash
133
+ # From this package directory
134
+ npm test
98
135
 
99
- ## API Surface
136
+ # Watch mode
137
+ npm test -- --watch
100
138
 
101
- - `Signal.State<T>`: writable signal state (`get`, `set`)
102
- - `Signal.Computed<T>`: lazy memoized derivations
103
- - `Signal.subtle.Watcher`: low-level watcher (`watch`, `unwatch`, `getPending`)
104
- - `watchSignal(signal, onValue)`: lifecycle-safe single-signal subscription helper
139
+ # With coverage (lines ≥ 90 %, functions ≥ 90 %, branches ≥ 85 %, statements ≥ 90 %)
140
+ npm run test:coverage
141
+ ```
105
142
 
106
- ## Architecture Notes
143
+ From the monorepo root:
107
144
 
108
- - **Signal-Only Reactivity (INV-05):** Signals are the reactive substrate.
109
- - **Passive Infrastructure (INV-04):** Adapters and frameworks only observe/forward.
110
- - **Actor Authority (INV-01):** Business validity and transitions stay in actors.
111
- - **Event-only mutation path:** Signals are not a business mutation channel.
145
+ ```bash
146
+ npm test -w @xmachines/play-signals
147
+ ```
112
148
 
113
- ## Resources
149
+ ## Requirements
114
150
 
115
- - [TC39 Signals Proposal](https://github.com/tc39/proposal-signals)
116
- - [signal-polyfill](https://github.com/proposal-signals/signal-polyfill)
117
- - [Play RFC](../docs/rfc/play.md)
151
+ - **Node.js** `>= 22.0.0`
152
+ - **TypeScript** `5.7+` (for consumers using TypeScript)
118
153
 
119
154
  ## License
120
155
 
121
- Copyright (c) 2016 [Mikael Karon](mailto:mikael@karon.se). All rights reserved.
122
-
123
- This work is licensed under the terms of the MIT license.
124
- For a copy, see <https://opensource.org/licenses/MIT>.
156
+ MIT — see [LICENSE](LICENSE).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xmachines/play-signals",
3
- "version": "1.0.0-beta.45",
3
+ "version": "1.0.0-beta.47",
4
4
  "private": false,
5
5
  "description": "TC39 Signals polyfill for XMachines - Fine-grained reactive state primitives",
6
6
  "keywords": [
@@ -51,7 +51,7 @@
51
51
  },
52
52
  "devDependencies": {
53
53
  "@types/node": "^25.6.0",
54
- "@xmachines/shared": "1.0.0-beta.45",
54
+ "@xmachines/shared": "1.0.0-beta.47",
55
55
  "oxfmt": "^0.45.0",
56
56
  "oxlint": "^1.60.0",
57
57
  "vitest": "^4.1.4"