@xmachines/play-signals 1.0.0-beta.4 → 1.0.0-beta.41
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 +21 -0
- package/README.md +29 -14
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/watch-signal.d.ts +21 -0
- package/dist/watch-signal.d.ts.map +1 -0
- package/dist/watch-signal.js +47 -0
- package/dist/watch-signal.js.map +1 -0
- package/package.json +17 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mikael Karon
|
|
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
CHANGED
|
@@ -21,6 +21,7 @@ npm install @xmachines/play-signals
|
|
|
21
21
|
## Current Exports
|
|
22
22
|
|
|
23
23
|
- `Signal` (re-export from `signal-polyfill`)
|
|
24
|
+
- `watchSignal(signal, onValue)`
|
|
24
25
|
- Type exports from `src/types.ts`: `SignalState`, `SignalComputed`, `SignalWatcher`, `SignalOptions`, `ComputedOptions`, `WatcherNotify`
|
|
25
26
|
|
|
26
27
|
## Quick Start
|
|
@@ -53,6 +54,28 @@ const dispose = () => {
|
|
|
53
54
|
void dispose;
|
|
54
55
|
```
|
|
55
56
|
|
|
57
|
+
### `watchSignal()` helper
|
|
58
|
+
|
|
59
|
+
For the common case of subscribing to one signal with deterministic cleanup, use `watchSignal()`:
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { watchSignal } from "@xmachines/play-signals";
|
|
63
|
+
|
|
64
|
+
const stopWatching = watchSignal(actor.currentRoute, (route) => {
|
|
65
|
+
console.log("Route changed:", route);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
stopWatching();
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`watchSignal()` is memory-safe:
|
|
72
|
+
|
|
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).
|
|
76
|
+
|
|
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.
|
|
78
|
+
|
|
56
79
|
## Canonical Watcher Lifecycle
|
|
57
80
|
|
|
58
81
|
Use one lifecycle pattern everywhere (React, Vue, Solid, router bridges, helper wrappers):
|
|
@@ -73,23 +96,12 @@ Always dispose explicitly. Do not rely on GC-only cleanup guidance.
|
|
|
73
96
|
- Framework lifecycles (`useEffect` cleanup, `onUnmounted`, `onCleanup`) must unwatch.
|
|
74
97
|
- Bridge lifecycles (`disconnect`, `dispose`) must unwatch and unsubscribe.
|
|
75
98
|
|
|
76
|
-
## Optional Helper Direction
|
|
77
|
-
|
|
78
|
-
Raw `Signal` remains canonical. Helper APIs are optional, additive guidance for consistency:
|
|
79
|
-
|
|
80
|
-
- `watchSignals(signals, onChange, options)`
|
|
81
|
-
- `createSignalEffect(effect, options)`
|
|
82
|
-
- `toSubscribable(signal, options)`
|
|
83
|
-
|
|
84
|
-
These helpers are intended to codify lifecycle-safe watcher scheduling and deterministic teardown. They do not replace direct `Signal` usage.
|
|
85
|
-
|
|
86
99
|
## API Surface
|
|
87
100
|
|
|
88
101
|
- `Signal.State<T>`: writable signal state (`get`, `set`)
|
|
89
102
|
- `Signal.Computed<T>`: lazy memoized derivations
|
|
90
103
|
- `Signal.subtle.Watcher`: low-level watcher (`watch`, `unwatch`, `getPending`)
|
|
91
|
-
|
|
92
|
-
Complete generated API docs: [docs/api/@xmachines/play-signals](../../docs/api/@xmachines/play-signals)
|
|
104
|
+
- `watchSignal(signal, onValue)`: lifecycle-safe single-signal subscription helper
|
|
93
105
|
|
|
94
106
|
## Architecture Notes
|
|
95
107
|
|
|
@@ -102,8 +114,11 @@ Complete generated API docs: [docs/api/@xmachines/play-signals](../../docs/api/@
|
|
|
102
114
|
|
|
103
115
|
- [TC39 Signals Proposal](https://github.com/tc39/proposal-signals)
|
|
104
116
|
- [signal-polyfill](https://github.com/proposal-signals/signal-polyfill)
|
|
105
|
-
- [
|
|
117
|
+
- [Play RFC](../docs/rfc/play.md)
|
|
106
118
|
|
|
107
119
|
## License
|
|
108
120
|
|
|
109
|
-
|
|
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>.
|
package/dist/index.d.ts
CHANGED
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
* count.set(5); // Logs: Count: 5 Doubled: 10
|
|
33
33
|
* ```
|
|
34
34
|
*
|
|
35
|
-
* @see
|
|
35
|
+
* @see [Play RFC](../../docs/rfc/play.md) - Invariant INV-05
|
|
36
36
|
* @see {@link https://github.com/tc39/proposal-signals | TC39 Signals Proposal}
|
|
37
37
|
*
|
|
38
38
|
* @remarks
|
|
@@ -46,5 +46,6 @@
|
|
|
46
46
|
* consuming packages. This architectural decision protects against Stage 1 API churn.
|
|
47
47
|
*/
|
|
48
48
|
export { Signal } from "signal-polyfill";
|
|
49
|
+
export { watchSignal } from "./watch-signal.js";
|
|
49
50
|
export type { SignalState, SignalComputed, SignalWatcher, SignalOptions, ComputedOptions, WatcherNotify, } from "./types.js";
|
|
50
51
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AAGH,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AAGH,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD,YAAY,EACX,WAAW,EACX,cAAc,EACd,aAAa,EACb,aAAa,EACb,eAAe,EACf,aAAa,GACb,MAAM,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
* count.set(5); // Logs: Count: 5 Doubled: 10
|
|
33
33
|
* ```
|
|
34
34
|
*
|
|
35
|
-
* @see
|
|
35
|
+
* @see [Play RFC](../../docs/rfc/play.md) - Invariant INV-05
|
|
36
36
|
* @see {@link https://github.com/tc39/proposal-signals | TC39 Signals Proposal}
|
|
37
37
|
*
|
|
38
38
|
* @remarks
|
|
@@ -47,4 +47,5 @@
|
|
|
47
47
|
*/
|
|
48
48
|
// Re-export complete Signal namespace from official polyfill
|
|
49
49
|
export { Signal } from "signal-polyfill";
|
|
50
|
+
export { watchSignal } from "./watch-signal.js";
|
|
50
51
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AAEH,6DAA6D;AAC7D,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AAEH,6DAA6D;AAC7D,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Signal } from "signal-polyfill";
|
|
2
|
+
/**
|
|
3
|
+
* Subscribe to a single signal using the canonical one-shot watcher lifecycle.
|
|
4
|
+
*
|
|
5
|
+
* The callback runs from a queued microtask after pending notifications are
|
|
6
|
+
* drained, then the watcher re-arms itself so future updates are not missed.
|
|
7
|
+
* The returned cleanup keeps teardown idempotent by tolerating already-detached
|
|
8
|
+
* watchers.
|
|
9
|
+
*
|
|
10
|
+
* **Memory safety (Phase 29):**
|
|
11
|
+
* - `disposed` flag prevents post-cleanup callback execution: if cleanup is
|
|
12
|
+
* called before a pending microtask fires, the microtask returns early.
|
|
13
|
+
* - `needsEnqueue` guard dedups rapid synchronous signal changes: only one
|
|
14
|
+
* microtask is ever queued per batch of synchronous mutations.
|
|
15
|
+
*
|
|
16
|
+
* @param signal - A `Signal.State` or `Signal.Computed` to subscribe to.
|
|
17
|
+
* @param onValue - Called with the current signal value after each change.
|
|
18
|
+
* @returns A cleanup function that unregisters the watcher.
|
|
19
|
+
*/
|
|
20
|
+
export declare function watchSignal<T>(signal: Signal.State<T> | Signal.Computed<T>, onValue: (value: T) => void): () => void;
|
|
21
|
+
//# sourceMappingURL=watch-signal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watch-signal.d.ts","sourceRoot":"","sources":["../src/watch-signal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAC5B,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAC5C,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,GACzB,MAAM,IAAI,CA0BZ"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Signal } from "signal-polyfill";
|
|
2
|
+
/**
|
|
3
|
+
* Subscribe to a single signal using the canonical one-shot watcher lifecycle.
|
|
4
|
+
*
|
|
5
|
+
* The callback runs from a queued microtask after pending notifications are
|
|
6
|
+
* drained, then the watcher re-arms itself so future updates are not missed.
|
|
7
|
+
* The returned cleanup keeps teardown idempotent by tolerating already-detached
|
|
8
|
+
* watchers.
|
|
9
|
+
*
|
|
10
|
+
* **Memory safety (Phase 29):**
|
|
11
|
+
* - `disposed` flag prevents post-cleanup callback execution: if cleanup is
|
|
12
|
+
* called before a pending microtask fires, the microtask returns early.
|
|
13
|
+
* - `needsEnqueue` guard dedups rapid synchronous signal changes: only one
|
|
14
|
+
* microtask is ever queued per batch of synchronous mutations.
|
|
15
|
+
*
|
|
16
|
+
* @param signal - A `Signal.State` or `Signal.Computed` to subscribe to.
|
|
17
|
+
* @param onValue - Called with the current signal value after each change.
|
|
18
|
+
* @returns A cleanup function that unregisters the watcher.
|
|
19
|
+
*/
|
|
20
|
+
export function watchSignal(signal, onValue) {
|
|
21
|
+
let disposed = false;
|
|
22
|
+
let needsEnqueue = true;
|
|
23
|
+
const watcher = new Signal.subtle.Watcher(() => {
|
|
24
|
+
if (disposed || !needsEnqueue)
|
|
25
|
+
return;
|
|
26
|
+
needsEnqueue = false;
|
|
27
|
+
queueMicrotask(() => {
|
|
28
|
+
if (disposed)
|
|
29
|
+
return;
|
|
30
|
+
needsEnqueue = true;
|
|
31
|
+
watcher.getPending();
|
|
32
|
+
onValue(signal.get());
|
|
33
|
+
watcher.watch(signal);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
watcher.watch(signal);
|
|
37
|
+
return () => {
|
|
38
|
+
disposed = true;
|
|
39
|
+
try {
|
|
40
|
+
watcher.unwatch(signal);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// Ignore detached watcher errors to keep cleanup idempotent.
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=watch-signal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watch-signal.js","sourceRoot":"","sources":["../src/watch-signal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,WAAW,CAC1B,MAA4C,EAC5C,OAA2B;IAE3B,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,IAAI,YAAY,GAAG,IAAI,CAAC;IAExB,MAAM,OAAO,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE;QAC9C,IAAI,QAAQ,IAAI,CAAC,YAAY;YAAE,OAAO;QACtC,YAAY,GAAG,KAAK,CAAC;QACrB,cAAc,CAAC,GAAG,EAAE;YACnB,IAAI,QAAQ;gBAAE,OAAO;YACrB,YAAY,GAAG,IAAI,CAAC;YACpB,OAAO,CAAC,UAAU,EAAE,CAAC;YACrB,OAAO,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC;YACtB,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACvB,CAAC,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAEtB,OAAO,GAAG,EAAE;QACX,QAAQ,GAAG,IAAI,CAAC;QAChB,IAAI,CAAC;YACJ,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACzB,CAAC;QAAC,MAAM,CAAC;YACR,6DAA6D;QAC9D,CAAC;IACF,CAAC,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xmachines/play-signals",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.41",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "TC39 Signals polyfill for XMachines - Fine-grained reactive state primitives",
|
|
6
6
|
"keywords": [
|
|
@@ -10,16 +10,25 @@
|
|
|
10
10
|
"tc39",
|
|
11
11
|
"xmachines"
|
|
12
12
|
],
|
|
13
|
+
"homepage": "https://gitlab.com/xmachin-es/xmachines-js/tree/main/packages/play-signals",
|
|
13
14
|
"license": "MIT",
|
|
14
15
|
"author": "XMachines Contributors",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+ssh://git@gitlab.com/xmachin-es/xmachines-js.git",
|
|
19
|
+
"directory": "packages/play-signals"
|
|
20
|
+
},
|
|
15
21
|
"files": [
|
|
16
22
|
"dist",
|
|
17
23
|
"README.md",
|
|
18
24
|
"LICENSE"
|
|
19
25
|
],
|
|
20
26
|
"type": "module",
|
|
27
|
+
"main": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
21
29
|
"exports": {
|
|
22
30
|
".": {
|
|
31
|
+
"source": "./src/index.ts",
|
|
23
32
|
"types": "./dist/index.d.ts",
|
|
24
33
|
"import": "./dist/index.js"
|
|
25
34
|
}
|
|
@@ -29,9 +38,8 @@
|
|
|
29
38
|
},
|
|
30
39
|
"scripts": {
|
|
31
40
|
"build": "tsc --build",
|
|
32
|
-
"clean": "rm -rf dist *.tsbuildinfo",
|
|
33
|
-
"
|
|
34
|
-
"test": "vitest run",
|
|
41
|
+
"clean": "rm -rf dist *.tsbuildinfo coverage .vitest-attachments test/browser/__screenshots__ node_modules/.svelte2tsx-*",
|
|
42
|
+
"test": "vitest",
|
|
35
43
|
"lint": "oxlint .",
|
|
36
44
|
"lint:fix": "oxlint --fix .",
|
|
37
45
|
"format": "oxfmt .",
|
|
@@ -42,8 +50,11 @@
|
|
|
42
50
|
"signal-polyfill": "^0.2.2"
|
|
43
51
|
},
|
|
44
52
|
"devDependencies": {
|
|
45
|
-
"@types/node": "^25.
|
|
46
|
-
"@xmachines/shared": "1.0.0-beta.
|
|
53
|
+
"@types/node": "^25.6.0",
|
|
54
|
+
"@xmachines/shared": "1.0.0-beta.41",
|
|
55
|
+
"oxfmt": "^0.45.0",
|
|
56
|
+
"oxlint": "^1.60.0",
|
|
57
|
+
"vitest": "^4.1.4"
|
|
47
58
|
},
|
|
48
59
|
"engines": {
|
|
49
60
|
"node": ">=22.0.0"
|