@systemfsoftware/xstate-store 0.0.0-stage → 5.0.0-beta.6
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/CHANGELOG.md +7 -0
- package/LICENSE +22 -0
- package/README.md +526 -2
- package/dist/index.d.ts +147 -0
- package/dist/index.mjs +70 -0
- package/dist/persist.d.ts +256 -0
- package/dist/persist.mjs +630 -0
- package/dist/reset.d.ts +51 -0
- package/dist/reset.mjs +61 -0
- package/dist/store-BMZdYhoE.mjs +755 -0
- package/dist/types-0n0RpfhK.d.ts +442 -0
- package/dist/undo.d.ts +77 -0
- package/dist/undo.mjs +222 -0
- package/dist/validate.d.ts +35 -0
- package/dist/validate.mjs +138 -0
- package/dist/validationError-Ndy-qk0W.mjs +6 -0
- package/package.json +80 -3
package/dist/reset.mjs
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { t as appendInternalEventTypes } from "./store-BMZdYhoE.mjs";
|
|
2
|
+
// src/reset.ts
|
|
3
|
+
function resetFromLogic(logic, options) {
|
|
4
|
+
return {
|
|
5
|
+
...logic,
|
|
6
|
+
eventTypes: appendInternalEventTypes(logic.eventTypes, ["reset"], "reset"),
|
|
7
|
+
getInitialSnapshot: () => logic.getInitialSnapshot(),
|
|
8
|
+
transition: (snapshot, event) => {
|
|
9
|
+
if (event.type === "reset") {
|
|
10
|
+
const initialSnapshot = logic.getInitialSnapshot();
|
|
11
|
+
const resetContext = options?.to ? options.to(initialSnapshot.context, snapshot.context) : initialSnapshot.context;
|
|
12
|
+
return [{
|
|
13
|
+
...snapshot,
|
|
14
|
+
context: resetContext
|
|
15
|
+
}, []];
|
|
16
|
+
}
|
|
17
|
+
return logic.transition(snapshot, event);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Creates a store extension that adds a `reset` trigger to reset the store
|
|
23
|
+
* context to its initial state.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { reset } from '@systemfsoftware/xstate-store/reset';
|
|
29
|
+
*
|
|
30
|
+
* const store = createStore({
|
|
31
|
+
* context: { count: 0 },
|
|
32
|
+
* on: { inc: (ctx) => ({ count: ctx.count + 1 }) }
|
|
33
|
+
* }).with(reset());
|
|
34
|
+
*
|
|
35
|
+
* store.trigger.inc();
|
|
36
|
+
* store.trigger.reset(); // count = 0
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* // Partial reset: keep some fields
|
|
43
|
+
* const store = createStore({
|
|
44
|
+
* context: { count: 0, user: null as string | null },
|
|
45
|
+
* on: {
|
|
46
|
+
* inc: (ctx) => ({ ...ctx, count: ctx.count + 1 }),
|
|
47
|
+
* login: (ctx, e: { user: string }) => ({ ...ctx, user: e.user })
|
|
48
|
+
* }
|
|
49
|
+
* }).with(
|
|
50
|
+
* reset({
|
|
51
|
+
* to: (initial, current) => ({ ...initial, user: current.user })
|
|
52
|
+
* })
|
|
53
|
+
* );
|
|
54
|
+
*
|
|
55
|
+
* store.trigger.reset(); // resets count, keeps user
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
function reset(options) {
|
|
59
|
+
return (logic) => resetFromLogic(logic, options);
|
|
60
|
+
}
|
|
61
|
+
export { reset };
|