@daltonr/pathwrite-store 0.10.0 → 0.10.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.
Files changed (2) hide show
  1. package/README.md +90 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # @daltonr/pathwrite-store
2
+
3
+ HTTP, localStorage, and AsyncStorage persistence for Pathwrite.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @daltonr/pathwrite-store
9
+ ```
10
+
11
+ `AsyncStorageStore` additionally requires `@react-native-async-storage/async-storage`, which is not installed automatically.
12
+
13
+ ## Quick start
14
+
15
+ ```typescript
16
+ import { PathEngine } from "@daltonr/pathwrite-core";
17
+ import { HttpStore, persistence, restoreOrStart } from "@daltonr/pathwrite-store";
18
+
19
+ const store = new HttpStore({
20
+ baseUrl: "/api/wizard",
21
+ headers: { Authorization: `Bearer ${token}` },
22
+ // Expects these three endpoints on your backend:
23
+ // PUT /api/wizard/state/{key} — save state (body: SerializedPathState)
24
+ // GET /api/wizard/state/{key} — load state (return 404 when not found)
25
+ // DELETE /api/wizard/state/{key} — delete state on completion
26
+ });
27
+
28
+ const key = `user:${userId}:onboarding`;
29
+
30
+ const { engine, restored } = await restoreOrStart({
31
+ store,
32
+ key,
33
+ path: onboardingWizard,
34
+ initialData: { name: "", email: "" },
35
+ observers: [
36
+ persistence({ store, key, strategy: "onNext" }),
37
+ ],
38
+ });
39
+
40
+ // Pass the engine to any framework adapter
41
+ // e.g. const { snapshot, next } = usePath({ engine });
42
+
43
+ if (restored) {
44
+ console.log("Resuming from saved progress.");
45
+ }
46
+ ```
47
+
48
+ ## Stores
49
+
50
+ | Store | Import | Use for |
51
+ |---|---|---|
52
+ | `HttpStore` | `@daltonr/pathwrite-store` | REST API backend (browser or Node). |
53
+ | `LocalStorageStore` | `@daltonr/pathwrite-store` | Browser `localStorage` or `sessionStorage`. Falls back to in-memory in Node/test environments. |
54
+ | `AsyncStorageStore` | `@daltonr/pathwrite-store` | React Native. Requires `@react-native-async-storage/async-storage` as a peer dependency. |
55
+
56
+ All three implement the `PathStore` interface from `@daltonr/pathwrite-core` (`save`, `load`, `delete`) and are interchangeable as far as `persistence()` and `restoreOrStart()` are concerned.
57
+
58
+ ## Save strategies
59
+
60
+ Pass `strategy` to `persistence()` to control when saves fire.
61
+
62
+ | Strategy | When it saves | API calls (5 keystrokes + Next) |
63
+ |---|---|---|
64
+ | `"onNext"` *(default)* | After `next()` navigates to a new step | 1 |
65
+ | `"onEveryChange"` | Every settled `stateChanged` event (add `debounceMs` for text inputs) | 6 (or 2 with `debounceMs: 500`) |
66
+ | `"onSubPathComplete"` | When a sub-path finishes and the parent path resumes | varies |
67
+ | `"onComplete"` | When the path completes; does not delete the record afterward | 0 mid-flow, 1 at end |
68
+ | `"manual"` | Never — call `store.save(key, engine.exportState()!)` yourself | 0 |
69
+
70
+ ## restoreOrStart()
71
+
72
+ `restoreOrStart()` handles the standard load/restore-or-start pattern in a single call. It tries `store.load(key)`; if a saved state is found it reconstructs the engine at the saved step via `PathEngine.fromState()`; if nothing is found it creates a fresh engine and calls `engine.start(path, initialData)`. Observers are wired before the first event in both cases, so the persistence observer never misses a state transition.
73
+
74
+ ```typescript
75
+ const { engine, restored } = await restoreOrStart({
76
+ store, // any PathStore
77
+ key, // string session key, e.g. "user:123:signup"
78
+ path, // PathDefinition for the wizard
79
+ initialData, // used only when starting fresh (not on restore)
80
+ observers, // PathObserver[] — wired before the first event
81
+ pathDefinitions // optional: required when the path uses sub-paths
82
+ });
83
+ ```
84
+
85
+ `engine` is a plain `PathEngine` ready to pass to any framework adapter. `restored` is `true` when a saved session was found. When the path later completes, the `persistence` observer automatically calls `store.delete(key)` so a returning user starts fresh.
86
+
87
+ ## Further reading
88
+
89
+ - [docs/guides/persistence.md](../../docs/guides/persistence.md) — strategies, offline patterns, custom stores, and the full `HttpStore` options reference
90
+ - [docs/README.md](../../docs/README.md) — documentation index
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@daltonr/pathwrite-store",
3
- "version": "0.10.0",
3
+ "version": "0.10.1",
4
4
  "description": "Persistence adapters for PathEngine — LocalStorageStore (browser), AsyncStorageStore (React Native / any async key-value store), and HttpStore (REST API)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -39,7 +39,7 @@
39
39
  "directory": "packages/store"
40
40
  },
41
41
  "dependencies": {
42
- "@daltonr/pathwrite-core": "^0.10.0"
42
+ "@daltonr/pathwrite-core": "^0.10.1"
43
43
  },
44
44
  "devDependencies": {
45
45
  "typescript": "^5.3.0",