small-state 0.0.0-stage → 0.2.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.
@@ -0,0 +1,389 @@
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 PureETypeString<E extends SendableEvent> = E extends unknown ? Exclude<keyof E, "type"> extends never ? E['type'] : never : any;
63
+ type FnBaseParams<C extends MContext, Ex extends SendableEvent, I, Additional = unknown, Prm = any> = [arg: Expand<{
64
+ c: C;
65
+ context: C;
66
+ e: Ex;
67
+ event: Ex;
68
+ input: I;
69
+ } & Additional>, params: Prm];
70
+ 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, {
71
+ check: CheckFn<C, Ex, I, G>;
72
+ enqueue: EnqueueFn<C, Ex, I, EM, A>;
73
+ update: UpdateFn<C>;
74
+ emit: EmitFn<EM>;
75
+ sendTo: SendToFn<S>;
76
+ }>;
77
+ type LimitedActionFnParams<C extends MContext, E extends SendableEvent, I, EM extends EmittableEvent, Prm = any> = FnBaseParams<C, E, I, {
78
+ update: UpdateFn<C>;
79
+ emit: EmitFn<EM>;
80
+ }, Prm>;
81
+ /**
82
+ * check(type: {[guardName: keyof G]: ParamsOfGuard}): boolean
83
+ * check(type: keyof G, params?: ParamsOfGuard): boolean
84
+ */
85
+ type CheckFn<C extends MContext, Ex extends SendableEvent, I, G extends Record<string, Placeholder>> = {
86
+ (type: Values<{ [K in keyof G & string as unknown extends G[K] ? never : K]: { [K3 in K]: ParamsInput<G[K], C, Ex, I>; }; }>): boolean;
87
+ <Gk extends keyof G & string | never>(type: Gk, ...args: ParamsInputArgsOf<G, Gk, C, Ex, I>): boolean;
88
+ };
89
+ /**
90
+ * enqueue(type: {[actionName: keyof A]: ParamsOfTheAction}): void
91
+ * enqueue(type: keyof A, params?: ParamsOfTheAction): void
92
+ * enqueue(fn: (arg: FnBaseParams & {update: UpdateFn, emit: EmitFn}) => void): void
93
+ */
94
+ type EnqueueFn<C extends MContext, Ex extends SendableEvent, I, EM extends EmittableEvent, A extends Record<string, Placeholder>> = {
95
+ (type: Values<{ [K in keyof A & string as unknown extends A[K] ? never : K]: { [K2 in K]: ParamsInput<A[K], C, Ex, I>; }; }>): void;
96
+ <Ak extends keyof A & string | never>(type: Ak, ...args: ParamsInputArgsOf<A, Ak, C, Ex, I>): void;
97
+ (fn: (arg: LimitedActionFnParams<C, Ex, I, EM>[0]) => void): void;
98
+ };
99
+ type UpdateFnParam<C extends MContext> = { [K in keyof C]?: C[K] | (() => (C[K])); };
100
+ type UpdateFn<C extends MContext> = (updates: UpdateFnParam<C>) => unknown;
101
+ type O_InferSendableEvent<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;
102
+ type SendToFn<S extends ServiceRecord> = {
103
+ <Sk extends keyof S>(serviceName: Sk, subKey: string | number, event: O_InferSendableEvent<S[Sk]>): void;
104
+ <Sk extends keyof S>(serviceName: Sk, event: O_InferSendableEvent<S[Sk]>): void;
105
+ };
106
+ type EmitFn<EM extends EmittableEvent> = (event: EM) => void;
107
+ type ListenFn<E extends SendableEvent> = (listener: (event: E) => void) => void;
108
+ 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>>;
109
+ 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>>;
110
+ type NormalizedTransition = {
111
+ to?: StateName;
112
+ fullTo?: string;
113
+ guards: X_NormalizedGuard[];
114
+ actions: NormalizedAction[];
115
+ };
116
+ type NormalizedSrc = {
117
+ type: 'function';
118
+ value: ServiceFn;
119
+ } | {
120
+ type: 'string';
121
+ value: string;
122
+ } | {
123
+ type: 'machine';
124
+ value: AnyMachine;
125
+ };
126
+ type NormalizedInvocation = {
127
+ mainKey: string;
128
+ subKey?: string | number;
129
+ input?: AnyFn;
130
+ emittableETypesToListen: string[];
131
+ src: NormalizedSrc;
132
+ };
133
+ type InternalEvent = {
134
+ cond?: (payload: InternalEPayload) => boolean;
135
+ transitions: NormalizedTransition[];
136
+ };
137
+ type NStateItemDef<Et extends string> = {
138
+ pPath: string;
139
+ final: boolean | AnyFn;
140
+ entryHooks: NormalizedAction[] | null;
141
+ exitHooks: NormalizedAction[] | null;
142
+ invocations: Array<NormalizedInvocation>;
143
+ internalEvents: { [K in InternalEType]?: InternalEvent; };
144
+ events: { [K in Et]?: NormalizedTransition[]; };
145
+ };
146
+ type NormalizedStateItem = {
147
+ def: NStateItemDef<string>;
148
+ sub: null | NormalizedDefinition | ParallelNormalizedDefinition;
149
+ };
150
+ type ParallelNormalizedStateItem = {
151
+ def: NStateItemDef<string>;
152
+ sub: NormalizedDefinition;
153
+ };
154
+ type NormalizedStateItemBoth = NormalizedStateItem | ParallelNormalizedStateItem;
155
+ type NormalizedDefinition = {
156
+ dType?: 'normal';
157
+ initial: string | undefined;
158
+ stateOf: Record<string, NormalizedStateItem>;
159
+ };
160
+ type ParallelNormalizedDefinition = {
161
+ dType: 'regions';
162
+ stateOf: Record<string, ParallelNormalizedStateItem>;
163
+ };
164
+ type ParamsInput<Prm, C extends MContext, Ex extends SendableEvent, I> = Prm | ((arg: FnBaseParams<C, Ex, I>[0]) => Prm);
165
+ 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>];
166
+ type ServiceFn<TInput = any> = (input: TInput, emit: EmitFn<EmittableEvent>, listen: ListenFn<any>, signal: AbortSignal) => unknown;
167
+ type ServiceRecord = Record<string, ServiceFn | AnyMachine>;
168
+ 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> = {
169
+ types?: {
170
+ context?: C;
171
+ events?: E;
172
+ emittable?: EM;
173
+ input?: I;
174
+ output?: O;
175
+ };
176
+ name?: string;
177
+ initialState?: StateName;
178
+ initContext?: (input: I) => LowInfer<C>;
179
+ output?: (arg: FnBaseParams<C, DoneEBody<unknown>, I>[0]) => O;
180
+ guards?: { [K in keyof G]: (arg: FnBaseParams<C, E, I>[0], params: G[K]) => boolean; };
181
+ actions?: { [K in keyof A & string]: (arg: LimitedActionFnParams<C, E, I, EM>[0], params: A[K]) => unknown; };
182
+ services?: S;
183
+ };
184
+ type TopLvStDefItem<Et extends string> = {
185
+ events: { [K in Et]?: NormalizedTransition[]; };
186
+ internalEvents: { [K in InternalEType]?: InternalEvent; };
187
+ };
188
+ type AnyMachine = Machine<any, any, any, any, any, any, any, any, any>;
189
+ declare const MachineSymbol: unique symbol;
190
+ 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> = {
191
+ name: string;
192
+ internal: {
193
+ [MachineSymbol]: true;
194
+ initContext: undefined | ((input: I) => C);
195
+ output: undefined | ((arg: FnBaseParams<C, DoneEBody<unknown>, I, unknown>[0]) => O);
196
+ guards: { [K in keyof G]: (arg: FnBaseParams<C, E | BuiltinSendableEvent, I>[0], params: G[K]) => boolean; };
197
+ actions: { [K in keyof A]: (arg: LimitedActionFnParams<C, E | BuiltinSendableEvent, I, EM>[0], params: A[K]) => unknown; };
198
+ services: S;
199
+ definition: ND;
200
+ topLvDefItem: TopLvStDefItem<E['type']>;
201
+ stateCatalog: Map<string, NormalizedStateItemBoth>;
202
+ };
203
+ provide: <const nS extends ServiceRecord, const nG extends Record<string, Placeholder> = Record<string, Placeholder>, const nA extends Record<string, Placeholder> = Record<string, Placeholder>>(implementations: {
204
+ guards?: { [K in keyof nG]: (arg: FnBaseParams<C, E | BuiltinSendableEvent, I>[0], params: nG[K]) => boolean; };
205
+ actions?: { [K in keyof nA]: (arg: LimitedActionFnParams<C, E | BuiltinSendableEvent, I, EM>[0], params: nA[K]) => unknown; };
206
+ services?: nS;
207
+ }) => Readonly<Machine<C, E, I, EM, O, Merge<G, nG>, Merge<A, nA>, Merge<S, nS>, ND>>;
208
+ };
209
+ //#endregion
210
+ //#region src/machine/machine.d.ts
211
+ type X_HiddenImpl = any & {
212
+ __brand: 'HiddenImpl';
213
+ };
214
+ type X_DefTypeRefluxer<C extends MContext, E extends SendableEvent, I // Input
215
+ , EM extends EmittableEvent, G extends Record<string, Placeholder>, A extends Record<string, Placeholder>, S extends ServiceRecord = ServiceRecord, Eb extends BuiltinEType = BuiltinEType, R = unknown> = () => R;
216
+ type X_DefineStatesTools = {
217
+ on: typeof on;
218
+ entryHook: typeof entryHook;
219
+ exitHook: typeof exitHook;
220
+ subStates: typeof subStates;
221
+ parallel: typeof parallel;
222
+ invoke: typeof invoke;
223
+ };
224
+ type X_ExtractEType<TStateDefinition> = TStateDefinition extends Array<infer El> ? El extends readonly [infer Et, AnyFn, any] ? Exclude<Et, BuiltinEType> : never : never;
225
+ 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;
226
+ type X_ImplKeys<IpR extends Record<string, Placeholder>> = Values<{ [K in keyof IpR]: unknown extends IpR[K] ? K : never; }>;
227
+ 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);
228
+ 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);
229
+ type StDefMetaInfo<C extends MContext, E extends SendableEvent, I> = {
230
+ final?: boolean | ((args: FnBaseParams<C, E, I>[0]) => unknown);
231
+ };
232
+ 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];
233
+ 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]>;
234
+ 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>[]];
235
+ 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>[]];
236
+ type O_ExtractEvent<E extends SendableEvent, Et extends E['type']> = Et extends ChildDoneEBody['type'] ? Expand<ChildDoneEBody> : Et extends BuiltinEType ? E : E extends {
237
+ type: Et;
238
+ } ? E : Et extends E['type'] ? E : never;
239
+ 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> = {
240
+ to?: StateName;
241
+ guard?: OneOrMany<Guard<C, Ex, I, G>>;
242
+ actions?: OneOrMany<Action<C, Ex, I, EM, G, A, S>>;
243
+ };
244
+ 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];
245
+ 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>, {
246
+ initial: [Initial] extends [never] ? undefined : Initial;
247
+ stateOf: { [K in keyof D]: {
248
+ def: NStateItemDef<X_ExtractEType<D[K]>>;
249
+ sub: X_ExtractSubStDefinition<D[K]>;
250
+ }; };
251
+ }];
252
+ 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;
253
+ type O_InnerDict<T> = T extends [...any[], infer Last] ? Last : never;
254
+ 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>, {
255
+ dType: "regions";
256
+ stateOf: { [K in keyof PD]: {
257
+ def: NStateItemDef<O_ExtractRegionEType<PD[K]>>;
258
+ sub: {
259
+ initial: string | undefined;
260
+ stateOf: { [L in keyof O_InnerDict<PD[K]>]: {
261
+ def: NStateItemDef<X_ExtractEType<O_InnerDict<PD[K]>[L]>>;
262
+ sub: X_ExtractSubStDefinition<O_InnerDict<PD[K]>[L]>;
263
+ }; };
264
+ };
265
+ }; };
266
+ }];
267
+ type InvokeSrcExpr<TInput> = ServiceFn<TInput> | Machine<any, any, TInput, any, any, any, any, any, any>;
268
+ type InvokeConfig<C extends MContext, E extends SendableEvent, I, S extends ServiceRecord, Src extends keyof S | InvokeSrcExpr<[SrcInput] extends [never] ? any : SrcInput>, SrcInput> = {
269
+ subKey?: string | number;
270
+ input?: (arg: FnBaseParams<C, E, I>[0]) => Src extends keyof S ? (S[Src] extends AnyFn ? Parameters<S[Src]>[0] : SrcInput) : SrcInput;
271
+ };
272
+ type InvocationEvents<C extends MContext, I, EM extends EmittableEvent, G extends Record<string, Placeholder>, A extends Record<string, Placeholder>, S extends ServiceRecord, TSrcTypes extends {
273
+ Type: 'm' | 'f';
274
+ EM: EmittableEvent;
275
+ O: unknown;
276
+ }> = 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)>;
277
+ type OO_ResolveSrc<Src, S extends ServiceRecord> = Src extends keyof S ? S[Src] : Src;
278
+ 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> ? {
279
+ EM: EM;
280
+ O: O;
281
+ Type: 'm';
282
+ } : T extends ServiceFn ? Parameters<T>[1] extends EmitFn<infer EM> ? IsUnknown<ReturnType<T>> extends false ? {
283
+ EM: NonNullable<EM>;
284
+ O: Awaited<ReturnType<T>>;
285
+ Type: 'f';
286
+ } : never : {
287
+ EM: EmittableEvent;
288
+ O: Awaited<ReturnType<T>>;
289
+ Type: 'f';
290
+ } : never : never;
291
+ 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>>];
292
+ 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>>];
293
+ 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]>;
294
+ export declare function defServiceFn<TFn extends ServiceFn>(fn: TFn): TFn;
295
+ 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): Readonly<Machine<C, E, I, EM, O, G, A, S, {
296
+ initial: string;
297
+ stateOf: { [K in keyof D]: {
298
+ def: NStateItemDef<X_ExtractEType<D[K]>>;
299
+ sub: X_ExtractSubStDefinition<D[K]>;
300
+ }; };
301
+ }>>;
302
+ //#endregion
303
+ //#region src/runtime/r.types.d.ts
304
+ type Child<TService> = TService extends AnyMachine ? Runtime<TService> : TService extends AnyFn ? {
305
+ stop: () => void;
306
+ send: (event: SendableEvent) => void;
307
+ } : never;
308
+ type Children<S extends ServiceRecord> = { [K in keyof S & string as K | `${K}:${string | number}`]?: Child<S[K]>; } & {
309
+ [key: string]: Child<any>;
310
+ };
311
+ type InferredMachineTypes<M extends AnyMachine> = IsAny<M> extends true ? {
312
+ C: any;
313
+ E: any;
314
+ I: any;
315
+ EM: any;
316
+ O: any;
317
+ G: any;
318
+ A: any;
319
+ S: any;
320
+ ND: any;
321
+ M: AnyMachine;
322
+ } : M extends Machine<infer C, infer E, infer I, infer EM, infer O, infer G, infer A, infer S, infer ND> ? {
323
+ C: C;
324
+ E: E;
325
+ I: I;
326
+ EM: EM;
327
+ O: O;
328
+ G: G;
329
+ A: A;
330
+ S: S;
331
+ ND: ND;
332
+ M: Machine<C, E, I, EM, O, G, A, S, ND>;
333
+ } : never;
334
+ type InferMInput<M extends AnyMachine> = M extends Machine<any, any, infer I, any, any, any, any, any, any> ? I : never;
335
+ type SnapshotFrom<RT extends Runtime<any>> = RT extends Runtime<any, infer Mt> ? Snapshot<Mt['C'], Mt['O'], Mt['S']> : never;
336
+ //#endregion
337
+ //#region src/runtime/Runtime.d.ts
338
+ type RuntimeStatus = 'notStarted' | 'active' | 'stopped' | 'done' | 'error';
339
+ type Snapshot<C extends MContext, O, S extends ServiceRecord> = {
340
+ runtimeId: string;
341
+ states: string[];
342
+ stateValue: string | string[] | undefined;
343
+ context: Expand<X_RemovePrivateKeys<C>>;
344
+ c: Expand<X_RemovePrivateKeys<C>>;
345
+ runtimeStatus: RuntimeStatus;
346
+ output: O | undefined;
347
+ error: unknown;
348
+ _children: Children<S>;
349
+ };
350
+ type Subscriber<C extends MContext, O, S extends ServiceRecord> = {
351
+ onSnapshot?: (current: Expand<Snapshot<C, O, S>>, prev: Expand<Snapshot<C, O, S>>) => void;
352
+ onError?: (err: unknown) => void;
353
+ };
354
+ type RuntimeOptions<I> = {
355
+ id?: string;
356
+ input?: () => I;
357
+ };
358
+ type X_RemovePrivateKeys<T extends Record<string, unknown>> = { [K in keyof T as K extends `_${string}` ? never : K]: T[K]; };
359
+ export declare function makeRuntime<M extends AnyMachine>(machine: M, options?: RuntimeOptions<InferMInput<M>>): Runtime<M, InferredMachineTypes<M>>;
360
+ type C$1<Mt extends InferredMachineTypes<any>> = Mt['C'];
361
+ type E$1<Mt extends InferredMachineTypes<any>> = Mt['E'];
362
+ type I$1<Mt extends InferredMachineTypes<any>> = Mt['I'];
363
+ type EM$1<Mt extends InferredMachineTypes<any>> = Mt['EM'];
364
+ type O$1<Mt extends InferredMachineTypes<any>> = Mt['O'];
365
+ type S$1<Mt extends InferredMachineTypes<any>> = Mt['S'];
366
+ declare class Runtime<M extends AnyMachine, Mt extends InferredMachineTypes<M> = InferredMachineTypes<M>> {
367
+ #private;
368
+ readonly id: string;
369
+ readonly machine: Mt['M'];
370
+ get states(): string[];
371
+ get stateValue(): string | string[] | undefined;
372
+ get context(): X_RemovePrivateKeys<C$1<Mt>> extends (infer T) ? { [K in keyof T]: X_RemovePrivateKeys<C$1<Mt>>[K]; } : never;
373
+ get runtimeStatus(): RuntimeStatus;
374
+ get output(): O$1<Mt> | undefined;
375
+ getSnapshot(): Snapshot<C$1<Mt>, O$1<Mt>, S$1<Mt>>;
376
+ constructor(machine: M, { id, input }?: RuntimeOptions<I$1<Mt>>);
377
+ subscribe(onSnapshot: Subscriber<C$1<Mt>, O$1<Mt>, S$1<Mt>>['onSnapshot'], onError?: Subscriber<C$1<Mt>, O$1<Mt>, S$1<Mt>>['onError']): () => void;
378
+ start(): this;
379
+ send(event: E$1<Mt> | PureETypeString<E$1<Mt>>): this;
380
+ stop(): this;
381
+ onEmitted(emittedEventType: EM$1<Mt>['type'], handler: (emittedEvent: EM$1<Mt>) => void): () => void;
382
+ }
383
+ //#endregion
384
+ //#region src/tools/waitFor.d.ts
385
+ export declare function waitFor<RT extends Runtime<any>>(runtime: RT, cond: (snapshot: SnapshotFrom<RT>) => boolean, options?: {
386
+ timeout?: number;
387
+ }): Promise<SnapshotFrom<RT>>;
388
+ //#endregion
389
+ export type { AnyMachine, BuiltinEType, EmitFn as Emit, EmittableEvent, ListenFn as Listen, MContext, Runtime, RuntimeOptions, RuntimeStatus, SendableEvent, ServiceFn };