@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/index.d.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { $ as StoreSchemas, A as ResolveStoreEventPayloadMap, B as StoreContext, C as InputFromStoreLogicCreator, D as ReducerAtom, E as ReadonlyAtom, F as SpecificStoreConfig, G as StoreFromStoreLogicCreator, H as StoreEffectEnqueue, I as StandardSchemaMap, J as StoreInspectedEventEvent, K as StoreInspectedActionEvent, L as Store, M as Selection, N as Selector, O as ResolveStoreContext, P as SnapshotFromStore, Q as StoreLogicCreator, R as StoreAssigner, S as InputFromAtomConfig, T as Readable, U as StoreExtension, V as StoreEffect, W as StoreFromStoreConfig, X as StoreInspectionEvent, Y as StoreInspectedSnapshotEvent, Z as StoreLogic, _ as EventObject, a as AnyStoreConfig, at as TriggerObject, b as InferSchemaOutput, c as AtomConfig, d as CanObject, et as StoreSelectorsConfig, f as ContextFromStoreConfig, g as EventFromStoreConfig, h as EventFromStore, i as AnyStore, it as Subscription, j as ResolvedStoreSelectors, k as ResolveStoreEmittedPayloadMap, l as AtomOptions, m as EnqueueObject, n as AnyAtom, nt as StoreWithSelectors, o as AnyStoreLogicCreator, ot as ValueFromAtomConfig, p as EmitsFromStoreConfig, q as StoreInspectedActorEvent, r as AnyAtomConfig, rt as Subscribable, s as Atom, st as StandardSchemaV1, t as ActorRefLike, tt as StoreSnapshot, u as BaseAtom, v as EventPayloadMap, w as Observer, x as InferSchemaPayloadMap, y as ExtractEvents, z as StoreConfig } from "./types-0n0RpfhK.js";
|
|
2
|
+
import { ActorLogic } from "@systemfsoftware/xstate";
|
|
3
|
+
/** Returns `true` if `value` is an atom (has `get` and `subscribe` methods). */
|
|
4
|
+
export declare function isAtom(value: unknown): value is AnyAtom;
|
|
5
|
+
/** The current state of an async atom. */
|
|
6
|
+
type AsyncAtomState<Data, Error = unknown> = {
|
|
7
|
+
status: 'pending';
|
|
8
|
+
} | {
|
|
9
|
+
status: 'done';
|
|
10
|
+
data: Data;
|
|
11
|
+
} | {
|
|
12
|
+
status: 'error';
|
|
13
|
+
error: Error;
|
|
14
|
+
};
|
|
15
|
+
/** Options passed to an async atom getter. */
|
|
16
|
+
interface AsyncAtomOptions {
|
|
17
|
+
/** Signal aborted when the async atom recomputes before this run settles. */
|
|
18
|
+
signal: AbortSignal;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates a read-only atom whose value is loaded from an async getter.
|
|
22
|
+
*
|
|
23
|
+
* The getter receives an `AbortSignal`; when the async atom recomputes, the
|
|
24
|
+
* previous signal is aborted and stale resolutions are ignored.
|
|
25
|
+
*/
|
|
26
|
+
export declare function createAsyncAtom<T>(getValue: (options: AsyncAtomOptions) => Promise<T>, options?: AtomOptions<AsyncAtomState<T>>): ReadonlyAtom<AsyncAtomState<T>>;
|
|
27
|
+
/**
|
|
28
|
+
* Creates an atom.
|
|
29
|
+
*
|
|
30
|
+
* Pass a value for a writable atom or a getter for a computed read-only atom.
|
|
31
|
+
*/
|
|
32
|
+
export declare function createAtom<T>(getValue: (prev?: T) => T, options?: AtomOptions<T>): ReadonlyAtom<T>;
|
|
33
|
+
export declare function createAtom<T>(initialValue: T, options?: AtomOptions<T>): Atom<T>;
|
|
34
|
+
/**
|
|
35
|
+
* Creates an inert atom config that can be instantiated later.
|
|
36
|
+
*
|
|
37
|
+
* Useful for framework hooks that need to create a stable local atom from
|
|
38
|
+
* component input.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createAtomConfig<T, TInput>(getInitialValue: (input: TInput) => T, options?: AtomOptions<T>): AtomConfig<T, TInput>;
|
|
41
|
+
export declare function createAtomConfig<T>(initialValue: T, options?: AtomOptions<T>): AtomConfig<T, undefined>;
|
|
42
|
+
/** Creates an atom whose updates are handled by a reducer function. */
|
|
43
|
+
export declare function createReducerAtom<TState, TEvent>(initialValue: TState, reducer: (state: TState, event: TEvent) => TState, options?: AtomOptions<TState>): ReducerAtom<TState, TEvent>;
|
|
44
|
+
type TransitionsFromEventPayloadMap<TEventPayloadMap extends EventPayloadMap, TContext extends StoreContext, TEmitted extends EventObject> = { [K in keyof TEventPayloadMap & string]?: StoreAssigner<TContext, {
|
|
45
|
+
type: K;
|
|
46
|
+
} & TEventPayloadMap[K], TEmitted, TEventPayloadMap>; };
|
|
47
|
+
/**
|
|
48
|
+
* Creates a **store** that has its own internal state and can be sent events
|
|
49
|
+
* that update its internal state based on transitions.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* const store = createStore({
|
|
55
|
+
* context: { count: 0, name: 'Ada' },
|
|
56
|
+
* on: {
|
|
57
|
+
* inc: (context, event: { by: number }) => ({
|
|
58
|
+
* ...context,
|
|
59
|
+
* count: context.count + event.by
|
|
60
|
+
* })
|
|
61
|
+
* }
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* store.subscribe((snapshot) => {
|
|
65
|
+
* console.log(snapshot);
|
|
66
|
+
* });
|
|
67
|
+
*
|
|
68
|
+
* store.send({ type: 'inc', by: 5 });
|
|
69
|
+
* // Logs { context: { count: 5, name: 'Ada' }, status: 'active', ... }
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @param config - The store configuration object:
|
|
73
|
+
* - `config.context` - The initial state of the store
|
|
74
|
+
* - `config.schemas` - Optional standard-schema definitions used as type
|
|
75
|
+
* sources for context, events, and emitted events
|
|
76
|
+
* - `config.on` - An object mapping event types to transition functions
|
|
77
|
+
* @returns A store instance with methods to send events and subscribe to state
|
|
78
|
+
* changes
|
|
79
|
+
*/
|
|
80
|
+
export declare function createStore<TContext extends StoreContext, const TEventPayloadMap extends EventPayloadMap, TEmittedPayloadMap extends EventPayloadMap = {}, TContextSchema extends StandardSchemaV1 | undefined = undefined, TEventSchemaMap extends StandardSchemaMap | undefined = undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined = undefined>(definition: StoreConfig<TContext, TEventPayloadMap, TEmittedPayloadMap, TContextSchema, TEventSchemaMap, TEmittedSchemaMap>): Store<ResolveStoreContext<TContext, TContextSchema>, ResolveStoreEventPayloadMap<TEventPayloadMap, TEventSchemaMap>, ExtractEvents<ResolveStoreEmittedPayloadMap<TEmittedPayloadMap, TEmittedSchemaMap>>>;
|
|
81
|
+
export declare function createStore<TContext extends StoreContext, TEvent extends EventObject, TEmitted extends EventObject, TContextSchema extends StandardSchemaV1 | undefined = undefined, TEventSchemaMap extends StandardSchemaMap | undefined = undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined = undefined>(definition: SpecificStoreConfig<TContext, TEvent, TEmitted, TContextSchema, TEventSchemaMap, TEmittedSchemaMap> | StoreLogic<StoreSnapshot<ResolveStoreContext<TContext, TContextSchema>>, TEvent, TEmitted>): Store<ResolveStoreContext<TContext, TContextSchema>, { [E in TEvent as E['type']]: E; }, TEmitted>;
|
|
82
|
+
export declare const createStoreConfig: {
|
|
83
|
+
<TContext extends StoreContext, TEventPayloadMap extends EventPayloadMap, TEmitted extends EventPayloadMap = {}, TContextSchema extends StandardSchemaV1 | undefined = undefined, TEventSchemaMap extends StandardSchemaMap | undefined = undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined = undefined>(definition: StoreConfig<TContext, TEventPayloadMap, TEmitted, TContextSchema, TEventSchemaMap, TEmittedSchemaMap>): StoreConfig<TContext, TEventPayloadMap, TEmitted, TContextSchema, TEventSchemaMap, TEmittedSchemaMap>;
|
|
84
|
+
};
|
|
85
|
+
export declare function createStoreLogic<TContext extends StoreContext, const TEventPayloadMap extends EventPayloadMap, TEmittedPayloadMap extends EventPayloadMap = {}, TInput = undefined, TContextSchema extends StandardSchemaV1 | undefined = undefined, TEventSchemaMap extends StandardSchemaMap | undefined = undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined = undefined, TSelectors extends StoreSelectorsConfig<ResolveStoreContext<TContext, TContextSchema>> = StoreSelectorsConfig<ResolveStoreContext<TContext, TContextSchema>>>(config: Omit<StoreConfig<TContext, TEventPayloadMap, TEmittedPayloadMap, TContextSchema, TEventSchemaMap, TEmittedSchemaMap>, 'context'> & {
|
|
86
|
+
context: (input: TInput) => ResolveStoreContext<TContext, TContextSchema>;
|
|
87
|
+
selectors: TSelectors;
|
|
88
|
+
}): StoreLogicCreator<ResolveStoreContext<TContext, TContextSchema>, ResolveStoreEventPayloadMap<TEventPayloadMap, TEventSchemaMap>, ExtractEvents<ResolveStoreEmittedPayloadMap<TEmittedPayloadMap, TEmittedSchemaMap>>, TInput, TSelectors>;
|
|
89
|
+
export declare function createStoreLogic<TContext extends StoreContext, const TEventPayloadMap extends EventPayloadMap, TEmittedPayloadMap extends EventPayloadMap = {}, TInput = undefined, TContextSchema extends StandardSchemaV1 | undefined = undefined, TEventSchemaMap extends StandardSchemaMap | undefined = undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined = undefined>(config: Omit<StoreConfig<TContext, TEventPayloadMap, TEmittedPayloadMap, TContextSchema, TEventSchemaMap, TEmittedSchemaMap>, 'context'> & {
|
|
90
|
+
context: (input: TInput) => ResolveStoreContext<TContext, TContextSchema>;
|
|
91
|
+
}): StoreLogicCreator<ResolveStoreContext<TContext, TContextSchema>, ResolveStoreEventPayloadMap<TEventPayloadMap, TEventSchemaMap>, ExtractEvents<ResolveStoreEmittedPayloadMap<TEmittedPayloadMap, TEmittedSchemaMap>>, TInput, {}>;
|
|
92
|
+
export declare function createStoreLogic<TContext extends StoreContext, const TEventPayloadMap extends EventPayloadMap, TEmittedPayloadMap extends EventPayloadMap = {}, TContextSchema extends StandardSchemaV1 | undefined = undefined, TEventSchemaMap extends StandardSchemaMap | undefined = undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined = undefined, TSelectors extends StoreSelectorsConfig<ResolveStoreContext<TContext, TContextSchema>> = {}>(config: StoreConfig<TContext, TEventPayloadMap, TEmittedPayloadMap, TContextSchema, TEventSchemaMap, TEmittedSchemaMap> & {
|
|
93
|
+
selectors: TSelectors;
|
|
94
|
+
}): StoreLogicCreator<ResolveStoreContext<TContext, TContextSchema>, ResolveStoreEventPayloadMap<TEventPayloadMap, TEventSchemaMap>, ExtractEvents<ResolveStoreEmittedPayloadMap<TEmittedPayloadMap, TEmittedSchemaMap>>, void, TSelectors>;
|
|
95
|
+
export declare function createStoreLogic<TContext extends StoreContext, const TEventPayloadMap extends EventPayloadMap, TEmittedPayloadMap extends EventPayloadMap = {}, TContextSchema extends StandardSchemaV1 | undefined = undefined, TEventSchemaMap extends StandardSchemaMap | undefined = undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined = undefined>(config: StoreConfig<TContext, TEventPayloadMap, TEmittedPayloadMap, TContextSchema, TEventSchemaMap, TEmittedSchemaMap>): StoreLogicCreator<ResolveStoreContext<TContext, TContextSchema>, ResolveStoreEventPayloadMap<TEventPayloadMap, TEventSchemaMap>, ExtractEvents<ResolveStoreEmittedPayloadMap<TEmittedPayloadMap, TEmittedSchemaMap>>, void, {}>;
|
|
96
|
+
declare global {
|
|
97
|
+
interface SymbolConstructor {
|
|
98
|
+
readonly observable: symbol;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
type FromStoreLogic<TContext extends StoreContext, TEvent extends EventObject, TInput, TEmitted extends EventObject> = ActorLogic<StoreSnapshot<TContext>, TEvent, TInput, any, TEmitted>;
|
|
102
|
+
type FromStoreEmittedEvents<TEmittedPayloadMap extends EventPayloadMap, TEmittedSchemaMap extends StandardSchemaMap | undefined> = ExtractEvents<ResolveStoreEmittedPayloadMap<TEmittedPayloadMap, TEmittedSchemaMap>>;
|
|
103
|
+
type InferredFromStoreTransitions<TContext extends StoreContext, TTransitions extends Record<string, (...args: any[]) => any>, TEmittedPayloadMap extends EventPayloadMap, TContextSchema extends StandardSchemaV1 | undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined> = Record<string, (context: NoInfer<ResolveStoreContext<TContext, TContextSchema>>, event: any, enq: EnqueueObject<ResolveStoreContext<TContext, TContextSchema>, FromStoreEmittedEvents<TEmittedPayloadMap, TEmittedSchemaMap>, InferredEventPayloadMap<TTransitions>>) => ResolveStoreContext<TContext, TContextSchema> | void>;
|
|
104
|
+
type InferredEventPayloadMap<TTransitions extends Record<string, (...args: any[]) => any>> = { [K in keyof TTransitions & string]: TTransitions[K] extends ((context: any, event: infer TEvent, ...args: any[]) => any) ? Omit<TEvent, 'type'> : {}; };
|
|
105
|
+
type SchemaFromStoreTransitions<TContext extends StoreContext, TEventSchemaMap extends StandardSchemaMap, TEmittedPayloadMap extends EventPayloadMap, TContextSchema extends StandardSchemaV1 | undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined> = TransitionsFromEventPayloadMap<InferSchemaPayloadMap<TEventSchemaMap>, NoInfer<ResolveStoreContext<TContext, TContextSchema>>, FromStoreEmittedEvents<TEmittedPayloadMap, TEmittedSchemaMap>>;
|
|
106
|
+
type FromStoreValueConfig<TContext extends StoreContext, TTransitions extends Record<string, (...args: any[]) => any>, TEmittedPayloadMap extends EventPayloadMap, TContextSchema extends StandardSchemaV1 | undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined> = {
|
|
107
|
+
context: ResolveStoreContext<TContext, TContextSchema>;
|
|
108
|
+
schemas?: StoreSchemas<TContextSchema, undefined, TEmittedSchemaMap>;
|
|
109
|
+
on: TTransitions & InferredFromStoreTransitions<TContext, TTransitions, TEmittedPayloadMap, TContextSchema, TEmittedSchemaMap>;
|
|
110
|
+
};
|
|
111
|
+
type FromStoreInputConfig<TContext extends StoreContext, TTransitions extends Record<string, (...args: any[]) => any>, TInput, TEmittedPayloadMap extends EventPayloadMap, TContextSchema extends StandardSchemaV1 | undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined> = {
|
|
112
|
+
context: (input: TInput) => ResolveStoreContext<TContext, TContextSchema>;
|
|
113
|
+
schemas?: StoreSchemas<TContextSchema, undefined, TEmittedSchemaMap>;
|
|
114
|
+
on: TTransitions & InferredFromStoreTransitions<TContext, TTransitions, TEmittedPayloadMap, TContextSchema, TEmittedSchemaMap>;
|
|
115
|
+
};
|
|
116
|
+
type SchemaFromStoreValueConfig<TContext extends StoreContext, TEventSchemaMap extends StandardSchemaMap, TEmittedPayloadMap extends EventPayloadMap, TContextSchema extends StandardSchemaV1 | undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined> = {
|
|
117
|
+
context: ResolveStoreContext<TContext, TContextSchema>;
|
|
118
|
+
schemas: StoreSchemas<TContextSchema, TEventSchemaMap, TEmittedSchemaMap> & {
|
|
119
|
+
events: TEventSchemaMap;
|
|
120
|
+
};
|
|
121
|
+
on: SchemaFromStoreTransitions<TContext, TEventSchemaMap, TEmittedPayloadMap, TContextSchema, TEmittedSchemaMap>;
|
|
122
|
+
};
|
|
123
|
+
type SchemaFromStoreInputConfig<TContext extends StoreContext, TEventSchemaMap extends StandardSchemaMap, TInput, TEmittedPayloadMap extends EventPayloadMap, TContextSchema extends StandardSchemaV1 | undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined> = {
|
|
124
|
+
context: (input: TInput) => ResolveStoreContext<TContext, TContextSchema>;
|
|
125
|
+
schemas: StoreSchemas<TContextSchema, TEventSchemaMap, TEmittedSchemaMap> & {
|
|
126
|
+
events: TEventSchemaMap;
|
|
127
|
+
};
|
|
128
|
+
on: SchemaFromStoreTransitions<TContext, TEventSchemaMap, TEmittedPayloadMap, TContextSchema, TEmittedSchemaMap>;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* An actor logic creator which creates store [actor
|
|
132
|
+
* logic](https://stately.ai/docs/actors#actor-logic) for use with XState.
|
|
133
|
+
*
|
|
134
|
+
* @param config An object containing the store configuration:
|
|
135
|
+
* - `config.context` The initial context for the store, either a function
|
|
136
|
+
* that returns context based on input, or the context itself
|
|
137
|
+
* - `config.schemas` Optional standard-schema definitions used as type
|
|
138
|
+
* sources for context, events, and emitted events
|
|
139
|
+
* - `config.on` An object defining the transitions for different event types
|
|
140
|
+
* @returns An actor logic creator function that creates store actor logic
|
|
141
|
+
*/
|
|
142
|
+
export declare function fromStore<TContext extends StoreContext, const TTransitions extends Record<string, (...args: any[]) => any>, TInput, TEmittedPayloadMap extends EventPayloadMap = EventPayloadMap, TContextSchema extends StandardSchemaV1 | undefined = undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined = undefined>(config: FromStoreInputConfig<TContext, TTransitions, TInput, TEmittedPayloadMap, TContextSchema, TEmittedSchemaMap>): FromStoreLogic<ResolveStoreContext<TContext, TContextSchema>, ExtractEvents<InferredEventPayloadMap<TTransitions>>, TInput, FromStoreEmittedEvents<TEmittedPayloadMap, TEmittedSchemaMap>>;
|
|
143
|
+
export declare function fromStore<TContext extends StoreContext, const TTransitions extends Record<string, (...args: any[]) => any>, TEmittedPayloadMap extends EventPayloadMap = EventPayloadMap, TContextSchema extends StandardSchemaV1 | undefined = undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined = undefined>(config: FromStoreValueConfig<TContext, TTransitions, TEmittedPayloadMap, TContextSchema, TEmittedSchemaMap>): FromStoreLogic<ResolveStoreContext<TContext, TContextSchema>, ExtractEvents<InferredEventPayloadMap<TTransitions>>, unknown, FromStoreEmittedEvents<TEmittedPayloadMap, TEmittedSchemaMap>>;
|
|
144
|
+
export declare function fromStore<TContext extends StoreContext, TEventSchemaMap extends StandardSchemaMap, TInput, TEmittedPayloadMap extends EventPayloadMap = EventPayloadMap, TContextSchema extends StandardSchemaV1 | undefined = undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined = undefined>(config: SchemaFromStoreInputConfig<TContext, TEventSchemaMap, TInput, TEmittedPayloadMap, TContextSchema, TEmittedSchemaMap>): FromStoreLogic<ResolveStoreContext<TContext, TContextSchema>, ExtractEvents<InferSchemaPayloadMap<TEventSchemaMap>>, TInput, FromStoreEmittedEvents<TEmittedPayloadMap, TEmittedSchemaMap>>;
|
|
145
|
+
export declare function fromStore<TContext extends StoreContext, TEventSchemaMap extends StandardSchemaMap, TEmittedPayloadMap extends EventPayloadMap = EventPayloadMap, TContextSchema extends StandardSchemaV1 | undefined = undefined, TEmittedSchemaMap extends StandardSchemaMap | undefined = undefined>(config: SchemaFromStoreValueConfig<TContext, TEventSchemaMap, TEmittedPayloadMap, TContextSchema, TEmittedSchemaMap>): FromStoreLogic<ResolveStoreContext<TContext, TContextSchema>, ExtractEvents<InferSchemaPayloadMap<TEventSchemaMap>>, unknown, FromStoreEmittedEvents<TEmittedPayloadMap, TEmittedSchemaMap>>;
|
|
146
|
+
export declare function shallowEqual(objA: any, objB: any): boolean;
|
|
147
|
+
export type { ActorRefLike, AnyAtom, AnyAtomConfig, AnyStore, AnyStoreConfig, AnyStoreLogicCreator, AsyncAtomOptions, AsyncAtomState, Atom, AtomConfig, AtomOptions, BaseAtom, CanObject, ContextFromStoreConfig, EmitsFromStoreConfig, EnqueueObject, EventFromStore, EventFromStoreConfig, EventObject, EventPayloadMap, ExtractEvents, InferSchemaOutput, InferSchemaPayloadMap, InputFromAtomConfig, InputFromStoreLogicCreator, Observer, Readable, ReadonlyAtom, ReducerAtom, ResolveStoreContext, ResolveStoreEmittedPayloadMap, ResolveStoreEventPayloadMap, ResolvedStoreSelectors, Selection, Selector, SnapshotFromStore, StandardSchemaMap, StandardSchemaV1, Store, StoreAssigner, StoreConfig, StoreContext, StoreEffect, StoreEffectEnqueue, StoreExtension, StoreFromStoreConfig, StoreFromStoreLogicCreator, StoreInspectedActionEvent, StoreInspectedActorEvent, StoreInspectedEventEvent, StoreInspectedSnapshotEvent, StoreInspectionEvent, StoreLogicCreator, StoreSchemas, StoreSelectorsConfig, StoreSnapshot, StoreWithSelectors, Subscribable, Subscription, TriggerObject, ValueFromAtomConfig };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { a as createStoreConfig, c as createAsyncAtom, d as createReducerAtom, f as isAtom, i as createStore, l as createAtom, o as createStoreLogic, s as createStoreTransition, u as createAtomConfig } from "./store-BMZdYhoE.mjs";
|
|
2
|
+
// src/fromStore.ts
|
|
3
|
+
function fromStore(config) {
|
|
4
|
+
const initialContext = config.context;
|
|
5
|
+
const transition = createStoreTransition(config.on);
|
|
6
|
+
const initialTransition = (input, _) => [{
|
|
7
|
+
status: "active",
|
|
8
|
+
context: typeof initialContext === "function" ? initialContext(input) : initialContext,
|
|
9
|
+
output: void 0,
|
|
10
|
+
error: void 0
|
|
11
|
+
}, []];
|
|
12
|
+
return {
|
|
13
|
+
transition: (snapshot, event, actorScope) => {
|
|
14
|
+
const [nextSnapshot, effects] = transition(snapshot, event);
|
|
15
|
+
return [nextSnapshot, effects.map((effect) => {
|
|
16
|
+
if (typeof effect === "function") {
|
|
17
|
+
const action = (system = actorScope.self.system) => {
|
|
18
|
+
const send = (event) => void system.sendEvent(actorScope.self, actorScope.self, event);
|
|
19
|
+
return effect({
|
|
20
|
+
send,
|
|
21
|
+
trigger: new Proxy({}, { get: (_, eventType) => {
|
|
22
|
+
return (payload) => send({
|
|
23
|
+
type: eventType,
|
|
24
|
+
...payload
|
|
25
|
+
});
|
|
26
|
+
} }),
|
|
27
|
+
getSnapshot: () => actorScope.self.getSnapshot()
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
kind: "action",
|
|
32
|
+
type: "effect",
|
|
33
|
+
action,
|
|
34
|
+
params: void 0,
|
|
35
|
+
args: [],
|
|
36
|
+
exec: action
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
kind: "emit",
|
|
41
|
+
type: effect.type,
|
|
42
|
+
source: actorScope.self,
|
|
43
|
+
event: effect,
|
|
44
|
+
params: void 0,
|
|
45
|
+
args: [],
|
|
46
|
+
exec: (system = actorScope.self.system) => system.emitEvent(actorScope.self, effect)
|
|
47
|
+
};
|
|
48
|
+
})];
|
|
49
|
+
},
|
|
50
|
+
initialTransition,
|
|
51
|
+
getInitialSnapshot: (actorScope, input) => initialTransition(input, actorScope)[0],
|
|
52
|
+
getPersistedSnapshot: (s) => s,
|
|
53
|
+
restoreSnapshot: (s) => s
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
// src/shallowEqual.ts
|
|
57
|
+
function is(x, y) {
|
|
58
|
+
if (x === y) return x !== 0 || y !== 0 || 1 / x === 1 / y;
|
|
59
|
+
else return x !== x && y !== y;
|
|
60
|
+
}
|
|
61
|
+
function shallowEqual(objA, objB) {
|
|
62
|
+
if (is(objA, objB)) return true;
|
|
63
|
+
if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) return false;
|
|
64
|
+
const keysA = Object.keys(objA);
|
|
65
|
+
const keysB = Object.keys(objB);
|
|
66
|
+
if (keysA.length !== keysB.length) return false;
|
|
67
|
+
for (const key of keysA) if (!Object.prototype.hasOwnProperty.call(objB, key) || !is(objA[key], objB[key])) return false;
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
export { createAsyncAtom, createAtom, createAtomConfig, createReducerAtom, createStore, createStoreConfig, createStoreLogic, fromStore, isAtom, shallowEqual };
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { B as StoreContext, U as StoreExtension, _ as EventObject, v as EventPayloadMap } from "./types-0n0RpfhK.js";
|
|
2
|
+
/**
|
|
3
|
+
* Storage interface compatible with `localStorage`, `sessionStorage`, and async
|
|
4
|
+
* storage adapters (React Native AsyncStorage, IndexedDB, etc.).
|
|
5
|
+
*/
|
|
6
|
+
export interface StateStorage {
|
|
7
|
+
getItem: (name: string) => string | null | Promise<string | null>;
|
|
8
|
+
setItem: (name: string, value: string) => void | Promise<void>;
|
|
9
|
+
removeItem: (name: string) => void | Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
/** The envelope persisted to storage for snapshot strategy. @public */
|
|
12
|
+
export interface PersistStorageValue<TContext> {
|
|
13
|
+
context: Partial<TContext>;
|
|
14
|
+
version: string | number;
|
|
15
|
+
}
|
|
16
|
+
/** The envelope persisted to storage for event strategy. @public */
|
|
17
|
+
export interface PersistEventStorageValue<TEvent extends EventObject = EventObject> {
|
|
18
|
+
events: TEvent[];
|
|
19
|
+
/**
|
|
20
|
+
* Snapshot checkpoint from which to replay events. When events are truncated
|
|
21
|
+
* by `maxEvents`, this stores the context at the truncation point so replay
|
|
22
|
+
* produces the correct state.
|
|
23
|
+
*/
|
|
24
|
+
checkpoint?: unknown;
|
|
25
|
+
version: string | number;
|
|
26
|
+
}
|
|
27
|
+
/** Base options shared by both persist strategies. @public */
|
|
28
|
+
export interface PersistBaseOptions {
|
|
29
|
+
/** Storage key (required). */
|
|
30
|
+
name: string;
|
|
31
|
+
/** Storage adapter. Defaults to `localStorage`. */
|
|
32
|
+
storage?: StateStorage;
|
|
33
|
+
/** Schema version. Defaults to 0. */
|
|
34
|
+
version?: string | number;
|
|
35
|
+
/** Minimum milliseconds between storage writes. Defaults to 0 (immediate). */
|
|
36
|
+
throttle?: number;
|
|
37
|
+
/** Called after a successful storage write. */
|
|
38
|
+
onDone?: (data: any) => void;
|
|
39
|
+
/** Called when a storage read or write fails. */
|
|
40
|
+
onError?: (error: unknown) => void;
|
|
41
|
+
/** Skip automatic hydration on store creation. Defaults to `false`. */
|
|
42
|
+
skipHydration?: boolean;
|
|
43
|
+
}
|
|
44
|
+
/** Options for the snapshot persist strategy (default). @public */
|
|
45
|
+
export interface PersistSnapshotOptions<TContext = StoreContext, TEvent extends EventObject = EventObject> extends PersistBaseOptions {
|
|
46
|
+
/** Persist strategy. Defaults to `'snapshot'`. */
|
|
47
|
+
strategy?: 'snapshot';
|
|
48
|
+
/**
|
|
49
|
+
* Controls whether an event should trigger a storage write. Return `false` to
|
|
50
|
+
* skip persisting for that event.
|
|
51
|
+
*/
|
|
52
|
+
filter?: (event: TEvent) => boolean;
|
|
53
|
+
/** Select which parts of context to persist. Defaults to full context. */
|
|
54
|
+
pick?: (context: TContext) => Partial<TContext>;
|
|
55
|
+
/** Migration function for version upgrades. */
|
|
56
|
+
migrate?: (persistedContext: any, version: string | number) => TContext;
|
|
57
|
+
/**
|
|
58
|
+
* Custom merge strategy when rehydrating. Defaults to shallow merge
|
|
59
|
+
* (`{ ...currentContext, ...persistedContext }`).
|
|
60
|
+
*/
|
|
61
|
+
merge?: (persistedContext: Partial<TContext>, currentContext: TContext) => TContext;
|
|
62
|
+
/** Custom serializer. Defaults to `JSON.stringify`. */
|
|
63
|
+
serialize?: (value: PersistStorageValue<TContext>) => string;
|
|
64
|
+
/** Custom deserializer. Defaults to `JSON.parse`. */
|
|
65
|
+
deserialize?: (str: string) => PersistStorageValue<TContext>;
|
|
66
|
+
}
|
|
67
|
+
/** Options for the event persist strategy. @public */
|
|
68
|
+
export interface PersistEventOptions<_TContext = StoreContext, TEvent extends EventObject = EventObject> extends PersistBaseOptions {
|
|
69
|
+
/** Persist strategy. */
|
|
70
|
+
strategy: 'event';
|
|
71
|
+
/**
|
|
72
|
+
* Maximum number of events to keep. When exceeded, a snapshot checkpoint is
|
|
73
|
+
* saved and oldest events are dropped. Replay starts from the checkpoint.
|
|
74
|
+
* Defaults to Infinity.
|
|
75
|
+
*/
|
|
76
|
+
maxEvents?: number;
|
|
77
|
+
/** Migration function for version upgrades. Receives the stored events array. */
|
|
78
|
+
migrate?: (persistedEvents: any[], version: string | number) => any[];
|
|
79
|
+
/** Custom serializer. Defaults to `JSON.stringify`. */
|
|
80
|
+
serialize?: (value: PersistEventStorageValue<TEvent>) => string;
|
|
81
|
+
/** Custom deserializer. Defaults to `JSON.parse`. */
|
|
82
|
+
deserialize?: (str: string) => PersistEventStorageValue<TEvent>;
|
|
83
|
+
}
|
|
84
|
+
/** Options for the `persist` store extension. @public */
|
|
85
|
+
export type PersistOptions<TContext = StoreContext, TEvent extends EventObject = EventObject> = PersistSnapshotOptions<TContext, TEvent> | PersistEventOptions<TContext, TEvent>;
|
|
86
|
+
/**
|
|
87
|
+
* Creates a store extension that persists context to storage.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { persist } from '@systemfsoftware/xstate-store/persist';
|
|
93
|
+
*
|
|
94
|
+
* const store = createStore({
|
|
95
|
+
* context: { count: 0 },
|
|
96
|
+
* on: { inc: (ctx) => ({ count: ctx.count + 1 }) }
|
|
97
|
+
* }).with(persist({ name: 'my-store' }));
|
|
98
|
+
* ```
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
*
|
|
102
|
+
* ```ts
|
|
103
|
+
* // Throttled writes with callbacks
|
|
104
|
+
* const store = createStore({
|
|
105
|
+
* context: { count: 0 },
|
|
106
|
+
* on: { inc: (ctx) => ({ count: ctx.count + 1 }) }
|
|
107
|
+
* }).with(
|
|
108
|
+
* persist({
|
|
109
|
+
* name: 'my-store',
|
|
110
|
+
* throttle: 1000,
|
|
111
|
+
* onDone: (ctx) => console.log('Persisted:', ctx),
|
|
112
|
+
* onError: (err) => console.error('Persist failed:', err)
|
|
113
|
+
* })
|
|
114
|
+
* );
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* // Async storage with manual rehydration
|
|
121
|
+
* import {
|
|
122
|
+
* persist,
|
|
123
|
+
* rehydrateStore,
|
|
124
|
+
* createJSONStorage
|
|
125
|
+
* } from '@systemfsoftware/xstate-store/persist';
|
|
126
|
+
*
|
|
127
|
+
* const store = createStore({
|
|
128
|
+
* context: { count: 0 },
|
|
129
|
+
* on: { inc: (ctx) => ({ count: ctx.count + 1 }) }
|
|
130
|
+
* }).with(
|
|
131
|
+
* persist({
|
|
132
|
+
* name: 'my-store',
|
|
133
|
+
* storage: createJSONStorage(() => AsyncStorage),
|
|
134
|
+
* skipHydration: true
|
|
135
|
+
* })
|
|
136
|
+
* );
|
|
137
|
+
*
|
|
138
|
+
* await rehydrateStore(store);
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
export declare function persist<TContext extends StoreContext, TEventPayloadMap extends EventPayloadMap, TEmitted extends EventObject>(options: PersistOptions<NoInfer<TContext>>): StoreExtension<TContext, TEventPayloadMap, {}, TEmitted>;
|
|
142
|
+
/**
|
|
143
|
+
* Creates a storage adapter with error handling and SSR safety.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
*
|
|
147
|
+
* ```ts
|
|
148
|
+
* import { createJSONStorage } from '@systemfsoftware/xstate-store/persist';
|
|
149
|
+
*
|
|
150
|
+
* // Safe for SSR — returns noop storage if localStorage is unavailable
|
|
151
|
+
* const storage = createJSONStorage(() => localStorage);
|
|
152
|
+
*
|
|
153
|
+
* // Async storage (React Native)
|
|
154
|
+
* const asyncStorage = createJSONStorage(() => AsyncStorage);
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
export declare function createJSONStorage(getStorage: () => StateStorage): StateStorage;
|
|
158
|
+
/**
|
|
159
|
+
* Removes persisted data from storage for a store that uses the `persist`
|
|
160
|
+
* extension.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
*
|
|
164
|
+
* ```ts
|
|
165
|
+
* import { clearStorage } from '@systemfsoftware/xstate-store/persist';
|
|
166
|
+
*
|
|
167
|
+
* clearStorage(store);
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
export declare function clearStorage(store: {
|
|
171
|
+
getSnapshot: () => any;
|
|
172
|
+
}): void | Promise<void>;
|
|
173
|
+
/**
|
|
174
|
+
* Flushes pending throttled updates and waits for already queued async writes.
|
|
175
|
+
* Synchronous storage writes complete before this function returns.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
*
|
|
179
|
+
* ```ts
|
|
180
|
+
* import { flushStorage } from '@systemfsoftware/xstate-store/persist';
|
|
181
|
+
*
|
|
182
|
+
* // Force write before page unload
|
|
183
|
+
* window.addEventListener('beforeunload', () => {
|
|
184
|
+
* flushStorage(store);
|
|
185
|
+
* });
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
export declare function flushStorage(store: {
|
|
189
|
+
getSnapshot: () => any;
|
|
190
|
+
}): void | Promise<void>;
|
|
191
|
+
/**
|
|
192
|
+
* Returns whether the store has been hydrated from storage.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
*
|
|
196
|
+
* ```ts
|
|
197
|
+
* import { isHydrated } from '@systemfsoftware/xstate-store/persist';
|
|
198
|
+
*
|
|
199
|
+
* if (isHydrated(store)) {
|
|
200
|
+
* // Safe to read persisted state
|
|
201
|
+
* }
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
export declare function isHydrated(store: {
|
|
205
|
+
getSnapshot: () => any;
|
|
206
|
+
}): boolean;
|
|
207
|
+
/**
|
|
208
|
+
* Reads persisted state from (potentially async) storage and rehydrates the
|
|
209
|
+
* store. Use this for async storage adapters or when `skipHydration` is
|
|
210
|
+
* `true`.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
*
|
|
214
|
+
* ```ts
|
|
215
|
+
* import { rehydrateStore } from '@systemfsoftware/xstate-store/persist';
|
|
216
|
+
*
|
|
217
|
+
* const store = createStore({ ... }).with(persist({
|
|
218
|
+
* name: 'my-store',
|
|
219
|
+
* storage: createJSONStorage(() => AsyncStorage),
|
|
220
|
+
* skipHydration: true
|
|
221
|
+
* }));
|
|
222
|
+
*
|
|
223
|
+
* await rehydrateStore(store);
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
export declare function rehydrateStore(store: {
|
|
227
|
+
getSnapshot: () => any;
|
|
228
|
+
send: (event: any) => void;
|
|
229
|
+
}): Promise<void>;
|
|
230
|
+
/** Options for `createBroadcastStorage(...)`. */
|
|
231
|
+
interface BroadcastStorageOptions {
|
|
232
|
+
/** BroadcastChannel name. Defaults to `"xstate-store"`. */
|
|
233
|
+
channel?: string;
|
|
234
|
+
}
|
|
235
|
+
/** Storage adapter returned by `createBroadcastStorage(...)`. */
|
|
236
|
+
type BroadcastStorage = StateStorage & {
|
|
237
|
+
/** Channel used to announce writes to other same-origin contexts. */
|
|
238
|
+
readonly channel: BroadcastChannel;
|
|
239
|
+
};
|
|
240
|
+
/**
|
|
241
|
+
* Wraps a storage adapter so writes are announced across same-origin tabs.
|
|
242
|
+
*
|
|
243
|
+
* Pair with `subscribeToBroadcastStorage(...)` on persisted stores that should
|
|
244
|
+
* rehydrate when another tab writes to the same storage key.
|
|
245
|
+
*/
|
|
246
|
+
export declare function createBroadcastStorage(baseStorage: StateStorage, options?: BroadcastStorageOptions): BroadcastStorage;
|
|
247
|
+
/**
|
|
248
|
+
* Subscribes a persisted store to broadcast storage updates.
|
|
249
|
+
*
|
|
250
|
+
* Returns an unsubscribe function that closes the broadcast listener.
|
|
251
|
+
*/
|
|
252
|
+
export declare function subscribeToBroadcastStorage(store: {
|
|
253
|
+
getSnapshot: () => any;
|
|
254
|
+
send: (event: any) => void;
|
|
255
|
+
}): () => void;
|
|
256
|
+
export {};
|