UNPKG

3.69 kBTypeScriptView Raw
1import { StateMachine } from "./StateMachine.js";
2import { ResolveTypegenMeta, TypegenConstraint, TypegenDisabled } from "./typegenTypes.js";
3import { AnyActorRef, EventObject, AnyEventObject, Cast, InternalMachineImplementations, IsNever, MachineConfig, MachineContext, MachineTypes, NonReducibleUnknown, ParameterizedObject, Prop, ProvidedActor, StateValue, ToChildren } from "./types.js";
4type TestValue = string | {
5 [k: string]: TestValue | undefined;
6};
7type _GroupTestValues<TTestValue extends string | TestValue> = TTestValue extends string ? TTestValue extends `${string}.${string}` ? [never, never] : [TTestValue, never] : [never, TTestValue];
8type GroupTestValues<TTestValue extends string | TestValue> = {
9 leafCandidates: _GroupTestValues<TTestValue>[0];
10 nonLeaf: _GroupTestValues<TTestValue>[1];
11};
12type FilterLeafValues<TLeafCandidate extends string, TNonLeaf extends {
13 [k: string]: TestValue | undefined;
14}> = IsNever<TNonLeaf> extends true ? TLeafCandidate : TLeafCandidate extends string ? TLeafCandidate extends keyof TNonLeaf ? never : TLeafCandidate : never;
15type ToStateValue<TTestValue extends string | TestValue> = FilterLeafValues<GroupTestValues<TTestValue>['leafCandidates'], GroupTestValues<TTestValue>['nonLeaf']> | (IsNever<GroupTestValues<TTestValue>['nonLeaf']> extends false ? {
16 [K in keyof GroupTestValues<TTestValue>['nonLeaf']]: ToStateValue<NonNullable<GroupTestValues<TTestValue>['nonLeaf'][K]>>;
17} : never);
18/**
19 * Creates a state machine (statechart) with the given configuration.
20 *
21 * The state machine represents the pure logic of a state machine actor.
22 *
23 * @param config The state machine configuration.
24 * @param options DEPRECATED: use `setup({ ... })` or `machine.provide({ ... })` to provide machine implementations instead.
25 *
26 * @example
27 ```ts
28 import { createMachine } from 'xstate';
29
30 const lightMachine = createMachine({
31 id: 'light',
32 initial: 'green',
33 states: {
34 green: {
35 on: {
36 TIMER: { target: 'yellow' }
37 }
38 },
39 yellow: {
40 on: {
41 TIMER: { target: 'red' }
42 }
43 },
44 red: {
45 on: {
46 TIMER: { target: 'green' }
47 }
48 }
49 }
50 });
51
52 const lightActor = createActor(lightMachine);
53 lightActor.start();
54
55 lightActor.send({ type: 'TIMER' });
56 ```
57 */
58export declare function createMachine<TContext extends MachineContext, TEvent extends AnyEventObject, // TODO: consider using a stricter `EventObject` here
59TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TTag extends string, TInput, TOutput extends NonReducibleUnknown, TEmitted extends EventObject, TTypesMeta extends TypegenConstraint = TypegenDisabled>(config: {
60 types?: MachineTypes<TContext, TEvent, TActor, TAction, TGuard, TDelay, TTag, TInput, TOutput, TEmitted, TTypesMeta>;
61 schemas?: unknown;
62} & MachineConfig<TContext, TEvent, TActor, TAction, TGuard, TDelay, TTag, TInput, TOutput, TEmitted, TTypesMeta>, implementations?: InternalMachineImplementations<TContext, ResolveTypegenMeta<TTypesMeta, TEvent, TActor, TAction, TGuard, TDelay, TTag, TEmitted>>): StateMachine<TContext, TEvent, Cast<ToChildren<TActor>, Record<string, AnyActorRef | undefined>>, TActor, TAction, TGuard, TDelay, 'matchesStates' extends keyof TTypesMeta ? ToStateValue<Cast<TTypesMeta['matchesStates'], TestValue>> : StateValue, Prop<ResolveTypegenMeta<TTypesMeta, TEvent, TActor, TAction, TGuard, TDelay, TTag, TEmitted>['resolved'], 'tags'> & string, TInput, TOutput, TEmitted, ResolveTypegenMeta<TTypesMeta, TEvent, TActor, TAction, TGuard, TDelay, TTag, TEmitted>>;
63export {};