@cssints/machine 0.33.0 → 0.35.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 +5 -5
- package/dist/solid.mjs +21 -29
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -161,9 +161,9 @@ const view = store.projection(
|
|
|
161
161
|
|
|
162
162
|
- `store.state` is a `createStore` of `{ ...context, phase }`, one setter call per transition, writing only the fields
|
|
163
163
|
that changed: a reader of `state.phase` does not re-run when only `progress` changes.
|
|
164
|
-
- `store.optimistic` is a
|
|
165
|
-
|
|
166
|
-
|
|
164
|
+
- `store.optimistic` is a second store over the same fields: it follows `state`, and an intent writes the phase it is
|
|
165
|
+
heading to on top. The lead is dropped as soon as a transition reaches a settled phase, so the view reverts to the
|
|
166
|
+
committed snapshot — an intent that is superseded before the finish rolls back instead of sticking.
|
|
167
167
|
- `store.projection(compute, seed)` is a `createProjection` over `state`: only a changed field notifies, so a reader of
|
|
168
168
|
`view.visible` does not re-run when `progress` changes. A projection reads the committed frame, not an optimistic
|
|
169
169
|
override (Solid holds the override whole), so the visual phase is read off `optimistic` directly.
|
|
@@ -193,5 +193,5 @@ cd packages/machine && bun run typecheck # src and test/types (the @ts-expect-
|
|
|
193
193
|
```
|
|
194
194
|
|
|
195
195
|
The store test runs in plain node with `--conditions=browser --conditions=development`: Solid's store layer needs no DOM,
|
|
196
|
-
but the node default is the SSR build, where
|
|
197
|
-
|
|
196
|
+
but the node default is the SSR build, where a store's client-side transactions do not live. The package's `test` script
|
|
197
|
+
carries the flags.
|
package/dist/solid.mjs
CHANGED
|
@@ -1,20 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createProjection, createStore, getOwner, onCleanup } from "solid-js";
|
|
2
2
|
// src/solid.ts
|
|
3
3
|
const fieldsOf = (state) => ({
|
|
4
4
|
...state.context,
|
|
5
5
|
phase: state.state
|
|
6
6
|
});
|
|
7
|
-
/**
|
|
8
|
-
const copyInto = (draft, source) => {
|
|
9
|
-
const target = draft;
|
|
10
|
-
const from = source;
|
|
11
|
-
for (const key of Object.keys(from)) {
|
|
12
|
-
const value = from[key];
|
|
13
|
-
if (!Object.is(target[key], value)) target[key] = value;
|
|
14
|
-
}
|
|
15
|
-
for (const key of Object.keys(target)) if (!Object.hasOwn(from, key)) delete target[key];
|
|
16
|
-
};
|
|
17
|
-
/** Write only the fields that changed: a store replaces nothing, so only the readers of a changed field re-run. */
|
|
7
|
+
/** Write `next` onto a store, changing only the fields that differ, so a reader of one field is not re-run by the rest. */
|
|
18
8
|
const writeFields = (setter, next) => {
|
|
19
9
|
setter((draft) => {
|
|
20
10
|
const target = draft;
|
|
@@ -30,30 +20,32 @@ const writeFields = (setter, next) => {
|
|
|
30
20
|
const createMachineStore = (machine, options = {}) => {
|
|
31
21
|
const initial = machine.getState();
|
|
32
22
|
const [state, setState] = createStore(fieldsOf(initial));
|
|
33
|
-
const [optimistic, setOptimistic] =
|
|
34
|
-
copyInto(draft, state);
|
|
35
|
-
}, fieldsOf(initial));
|
|
23
|
+
const [optimistic, setOptimistic] = createStore(fieldsOf(initial));
|
|
36
24
|
const settled = options.settled ?? (() => true);
|
|
37
|
-
|
|
25
|
+
/** The phase an in-flight intent is heading to (`undefined` when nothing is in flight). */
|
|
26
|
+
let lead;
|
|
27
|
+
/** The optimistic fields: the committed snapshot, with the intent's phase on top while one is in flight. */
|
|
28
|
+
const syncOptimistic = (snapshot) => {
|
|
29
|
+
writeFields(setOptimistic, fieldsOf(snapshot));
|
|
30
|
+
const heading = lead;
|
|
31
|
+
if (heading !== void 0) setOptimistic((draft) => {
|
|
32
|
+
draft.phase = heading;
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
/** Drop the lead: the optimistic view goes back to the committed snapshot, which is where the machine really is. */
|
|
38
36
|
const endIntent = () => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
if (lead === void 0) return;
|
|
38
|
+
lead = void 0;
|
|
39
|
+
syncOptimistic(machine.getState());
|
|
42
40
|
};
|
|
43
41
|
const startIntent = (phase) => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
});
|
|
47
|
-
action(async function* () {
|
|
48
|
-
setOptimistic((draft) => {
|
|
49
|
-
draft.phase = phase;
|
|
50
|
-
});
|
|
51
|
-
yield gate;
|
|
52
|
-
})();
|
|
42
|
+
lead = phase;
|
|
43
|
+
syncOptimistic(machine.getState());
|
|
53
44
|
};
|
|
54
45
|
const off = machine.subscribe((snapshot) => {
|
|
55
46
|
writeFields(setState, fieldsOf(snapshot));
|
|
56
|
-
if (settled(snapshot.state))
|
|
47
|
+
if (lead !== void 0 && settled(snapshot.state)) lead = void 0;
|
|
48
|
+
syncOptimistic(snapshot);
|
|
57
49
|
});
|
|
58
50
|
if (getOwner() !== null) onCleanup(() => {
|
|
59
51
|
endIntent();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cssints/machine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.35.0",
|
|
4
4
|
"description": "A minimal, dependency-free, type-safe state machine for UI protocols: effects as data, a stale effect cannot complete, debounce with a flush, an injectable clock and a harness that proves it without a browser",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
}
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"solid-js": "^2.0.0-rc.
|
|
28
|
+
"solid-js": "^2.0.0-rc.21"
|
|
29
29
|
},
|
|
30
30
|
"peerDependenciesMeta": {
|
|
31
31
|
"solid-js": {
|