@lite-fsm/persist 1.0.0 → 1.0.1
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 +107 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# @lite-fsm/persist
|
|
2
|
+
|
|
3
|
+
Persistence helpers for `MachineManager`. The package saves manager snapshots to a storage adapter, restores them with `manager.hydrate()`, tracks restore status, and can integrate with React through a small status hook entry point.
|
|
4
|
+
|
|
5
|
+
Use it for browser session persistence, tab synchronization, custom storage backends, or explicit snapshot save/restore flows.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @lite-fsm/persist
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
For React status hooks:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @lite-fsm/react @lite-fsm/persist
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Entry Points
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { createJsonStorage, persistManager } from "@lite-fsm/persist";
|
|
23
|
+
import type { PersistController, PersistStorage, PersistStatus } from "@lite-fsm/persist";
|
|
24
|
+
|
|
25
|
+
import { useIsPersistRestoring, usePersistStatus } from "@lite-fsm/persist/react";
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Example
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { MachineManager, createMachine, type FSMEvent } from "@lite-fsm/core";
|
|
32
|
+
import { createJsonStorage, persistManager } from "@lite-fsm/persist";
|
|
33
|
+
|
|
34
|
+
type CounterEvent = FSMEvent<"INCREMENT"> | FSMEvent<"RESET">;
|
|
35
|
+
|
|
36
|
+
const counter = createMachine<CounterEvent>({
|
|
37
|
+
config: {
|
|
38
|
+
READY: {
|
|
39
|
+
INCREMENT: null,
|
|
40
|
+
RESET: null,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
initialState: "READY",
|
|
44
|
+
initialContext: { count: 0 },
|
|
45
|
+
reducer: (slice, event) => ({
|
|
46
|
+
state: slice.state,
|
|
47
|
+
context: {
|
|
48
|
+
count: event.type === "RESET" ? 0 : slice.context.count + 1,
|
|
49
|
+
},
|
|
50
|
+
}),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const machines = { counter };
|
|
54
|
+
const manager = MachineManager<typeof machines>(machines, { schemaVersion: 1 });
|
|
55
|
+
|
|
56
|
+
const persist = persistManager(manager, {
|
|
57
|
+
storage: createJsonStorage<typeof machines>({
|
|
58
|
+
key: "app:state:v1",
|
|
59
|
+
storage: window.localStorage,
|
|
60
|
+
}),
|
|
61
|
+
storageVersion: 1,
|
|
62
|
+
machines: ["counter"],
|
|
63
|
+
throttleMs: 500,
|
|
64
|
+
onError: console.error,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const stop = persist.start();
|
|
68
|
+
|
|
69
|
+
manager.transition({ type: "INCREMENT" });
|
|
70
|
+
await persist.flush();
|
|
71
|
+
|
|
72
|
+
stop();
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## React Integration
|
|
76
|
+
|
|
77
|
+
`FSMContextProvider` can start and stop a persist controller for you:
|
|
78
|
+
|
|
79
|
+
```tsx
|
|
80
|
+
import { FSMContextProvider } from "@lite-fsm/react";
|
|
81
|
+
|
|
82
|
+
export function App() {
|
|
83
|
+
return (
|
|
84
|
+
<FSMContextProvider machineManager={manager} persist={persist}>
|
|
85
|
+
<Page />
|
|
86
|
+
</FSMContextProvider>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Read restore status from React with `@lite-fsm/persist/react`:
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
import { usePersistStatus } from "@lite-fsm/persist/react";
|
|
95
|
+
import type { PersistController } from "@lite-fsm/persist";
|
|
96
|
+
|
|
97
|
+
function PersistStatusView({ controller }: { controller: PersistController }) {
|
|
98
|
+
const status = usePersistStatus(controller);
|
|
99
|
+
return <span>{status.phase}</span>;
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Documentation
|
|
104
|
+
|
|
105
|
+
- [Full documentation](https://alexandergureev.github.io/lite-fsm/)
|
|
106
|
+
- [Persist package guide](https://alexandergureev.github.io/lite-fsm/packages/persist)
|
|
107
|
+
- [Persist API reference](https://alexandergureev.github.io/lite-fsm/api/persist)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lite-fsm/persist",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Persistence helpers for lite-fsm",
|
|
6
6
|
"license": "MIT",
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
},
|
|
71
71
|
"dependencies": {
|
|
72
72
|
"use-sync-external-store": ">=1.2.0",
|
|
73
|
-
"@lite-fsm/core": "2.0.
|
|
73
|
+
"@lite-fsm/core": "2.0.1"
|
|
74
74
|
},
|
|
75
75
|
"peerDependencies": {
|
|
76
76
|
"react": ">=18.0.0"
|