small-state 0.0.0-stage → 0.3.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/README.ja.md +564 -0
- package/README.md +566 -2
- package/dist/index.cjs +1754 -0
- package/dist/index.d.cts +369 -0
- package/dist/index.d.mts +369 -0
- package/dist/index.mjs +1748 -0
- package/package.json +47 -4
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
//#region src/shared/general.types.d.ts
|
|
2
|
+
type Expand<T> = { [K in keyof T]: T[K]; } & {};
|
|
3
|
+
type LowInfer<T> = T & NonNullable<unknown>;
|
|
4
|
+
type Values<T> = T[keyof T];
|
|
5
|
+
type Merge<A, B> = Omit<A, keyof B> & B;
|
|
6
|
+
type IsAny<T> = 0 extends (1 & T) ? true : false;
|
|
7
|
+
type IsUnknown<T> = IsAny<T> extends true ? false : unknown extends T ? true : false;
|
|
8
|
+
type IsEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? ((<T>() => T extends Y ? 1 : 2) extends (<T>() => T extends X ? 1 : 2) ? true : false) : false;
|
|
9
|
+
type EmptyRecord = Record<PropertyKey, never>;
|
|
10
|
+
type AnyFn<R = unknown> = (...arg: any[]) => R;
|
|
11
|
+
type OneOrMany<T> = T | T[];
|
|
12
|
+
type Placeholder = {} | null | undefined;
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/machine/m.types.d.ts
|
|
15
|
+
declare const EntrySymbol: unique symbol;
|
|
16
|
+
declare const ExitSymbol: unique symbol;
|
|
17
|
+
declare const SubStatesSymbol: unique symbol;
|
|
18
|
+
declare const ParallelSymbol: unique symbol;
|
|
19
|
+
declare const InvokeSymbol: unique symbol;
|
|
20
|
+
type ChildDoneEBody = {
|
|
21
|
+
type: ':childDone:';
|
|
22
|
+
output: unknown;
|
|
23
|
+
};
|
|
24
|
+
type BuiltinEType = ':always:' | ChildDoneEBody['type'];
|
|
25
|
+
type BuiltinSendableEvent = {
|
|
26
|
+
type: BuiltinEType;
|
|
27
|
+
[key: string]: any;
|
|
28
|
+
};
|
|
29
|
+
type DoneEBody<O> = {
|
|
30
|
+
type: ':done:';
|
|
31
|
+
output: O;
|
|
32
|
+
};
|
|
33
|
+
type ErrorEBody = {
|
|
34
|
+
type: ':error:';
|
|
35
|
+
error: unknown;
|
|
36
|
+
};
|
|
37
|
+
declare const SystemEventSymbol: unique symbol;
|
|
38
|
+
type SystemSendableEvent = {
|
|
39
|
+
type: `invoke/${string}[${number}]`;
|
|
40
|
+
[SystemEventSymbol]: true;
|
|
41
|
+
payload: {
|
|
42
|
+
type: string;
|
|
43
|
+
[key: string]: any;
|
|
44
|
+
};
|
|
45
|
+
} | {
|
|
46
|
+
type: `system/${string}`;
|
|
47
|
+
};
|
|
48
|
+
type InternalEType = BuiltinEType | SystemSendableEvent['type'];
|
|
49
|
+
type InternalEPayload = {
|
|
50
|
+
path: string;
|
|
51
|
+
} | EmptyRecord;
|
|
52
|
+
type MContext = Record<string, any>;
|
|
53
|
+
type SendableEvent = {
|
|
54
|
+
type: string;
|
|
55
|
+
[key: string]: any;
|
|
56
|
+
};
|
|
57
|
+
type EmittableEvent = {
|
|
58
|
+
type: string;
|
|
59
|
+
[key: string]: any;
|
|
60
|
+
};
|
|
61
|
+
type StateName = string;
|
|
62
|
+
type O_PureETypeString<E extends SendableEvent> = E extends unknown ? Exclude<keyof E, "type"> extends never ? E['type'] : never : any;
|
|
63
|
+
type O_InferMachineSendableEvent<M extends AnyMachine> = M extends Machine<any, infer E, any, any, any, any, any, any, any> ? E : never;
|
|
64
|
+
type SendFnEvent<M extends AnyMachine> = O_InferMachineSendableEvent<M> extends (infer E extends SendableEvent) ? E | O_PureETypeString<E> : never;
|
|
65
|
+
type FnBaseParams<C extends MContext, Ex extends SendableEvent, I, Additional = unknown, Prm = any> = [arg: Expand<{
|
|
66
|
+
c: C;
|
|
67
|
+
context: C;
|
|
68
|
+
e: Ex;
|
|
69
|
+
event: Ex;
|
|
70
|
+
input: I;
|
|
71
|
+
} & Additional>, params: Prm];
|
|
72
|
+
type ActionFnParams<C extends MContext, Ex extends SendableEvent, I, EM extends EmittableEvent, G extends Record<string, Placeholder>, A extends Record<string, Placeholder>, S extends ServiceRecord> = FnBaseParams<C, Ex, I, {
|
|
73
|
+
check: CheckFn<C, Ex, I, G>;
|
|
74
|
+
enqueue: EnqueueFn<C, Ex, I, EM, A>;
|
|
75
|
+
update: UpdateFn<C>;
|
|
76
|
+
emit: EmitFn<EM>;
|
|
77
|
+
sendTo: SendToFn<S>;
|
|
78
|
+
}>;
|
|
79
|
+
type LimitedActionFnParams<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, Prm = any> = FnBaseParams<C, E, I, {
|
|
80
|
+
update: UpdateFn<C>;
|
|
81
|
+
emit: EmitFn<EM>;
|
|
82
|
+
}, Prm>;
|
|
83
|
+
/**
|
|
84
|
+
* check(type: {[guardName: keyof G]: ParamsOfGuard}): boolean
|
|
85
|
+
* check(type: keyof G, params?: ParamsOfGuard): boolean
|
|
86
|
+
*/
|
|
87
|
+
type CheckFn<C extends MContext, Ex extends SendableEvent, I, G extends Record<string, Placeholder>> = {
|
|
88
|
+
(type: Values<{ [K in keyof G & string as unknown extends G[K] ? never : K]: { [K3 in K]: ParamsInput<G[K], C, Ex, I>; }; }>): boolean;
|
|
89
|
+
<Gk extends keyof G & string | never>(type: Gk, ...args: ParamsInputArgsOf<G, Gk, C, Ex, I>): boolean;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* enqueue(type: {[actionName: keyof A]: ParamsOfTheAction}): void
|
|
93
|
+
* enqueue(type: keyof A, params?: ParamsOfTheAction): void
|
|
94
|
+
* enqueue(fn: (arg: FnBaseParams & {update: UpdateFn, emit: EmitFn}) => void): void
|
|
95
|
+
*/
|
|
96
|
+
type EnqueueFn<C extends MContext, Ex extends SendableEvent, I, EM extends EmittableEvent, A extends Record<string, Placeholder>> = {
|
|
97
|
+
(type: Values<{ [K in keyof A & string as unknown extends A[K] ? never : K]: { [K2 in K]: ParamsInput<A[K], C, Ex, I>; }; }>): void;
|
|
98
|
+
<Ak extends keyof A & string | never>(type: Ak, ...args: ParamsInputArgsOf<A, Ak, C, Ex, I>): void;
|
|
99
|
+
(fn: (arg: LimitedActionFnParams<C, Ex, I, EM>[0]) => void): void;
|
|
100
|
+
};
|
|
101
|
+
type UpdateFnParam<C extends MContext> = { [K in keyof C]?: C[K] | (() => (C[K])); };
|
|
102
|
+
type UpdateFn<C extends MContext> = (updates: UpdateFnParam<C>) => unknown;
|
|
103
|
+
type O_InferServiceSendableEvent<S extends ServiceRecord[string]> = S extends Machine<any, infer E, any, any, any, any, any, any, any> ? E : S extends AnyFn ? (Parameters<S>[2] extends ListenFn<infer Er> ? Er : never) : never;
|
|
104
|
+
type SendToFn<S extends ServiceRecord> = {
|
|
105
|
+
<Sk extends keyof S>(serviceName: Sk, subKey: string | number, event: O_InferServiceSendableEvent<S[Sk]>): void;
|
|
106
|
+
<Sk extends keyof S>(serviceName: Sk, event: O_InferServiceSendableEvent<S[Sk]>): void;
|
|
107
|
+
};
|
|
108
|
+
type EmitFn<EM extends EmittableEvent> = (event: EM) => void;
|
|
109
|
+
type ListenFn<E extends SendableEvent> = (listener: (event: E) => void) => void;
|
|
110
|
+
type X_NormalizedGuard<C extends MContext = any, Ex extends SendableEvent = any, I = any> = ((arg: FnBaseParams<C, Ex, I>[0]) => boolean) | string | Record<string, ParamsInput<unknown, C, Ex, I>>;
|
|
111
|
+
type NormalizedAction<C extends MContext = any, Ex extends SendableEvent = any, I = any, EM extends EmittableEvent = any, G extends Record<string, Placeholder> = any, A extends Record<string, Placeholder> = any, S extends ServiceRecord = ServiceRecord> = ((arg: ActionFnParams<C, Ex, I, EM, G, A, S>[0]) => unknown) | string | Record<string, ParamsInput<unknown, C, Ex, I>>;
|
|
112
|
+
type NormalizedTransition = {
|
|
113
|
+
to?: StateName;
|
|
114
|
+
fullTo?: string;
|
|
115
|
+
guards: X_NormalizedGuard[];
|
|
116
|
+
actions: NormalizedAction[];
|
|
117
|
+
};
|
|
118
|
+
type NormalizedSrc = {
|
|
119
|
+
type: 'function';
|
|
120
|
+
value: ServiceFn;
|
|
121
|
+
} | {
|
|
122
|
+
type: 'string';
|
|
123
|
+
value: string;
|
|
124
|
+
} | {
|
|
125
|
+
type: 'machine';
|
|
126
|
+
value: AnyMachine;
|
|
127
|
+
};
|
|
128
|
+
type NormalizedInvocation = {
|
|
129
|
+
mainKey: string;
|
|
130
|
+
subKey?: string | number;
|
|
131
|
+
input?: AnyFn;
|
|
132
|
+
emittableETypesToListen: string[];
|
|
133
|
+
src: NormalizedSrc;
|
|
134
|
+
};
|
|
135
|
+
type InternalEvent = {
|
|
136
|
+
cond?: (payload: InternalEPayload) => boolean;
|
|
137
|
+
transitions: NormalizedTransition[];
|
|
138
|
+
};
|
|
139
|
+
type NStateItemDef<Et extends string> = {
|
|
140
|
+
pPath: string;
|
|
141
|
+
final: boolean | AnyFn;
|
|
142
|
+
entryHooks: NormalizedAction[] | null;
|
|
143
|
+
exitHooks: NormalizedAction[] | null;
|
|
144
|
+
invocations: Array<NormalizedInvocation>;
|
|
145
|
+
internalEvents: { [K in InternalEType]?: InternalEvent; };
|
|
146
|
+
events: { [K in Et]?: NormalizedTransition[]; };
|
|
147
|
+
};
|
|
148
|
+
type NormalizedStateItem = {
|
|
149
|
+
def: NStateItemDef<string>;
|
|
150
|
+
sub: null | NormalizedDefinition | ParallelNormalizedDefinition;
|
|
151
|
+
};
|
|
152
|
+
type ParallelNormalizedStateItem = {
|
|
153
|
+
def: NStateItemDef<string>;
|
|
154
|
+
sub: NormalizedDefinition;
|
|
155
|
+
};
|
|
156
|
+
type NormalizedStateItemBoth = NormalizedStateItem | ParallelNormalizedStateItem;
|
|
157
|
+
type NormalizedDefinition = {
|
|
158
|
+
dType?: 'normal';
|
|
159
|
+
initial: string | undefined;
|
|
160
|
+
stateOf: Record<string, NormalizedStateItem>;
|
|
161
|
+
};
|
|
162
|
+
type ParallelNormalizedDefinition = {
|
|
163
|
+
dType: 'regions';
|
|
164
|
+
stateOf: Record<string, ParallelNormalizedStateItem>;
|
|
165
|
+
};
|
|
166
|
+
type ParamsInput<Prm, C extends MContext, Ex extends SendableEvent, I> = Prm | ((arg: FnBaseParams<C, Ex, I>[0]) => Prm);
|
|
167
|
+
type ParamsInputArgsOf<IR extends Record<string, Placeholder>, IRk extends keyof IR, C extends MContext, Ex extends SendableEvent, I> = unknown extends IR[IRk] ? [] : [params: ParamsInput<IR[IRk], C, Ex, I>];
|
|
168
|
+
type ServiceFn<TInput = any> = (input: TInput, emit: EmitFn<EmittableEvent>, listen: ListenFn<any>, signal: AbortSignal) => unknown;
|
|
169
|
+
type ServiceRecord = Record<string, ServiceFn | AnyMachine>;
|
|
170
|
+
type MachineConfig<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, O, G extends Record<string, Placeholder>, A extends Record<string, Placeholder>, S extends ServiceRecord> = {
|
|
171
|
+
types?: {
|
|
172
|
+
context?: C;
|
|
173
|
+
events?: E;
|
|
174
|
+
emittable?: EM;
|
|
175
|
+
input?: I;
|
|
176
|
+
output?: O;
|
|
177
|
+
};
|
|
178
|
+
name?: string;
|
|
179
|
+
initialState?: StateName;
|
|
180
|
+
initContext?: (input: I) => LowInfer<C>;
|
|
181
|
+
output?: (arg: FnBaseParams<C, DoneEBody<unknown>, I>[0]) => O;
|
|
182
|
+
guards?: { [K in keyof G]: (arg: FnBaseParams<C, E, I>[0], params: G[K]) => boolean; };
|
|
183
|
+
actions?: { [K in keyof A & string]: (arg: LimitedActionFnParams<C, E, I, EM>[0], params: A[K]) => unknown; };
|
|
184
|
+
services?: S;
|
|
185
|
+
};
|
|
186
|
+
type TopLvStDefItem<Et extends string> = {
|
|
187
|
+
events: { [K in Et]?: NormalizedTransition[]; };
|
|
188
|
+
internalEvents: { [K in InternalEType]?: InternalEvent; };
|
|
189
|
+
};
|
|
190
|
+
type AnyMachine = Machine<any, any, any, any, any, any, any, any, any>;
|
|
191
|
+
declare const MachineSymbol: unique symbol;
|
|
192
|
+
type Machine<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, O, G extends Record<string, Placeholder>, A extends Record<string, Placeholder>, S extends ServiceRecord, ND extends NormalizedDefinition> = {
|
|
193
|
+
name: string;
|
|
194
|
+
internal: {
|
|
195
|
+
[MachineSymbol]: true;
|
|
196
|
+
initContext: undefined | ((input: I) => C);
|
|
197
|
+
output: undefined | ((arg: FnBaseParams<C, DoneEBody<unknown>, I, unknown>[0]) => O);
|
|
198
|
+
guards: { [K in keyof G]: (arg: FnBaseParams<C, E | BuiltinSendableEvent, I>[0], params: G[K]) => boolean; };
|
|
199
|
+
actions: { [K in keyof A]: (arg: LimitedActionFnParams<C, E | BuiltinSendableEvent, I, EM>[0], params: A[K]) => unknown; };
|
|
200
|
+
services: S;
|
|
201
|
+
definition: ND;
|
|
202
|
+
topLvDefItem: TopLvStDefItem<E['type']>;
|
|
203
|
+
stateCatalog: Map<string, NormalizedStateItemBoth>;
|
|
204
|
+
};
|
|
205
|
+
provide: <const nS extends ServiceRecord, const nG extends Record<string, Placeholder> = Record<string, Placeholder>, const nA extends Record<string, Placeholder> = Record<string, Placeholder>>(implementations: {
|
|
206
|
+
guards?: { [K in keyof nG]: (arg: FnBaseParams<C, E | BuiltinSendableEvent, I>[0], params: nG[K]) => boolean; };
|
|
207
|
+
actions?: { [K in keyof nA]: (arg: LimitedActionFnParams<C, E | BuiltinSendableEvent, I, EM>[0], params: nA[K]) => unknown; };
|
|
208
|
+
services?: nS;
|
|
209
|
+
}) => Readonly<Machine<C, E, I, EM, O, Merge<G, nG>, Merge<A, nA>, Merge<S, nS>, ND>>;
|
|
210
|
+
};
|
|
211
|
+
//#endregion
|
|
212
|
+
//#region src/machine/machine.d.ts
|
|
213
|
+
type X_HiddenImpl = any & {
|
|
214
|
+
__brand: 'HiddenImpl';
|
|
215
|
+
};
|
|
216
|
+
type X_DefTypeRefluxer<C extends MContext, E extends SendableEvent, I // Input
|
|
217
|
+
, EM extends EmittableEvent, G extends Record<string, Placeholder>, A extends Record<string, Placeholder>, S extends ServiceRecord = ServiceRecord, Eb extends BuiltinEType = BuiltinEType, Ret = unknown> = () => Ret;
|
|
218
|
+
type X_DefineStatesTools = {
|
|
219
|
+
on: typeof on;
|
|
220
|
+
entryHook: typeof entryHook;
|
|
221
|
+
exitHook: typeof exitHook;
|
|
222
|
+
subStates: typeof subStates;
|
|
223
|
+
parallel: typeof parallel;
|
|
224
|
+
invoke: typeof invoke;
|
|
225
|
+
};
|
|
226
|
+
type X_ExtractEType<TStateDefinition> = TStateDefinition extends Array<infer El> ? El extends readonly [infer Et, AnyFn, any] ? Exclude<Et, BuiltinEType> : never : never;
|
|
227
|
+
type X_ExtractSubStDefinition<TStateDefinition> = TStateDefinition extends Array<infer El> ? [El] extends [never] ? null : El extends readonly [AnyFn<typeof SubStatesSymbol | typeof ParallelSymbol>, infer Sub] ? [Sub] extends [never] ? null : Sub extends NormalizedDefinition | ParallelNormalizedDefinition ? Sub : null : null : null;
|
|
228
|
+
type X_ImplKeys<IpR extends Record<string, Placeholder>> = Values<{ [K in keyof IpR]: unknown extends IpR[K] ? K : never; }>;
|
|
229
|
+
type Guard<C extends MContext, Ex extends SendableEvent, I, G extends Record<string, Placeholder>> = Expand<X_ImplKeys<G>> | Values<{ [K in keyof G & string as unknown extends G[K] ? never : K]: { [K2 in K]: ParamsInput<G[K], C, Ex, I>; }; }> | ((arg: FnBaseParams<C, Ex, I>[0]) => boolean);
|
|
230
|
+
type Action<C extends MContext, Ex extends SendableEvent, I, EM extends EmittableEvent, G extends Record<string, Placeholder>, A extends Record<string, Placeholder>, S extends ServiceRecord> = Expand<X_ImplKeys<A>> | Values<{ [K in keyof A & string as unknown extends A[K] ? never : K]: { [K2 in K]: ParamsInput<A[K], C, Ex, I>; }; }> | ((arg: ActionFnParams<C, Ex, I, EM, G, A, S>[0]) => unknown);
|
|
231
|
+
type StDefMetaInfo<C extends MContext, E extends SendableEvent, I> = {
|
|
232
|
+
final?: boolean | ((args: FnBaseParams<C, E, I>[0]) => unknown);
|
|
233
|
+
};
|
|
234
|
+
type StDefCoreItem<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, G extends Record<string, Placeholder>, A extends Record<string, Placeholder>, S extends ServiceRecord> = StDefMetaInfo<C, E, I> | readonly [E['type'] | BuiltinEType, X_DefTypeRefluxer<C, E, I, EM, G, A, S>, X_HiddenImpl] | readonly [typeof EntrySymbol, X_DefTypeRefluxer<C, E, I, EM, G, A, S>, null, X_HiddenImpl] | readonly [typeof ExitSymbol, X_DefTypeRefluxer<C, E, I, EM, G, A, S>, null, X_HiddenImpl];
|
|
235
|
+
type StateDefinition<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, G extends Record<string, Placeholder>, A extends Record<string, Placeholder>, S extends ServiceRecord> = null | Array<StDefCoreItem<C, E, I, EM, G, A, S> | readonly [X_DefTypeRefluxer<C, E, I, EM, G, A, S, BuiltinEType, any>, unknown] | readonly [typeof InvokeSymbol, X_DefTypeRefluxer<C, E, I, EM, G, A, S, never>, null, unknown, any, any]>;
|
|
236
|
+
declare function entryHook<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, const G extends Record<string, Placeholder>, const A extends Record<string, Placeholder>, S extends ServiceRecord>(...actions: Array<Action<C, E, I, EM, G, A, S>>): readonly [typeof EntrySymbol, X_DefTypeRefluxer<C, E, I, EM, G, A, S, BuiltinEType, unknown>, null, Action<C, E, I, EM, G, A, S>[]];
|
|
237
|
+
declare function exitHook<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, const G extends Record<string, Placeholder>, const A extends Record<string, Placeholder>, S extends ServiceRecord>(...actions: Array<Action<C, E, I, EM, G, A, S>>): readonly [typeof ExitSymbol, X_DefTypeRefluxer<C, E, I, EM, G, A, S, BuiltinEType, unknown>, null, Action<C, E, I, EM, G, A, S>[]];
|
|
238
|
+
type O_ExtractEvent<E extends SendableEvent, Et extends E['type']> = Et extends ChildDoneEBody['type'] ? Expand<ChildDoneEBody> : Et extends BuiltinEType ? E : E extends {
|
|
239
|
+
type: Et;
|
|
240
|
+
} ? E : Et extends E['type'] ? E : never;
|
|
241
|
+
type O_Transition<C extends MContext, Ex extends SendableEvent, I, EM extends EmittableEvent, G extends Record<string, Placeholder>, A extends Record<string, Placeholder>, S extends ServiceRecord> = {
|
|
242
|
+
to?: StateName;
|
|
243
|
+
guard?: OneOrMany<Guard<C, Ex, I, G>>;
|
|
244
|
+
actions?: OneOrMany<Action<C, Ex, I, EM, G, A, S>>;
|
|
245
|
+
};
|
|
246
|
+
declare function on<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, const G extends Record<string, Placeholder>, const A extends Record<string, Placeholder>, S extends ServiceRecord, Et extends [E] extends [never] ? string : E['type'] | Eb | never, Eb extends BuiltinEType = BuiltinEType>(eventType: Et, config: OneOrMany<Expand<O_Transition<C, O_ExtractEvent<E, Et>, I, EM, G, A, S>>>): readonly [Et, X_DefTypeRefluxer<C, E, I, EM, G, A, S, Eb, unknown>, any];
|
|
247
|
+
declare function subStates<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, const G extends Record<string, Placeholder>, const A extends Record<string, Placeholder>, S extends ServiceRecord, const D extends { [K in string]: StateDefinition<C, E, I, EM, G, A, S>; }, const Initial extends keyof D & string>(statesDefinition: D, initialState?: Initial): readonly [X_DefTypeRefluxer<C, E, I, EM, G, A, S, BuiltinEType, typeof SubStatesSymbol>, {
|
|
248
|
+
initial: [Initial] extends [never] ? undefined : Initial;
|
|
249
|
+
stateOf: { [K in keyof D]: {
|
|
250
|
+
def: NStateItemDef<X_ExtractEType<D[K]>>;
|
|
251
|
+
sub: X_ExtractSubStDefinition<D[K]>;
|
|
252
|
+
}; };
|
|
253
|
+
}];
|
|
254
|
+
type O_ExtractRegionEType<De> = De extends [infer U, Record<string, StateDefinition<any, any, any, any, any, any, any>>] ? U extends Array<infer El> ? El extends readonly [infer Et, AnyFn, any] ? Exclude<Et, BuiltinEType> : never : never : never;
|
|
255
|
+
type O_InnerDict<T> = T extends [...any[], infer Last] ? Last : never;
|
|
256
|
+
declare function parallel<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, G extends Record<string, Placeholder>, A extends Record<string, Placeholder>, S extends ServiceRecord, const PD extends { [K in string]: [Array<StDefCoreItem<C, E, I, EM, G, A, S>>, { [L in string]: StateDefinition<C, E, I, EM, G, A, S>; }] | [{ [L in string]: StateDefinition<C, E, I, EM, G, A, S>; }?]; }>(parallelDefinition: PD): readonly [X_DefTypeRefluxer<C, E, I, EM, G, A, S, BuiltinEType, typeof ParallelSymbol>, {
|
|
257
|
+
dType: "regions";
|
|
258
|
+
stateOf: { [K in keyof PD]: {
|
|
259
|
+
def: NStateItemDef<O_ExtractRegionEType<PD[K]>>;
|
|
260
|
+
sub: {
|
|
261
|
+
initial: string | undefined;
|
|
262
|
+
stateOf: { [L in keyof O_InnerDict<PD[K]>]: {
|
|
263
|
+
def: NStateItemDef<X_ExtractEType<O_InnerDict<PD[K]>[L]>>;
|
|
264
|
+
sub: X_ExtractSubStDefinition<O_InnerDict<PD[K]>[L]>;
|
|
265
|
+
}; };
|
|
266
|
+
};
|
|
267
|
+
}; };
|
|
268
|
+
}];
|
|
269
|
+
type InvokeSrcExpr<TInput> = ServiceFn<TInput> | Machine<any, any, TInput, any, any, any, any, any, any>;
|
|
270
|
+
type InvokeConfig<C extends MContext, E extends SendableEvent, I, S extends ServiceRecord, Src extends keyof S | InvokeSrcExpr<[SrcInput] extends [never] ? any : SrcInput>, SrcInput> = {
|
|
271
|
+
subKey?: string | number;
|
|
272
|
+
input?: (arg: FnBaseParams<C, E, I>[0]) => Src extends keyof S ? (S[Src] extends AnyFn ? Parameters<S[Src]>[0] : SrcInput) : SrcInput;
|
|
273
|
+
};
|
|
274
|
+
type InvocationEvents<C extends MContext, I, EM extends EmittableEvent, G extends Record<string, Placeholder>, A extends Record<string, Placeholder>, S extends ServiceRecord, TSrcTypes extends {
|
|
275
|
+
Type: 'm' | 'f';
|
|
276
|
+
EM: EmittableEvent;
|
|
277
|
+
O: unknown;
|
|
278
|
+
}> = Array<readonly [string, NoInfer<X_DefTypeRefluxer<C, Expand<DoneEBody<TSrcTypes['O']>>, I, EM, G, A, S, never>>, X_HiddenImpl] | readonly [string, X_DefTypeRefluxer<C, ErrorEBody, I, EM, G, A, S, never>, X_HiddenImpl] | (TSrcTypes['Type'] extends 'm' ? IsAny<TSrcTypes['EM']> extends false ? IsEqual<TSrcTypes['EM'], EmittableEvent> extends false ? readonly [string, X_DefTypeRefluxer<C, TSrcTypes['EM'], I, EM, G, A, S, never>, X_HiddenImpl] : never : never : never) | (TSrcTypes['Type'] extends 'f' ? IsEqual<TSrcTypes['EM'], EmittableEvent> extends false ? readonly [string, X_DefTypeRefluxer<C, TSrcTypes['EM'], I, EM, G, A, S, never>, X_HiddenImpl] : never : never)>;
|
|
279
|
+
type OO_ResolveSrc<Src, S extends ServiceRecord> = Src extends keyof S ? S[Src] : Src;
|
|
280
|
+
type O_InferIvkSrcTypes<Src, S extends ServiceRecord> = OO_ResolveSrc<Src, S> extends (infer T) ? T extends Machine<any, any, any, infer EM, infer O, any, any, any, any> ? {
|
|
281
|
+
EM: EM;
|
|
282
|
+
O: O;
|
|
283
|
+
Type: 'm';
|
|
284
|
+
} : T extends ServiceFn ? Parameters<T>[1] extends EmitFn<infer EM> ? IsUnknown<ReturnType<T>> extends false ? {
|
|
285
|
+
EM: NonNullable<EM>;
|
|
286
|
+
O: Awaited<ReturnType<T>>;
|
|
287
|
+
Type: 'f';
|
|
288
|
+
} : never : {
|
|
289
|
+
EM: EmittableEvent;
|
|
290
|
+
O: Awaited<ReturnType<T>>;
|
|
291
|
+
Type: 'f';
|
|
292
|
+
} : never : never;
|
|
293
|
+
declare function invoke<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, const G extends Record<string, Placeholder>, const A extends Record<string, Placeholder>, S extends ServiceRecord, Src extends keyof S | InvokeSrcExpr<[SrcInput] extends [never] ? any : SrcInput>, SrcInput = never>(service: Src, ...events: InvocationEvents<C, I, EM, G, A, S, O_InferIvkSrcTypes<Src, S>>): readonly [typeof InvokeSymbol, X_DefTypeRefluxer<C, E, I, EM, G, A, S, BuiltinEType, unknown>, null, X_HiddenImpl, X_HiddenImpl, InvocationEvents<C, I, EM, G, A, S, O_InferIvkSrcTypes<Src, S>>];
|
|
294
|
+
declare function invoke<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, const G extends Record<string, Placeholder>, const A extends Record<string, Placeholder>, S extends ServiceRecord, Src extends keyof S | InvokeSrcExpr<[SrcInput] extends [never] ? any : SrcInput>, SrcInput = never>(service: Src, config: InvokeConfig<C, E, I, S, Src, SrcInput>, ...events: InvocationEvents<C, I, EM, G, A, S, O_InferIvkSrcTypes<Src, S>>): readonly [typeof InvokeSymbol, X_DefTypeRefluxer<C, E, I, EM, G, A, S, BuiltinEType, unknown>, null, X_HiddenImpl, X_HiddenImpl, InvocationEvents<C, I, EM, G, A, S, O_InferIvkSrcTypes<Src, S>>];
|
|
295
|
+
type X_GlobalEvents<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, G extends Record<string, Placeholder>, A extends Record<string, Placeholder>> = Array<readonly [E['type'] | BuiltinEType, X_DefTypeRefluxer<C, E, I, EM, G, A>, X_HiddenImpl]>;
|
|
296
|
+
export declare function defServiceFn<TFn extends ServiceFn>(fn: TFn): TFn;
|
|
297
|
+
export declare function isMachine(v: unknown): v is AnyMachine;
|
|
298
|
+
export declare function makeMachine<C extends MContext, const E extends SendableEvent, const I, const EM extends EmittableEvent, const O, const S extends ServiceRecord, const D extends { [K in string]: StateDefinition<C, E, I, EM, G, A, S>; }, const G extends Record<string, Placeholder> = Record<string, Placeholder>, const A extends Record<string, Placeholder> = Record<string, Placeholder>>(config: Expand<MachineConfig<C, E, I, EM, O, G, A, S>>, defineStates: (defineStatesTools: Expand<X_DefineStatesTools>) => readonly [X_GlobalEvents<C, E, I, EM, G, A>, D] | readonly [D] | D): Machine<C, E, I, EM, O, G, A, S, {
|
|
299
|
+
initial: string;
|
|
300
|
+
stateOf: { [K in keyof D]: {
|
|
301
|
+
def: NStateItemDef<X_ExtractEType<D[K]>>;
|
|
302
|
+
sub: X_ExtractSubStDefinition<D[K]>;
|
|
303
|
+
}; };
|
|
304
|
+
}>;
|
|
305
|
+
//#endregion
|
|
306
|
+
//#region src/runtime/r.types.d.ts
|
|
307
|
+
type Child<TService> = TService extends AnyMachine ? Runtime<TService> : TService extends AnyFn ? {
|
|
308
|
+
stop: () => void;
|
|
309
|
+
send: (event: SendableEvent) => void;
|
|
310
|
+
} : never;
|
|
311
|
+
type Children<S extends ServiceRecord> = { [K in keyof S & string as K | `${K}:${string | number}`]?: Child<S[K]>; } & {
|
|
312
|
+
[key: string]: Child<any>;
|
|
313
|
+
};
|
|
314
|
+
type SnapshotFrom<R extends Runtime<any>> = R extends Runtime<infer M> ? Snapshot<M> : never;
|
|
315
|
+
//#endregion
|
|
316
|
+
//#region src/runtime/Runtime.d.ts
|
|
317
|
+
type RuntimeStatus = 'notStarted' | 'active' | 'stopped' | 'done' | 'error';
|
|
318
|
+
type C<M extends AnyMachine> = M extends Machine<infer C, any, any, any, any, any, any, any, any> ? C : never;
|
|
319
|
+
type I<M extends AnyMachine> = M extends Machine<any, any, infer I, any, any, any, any, any, any> ? I : never;
|
|
320
|
+
type EM$1<M extends AnyMachine> = M extends Machine<any, any, any, infer EM, any, any, any, any, any> ? EM : never;
|
|
321
|
+
type O$1<M extends AnyMachine> = M extends Machine<any, any, any, any, infer O, any, any, any, any> ? O : never;
|
|
322
|
+
type S<M extends AnyMachine> = M extends Machine<any, any, any, any, any, any, any, infer S, any> ? S : never;
|
|
323
|
+
type X_RemovePrivateKeys<T extends Record<string, unknown>> = { [K in keyof T as K extends `_${string}` ? never : K]: T[K]; };
|
|
324
|
+
type Snapshot<M extends AnyMachine> = {
|
|
325
|
+
runtimeId: string;
|
|
326
|
+
states: string[];
|
|
327
|
+
stateValue: string | string[] | undefined;
|
|
328
|
+
context: Expand<X_RemovePrivateKeys<C<M>>>;
|
|
329
|
+
c: Expand<X_RemovePrivateKeys<C<M>>>;
|
|
330
|
+
runtimeStatus: RuntimeStatus;
|
|
331
|
+
output: O$1<M> | undefined;
|
|
332
|
+
error: unknown;
|
|
333
|
+
_children: Children<S<M>>;
|
|
334
|
+
_runtime: Runtime<M>;
|
|
335
|
+
};
|
|
336
|
+
type Subscriber<M extends AnyMachine> = {
|
|
337
|
+
onSnapshot?: (current: Expand<Snapshot<M>>, prev: Expand<Snapshot<M>>) => void;
|
|
338
|
+
onError?: (err: unknown) => void;
|
|
339
|
+
};
|
|
340
|
+
export declare function isRuntime(v: unknown): v is Runtime<AnyMachine>;
|
|
341
|
+
type RuntimeOptions<M extends AnyMachine> = {
|
|
342
|
+
id?: string;
|
|
343
|
+
input?: () => I<M>;
|
|
344
|
+
};
|
|
345
|
+
export declare function makeRuntime<M extends AnyMachine>(machine: M, options?: RuntimeOptions<M>): Runtime<M>;
|
|
346
|
+
declare class Runtime<M extends AnyMachine> {
|
|
347
|
+
#private;
|
|
348
|
+
readonly id: string;
|
|
349
|
+
readonly machine: M;
|
|
350
|
+
get states(): string[];
|
|
351
|
+
get stateValue(): string | string[] | undefined;
|
|
352
|
+
get context(): X_RemovePrivateKeys<C<M>> extends (infer T) ? { [K in keyof T]: X_RemovePrivateKeys<C<M>>[K]; } : never;
|
|
353
|
+
get runtimeStatus(): RuntimeStatus;
|
|
354
|
+
get output(): O$1<M> | undefined;
|
|
355
|
+
getSnapshot(): Snapshot<M>;
|
|
356
|
+
constructor(machine: M, { id, input }?: RuntimeOptions<I<M>>);
|
|
357
|
+
subscribe(onSnapshot: Subscriber<M>['onSnapshot'], onError?: Subscriber<M>['onError']): () => void;
|
|
358
|
+
start(): this;
|
|
359
|
+
send(event: SendFnEvent<M>): this;
|
|
360
|
+
stop(): this;
|
|
361
|
+
onEmitted(emittedEventType: EM$1<M>['type'], handler: (emittedEvent: EM$1<M>) => void): () => void;
|
|
362
|
+
}
|
|
363
|
+
//#endregion
|
|
364
|
+
//#region src/tools/waitFor.d.ts
|
|
365
|
+
export declare function waitFor<R extends Runtime<any>>(runtime: R, cond: (snapshot: SnapshotFrom<R>) => boolean, options?: {
|
|
366
|
+
timeout?: number;
|
|
367
|
+
}): Promise<SnapshotFrom<R>>;
|
|
368
|
+
//#endregion
|
|
369
|
+
export type { AnyMachine, BuiltinEType, EmitFn as Emit, EmittableEvent, ListenFn as Listen, MContext, Runtime, RuntimeOptions, RuntimeStatus, SendFnEvent, SendableEvent, ServiceFn, Snapshot };
|