@xmachines/play-atom 5.0.2 → 6.0.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/README.md CHANGED
@@ -2,17 +2,16 @@
2
2
 
3
3
  The atom primitives for XMachines. They give the Play Architecture fine-grained reactive state. The primitives propagate state without a glitch and without a subscription of the consumer.
4
4
 
5
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Version](https://img.shields.io/badge/version-5.0.2-blue)](https://www.npmjs.com/package/@xmachines/play-atom)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Version](https://img.shields.io/badge/version-6.0.0-blue)](https://www.npmjs.com/package/@xmachines/play-atom)
6
6
 
7
7
  ## Installation
8
8
 
9
9
  ```bash
10
- pnpm add @xmachines/play-atom @xmachines/play @xstate/store
10
+ pnpm add @xmachines/play-atom @xstate/store
11
11
  ```
12
12
 
13
- **Peer dependencies.** Install them with the package:
13
+ **Peer dependency.** Install it with the package:
14
14
 
15
- - [`@xmachines/play`](../play/README.md) — the core protocol. This package reads `asCleanup` and `Cleanup` from it.
16
15
  - `@xstate/store` `^4.2.3` — the atom engine.
17
16
 
18
17
  Install ONE copy of `@xstate/store` — see [One instance](#one-instance).
@@ -107,7 +106,7 @@ count.set(3); // → logs "count changed: 3" once, from a microtask
107
106
  cleanup();
108
107
  ```
109
108
 
110
- The cleanup function is idempotent. A second call is safe, and it does not throw. It is also a `Disposable`, so `using cleanup = watchAtom(...)` releases it at the end of the scope.
109
+ The cleanup function is idempotent. A second call is safe, and it does not throw. It is a plain function. To release it at the end of a scope, publish it with `asDisposable` of [`@xmachines/core/utils`](../core/README.md): `using cleanup = asDisposable(watchAtom(...))`.
111
110
 
112
111
  ### `atom.subscribe` — synchronous observation
113
112
 
@@ -118,6 +117,18 @@ const subscription = count.subscribe((value) => console.log(value));
118
117
  subscription.unsubscribe();
119
118
  ```
120
119
 
120
+ The subscription is the plain `{ unsubscribe }` of the engine. To hold it with `using`, publish it with `asDisposable` of [`@xmachines/core/utils`](../core/README.md):
121
+
122
+ ```typescript
123
+ import { asDisposable } from "@xmachines/core/utils";
124
+
125
+ {
126
+ const plain = count.subscribe((value) => console.log(value));
127
+ using subscription = asDisposable(plain, () => plain.unsubscribe());
128
+ count.set(1);
129
+ } // the scope calls unsubscribe()
130
+ ```
131
+
121
132
  ### Custom equality
122
133
 
123
134
  `createAtom` accepts a `compare` option. It controls when an atom notifies its dependents. The default is `Object.is`.
@@ -143,25 +154,24 @@ This package therefore declares `@xstate/store` as a peer dependency. A renderer
143
154
 
144
155
  ## API Summary
145
156
 
146
- | Export | Kind | Description |
147
- | ---------------------------------- | --------- | --------------------------------------------------------------------------------------------- |
148
- | `createAtom(value, options?)` | function | The writable atom |
149
- | `createAtom(fn, options?)` | function | The computed atom |
150
- | `createWritableAtom(value, opts?)` | function | The writable atom, with one signature, for a generic value type |
151
- | `createAsyncAtom(fn, options?)` | function | The computed atom of a promise. It holds an `AsyncAtomState`. `fn` reads an `AbortSignal` |
152
- | `watchAtom(atom, onValue)` | function | The coalesced subscription helper. It returns a `Cleanup` |
153
- | `Atom<T>` | interface | The writable atom (`.get()`, `.set()`, `.subscribe()`) |
154
- | `ReadonlyAtom<T>` | interface | The computed atom (`.get()`, `.subscribe()`) |
155
- | `Readable<T>` | interface | The read side of either one (`.get()`, `.subscribe()`) |
156
- | `BaseAtom<T>` | interface | The common base of both kinds |
157
- | `AnyAtom` | type | An atom of an unknown value type |
158
- | `AtomOptions<T>` | interface | The options object of `createAtom` (`compare?`) |
159
- | `AtomObserver<T>` | interface | The observer of `subscribe`, in the object form |
160
- | `AtomSubscription` | interface | The release that `subscribe` returns |
161
- | `AsyncAtomState<TData>` | type | The state of `createAsyncAtom`: `pending`, `done`, or `error` |
162
- | `AsyncAtomOptions` | interface | What `createAsyncAtom` hands its getter: the `signal` that aborts a stale run |
163
- | `CreateAtom` | interface | The two call signatures of `createAtom` |
164
- | `Cleanup` | type | The release that `watchAtom` returns, re-exported from [`@xmachines/play`](../play/README.md) |
157
+ | Export | Kind | Description |
158
+ | ---------------------------------- | --------- | ----------------------------------------------------------------------------------------- |
159
+ | `createAtom(value, options?)` | function | The writable atom |
160
+ | `createAtom(fn, options?)` | function | The computed atom |
161
+ | `createWritableAtom(value, opts?)` | function | The writable atom, with one signature, for a generic value type |
162
+ | `createAsyncAtom(fn, options?)` | function | The computed atom of a promise. It holds an `AsyncAtomState`. `fn` reads an `AbortSignal` |
163
+ | `watchAtom(atom, onValue)` | function | The coalesced subscription helper. It returns a plain release |
164
+ | `Atom<T>` | interface | The writable atom (`.get()`, `.set()`, `.subscribe()`) |
165
+ | `ReadonlyAtom<T>` | interface | The computed atom (`.get()`, `.subscribe()`) |
166
+ | `Readable<T>` | interface | The read side of either one (`.get()`, `.subscribe()`) |
167
+ | `BaseAtom<T>` | interface | The common base of both kinds |
168
+ | `AnyAtom` | type | An atom of an unknown value type |
169
+ | `AtomOptions<T>` | interface | The options object of `createAtom` (`compare?`) |
170
+ | `AtomObserver<T>` | interface | The observer of `subscribe`, in the object form |
171
+ | `AtomSubscription` | interface | The release that `subscribe` returns |
172
+ | `AsyncAtomState<TData>` | type | The state of `createAsyncAtom`: `pending`, `done`, or `error` |
173
+ | `AsyncAtomOptions` | interface | What `createAsyncAtom` hands its getter: the `signal` that aborts a stale run |
174
+ | `CreateAtom` | interface | The two call signatures of `createAtom` |
165
175
 
166
176
  The observer types carry the `Atom` prefix on purpose: `xstate` declares `Observer` and `Subscription` too, so a plain name here would put two declarations of one name in one file. `Subscribable` stays out of the surface for the same reason.
167
177
 
package/dist/index.d.ts CHANGED
@@ -59,5 +59,4 @@ export { createWritableAtom } from "./create-writable-atom.js";
59
59
  export { watchAtom } from "./watch-atom.js";
60
60
  export type { CreateAtom } from "./create-atom.js";
61
61
  export type { AnyAtom, AsyncAtomOptions, AsyncAtomState, Atom, AtomObserver, AtomOptions, AtomSubscription, BaseAtom, Readable, ReadonlyAtom, } from "./types.js";
62
- export { type Cleanup } from "@xmachines/play";
63
62
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AAGH,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,YAAY,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,YAAY,EACX,OAAO,EACP,gBAAgB,EAChB,cAAc,EACd,IAAI,EACJ,YAAY,EACZ,WAAW,EACX,gBAAgB,EAChB,QAAQ,EACR,QAAQ,EACR,YAAY,GACZ,MAAM,YAAY,CAAC;AAKpB,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AAGH,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,YAAY,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,YAAY,EACX,OAAO,EACP,gBAAgB,EAChB,cAAc,EACd,IAAI,EACJ,YAAY,EACZ,WAAW,EACX,gBAAgB,EAChB,QAAQ,EACR,QAAQ,EACR,YAAY,GACZ,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -58,9 +58,4 @@
58
58
  export { createAsyncAtom, createAtom } from "./create-atom.js";
59
59
  export { createWritableAtom } from "./create-writable-atom.js";
60
60
  export { watchAtom } from "./watch-atom.js";
61
- // The release protocol of @xmachines/play. This package's own published `.d.ts` names
62
- // exactly these, so a consumer reads them from here and needs no second manifest
63
- // entry. It names `asCleanup` nowhere: a consumer of this package RECEIVES a
64
- // release, and builds one only with @xmachines/play itself.
65
- export {} from "@xmachines/play";
66
61
  //# 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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AAEH,oEAAoE;AACpE,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAiB5C,sFAAsF;AACtF,iFAAiF;AACjF,6EAA6E;AAC7E,4DAA4D;AAC5D,OAAO,EAAgB,MAAM,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AAEH,oEAAoE;AACpE,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
@@ -1,4 +1,3 @@
1
- import { type Cleanup } from "@xmachines/play";
2
1
  import type { Readable } from "./types.js";
3
2
  /**
4
3
  * Subscribes to one atom, and coalesces the notifications into one microtask.
@@ -29,12 +28,12 @@ import type { Readable } from "./types.js";
29
28
  * function bounds it not. A write of the SAME value ends the sequence by itself,
30
29
  * because an atom notifies for a value that it holds already never.
31
30
  *
32
- * The return is a {@link Cleanup}: a function, and a `Disposable`. A caller that keeps
33
- * the release in a field and runs it from a teardown keeps that code. A caller inside
34
- * one scope writes `using` and writes no teardown:
31
+ * The return is a plain function. A caller that keeps the release in a field runs it
32
+ * from a teardown. A caller inside one scope publishes it with `asDisposable` of
33
+ * `@xmachines/core/utils`, writes `using`, and writes no teardown:
35
34
  *
36
35
  * ```ts
37
- * using stop = watchAtom(count, render);
36
+ * using stop = asDisposable(watchAtom(count, render));
38
37
  * ```
39
38
  *
40
39
  * @param atom - The `Atom` or the `ReadonlyAtom` to subscribe to.
@@ -54,5 +53,5 @@ import type { Readable } from "./types.js";
54
53
  * stop();
55
54
  * ```
56
55
  */
57
- export declare function watchAtom<T>(atom: Readable<T>, onValue: (value: T) => void): Cleanup;
56
+ export declare function watchAtom<T>(atom: Readable<T>, onValue: (value: T) => void): () => void;
58
57
  //# sourceMappingURL=watch-atom.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"watch-atom.d.ts","sourceRoot":"","sources":["../src/watch-atom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,GAAG,OAAO,CA0BpF"}
1
+ {"version":3,"file":"watch-atom.d.ts","sourceRoot":"","sources":["../src/watch-atom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,GAAG,MAAM,IAAI,CA0BvF"}
@@ -1,4 +1,3 @@
1
- import { asCleanup } from "@xmachines/play";
2
1
  /**
3
2
  * Subscribes to one atom, and coalesces the notifications into one microtask.
4
3
  *
@@ -28,12 +27,12 @@ import { asCleanup } from "@xmachines/play";
28
27
  * function bounds it not. A write of the SAME value ends the sequence by itself,
29
28
  * because an atom notifies for a value that it holds already never.
30
29
  *
31
- * The return is a {@link Cleanup}: a function, and a `Disposable`. A caller that keeps
32
- * the release in a field and runs it from a teardown keeps that code. A caller inside
33
- * one scope writes `using` and writes no teardown:
30
+ * The return is a plain function. A caller that keeps the release in a field runs it
31
+ * from a teardown. A caller inside one scope publishes it with `asDisposable` of
32
+ * `@xmachines/core/utils`, writes `using`, and writes no teardown:
34
33
  *
35
34
  * ```ts
36
- * using stop = watchAtom(count, render);
35
+ * using stop = asDisposable(watchAtom(count, render));
37
36
  * ```
38
37
  *
39
38
  * @param atom - The `Atom` or the `ReadonlyAtom` to subscribe to.
@@ -74,11 +73,11 @@ export function watchAtom(atom, onValue) {
74
73
  onValue(atom.get());
75
74
  });
76
75
  });
77
- return asCleanup(() => {
76
+ return () => {
78
77
  if (disposed)
79
78
  return;
80
79
  disposed = true;
81
80
  subscription.unsubscribe();
82
- });
81
+ };
83
82
  }
84
83
  //# sourceMappingURL=watch-atom.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"watch-atom.js","sourceRoot":"","sources":["../src/watch-atom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAgB,MAAM,iBAAiB,CAAC;AAG1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AA+E;YAC/E,+EAA+E;YAC/E,gFAAgF;YAChF,8EAA8E;YAC9E,iFAAiF;YACjF,UAAU;YACV,SAAS,GAAG,KAAK,CAAC;YAClB,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;QACrB,CAAC,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,GAAG,EAAE;QACX,IAAI,QAAQ;YAAE,OAAO;QACrB,QAAQ,GAAG,IAAI,CAAC;QAChB,YAAY,CAAC,WAAW,EAAE,CAAC;IAC5B,CAAC,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xmachines/play-atom",
3
- "version": "5.0.2",
3
+ "version": "6.0.0",
4
4
  "private": false,
5
5
  "description": "Atom primitives for XMachines - fine-grained reactive state on @xstate/store",
6
6
  "keywords": [
@@ -52,7 +52,6 @@
52
52
  "@testing-library/jest-dom": "^7.0.1",
53
53
  "@types/node": "^26.6.2",
54
54
  "@vitest/browser-playwright": "^5.0.1",
55
- "@xmachines/play": "5.0.2",
56
55
  "@xstate/store": "^4.2.3",
57
56
  "oxfmt": "^0.70.0",
58
57
  "oxlint": "^1.85.0",
@@ -60,7 +59,6 @@
60
59
  "vitest": "^5.0.1"
61
60
  },
62
61
  "peerDependencies": {
63
- "@xmachines/play": "5.0.2",
64
62
  "@xstate/store": "^4.2.3"
65
63
  },
66
64
  "engines": {