@lite-fsm/core 2.0.0
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/LICENSE +21 -0
- package/dist/Machine.d.cts +33 -0
- package/dist/Machine.d.ts +33 -0
- package/dist/MachineManager.d.cts +3 -0
- package/dist/MachineManager.d.ts +3 -0
- package/dist/actor.d.cts +67 -0
- package/dist/actor.d.ts +67 -0
- package/dist/actorEffects.d.cts +36 -0
- package/dist/actorEffects.d.ts +36 -0
- package/dist/createMachine.d.cts +34 -0
- package/dist/createMachine.d.ts +34 -0
- package/dist/dispatchContext.d.cts +26 -0
- package/dist/dispatchContext.d.ts +26 -0
- package/dist/hydration.d.cts +25 -0
- package/dist/hydration.d.ts +25 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +1 -0
- package/dist/interfaces.d.cts +44 -0
- package/dist/interfaces.d.ts +44 -0
- package/dist/internal.d.cts +2 -0
- package/dist/internal.d.ts +2 -0
- package/dist/managerIndexes.d.cts +18 -0
- package/dist/managerIndexes.d.ts +18 -0
- package/dist/managerNormalize.d.cts +15 -0
- package/dist/managerNormalize.d.ts +15 -0
- package/dist/managerRouting.d.cts +18 -0
- package/dist/managerRouting.d.ts +18 -0
- package/dist/sidecar.d.cts +42 -0
- package/dist/sidecar.d.ts +42 -0
- package/dist/types.d.cts +308 -0
- package/dist/types.d.ts +308 -0
- package/dist/utils.d.cts +26 -0
- package/dist/utils.d.ts +26 -0
- package/package.json +61 -0
package/dist/types.d.cts
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
export type SType = string | number | symbol;
|
|
2
|
+
export type WILDCARD = "*";
|
|
3
|
+
export type State<S extends SType> = Exclude<S, WILDCARD | number | symbol>;
|
|
4
|
+
export type AnyRecord = Record<string, unknown>;
|
|
5
|
+
export type AnyEvent = {
|
|
6
|
+
type: string;
|
|
7
|
+
payload?: unknown;
|
|
8
|
+
};
|
|
9
|
+
export type StateName<C extends object> = State<keyof C & SType>;
|
|
10
|
+
export type FSMEventMeta = {
|
|
11
|
+
actorId?: string | string[];
|
|
12
|
+
groupId?: string | string[];
|
|
13
|
+
groupTag?: string | string[];
|
|
14
|
+
senderActorId?: string;
|
|
15
|
+
senderGroupId?: string;
|
|
16
|
+
senderGroupTag?: string;
|
|
17
|
+
};
|
|
18
|
+
export type ManagerAction<P extends AnyEvent> = P & {
|
|
19
|
+
meta?: FSMEventMeta;
|
|
20
|
+
};
|
|
21
|
+
export type ActorMeta = {
|
|
22
|
+
actorId: string;
|
|
23
|
+
groupId: string;
|
|
24
|
+
groupTag: string;
|
|
25
|
+
};
|
|
26
|
+
export type Self = ActorMeta;
|
|
27
|
+
export type SpawnIdContext<P extends AnyEvent> = {
|
|
28
|
+
templateKey: string;
|
|
29
|
+
groupTag: string;
|
|
30
|
+
counter: number;
|
|
31
|
+
originId: string | undefined;
|
|
32
|
+
action: ManagerAction<P>;
|
|
33
|
+
};
|
|
34
|
+
export type GenerateSpawnIdFn<P extends AnyEvent> = (ctx: SpawnIdContext<P>) => string;
|
|
35
|
+
export type ActorTerminalState = "__RESOLVED" | "__REJECTED" | "__CANCELLED";
|
|
36
|
+
export type ActorSystemState = "__INIT" | ActorTerminalState;
|
|
37
|
+
export type ActorPublicState<C extends object> = Exclude<StateName<C>, ActorSystemState>;
|
|
38
|
+
export type DomainTransitionTarget<States extends SType> = State<States> | null;
|
|
39
|
+
export type ActorTransitionTarget<States extends SType> = Exclude<State<States>, "__INIT"> | ActorTerminalState | null;
|
|
40
|
+
type HasLiteralInit<C extends object> = string extends keyof C ? false : "__INIT" extends keyof C ? true : false;
|
|
41
|
+
export type TransitionTargetForConfig<C extends object, States extends SType> = HasLiteralInit<C> extends true ? ActorTransitionTarget<States> : DomainTransitionTarget<States>;
|
|
42
|
+
export type TransitionNextState<C extends object> = HasLiteralInit<C> extends true ? ActorPublicState<C> | ActorTerminalState : StateName<C>;
|
|
43
|
+
type TransitionMap<C extends object, States extends SType, P extends AnyEvent> = Partial<Record<P["type"], TransitionTargetForConfig<C, States>>>;
|
|
44
|
+
type StrictTransitionMap<Root extends object, Edges, States extends SType, P extends AnyEvent> = TransitionMap<Root, States, P> & {
|
|
45
|
+
[Event in Exclude<keyof Edges, P["type"]>]: never;
|
|
46
|
+
};
|
|
47
|
+
export type CFG<R extends object, P extends AnyEvent, K extends SType = keyof R & SType, KTargets extends SType = K> = {
|
|
48
|
+
[state in K]?: state extends keyof R ? StrictTransitionMap<R, R[state], KTargets, P> : TransitionMap<R, KTargets, P>;
|
|
49
|
+
};
|
|
50
|
+
export type StateType<C extends object, T extends AnyRecord> = {
|
|
51
|
+
context: T;
|
|
52
|
+
state: StateName<C>;
|
|
53
|
+
};
|
|
54
|
+
export type MachineState<C extends object, T extends AnyRecord> = StateType<C, T>;
|
|
55
|
+
export type PublicActorSlice<C extends object, T extends AnyRecord> = {
|
|
56
|
+
state: ActorPublicState<C>;
|
|
57
|
+
context: T;
|
|
58
|
+
meta: Readonly<ActorMeta>;
|
|
59
|
+
};
|
|
60
|
+
export type ActorPersistence = "runtime" | "snapshot";
|
|
61
|
+
export type ActorDataSlice<C extends object, T extends AnyRecord> = {
|
|
62
|
+
state: ActorPublicState<C>;
|
|
63
|
+
context: T;
|
|
64
|
+
};
|
|
65
|
+
export type DefaultActorSnapshot<C extends object, T extends AnyRecord> = ActorDataSlice<C, T>;
|
|
66
|
+
export type ActorSnapshotEntry<Snapshot> = {
|
|
67
|
+
snapshot: Snapshot;
|
|
68
|
+
meta: Readonly<ActorMeta>;
|
|
69
|
+
};
|
|
70
|
+
export type ActorHydrateHook<C extends object, T extends AnyRecord, Snapshot> = (prev: ActorDataSlice<C, T> | undefined, snapshot: Snapshot, meta: HydrateMeta) => ActorDataSlice<C, T>;
|
|
71
|
+
export type ActorDehydrateHook<C extends object, T extends AnyRecord, Snapshot> = (slice: ActorDataSlice<C, T>) => Snapshot;
|
|
72
|
+
export type ActorTemplateSnapshot<C extends object, T extends AnyRecord> = Record<string, ActorSnapshotEntry<DefaultActorSnapshot<C, T>>>;
|
|
73
|
+
export type MachineReducerState<C extends object, T extends AnyRecord> = HasLiteralInit<C> extends true ? {
|
|
74
|
+
state: ActorPublicState<C> | ActorTerminalState;
|
|
75
|
+
context: T;
|
|
76
|
+
} : StateType<C, T>;
|
|
77
|
+
export type MachineReducerInputState<C extends object, T extends AnyRecord> = HasLiteralInit<C> extends true ? {
|
|
78
|
+
state: StateName<C> | ActorTerminalState;
|
|
79
|
+
context: T;
|
|
80
|
+
} : StateType<C, T>;
|
|
81
|
+
export type HydrateStrategy = "replace" | "merge";
|
|
82
|
+
export type HydrateOptions = {
|
|
83
|
+
strategy?: HydrateStrategy;
|
|
84
|
+
};
|
|
85
|
+
export type HydratePreviewOptions<S extends MachineStore> = HydrateOptions & {
|
|
86
|
+
baseState?: MachinesState<S>;
|
|
87
|
+
};
|
|
88
|
+
export type HydrateMeta = {
|
|
89
|
+
strategy: HydrateStrategy;
|
|
90
|
+
};
|
|
91
|
+
export type Subscriber<C extends object, T extends AnyRecord, P extends AnyEvent = AnyEvent> = (prevState: StateType<C, T>, currentState: StateType<C, T>, action: P) => void;
|
|
92
|
+
export type Reducer<S, P extends AnyEvent = AnyEvent> = (state: S, action: P) => S;
|
|
93
|
+
export type UnknownMachineKeyContext = "hydrate" | "opts.snapshot";
|
|
94
|
+
export type HydrateAction<S extends MachineStore> = {
|
|
95
|
+
type: "@@lite-fsm/HYDRATE";
|
|
96
|
+
payload: {
|
|
97
|
+
strategy: HydrateStrategy;
|
|
98
|
+
snapshot: MachineManagerSnapshot<S>;
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
export type ManagerCommitAction<S extends MachineStore, P extends AnyEvent = AnyEvent> = P | HydrateAction<S>;
|
|
102
|
+
export type MiddlewareApi<S, P extends AnyEvent = AnyEvent> = {
|
|
103
|
+
getState: () => S;
|
|
104
|
+
transition: (action: ManagerAction<P>) => ManagerAction<P>;
|
|
105
|
+
replaceReducer: (cb: (reducer: Reducer<S, ManagerAction<P>>) => Reducer<S, ManagerAction<P>>) => void;
|
|
106
|
+
onTransition: (cb: (prevState: S, currentState: S, action: ManagerCommitAction<MachineStore, AnyEvent>) => void) => () => void;
|
|
107
|
+
condition: (predicate: (a: ManagerAction<P>) => boolean) => Promise<boolean>;
|
|
108
|
+
};
|
|
109
|
+
export type Middleware<S = unknown, P extends AnyEvent = AnyEvent> = (api: MiddlewareApi<S, P>) => (next: (action: ManagerAction<P>) => ManagerAction<P>) => (action: ManagerAction<P>) => ManagerAction<P>;
|
|
110
|
+
export type GenericMiddleware = <S, P extends AnyEvent>(api: MiddlewareApi<S, P>) => (next: (action: P) => P) => (action: P) => P;
|
|
111
|
+
export type VoidReducerMiddleware = GenericMiddleware & {
|
|
112
|
+
__liteFsmAllowVoidReducer: true;
|
|
113
|
+
};
|
|
114
|
+
export type MachineReducer<C extends object, P extends AnyEvent, T extends AnyRecord> = (state: MachineReducerInputState<C, T>, payload: ManagerAction<P>, meta: {
|
|
115
|
+
nextState: TransitionNextState<C>;
|
|
116
|
+
config: C;
|
|
117
|
+
}) => MachineReducerState<C, T> | void;
|
|
118
|
+
type EventKeysForTarget<Edges, Target extends SType> = {
|
|
119
|
+
[Event in keyof Edges]: Edges[Event] extends Target ? Event : never;
|
|
120
|
+
}[keyof Edges];
|
|
121
|
+
export type IncomingEventTypes<C extends object, N extends SType> = {
|
|
122
|
+
[Source in keyof C]: C[Source] extends object ? EventKeysForTarget<C[Source], N> : never;
|
|
123
|
+
}[keyof C] & string;
|
|
124
|
+
export type ActionForState<C extends object, N extends SType, P extends AnyEvent> = WILDCARD extends N ? P : Extract<P, {
|
|
125
|
+
type: IncomingEventTypes<C, N>;
|
|
126
|
+
}>;
|
|
127
|
+
type PlainAction<P extends AnyEvent> = P & {
|
|
128
|
+
meta?: never;
|
|
129
|
+
};
|
|
130
|
+
export type ActorTransition<P extends AnyEvent> = {
|
|
131
|
+
(action: ManagerAction<P>): ManagerAction<P>;
|
|
132
|
+
unscoped: (action: PlainAction<P>) => ManagerAction<P>;
|
|
133
|
+
actor: (id: string | string[], action: PlainAction<P>) => ManagerAction<P>;
|
|
134
|
+
group: (id: string | string[], action: PlainAction<P>) => ManagerAction<P>;
|
|
135
|
+
tag: (id: string | string[], action: PlainAction<P>) => ManagerAction<P>;
|
|
136
|
+
};
|
|
137
|
+
export type DefaultDeps<N extends SType = WILDCARD, C extends object = Record<string, never>, P extends AnyEvent = AnyEvent> = {
|
|
138
|
+
transition: (data: ManagerAction<P>) => ManagerAction<P>;
|
|
139
|
+
action: ActionForState<C, N, P>;
|
|
140
|
+
condition: (predicate: (a: P) => boolean) => Promise<boolean>;
|
|
141
|
+
};
|
|
142
|
+
export type ActorActionForState<C extends object, N extends SType, P extends AnyEvent> = ManagerAction<ActionForState<C, N, P>>;
|
|
143
|
+
export type ActorDefaultDeps<N extends SType, C extends object, P extends AnyEvent> = Omit<DefaultDeps<N, C, P>, "action" | "transition" | "condition"> & {
|
|
144
|
+
transition: ActorTransition<P>;
|
|
145
|
+
action: ActorActionForState<C, N, P>;
|
|
146
|
+
condition: (predicate: (a: ManagerAction<P>) => boolean) => Promise<boolean>;
|
|
147
|
+
self: Self;
|
|
148
|
+
};
|
|
149
|
+
export type MachineEffect<N extends SType = WILDCARD, C extends object = Record<string, never>, P extends AnyEvent = AnyEvent, D extends AnyRecord = {}> = (deps: D & (HasLiteralInit<C> extends true ? ActorDefaultDeps<N, C, P> : DefaultDeps<N, C, P>)) => Promise<void> | void;
|
|
150
|
+
export type EffectStateName<C extends object> = HasLiteralInit<C> extends true ? ActorPublicState<C> | WILDCARD : StateName<C> | WILDCARD;
|
|
151
|
+
type BaseMachineConfig<C extends object, T extends AnyRecord, P extends AnyEvent, D extends AnyRecord> = {
|
|
152
|
+
config: C;
|
|
153
|
+
initialState: StateName<C>;
|
|
154
|
+
initialContext: T;
|
|
155
|
+
reducer?: MachineReducer<C, P, T>;
|
|
156
|
+
effects?: {
|
|
157
|
+
[key in EffectStateName<C>]?: MachineEffect<key, C, P, D>;
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
type DomainPersistenceConfig<C extends object, T extends AnyRecord, Snapshot> = {
|
|
161
|
+
hydrate?: (prev: StateType<C, T>, snapshot: Snapshot, meta: HydrateMeta) => StateType<C, T>;
|
|
162
|
+
dehydrate?: (state: StateType<C, T>) => Snapshot;
|
|
163
|
+
};
|
|
164
|
+
type ActorSnapshotHooks<C extends object, T extends AnyRecord, Snapshot> = {
|
|
165
|
+
hydrate?: ActorHydrateHook<C, T, Snapshot>;
|
|
166
|
+
dehydrate?: ActorDehydrateHook<C, T, Snapshot>;
|
|
167
|
+
};
|
|
168
|
+
type ActorPersistenceConfig<C extends object, T extends AnyRecord, Snapshot> = {
|
|
169
|
+
persistence?: ActorPersistence;
|
|
170
|
+
groupTag?: string;
|
|
171
|
+
} & ActorSnapshotHooks<C, T, Snapshot> & ({
|
|
172
|
+
persistence?: "runtime";
|
|
173
|
+
hydrate?: never;
|
|
174
|
+
dehydrate?: never;
|
|
175
|
+
} | {
|
|
176
|
+
persistence: "snapshot";
|
|
177
|
+
});
|
|
178
|
+
export type MachineConfig<C extends object, T extends AnyRecord, P extends AnyEvent, D extends AnyRecord = {}, Snapshot = HasLiteralInit<C> extends true ? DefaultActorSnapshot<C, T> : StateType<C, T>> = BaseMachineConfig<C, T, P, D> & (HasLiteralInit<C> extends true ? ActorPersistenceConfig<C, T, Snapshot> : DomainPersistenceConfig<C, T, Snapshot>);
|
|
179
|
+
type AnyMachineConfig = {
|
|
180
|
+
config: object;
|
|
181
|
+
initialState: string;
|
|
182
|
+
initialContext: AnyRecord;
|
|
183
|
+
groupTag?: string;
|
|
184
|
+
persistence?: unknown;
|
|
185
|
+
reducer?: unknown;
|
|
186
|
+
hydrate?: unknown;
|
|
187
|
+
dehydrate?: unknown;
|
|
188
|
+
effects?: unknown;
|
|
189
|
+
};
|
|
190
|
+
export type MachineStore = Record<string, AnyMachineConfig>;
|
|
191
|
+
export type IsActorTemplate<M> = M extends {
|
|
192
|
+
config: infer C extends object;
|
|
193
|
+
} ? HasLiteralInit<C> : false;
|
|
194
|
+
type ActorRuntimeRecord<C extends object, T extends AnyRecord> = Record<string, PublicActorSlice<C, T>>;
|
|
195
|
+
export type MachineSliceState<M> = M extends {
|
|
196
|
+
config: infer C extends object;
|
|
197
|
+
initialContext: infer T extends AnyRecord;
|
|
198
|
+
} ? IsActorTemplate<M> extends true ? ActorRuntimeRecord<C, T> : {
|
|
199
|
+
state: StateName<C>;
|
|
200
|
+
context: T;
|
|
201
|
+
} : never;
|
|
202
|
+
export type MachinesState<S extends MachineStore> = {
|
|
203
|
+
[key in keyof S]: MachineSliceState<S[key]>;
|
|
204
|
+
};
|
|
205
|
+
export type MachineRuntimeSnapshot<C extends object, T extends AnyRecord> = StateType<C, T>;
|
|
206
|
+
type RuntimeSnapshotForShape<M, C extends object, T extends AnyRecord> = IsActorTemplate<M> extends true ? ActorRuntimeRecord<C, T> : MachineRuntimeSnapshot<C, T>;
|
|
207
|
+
type RuntimeSnapshotForConfigMatch<M, C extends object, T extends AnyRecord, P, D, Snapshot> = [
|
|
208
|
+
P,
|
|
209
|
+
D,
|
|
210
|
+
Snapshot
|
|
211
|
+
] extends [AnyEvent, AnyRecord, unknown] ? RuntimeSnapshotForShape<M, C, T> : never;
|
|
212
|
+
type RuntimeSnapshotForPlainShape<M> = M extends {
|
|
213
|
+
config: infer C extends object;
|
|
214
|
+
initialContext: infer T extends AnyRecord;
|
|
215
|
+
} ? RuntimeSnapshotForShape<M, C, T> : never;
|
|
216
|
+
export type MachineRuntimeSnapshotForMachine<M> = M extends MachineConfig<infer C, infer T, infer P, infer D, infer Snapshot> ? RuntimeSnapshotForConfigMatch<M, C, T, P, D, Snapshot> : RuntimeSnapshotForPlainShape<M>;
|
|
217
|
+
type SnapshotFromDehydrate<M> = M extends {
|
|
218
|
+
dehydrate: (...args: any[]) => infer Snapshot;
|
|
219
|
+
} ? Snapshot : never;
|
|
220
|
+
type SnapshotFromActorHydrate<M> = M extends {
|
|
221
|
+
hydrate: (prev: any, snapshot: infer Snapshot) => unknown;
|
|
222
|
+
} ? Snapshot : DefaultActorSnapshotForMachine<M>;
|
|
223
|
+
type DefaultActorSnapshotForMachine<M> = M extends {
|
|
224
|
+
config: infer C extends object;
|
|
225
|
+
initialContext: infer T extends AnyRecord;
|
|
226
|
+
} ? DefaultActorSnapshot<C, T> : never;
|
|
227
|
+
type ActorHookSnapshotValueForMachine<M> = M extends {
|
|
228
|
+
dehydrate: (...args: any[]) => unknown;
|
|
229
|
+
} ? SnapshotFromDehydrate<M> : SnapshotFromActorHydrate<M>;
|
|
230
|
+
type ActorSnapshotValueForMachine<M> = M extends MachineConfig<infer _C, infer _T, infer _P, infer _D, infer Snapshot> ? Snapshot : ActorHookSnapshotValueForMachine<M>;
|
|
231
|
+
type ActorSnapshotForMachine<M> = M extends {
|
|
232
|
+
persistence: "snapshot";
|
|
233
|
+
} ? Record<string, ActorSnapshotEntry<ActorSnapshotValueForMachine<M>>> : never;
|
|
234
|
+
type SnapshotFromDomainHydrate<M> = M extends {
|
|
235
|
+
hydrate: (prev: any, snapshot: infer Snapshot, meta: any) => unknown;
|
|
236
|
+
} ? Snapshot : MachineRuntimeSnapshotForMachine<M>;
|
|
237
|
+
type DomainSnapshotForMachine<M> = M extends {
|
|
238
|
+
dehydrate: (...args: any[]) => unknown;
|
|
239
|
+
} ? SnapshotFromDehydrate<M> : SnapshotFromDomainHydrate<M>;
|
|
240
|
+
export type SnapshotForMachine<M> = IsActorTemplate<M> extends true ? ActorSnapshotForMachine<M> : DomainSnapshotForMachine<M>;
|
|
241
|
+
export type MachineSnapshot<M> = SnapshotForMachine<M>;
|
|
242
|
+
type ActorTemplateKey<S extends MachineStore> = {
|
|
243
|
+
[K in keyof S]: IsActorTemplate<S[K]> extends true ? K : never;
|
|
244
|
+
}[keyof S];
|
|
245
|
+
type DomainKey<S extends MachineStore> = Exclude<keyof S, ActorTemplateKey<S>>;
|
|
246
|
+
export type SnapshotActorTemplateKey<S extends MachineStore> = {
|
|
247
|
+
[K in keyof S]: IsActorTemplate<S[K]> extends true ? (S[K] extends {
|
|
248
|
+
persistence: "snapshot";
|
|
249
|
+
} ? K : never) : never;
|
|
250
|
+
}[keyof S];
|
|
251
|
+
export type SnapshotMachineKey<S extends MachineStore> = DomainKey<S> | SnapshotActorTemplateKey<S>;
|
|
252
|
+
export type MachineManagerSnapshot<S extends MachineStore> = {
|
|
253
|
+
schemaVersion?: number;
|
|
254
|
+
machines: Partial<{
|
|
255
|
+
[key in SnapshotMachineKey<S>]: SnapshotForMachine<S[key]>;
|
|
256
|
+
}>;
|
|
257
|
+
};
|
|
258
|
+
export type MachineManagerDehydratedSnapshot<S extends MachineStore, K extends SnapshotMachineKey<S> = SnapshotMachineKey<S>> = {
|
|
259
|
+
schemaVersion?: number;
|
|
260
|
+
machines: {
|
|
261
|
+
[key in K]: SnapshotForMachine<S[key]>;
|
|
262
|
+
};
|
|
263
|
+
};
|
|
264
|
+
export type MachineManagerDehydrateResult<S extends MachineStore, Keys extends ReadonlyArray<SnapshotMachineKey<S>>> = number extends Keys["length"] ? MachineManagerSnapshot<S> : MachineManagerDehydratedSnapshot<S, Keys[number]>;
|
|
265
|
+
export type MachineManagerRuntimeSnapshot<S extends MachineStore> = {
|
|
266
|
+
schemaVersion?: number;
|
|
267
|
+
machines: {
|
|
268
|
+
[key in keyof S]: MachineRuntimeSnapshotForMachine<S[key]>;
|
|
269
|
+
};
|
|
270
|
+
};
|
|
271
|
+
export type DehydrateOptions<S extends MachineStore, K extends SnapshotMachineKey<S> = SnapshotMachineKey<S>> = {
|
|
272
|
+
machines?: ReadonlyArray<K>;
|
|
273
|
+
};
|
|
274
|
+
export type MachineManagerDehydrateFn<S extends MachineStore> = {
|
|
275
|
+
(opts?: {
|
|
276
|
+
machines?: undefined;
|
|
277
|
+
}): MachineManagerDehydratedSnapshot<S>;
|
|
278
|
+
<const Keys extends ReadonlyArray<SnapshotMachineKey<S>>>(opts: {
|
|
279
|
+
machines: Keys;
|
|
280
|
+
}): MachineManagerDehydrateResult<S, Keys>;
|
|
281
|
+
(opts: DehydrateOptions<S>): MachineManagerSnapshot<S>;
|
|
282
|
+
};
|
|
283
|
+
export type TransitionSubscriber<S extends MachineStore, P extends AnyEvent = AnyEvent> = (prevState: MachinesState<S>, currentState: MachinesState<S>, action: ManagerCommitAction<S, ManagerAction<P>>) => void;
|
|
284
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
285
|
+
type NoPayload = {
|
|
286
|
+
readonly __liteFsmNoPayload: never;
|
|
287
|
+
};
|
|
288
|
+
export type FSMEvent<Name extends string, Payload = NoPayload> = Name extends string ? IsAny<Payload> extends true ? {
|
|
289
|
+
type: Name;
|
|
290
|
+
payload: Payload;
|
|
291
|
+
} : [Payload] extends [NoPayload] ? {
|
|
292
|
+
type: Name;
|
|
293
|
+
} : {
|
|
294
|
+
type: Name;
|
|
295
|
+
payload: Payload;
|
|
296
|
+
} : never;
|
|
297
|
+
export type TypedCreateReducerFn<P extends AnyEvent = AnyEvent> = <C extends object, T extends AnyRecord>(reducer: MachineReducer<C, P, T>) => MachineReducer<C, P, T>;
|
|
298
|
+
export type TypedCreateConfigFn<P extends AnyEvent = AnyEvent> = <C extends object>(cfg: C & CFG<C, P, StateName<C> | WILDCARD>) => C;
|
|
299
|
+
export type EffectType = "every" | "latest";
|
|
300
|
+
type MachineConfigShape<C extends object> = {
|
|
301
|
+
[key in keyof C]: object;
|
|
302
|
+
};
|
|
303
|
+
export type TypedCreateEffectFn<P extends AnyEvent = AnyEvent, D extends AnyRecord = {}> = <C extends MachineConfigShape<C> = Record<string, never>, N extends StateName<C> | WILDCARD = StateName<C> | WILDCARD>(opts: {
|
|
304
|
+
effect: MachineEffect<N, C, P, D>;
|
|
305
|
+
type?: EffectType;
|
|
306
|
+
cancelFn?: (deps: Parameters<MachineEffect<N, C, P, D>>[0]) => () => boolean;
|
|
307
|
+
}) => MachineEffect<N, C, P, D>;
|
|
308
|
+
export {};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
export type SType = string | number | symbol;
|
|
2
|
+
export type WILDCARD = "*";
|
|
3
|
+
export type State<S extends SType> = Exclude<S, WILDCARD | number | symbol>;
|
|
4
|
+
export type AnyRecord = Record<string, unknown>;
|
|
5
|
+
export type AnyEvent = {
|
|
6
|
+
type: string;
|
|
7
|
+
payload?: unknown;
|
|
8
|
+
};
|
|
9
|
+
export type StateName<C extends object> = State<keyof C & SType>;
|
|
10
|
+
export type FSMEventMeta = {
|
|
11
|
+
actorId?: string | string[];
|
|
12
|
+
groupId?: string | string[];
|
|
13
|
+
groupTag?: string | string[];
|
|
14
|
+
senderActorId?: string;
|
|
15
|
+
senderGroupId?: string;
|
|
16
|
+
senderGroupTag?: string;
|
|
17
|
+
};
|
|
18
|
+
export type ManagerAction<P extends AnyEvent> = P & {
|
|
19
|
+
meta?: FSMEventMeta;
|
|
20
|
+
};
|
|
21
|
+
export type ActorMeta = {
|
|
22
|
+
actorId: string;
|
|
23
|
+
groupId: string;
|
|
24
|
+
groupTag: string;
|
|
25
|
+
};
|
|
26
|
+
export type Self = ActorMeta;
|
|
27
|
+
export type SpawnIdContext<P extends AnyEvent> = {
|
|
28
|
+
templateKey: string;
|
|
29
|
+
groupTag: string;
|
|
30
|
+
counter: number;
|
|
31
|
+
originId: string | undefined;
|
|
32
|
+
action: ManagerAction<P>;
|
|
33
|
+
};
|
|
34
|
+
export type GenerateSpawnIdFn<P extends AnyEvent> = (ctx: SpawnIdContext<P>) => string;
|
|
35
|
+
export type ActorTerminalState = "__RESOLVED" | "__REJECTED" | "__CANCELLED";
|
|
36
|
+
export type ActorSystemState = "__INIT" | ActorTerminalState;
|
|
37
|
+
export type ActorPublicState<C extends object> = Exclude<StateName<C>, ActorSystemState>;
|
|
38
|
+
export type DomainTransitionTarget<States extends SType> = State<States> | null;
|
|
39
|
+
export type ActorTransitionTarget<States extends SType> = Exclude<State<States>, "__INIT"> | ActorTerminalState | null;
|
|
40
|
+
type HasLiteralInit<C extends object> = string extends keyof C ? false : "__INIT" extends keyof C ? true : false;
|
|
41
|
+
export type TransitionTargetForConfig<C extends object, States extends SType> = HasLiteralInit<C> extends true ? ActorTransitionTarget<States> : DomainTransitionTarget<States>;
|
|
42
|
+
export type TransitionNextState<C extends object> = HasLiteralInit<C> extends true ? ActorPublicState<C> | ActorTerminalState : StateName<C>;
|
|
43
|
+
type TransitionMap<C extends object, States extends SType, P extends AnyEvent> = Partial<Record<P["type"], TransitionTargetForConfig<C, States>>>;
|
|
44
|
+
type StrictTransitionMap<Root extends object, Edges, States extends SType, P extends AnyEvent> = TransitionMap<Root, States, P> & {
|
|
45
|
+
[Event in Exclude<keyof Edges, P["type"]>]: never;
|
|
46
|
+
};
|
|
47
|
+
export type CFG<R extends object, P extends AnyEvent, K extends SType = keyof R & SType, KTargets extends SType = K> = {
|
|
48
|
+
[state in K]?: state extends keyof R ? StrictTransitionMap<R, R[state], KTargets, P> : TransitionMap<R, KTargets, P>;
|
|
49
|
+
};
|
|
50
|
+
export type StateType<C extends object, T extends AnyRecord> = {
|
|
51
|
+
context: T;
|
|
52
|
+
state: StateName<C>;
|
|
53
|
+
};
|
|
54
|
+
export type MachineState<C extends object, T extends AnyRecord> = StateType<C, T>;
|
|
55
|
+
export type PublicActorSlice<C extends object, T extends AnyRecord> = {
|
|
56
|
+
state: ActorPublicState<C>;
|
|
57
|
+
context: T;
|
|
58
|
+
meta: Readonly<ActorMeta>;
|
|
59
|
+
};
|
|
60
|
+
export type ActorPersistence = "runtime" | "snapshot";
|
|
61
|
+
export type ActorDataSlice<C extends object, T extends AnyRecord> = {
|
|
62
|
+
state: ActorPublicState<C>;
|
|
63
|
+
context: T;
|
|
64
|
+
};
|
|
65
|
+
export type DefaultActorSnapshot<C extends object, T extends AnyRecord> = ActorDataSlice<C, T>;
|
|
66
|
+
export type ActorSnapshotEntry<Snapshot> = {
|
|
67
|
+
snapshot: Snapshot;
|
|
68
|
+
meta: Readonly<ActorMeta>;
|
|
69
|
+
};
|
|
70
|
+
export type ActorHydrateHook<C extends object, T extends AnyRecord, Snapshot> = (prev: ActorDataSlice<C, T> | undefined, snapshot: Snapshot, meta: HydrateMeta) => ActorDataSlice<C, T>;
|
|
71
|
+
export type ActorDehydrateHook<C extends object, T extends AnyRecord, Snapshot> = (slice: ActorDataSlice<C, T>) => Snapshot;
|
|
72
|
+
export type ActorTemplateSnapshot<C extends object, T extends AnyRecord> = Record<string, ActorSnapshotEntry<DefaultActorSnapshot<C, T>>>;
|
|
73
|
+
export type MachineReducerState<C extends object, T extends AnyRecord> = HasLiteralInit<C> extends true ? {
|
|
74
|
+
state: ActorPublicState<C> | ActorTerminalState;
|
|
75
|
+
context: T;
|
|
76
|
+
} : StateType<C, T>;
|
|
77
|
+
export type MachineReducerInputState<C extends object, T extends AnyRecord> = HasLiteralInit<C> extends true ? {
|
|
78
|
+
state: StateName<C> | ActorTerminalState;
|
|
79
|
+
context: T;
|
|
80
|
+
} : StateType<C, T>;
|
|
81
|
+
export type HydrateStrategy = "replace" | "merge";
|
|
82
|
+
export type HydrateOptions = {
|
|
83
|
+
strategy?: HydrateStrategy;
|
|
84
|
+
};
|
|
85
|
+
export type HydratePreviewOptions<S extends MachineStore> = HydrateOptions & {
|
|
86
|
+
baseState?: MachinesState<S>;
|
|
87
|
+
};
|
|
88
|
+
export type HydrateMeta = {
|
|
89
|
+
strategy: HydrateStrategy;
|
|
90
|
+
};
|
|
91
|
+
export type Subscriber<C extends object, T extends AnyRecord, P extends AnyEvent = AnyEvent> = (prevState: StateType<C, T>, currentState: StateType<C, T>, action: P) => void;
|
|
92
|
+
export type Reducer<S, P extends AnyEvent = AnyEvent> = (state: S, action: P) => S;
|
|
93
|
+
export type UnknownMachineKeyContext = "hydrate" | "opts.snapshot";
|
|
94
|
+
export type HydrateAction<S extends MachineStore> = {
|
|
95
|
+
type: "@@lite-fsm/HYDRATE";
|
|
96
|
+
payload: {
|
|
97
|
+
strategy: HydrateStrategy;
|
|
98
|
+
snapshot: MachineManagerSnapshot<S>;
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
export type ManagerCommitAction<S extends MachineStore, P extends AnyEvent = AnyEvent> = P | HydrateAction<S>;
|
|
102
|
+
export type MiddlewareApi<S, P extends AnyEvent = AnyEvent> = {
|
|
103
|
+
getState: () => S;
|
|
104
|
+
transition: (action: ManagerAction<P>) => ManagerAction<P>;
|
|
105
|
+
replaceReducer: (cb: (reducer: Reducer<S, ManagerAction<P>>) => Reducer<S, ManagerAction<P>>) => void;
|
|
106
|
+
onTransition: (cb: (prevState: S, currentState: S, action: ManagerCommitAction<MachineStore, AnyEvent>) => void) => () => void;
|
|
107
|
+
condition: (predicate: (a: ManagerAction<P>) => boolean) => Promise<boolean>;
|
|
108
|
+
};
|
|
109
|
+
export type Middleware<S = unknown, P extends AnyEvent = AnyEvent> = (api: MiddlewareApi<S, P>) => (next: (action: ManagerAction<P>) => ManagerAction<P>) => (action: ManagerAction<P>) => ManagerAction<P>;
|
|
110
|
+
export type GenericMiddleware = <S, P extends AnyEvent>(api: MiddlewareApi<S, P>) => (next: (action: P) => P) => (action: P) => P;
|
|
111
|
+
export type VoidReducerMiddleware = GenericMiddleware & {
|
|
112
|
+
__liteFsmAllowVoidReducer: true;
|
|
113
|
+
};
|
|
114
|
+
export type MachineReducer<C extends object, P extends AnyEvent, T extends AnyRecord> = (state: MachineReducerInputState<C, T>, payload: ManagerAction<P>, meta: {
|
|
115
|
+
nextState: TransitionNextState<C>;
|
|
116
|
+
config: C;
|
|
117
|
+
}) => MachineReducerState<C, T> | void;
|
|
118
|
+
type EventKeysForTarget<Edges, Target extends SType> = {
|
|
119
|
+
[Event in keyof Edges]: Edges[Event] extends Target ? Event : never;
|
|
120
|
+
}[keyof Edges];
|
|
121
|
+
export type IncomingEventTypes<C extends object, N extends SType> = {
|
|
122
|
+
[Source in keyof C]: C[Source] extends object ? EventKeysForTarget<C[Source], N> : never;
|
|
123
|
+
}[keyof C] & string;
|
|
124
|
+
export type ActionForState<C extends object, N extends SType, P extends AnyEvent> = WILDCARD extends N ? P : Extract<P, {
|
|
125
|
+
type: IncomingEventTypes<C, N>;
|
|
126
|
+
}>;
|
|
127
|
+
type PlainAction<P extends AnyEvent> = P & {
|
|
128
|
+
meta?: never;
|
|
129
|
+
};
|
|
130
|
+
export type ActorTransition<P extends AnyEvent> = {
|
|
131
|
+
(action: ManagerAction<P>): ManagerAction<P>;
|
|
132
|
+
unscoped: (action: PlainAction<P>) => ManagerAction<P>;
|
|
133
|
+
actor: (id: string | string[], action: PlainAction<P>) => ManagerAction<P>;
|
|
134
|
+
group: (id: string | string[], action: PlainAction<P>) => ManagerAction<P>;
|
|
135
|
+
tag: (id: string | string[], action: PlainAction<P>) => ManagerAction<P>;
|
|
136
|
+
};
|
|
137
|
+
export type DefaultDeps<N extends SType = WILDCARD, C extends object = Record<string, never>, P extends AnyEvent = AnyEvent> = {
|
|
138
|
+
transition: (data: ManagerAction<P>) => ManagerAction<P>;
|
|
139
|
+
action: ActionForState<C, N, P>;
|
|
140
|
+
condition: (predicate: (a: P) => boolean) => Promise<boolean>;
|
|
141
|
+
};
|
|
142
|
+
export type ActorActionForState<C extends object, N extends SType, P extends AnyEvent> = ManagerAction<ActionForState<C, N, P>>;
|
|
143
|
+
export type ActorDefaultDeps<N extends SType, C extends object, P extends AnyEvent> = Omit<DefaultDeps<N, C, P>, "action" | "transition" | "condition"> & {
|
|
144
|
+
transition: ActorTransition<P>;
|
|
145
|
+
action: ActorActionForState<C, N, P>;
|
|
146
|
+
condition: (predicate: (a: ManagerAction<P>) => boolean) => Promise<boolean>;
|
|
147
|
+
self: Self;
|
|
148
|
+
};
|
|
149
|
+
export type MachineEffect<N extends SType = WILDCARD, C extends object = Record<string, never>, P extends AnyEvent = AnyEvent, D extends AnyRecord = {}> = (deps: D & (HasLiteralInit<C> extends true ? ActorDefaultDeps<N, C, P> : DefaultDeps<N, C, P>)) => Promise<void> | void;
|
|
150
|
+
export type EffectStateName<C extends object> = HasLiteralInit<C> extends true ? ActorPublicState<C> | WILDCARD : StateName<C> | WILDCARD;
|
|
151
|
+
type BaseMachineConfig<C extends object, T extends AnyRecord, P extends AnyEvent, D extends AnyRecord> = {
|
|
152
|
+
config: C;
|
|
153
|
+
initialState: StateName<C>;
|
|
154
|
+
initialContext: T;
|
|
155
|
+
reducer?: MachineReducer<C, P, T>;
|
|
156
|
+
effects?: {
|
|
157
|
+
[key in EffectStateName<C>]?: MachineEffect<key, C, P, D>;
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
type DomainPersistenceConfig<C extends object, T extends AnyRecord, Snapshot> = {
|
|
161
|
+
hydrate?: (prev: StateType<C, T>, snapshot: Snapshot, meta: HydrateMeta) => StateType<C, T>;
|
|
162
|
+
dehydrate?: (state: StateType<C, T>) => Snapshot;
|
|
163
|
+
};
|
|
164
|
+
type ActorSnapshotHooks<C extends object, T extends AnyRecord, Snapshot> = {
|
|
165
|
+
hydrate?: ActorHydrateHook<C, T, Snapshot>;
|
|
166
|
+
dehydrate?: ActorDehydrateHook<C, T, Snapshot>;
|
|
167
|
+
};
|
|
168
|
+
type ActorPersistenceConfig<C extends object, T extends AnyRecord, Snapshot> = {
|
|
169
|
+
persistence?: ActorPersistence;
|
|
170
|
+
groupTag?: string;
|
|
171
|
+
} & ActorSnapshotHooks<C, T, Snapshot> & ({
|
|
172
|
+
persistence?: "runtime";
|
|
173
|
+
hydrate?: never;
|
|
174
|
+
dehydrate?: never;
|
|
175
|
+
} | {
|
|
176
|
+
persistence: "snapshot";
|
|
177
|
+
});
|
|
178
|
+
export type MachineConfig<C extends object, T extends AnyRecord, P extends AnyEvent, D extends AnyRecord = {}, Snapshot = HasLiteralInit<C> extends true ? DefaultActorSnapshot<C, T> : StateType<C, T>> = BaseMachineConfig<C, T, P, D> & (HasLiteralInit<C> extends true ? ActorPersistenceConfig<C, T, Snapshot> : DomainPersistenceConfig<C, T, Snapshot>);
|
|
179
|
+
type AnyMachineConfig = {
|
|
180
|
+
config: object;
|
|
181
|
+
initialState: string;
|
|
182
|
+
initialContext: AnyRecord;
|
|
183
|
+
groupTag?: string;
|
|
184
|
+
persistence?: unknown;
|
|
185
|
+
reducer?: unknown;
|
|
186
|
+
hydrate?: unknown;
|
|
187
|
+
dehydrate?: unknown;
|
|
188
|
+
effects?: unknown;
|
|
189
|
+
};
|
|
190
|
+
export type MachineStore = Record<string, AnyMachineConfig>;
|
|
191
|
+
export type IsActorTemplate<M> = M extends {
|
|
192
|
+
config: infer C extends object;
|
|
193
|
+
} ? HasLiteralInit<C> : false;
|
|
194
|
+
type ActorRuntimeRecord<C extends object, T extends AnyRecord> = Record<string, PublicActorSlice<C, T>>;
|
|
195
|
+
export type MachineSliceState<M> = M extends {
|
|
196
|
+
config: infer C extends object;
|
|
197
|
+
initialContext: infer T extends AnyRecord;
|
|
198
|
+
} ? IsActorTemplate<M> extends true ? ActorRuntimeRecord<C, T> : {
|
|
199
|
+
state: StateName<C>;
|
|
200
|
+
context: T;
|
|
201
|
+
} : never;
|
|
202
|
+
export type MachinesState<S extends MachineStore> = {
|
|
203
|
+
[key in keyof S]: MachineSliceState<S[key]>;
|
|
204
|
+
};
|
|
205
|
+
export type MachineRuntimeSnapshot<C extends object, T extends AnyRecord> = StateType<C, T>;
|
|
206
|
+
type RuntimeSnapshotForShape<M, C extends object, T extends AnyRecord> = IsActorTemplate<M> extends true ? ActorRuntimeRecord<C, T> : MachineRuntimeSnapshot<C, T>;
|
|
207
|
+
type RuntimeSnapshotForConfigMatch<M, C extends object, T extends AnyRecord, P, D, Snapshot> = [
|
|
208
|
+
P,
|
|
209
|
+
D,
|
|
210
|
+
Snapshot
|
|
211
|
+
] extends [AnyEvent, AnyRecord, unknown] ? RuntimeSnapshotForShape<M, C, T> : never;
|
|
212
|
+
type RuntimeSnapshotForPlainShape<M> = M extends {
|
|
213
|
+
config: infer C extends object;
|
|
214
|
+
initialContext: infer T extends AnyRecord;
|
|
215
|
+
} ? RuntimeSnapshotForShape<M, C, T> : never;
|
|
216
|
+
export type MachineRuntimeSnapshotForMachine<M> = M extends MachineConfig<infer C, infer T, infer P, infer D, infer Snapshot> ? RuntimeSnapshotForConfigMatch<M, C, T, P, D, Snapshot> : RuntimeSnapshotForPlainShape<M>;
|
|
217
|
+
type SnapshotFromDehydrate<M> = M extends {
|
|
218
|
+
dehydrate: (...args: any[]) => infer Snapshot;
|
|
219
|
+
} ? Snapshot : never;
|
|
220
|
+
type SnapshotFromActorHydrate<M> = M extends {
|
|
221
|
+
hydrate: (prev: any, snapshot: infer Snapshot) => unknown;
|
|
222
|
+
} ? Snapshot : DefaultActorSnapshotForMachine<M>;
|
|
223
|
+
type DefaultActorSnapshotForMachine<M> = M extends {
|
|
224
|
+
config: infer C extends object;
|
|
225
|
+
initialContext: infer T extends AnyRecord;
|
|
226
|
+
} ? DefaultActorSnapshot<C, T> : never;
|
|
227
|
+
type ActorHookSnapshotValueForMachine<M> = M extends {
|
|
228
|
+
dehydrate: (...args: any[]) => unknown;
|
|
229
|
+
} ? SnapshotFromDehydrate<M> : SnapshotFromActorHydrate<M>;
|
|
230
|
+
type ActorSnapshotValueForMachine<M> = M extends MachineConfig<infer _C, infer _T, infer _P, infer _D, infer Snapshot> ? Snapshot : ActorHookSnapshotValueForMachine<M>;
|
|
231
|
+
type ActorSnapshotForMachine<M> = M extends {
|
|
232
|
+
persistence: "snapshot";
|
|
233
|
+
} ? Record<string, ActorSnapshotEntry<ActorSnapshotValueForMachine<M>>> : never;
|
|
234
|
+
type SnapshotFromDomainHydrate<M> = M extends {
|
|
235
|
+
hydrate: (prev: any, snapshot: infer Snapshot, meta: any) => unknown;
|
|
236
|
+
} ? Snapshot : MachineRuntimeSnapshotForMachine<M>;
|
|
237
|
+
type DomainSnapshotForMachine<M> = M extends {
|
|
238
|
+
dehydrate: (...args: any[]) => unknown;
|
|
239
|
+
} ? SnapshotFromDehydrate<M> : SnapshotFromDomainHydrate<M>;
|
|
240
|
+
export type SnapshotForMachine<M> = IsActorTemplate<M> extends true ? ActorSnapshotForMachine<M> : DomainSnapshotForMachine<M>;
|
|
241
|
+
export type MachineSnapshot<M> = SnapshotForMachine<M>;
|
|
242
|
+
type ActorTemplateKey<S extends MachineStore> = {
|
|
243
|
+
[K in keyof S]: IsActorTemplate<S[K]> extends true ? K : never;
|
|
244
|
+
}[keyof S];
|
|
245
|
+
type DomainKey<S extends MachineStore> = Exclude<keyof S, ActorTemplateKey<S>>;
|
|
246
|
+
export type SnapshotActorTemplateKey<S extends MachineStore> = {
|
|
247
|
+
[K in keyof S]: IsActorTemplate<S[K]> extends true ? (S[K] extends {
|
|
248
|
+
persistence: "snapshot";
|
|
249
|
+
} ? K : never) : never;
|
|
250
|
+
}[keyof S];
|
|
251
|
+
export type SnapshotMachineKey<S extends MachineStore> = DomainKey<S> | SnapshotActorTemplateKey<S>;
|
|
252
|
+
export type MachineManagerSnapshot<S extends MachineStore> = {
|
|
253
|
+
schemaVersion?: number;
|
|
254
|
+
machines: Partial<{
|
|
255
|
+
[key in SnapshotMachineKey<S>]: SnapshotForMachine<S[key]>;
|
|
256
|
+
}>;
|
|
257
|
+
};
|
|
258
|
+
export type MachineManagerDehydratedSnapshot<S extends MachineStore, K extends SnapshotMachineKey<S> = SnapshotMachineKey<S>> = {
|
|
259
|
+
schemaVersion?: number;
|
|
260
|
+
machines: {
|
|
261
|
+
[key in K]: SnapshotForMachine<S[key]>;
|
|
262
|
+
};
|
|
263
|
+
};
|
|
264
|
+
export type MachineManagerDehydrateResult<S extends MachineStore, Keys extends ReadonlyArray<SnapshotMachineKey<S>>> = number extends Keys["length"] ? MachineManagerSnapshot<S> : MachineManagerDehydratedSnapshot<S, Keys[number]>;
|
|
265
|
+
export type MachineManagerRuntimeSnapshot<S extends MachineStore> = {
|
|
266
|
+
schemaVersion?: number;
|
|
267
|
+
machines: {
|
|
268
|
+
[key in keyof S]: MachineRuntimeSnapshotForMachine<S[key]>;
|
|
269
|
+
};
|
|
270
|
+
};
|
|
271
|
+
export type DehydrateOptions<S extends MachineStore, K extends SnapshotMachineKey<S> = SnapshotMachineKey<S>> = {
|
|
272
|
+
machines?: ReadonlyArray<K>;
|
|
273
|
+
};
|
|
274
|
+
export type MachineManagerDehydrateFn<S extends MachineStore> = {
|
|
275
|
+
(opts?: {
|
|
276
|
+
machines?: undefined;
|
|
277
|
+
}): MachineManagerDehydratedSnapshot<S>;
|
|
278
|
+
<const Keys extends ReadonlyArray<SnapshotMachineKey<S>>>(opts: {
|
|
279
|
+
machines: Keys;
|
|
280
|
+
}): MachineManagerDehydrateResult<S, Keys>;
|
|
281
|
+
(opts: DehydrateOptions<S>): MachineManagerSnapshot<S>;
|
|
282
|
+
};
|
|
283
|
+
export type TransitionSubscriber<S extends MachineStore, P extends AnyEvent = AnyEvent> = (prevState: MachinesState<S>, currentState: MachinesState<S>, action: ManagerCommitAction<S, ManagerAction<P>>) => void;
|
|
284
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
285
|
+
type NoPayload = {
|
|
286
|
+
readonly __liteFsmNoPayload: never;
|
|
287
|
+
};
|
|
288
|
+
export type FSMEvent<Name extends string, Payload = NoPayload> = Name extends string ? IsAny<Payload> extends true ? {
|
|
289
|
+
type: Name;
|
|
290
|
+
payload: Payload;
|
|
291
|
+
} : [Payload] extends [NoPayload] ? {
|
|
292
|
+
type: Name;
|
|
293
|
+
} : {
|
|
294
|
+
type: Name;
|
|
295
|
+
payload: Payload;
|
|
296
|
+
} : never;
|
|
297
|
+
export type TypedCreateReducerFn<P extends AnyEvent = AnyEvent> = <C extends object, T extends AnyRecord>(reducer: MachineReducer<C, P, T>) => MachineReducer<C, P, T>;
|
|
298
|
+
export type TypedCreateConfigFn<P extends AnyEvent = AnyEvent> = <C extends object>(cfg: C & CFG<C, P, StateName<C> | WILDCARD>) => C;
|
|
299
|
+
export type EffectType = "every" | "latest";
|
|
300
|
+
type MachineConfigShape<C extends object> = {
|
|
301
|
+
[key in keyof C]: object;
|
|
302
|
+
};
|
|
303
|
+
export type TypedCreateEffectFn<P extends AnyEvent = AnyEvent, D extends AnyRecord = {}> = <C extends MachineConfigShape<C> = Record<string, never>, N extends StateName<C> | WILDCARD = StateName<C> | WILDCARD>(opts: {
|
|
304
|
+
effect: MachineEffect<N, C, P, D>;
|
|
305
|
+
type?: EffectType;
|
|
306
|
+
cancelFn?: (deps: Parameters<MachineEffect<N, C, P, D>>[0]) => () => boolean;
|
|
307
|
+
}) => MachineEffect<N, C, P, D>;
|
|
308
|
+
export {};
|
package/dist/utils.d.cts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare function compose(): <T>(x: T) => T;
|
|
2
|
+
export declare function compose<A, R>(f1: (a: A) => R): (a: A) => R;
|
|
3
|
+
export declare function compose<A, B, R>(f1: (b: B) => R, f2: (a: A) => B): (a: A) => R;
|
|
4
|
+
export declare function compose<A, B, C, R>(f1: (c: C) => R, f2: (b: B) => C, f3: (a: A) => B): (a: A) => R;
|
|
5
|
+
export declare function compose<A, B, C, D, R>(f1: (d: D) => R, f2: (c: C) => D, f3: (b: B) => C, f4: (a: A) => B): (a: A) => R;
|
|
6
|
+
export declare function compose<A, B, C, D, E, R>(f1: (e: E) => R, f2: (d: D) => E, f3: (c: C) => D, f4: (b: B) => C, f5: (a: A) => B): (a: A) => R;
|
|
7
|
+
export declare function compose<T, R = T>(...fns: Array<(next: (action: T) => R) => (action: T) => R>): (next: (action: T) => R) => (action: T) => R;
|
|
8
|
+
export declare const WILDCARD = "*";
|
|
9
|
+
export declare const LITE_FSM_SYSTEM_ACTION_PREFIX = "@@lite-fsm/";
|
|
10
|
+
export declare const HYDRATE_ACTION_TYPE = "@@lite-fsm/HYDRATE";
|
|
11
|
+
export declare const VOID_REDUCER_MIDDLEWARE_MARKER = "__liteFsmAllowVoidReducer";
|
|
12
|
+
export declare const VOID_REDUCER_ERROR = "Reducer returned undefined. Return the next state, or use immerMiddleware to mutate draft state without return.";
|
|
13
|
+
export declare const supportsVoidReducer: (middleware: unknown) => boolean;
|
|
14
|
+
export type LiteFsmErrorCode = "LITE_FSM_ACTOR_DISPOSED" | "LITE_FSM_INVALID_ACTOR_CONFIG" | "LITE_FSM_INVALID_ACTOR_SLICE" | "LITE_FSM_INVALID_GENERATED_ID" | "LITE_FSM_INVALID_HYDRATION_ENVELOPE" | "LITE_FSM_INVALID_OPTIONS" | "LITE_FSM_STANDALONE_ACTOR_TEMPLATE";
|
|
15
|
+
export declare class LiteFsmError extends Error {
|
|
16
|
+
readonly code: LiteFsmErrorCode;
|
|
17
|
+
constructor(code: LiteFsmErrorCode, message: string);
|
|
18
|
+
}
|
|
19
|
+
export declare const validateGeneratedId: (id: unknown, kind: "actor" | "group") => string;
|
|
20
|
+
export declare const isSystemAction: (action: {
|
|
21
|
+
type?: unknown;
|
|
22
|
+
}) => action is {
|
|
23
|
+
type: `${typeof LITE_FSM_SYSTEM_ACTION_PREFIX}${string}`;
|
|
24
|
+
};
|
|
25
|
+
export declare const IS_DEV: boolean;
|
|
26
|
+
export declare const deepFreeze: <T>(obj: T) => T;
|