@systemfsoftware/xstate-store 0.0.0-bootstrap.0 → 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/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 };