xstate 5.4.0 → 5.5.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/actions/dist/xstate-actions.cjs.js +2 -2
- package/actions/dist/xstate-actions.development.cjs.js +2 -2
- package/actions/dist/xstate-actions.development.esm.js +2 -2
- package/actions/dist/xstate-actions.esm.js +2 -2
- package/actions/dist/xstate-actions.umd.min.js +1 -1
- package/actions/dist/xstate-actions.umd.min.js.map +1 -1
- package/actors/dist/xstate-actors.cjs.js +1 -1
- package/actors/dist/xstate-actors.development.cjs.js +1 -1
- package/actors/dist/xstate-actors.development.esm.js +1 -1
- package/actors/dist/xstate-actors.esm.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/declarations/src/State.d.ts +43 -17
- package/dist/declarations/src/StateMachine.d.ts +16 -15
- package/dist/declarations/src/StateNode.d.ts +0 -4
- package/dist/declarations/src/actions/assign.d.ts +28 -1
- package/dist/declarations/src/actions/cancel.d.ts +23 -4
- package/dist/declarations/src/actions/enqueueActions.d.ts +20 -0
- package/dist/declarations/src/actions/send.d.ts +2 -2
- package/dist/declarations/src/{interpreter.d.ts → createActor.d.ts} +2 -4
- package/dist/declarations/src/createMachine.d.ts +40 -0
- package/dist/declarations/src/getNextSnapshot.d.ts +32 -0
- package/dist/declarations/src/guards.d.ts +86 -0
- package/dist/declarations/src/index.d.ts +2 -1
- package/dist/declarations/src/spawn.d.ts +2 -2
- package/dist/declarations/src/typegenTypes.d.ts +26 -14
- package/dist/declarations/src/types.d.ts +17 -36
- package/dist/{log-e870aec8.esm.js → log-22b3587f.esm.js} +67 -10
- package/dist/{log-580765a2.development.esm.js → log-285f62db.development.esm.js} +67 -10
- package/dist/{log-a32b44b3.cjs.js → log-742895c6.cjs.js} +67 -10
- package/dist/{log-cd3d7c14.development.cjs.js → log-da322832.development.cjs.js} +67 -10
- package/dist/{raise-7df513e7.esm.js → raise-0e64ee6e.esm.js} +125 -16
- package/dist/{raise-e3ff3de1.development.cjs.js → raise-7af39710.development.cjs.js} +125 -16
- package/dist/{raise-0fc3a80c.development.esm.js → raise-8da27ebb.development.esm.js} +125 -16
- package/dist/{raise-c3bbdd3a.cjs.js → raise-ad8bb7c2.cjs.js} +125 -16
- package/dist/xstate.cjs.js +105 -4
- package/dist/xstate.cjs.mjs +1 -0
- package/dist/xstate.development.cjs.js +105 -4
- package/dist/xstate.development.cjs.mjs +1 -0
- package/dist/xstate.development.esm.js +107 -7
- package/dist/xstate.esm.js +107 -7
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.esm.js +1 -1
- package/guards/dist/xstate-guards.esm.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { MachineSnapshot } from "./State.js";
|
|
2
2
|
import { StateNode } from "./StateNode.js";
|
|
3
|
-
import
|
|
4
|
-
import
|
|
3
|
+
import { AnyActorSystem } from "./system.js";
|
|
4
|
+
import { ResolveTypegenMeta, TypegenDisabled } from "./typegenTypes.js";
|
|
5
|
+
import type { ActorLogic, ActorScope, AnyActorRef, AnyActorScope, Equals, EventDescriptor, EventObject, HistoryValue, InternalMachineImplementations, MachineConfig, MachineContext, MachineImplementationsSimplified, NoInfer, ParameterizedObject, ProvidedActor, Snapshot, StateMachineDefinition, StateValue, TransitionDefinition } from "./types.js";
|
|
5
6
|
export declare const STATE_IDENTIFIER = "#";
|
|
6
7
|
export declare const WILDCARD = "*";
|
|
7
|
-
export declare class StateMachine<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TStateValue extends StateValue, TTag extends string, TInput, TOutput, TResolvedTypesMeta = ResolveTypegenMeta<TypegenDisabled, NoInfer<TEvent>, TActor, TAction, TGuard, TDelay, TTag>> implements ActorLogic<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput
|
|
8
|
+
export declare class StateMachine<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TStateValue extends StateValue, TTag extends string, TInput, TOutput, TResolvedTypesMeta = ResolveTypegenMeta<TypegenDisabled, NoInfer<TEvent>, TActor, TAction, TGuard, TDelay, TTag>> implements ActorLogic<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput>, TEvent, TInput, AnyActorSystem> {
|
|
8
9
|
/**
|
|
9
10
|
* The raw config used to create the machine.
|
|
10
11
|
*/
|
|
@@ -14,8 +15,6 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
|
|
|
14
15
|
*/
|
|
15
16
|
version?: string;
|
|
16
17
|
implementations: MachineImplementationsSimplified<TContext, TEvent>;
|
|
17
|
-
__xstatenode: true;
|
|
18
|
-
idMap: Map<string, StateNode<TContext, TEvent>>;
|
|
19
18
|
root: StateNode<TContext, TEvent>;
|
|
20
19
|
id: string;
|
|
21
20
|
states: StateNode<TContext, TEvent>['states'];
|
|
@@ -34,7 +33,7 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
|
|
|
34
33
|
*
|
|
35
34
|
* @returns A new `StateMachine` instance with the provided implementations.
|
|
36
35
|
*/
|
|
37
|
-
provide(implementations: InternalMachineImplementations<TContext, TResolvedTypesMeta, true>): StateMachine<TContext, TEvent, TChildren, TActor, TAction, TGuard, TDelay, TStateValue, TTag, TInput, TOutput,
|
|
36
|
+
provide(implementations: InternalMachineImplementations<TContext, TResolvedTypesMeta, true>): StateMachine<TContext, TEvent, TChildren, TActor, TAction, TGuard, TDelay, TStateValue, TTag, TInput, TOutput, TResolvedTypesMeta>;
|
|
38
37
|
resolveState(config: {
|
|
39
38
|
value: StateValue;
|
|
40
39
|
context?: TContext;
|
|
@@ -44,7 +43,7 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
|
|
|
44
43
|
error?: unknown;
|
|
45
44
|
} & (Equals<TContext, MachineContext> extends false ? {
|
|
46
45
|
context: unknown;
|
|
47
|
-
} : {})): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput
|
|
46
|
+
} : {})): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput>;
|
|
48
47
|
/**
|
|
49
48
|
* Determines the next snapshot given the current `snapshot` and received `event`.
|
|
50
49
|
* Calculates a full macrostep from all microsteps.
|
|
@@ -52,7 +51,7 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
|
|
|
52
51
|
* @param snapshot The current snapshot
|
|
53
52
|
* @param event The received event
|
|
54
53
|
*/
|
|
55
|
-
transition(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput
|
|
54
|
+
transition(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput>, event: TEvent, actorScope: ActorScope<typeof snapshot, TEvent>): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput>;
|
|
56
55
|
/**
|
|
57
56
|
* Determines the next state given the current `state` and `event`.
|
|
58
57
|
* Calculates a microstep.
|
|
@@ -60,8 +59,8 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
|
|
|
60
59
|
* @param state The current state
|
|
61
60
|
* @param event The received event
|
|
62
61
|
*/
|
|
63
|
-
microstep(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput
|
|
64
|
-
getTransitionData(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput
|
|
62
|
+
microstep(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput>, event: TEvent, actorScope: AnyActorScope): Array<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput>>;
|
|
63
|
+
getTransitionData(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput>, event: TEvent): Array<TransitionDefinition<TContext, TEvent>>;
|
|
65
64
|
/**
|
|
66
65
|
* The initial state _before_ evaluating any microsteps.
|
|
67
66
|
* This "pre-initial" state is provided to initial actions executed in the initial state.
|
|
@@ -70,13 +69,15 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
|
|
|
70
69
|
/**
|
|
71
70
|
* Returns the initial `State` instance, with reference to `self` as an `ActorRef`.
|
|
72
71
|
*/
|
|
73
|
-
getInitialSnapshot(actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput
|
|
74
|
-
start(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput
|
|
72
|
+
getInitialSnapshot(actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput>, TEvent>, input?: TInput): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput>;
|
|
73
|
+
start(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput>): void;
|
|
75
74
|
getStateNodeById(stateId: string): StateNode<TContext, TEvent>;
|
|
76
75
|
get definition(): StateMachineDefinition<TContext, TEvent>;
|
|
77
76
|
toJSON(): StateMachineDefinition<TContext, TEvent>;
|
|
78
|
-
getPersistedSnapshot(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput
|
|
79
|
-
restoreSnapshot(snapshot: Snapshot<unknown>, _actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput
|
|
80
|
-
/**
|
|
77
|
+
getPersistedSnapshot(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput>, options?: unknown): Snapshot<unknown>;
|
|
78
|
+
restoreSnapshot(snapshot: Snapshot<unknown>, _actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput>, TEvent>): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput>;
|
|
79
|
+
/**
|
|
80
|
+
* @deprecated an internal property that was acting as a "phantom" type, it's not used by anything right now but it's kept around for compatibility reasons
|
|
81
|
+
**/
|
|
81
82
|
__TResolvedTypesMeta: TResolvedTypesMeta;
|
|
82
83
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { MachineSnapshot } from "./State.js";
|
|
2
1
|
import type { StateMachine } from "./StateMachine.js";
|
|
3
2
|
import type { DelayedTransitionDefinition, EventObject, InitialTransitionDefinition, InvokeDefinition, MachineContext, Mapper, StateNodeConfig, StateNodeDefinition, StateNodesConfig, TransitionDefinition, TransitionDefinitionMap, TODO, UnknownAction, ParameterizedObject, AnyStateMachine, ProvidedActor, NonReducibleUnknown, EventDescriptor } from "./types.js";
|
|
4
3
|
interface StateNodeOptions<TContext extends MachineContext, TEvent extends EventObject> {
|
|
@@ -99,12 +98,10 @@ export declare class StateNode<TContext extends MachineContext = MachineContext,
|
|
|
99
98
|
TODO, // guards
|
|
100
99
|
TODO, // delays
|
|
101
100
|
TODO>, options: StateNodeOptions<TContext, TEvent>);
|
|
102
|
-
_initialize(): void;
|
|
103
101
|
/**
|
|
104
102
|
* The well-structured state node definition.
|
|
105
103
|
*/
|
|
106
104
|
get definition(): StateNodeDefinition<TContext, TEvent>;
|
|
107
|
-
toJSON(): StateNodeDefinition<TContext, TEvent>;
|
|
108
105
|
/**
|
|
109
106
|
* The logic invoked as actors by this state node.
|
|
110
107
|
*/
|
|
@@ -115,7 +112,6 @@ export declare class StateNode<TContext extends MachineContext = MachineContext,
|
|
|
115
112
|
get on(): TransitionDefinitionMap<TContext, TEvent>;
|
|
116
113
|
get after(): Array<DelayedTransitionDefinition<TContext, TEvent>>;
|
|
117
114
|
get initial(): InitialTransitionDefinition<TContext, TEvent>;
|
|
118
|
-
next(snapshot: MachineSnapshot<TContext, TEvent, any, any, any, any>, event: TEvent): TransitionDefinition<TContext, TEvent>[] | undefined;
|
|
119
115
|
/**
|
|
120
116
|
* All the event types accepted by this state node and its descendants.
|
|
121
117
|
*/
|
|
@@ -10,7 +10,34 @@ export interface AssignAction<TContext extends MachineContext, TExpressionEvent
|
|
|
10
10
|
/**
|
|
11
11
|
* Updates the current context of the machine.
|
|
12
12
|
*
|
|
13
|
-
* @param assignment An object that represents the partial context to update
|
|
13
|
+
* @param assignment An object that represents the partial context to update, or a
|
|
14
|
+
* function that returns an object that represents the partial context to update.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
```ts
|
|
18
|
+
import { createMachine, assign } from 'xstate';
|
|
19
|
+
|
|
20
|
+
const countMachine = createMachine({
|
|
21
|
+
context: {
|
|
22
|
+
count: 0,
|
|
23
|
+
message: ''
|
|
24
|
+
},
|
|
25
|
+
on: {
|
|
26
|
+
inc: {
|
|
27
|
+
actions: assign({
|
|
28
|
+
count: ({ context }) => context.count + 1
|
|
29
|
+
})
|
|
30
|
+
},
|
|
31
|
+
updateMessage: {
|
|
32
|
+
actions: assign(({ context, event }) => {
|
|
33
|
+
return {
|
|
34
|
+
message: event.message.trim()
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
```
|
|
14
41
|
*/
|
|
15
42
|
export declare function assign<TContext extends MachineContext, TExpressionEvent extends AnyEventObject = AnyEventObject, // TODO: consider using a stricter `EventObject` here
|
|
16
43
|
TParams extends ParameterizedObject['params'] | undefined = ParameterizedObject['params'] | undefined, TEvent extends EventObject = EventObject, TActor extends ProvidedActor = ProvidedActor>(assignment: Assigner<LowInfer<TContext>, TExpressionEvent, TParams, TEvent, TActor> | PropertyAssigner<LowInfer<TContext>, TExpressionEvent, TParams, TEvent, TActor>): AssignAction<TContext, TExpressionEvent, TParams, TEvent, TActor>;
|
|
@@ -4,11 +4,30 @@ export interface CancelAction<TContext extends MachineContext, TExpressionEvent
|
|
|
4
4
|
(args: ActionArgs<TContext, TExpressionEvent, TEvent>, params: TParams): void;
|
|
5
5
|
}
|
|
6
6
|
/**
|
|
7
|
-
* Cancels
|
|
8
|
-
*
|
|
9
|
-
* (e.g., if `cancel(...)` is called after the `send(...)` action's `delay`).
|
|
7
|
+
* Cancels a delayed `sendTo(...)` action that is waiting to be executed. The canceled `sendTo(...)` action
|
|
8
|
+
* will not send its event or execute, unless the `delay` has already elapsed before `cancel(...)` is called.
|
|
10
9
|
*
|
|
11
|
-
* @param sendId The `id` of the `
|
|
10
|
+
* @param sendId The `id` of the `sendTo(...)` action to cancel.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
```ts
|
|
14
|
+
import { createMachine, sendTo, cancel } from 'xstate';
|
|
15
|
+
|
|
16
|
+
const machine = createMachine({
|
|
17
|
+
// ...
|
|
18
|
+
on: {
|
|
19
|
+
sendEvent: {
|
|
20
|
+
actions: sendTo('some-actor', { type: 'someEvent' }, {
|
|
21
|
+
id: 'some-id',
|
|
22
|
+
delay: 1000
|
|
23
|
+
})
|
|
24
|
+
},
|
|
25
|
+
cancelEvent: {
|
|
26
|
+
actions: cancel('some-id')
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
```
|
|
12
31
|
*/
|
|
13
32
|
export declare function cancel<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject>(sendId: ResolvableSendId<TContext, TExpressionEvent, TParams, TEvent>): CancelAction<TContext, TExpressionEvent, TParams, TEvent>;
|
|
14
33
|
export {};
|
|
@@ -28,5 +28,25 @@ interface CollectActionsArg<TContext extends MachineContext, TExpressionEvent ex
|
|
|
28
28
|
enqueue: ActionEnqueuer<TContext, TExpressionEvent, TEvent, TActor, TAction, TGuard, TDelay>;
|
|
29
29
|
}
|
|
30
30
|
type CollectActions<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string> = ({ context, event, check, enqueue, self }: CollectActionsArg<TContext, TExpressionEvent, TEvent, TActor, TAction, TGuard, TDelay>) => void;
|
|
31
|
+
/**
|
|
32
|
+
* Creates an action object that will execute actions that are queued by the `enqueue(action)` function.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
```ts
|
|
36
|
+
import { createMachine, enqueueActions } from 'xstate';
|
|
37
|
+
|
|
38
|
+
const machine = createMachine({
|
|
39
|
+
entry: enqueueActions(({ enqueue, check }) => {
|
|
40
|
+
enqueue.assign({ count: 0 });
|
|
41
|
+
|
|
42
|
+
if (check('someGuard')) {
|
|
43
|
+
enqueue.assign({ count: 1 });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
enqueue('someAction');
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
```
|
|
50
|
+
*/
|
|
31
51
|
export declare function enqueueActions<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject = TExpressionEvent, TActor extends ProvidedActor = ProvidedActor, TAction extends ParameterizedObject = ParameterizedObject, TGuard extends ParameterizedObject = ParameterizedObject, TDelay extends string = string>(collect: CollectActions<TContext, TExpressionEvent, TEvent, TActor, TAction, TGuard, TDelay>): EnqueueActionsAction<TContext, TExpressionEvent, TEvent, TActor, TAction, TGuard, TDelay>;
|
|
32
52
|
export {};
|
|
@@ -22,9 +22,9 @@ export declare function sendTo<TContext extends MachineContext, TExpressionEvent
|
|
|
22
22
|
export declare function sendParent<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TSentEvent extends EventObject = AnyEventObject, TEvent extends EventObject = AnyEventObject, TDelay extends string = string>(event: TSentEvent | SendExpr<TContext, TExpressionEvent, TParams, TSentEvent, TEvent>, options?: SendToActionOptions<TContext, TExpressionEvent, TParams, TEvent, TDelay>): SendToAction<TContext, TExpressionEvent, TParams, TEvent, TDelay>;
|
|
23
23
|
type Target<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject> = string | ActorRef<any, any> | ((args: ActionArgs<TContext, TExpressionEvent, TEvent>, params: TParams) => string | ActorRef<any, any>);
|
|
24
24
|
/**
|
|
25
|
-
* Forwards (sends) an event to
|
|
25
|
+
* Forwards (sends) an event to the `target` actor.
|
|
26
26
|
*
|
|
27
|
-
* @param target The target
|
|
27
|
+
* @param target The target actor to forward the event to.
|
|
28
28
|
* @param options Options to pass into the send action creator.
|
|
29
29
|
*/
|
|
30
30
|
export declare function forwardTo<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject, TDelay extends string>(target: Target<TContext, TExpressionEvent, TParams, TEvent>, options?: SendToActionOptions<TContext, TExpressionEvent, TParams, TEvent, TDelay>): SendToAction<TContext, TExpressionEvent, TParams, TEvent, TDelay>;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { symbolObservable } from "./symbolObservable.js";
|
|
2
2
|
import { AnyActorSystem, Clock } from "./system.js";
|
|
3
|
-
import {
|
|
4
|
-
import type { AnyActorLogic, AnyStateMachine, EventFromLogic, Snapshot, SnapshotFrom } from "./types.js";
|
|
3
|
+
import type { AnyActorLogic, EventFromLogic, Snapshot, SnapshotFrom } from "./types.js";
|
|
5
4
|
import { ActorOptions, ActorRef, EventObject, InteropSubscribable, Observer, Subscription } from "./types.js";
|
|
6
5
|
export declare const $$ACTOR_TYPE = 1;
|
|
7
6
|
export type SnapshotListener<TLogic extends AnyActorLogic> = (snapshot: SnapshotFrom<TLogic>) => void;
|
|
@@ -35,7 +34,6 @@ export declare class Actor<TLogic extends AnyActorLogic> implements ActorRef<Sna
|
|
|
35
34
|
private observers;
|
|
36
35
|
private logger;
|
|
37
36
|
_parent?: ActorRef<any, any>;
|
|
38
|
-
_syncSnapshot?: boolean;
|
|
39
37
|
ref: ActorRef<SnapshotFrom<TLogic>, EventFromLogic<TLogic>>;
|
|
40
38
|
private _actorScope;
|
|
41
39
|
private _systemId;
|
|
@@ -196,7 +194,7 @@ export declare class Actor<TLogic extends AnyActorLogic> implements ActorRef<Sna
|
|
|
196
194
|
* @param logic - The actor logic to create an actor from. For a state machine actor logic creator, see {@link createMachine}. Other actor logic creators include {@link fromCallback}, {@link fromEventObservable}, {@link fromObservable}, {@link fromPromise}, and {@link fromTransition}.
|
|
197
195
|
* @param options - Actor options
|
|
198
196
|
*/
|
|
199
|
-
export declare function createActor<TLogic extends AnyActorLogic>(logic: TLogic
|
|
197
|
+
export declare function createActor<TLogic extends AnyActorLogic>(logic: TLogic, options?: ActorOptions<TLogic>): Actor<TLogic>;
|
|
200
198
|
/**
|
|
201
199
|
* Creates a new Interpreter instance for the given machine with the provided options, if any.
|
|
202
200
|
*
|
|
@@ -15,6 +15,46 @@ type FilterLeafValues<TLeafCandidate extends string, TNonLeaf extends {
|
|
|
15
15
|
type ToStateValue<TTestValue extends string | TestValue> = FilterLeafValues<GroupTestValues<TTestValue>['leafCandidates'], GroupTestValues<TTestValue>['nonLeaf']> | (IsNever<GroupTestValues<TTestValue>['nonLeaf']> extends false ? {
|
|
16
16
|
[K in keyof GroupTestValues<TTestValue>['nonLeaf']]: ToStateValue<NonNullable<GroupTestValues<TTestValue>['nonLeaf'][K]>>;
|
|
17
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
|
+
*/
|
|
18
58
|
export declare function createMachine<TContext extends MachineContext, TEvent extends AnyEventObject, // TODO: consider using a stricter `EventObject` here
|
|
19
59
|
TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TTag extends string, TInput, TOutput extends NonReducibleUnknown, TTypesMeta extends TypegenConstraint = TypegenDisabled>(config: MachineConfig<TContext, TEvent, TActor, TAction, TGuard, TDelay, TTag, TInput, TOutput, TTypesMeta>, implementations?: InternalMachineImplementations<TContext, ResolveTypegenMeta<TTypesMeta, TEvent, TActor, TAction, TGuard, TDelay, TTag>>): 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>['resolved'], 'tags'> & string, TInput, TOutput, ResolveTypegenMeta<TTypesMeta, TEvent, TActor, TAction, TGuard, TDelay, TTag>>;
|
|
20
60
|
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { AnyActorLogic, EventFromLogic, InputFrom, SnapshotFrom } from "./types.js";
|
|
2
|
+
export declare function getInitialSnapshot<T extends AnyActorLogic>(actorLogic: T, ...[input]: undefined extends InputFrom<T> ? [input?: InputFrom<T>] : [input: InputFrom<T>]): SnapshotFrom<T>;
|
|
3
|
+
/**
|
|
4
|
+
* Determines the next snapshot for the given `actorLogic` based on
|
|
5
|
+
* the given `snapshot` and `event`.
|
|
6
|
+
*
|
|
7
|
+
* If the `snapshot` is `undefined`, the initial snapshot of the
|
|
8
|
+
* `actorLogic` is used.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
```ts
|
|
12
|
+
import { getNextSnapshot } from 'xstate';
|
|
13
|
+
import { trafficLightMachine } from './trafficLightMachine.ts';
|
|
14
|
+
|
|
15
|
+
const nextSnapshot = getNextSnapshot(
|
|
16
|
+
trafficLightMachine, // actor logic
|
|
17
|
+
undefined, // snapshot (or initial state if undefined)
|
|
18
|
+
{ type: 'TIMER' }); // event object
|
|
19
|
+
|
|
20
|
+
console.log(nextSnapshot.value);
|
|
21
|
+
// => 'yellow'
|
|
22
|
+
|
|
23
|
+
const nextSnapshot2 = getNextSnapshot(
|
|
24
|
+
trafficLightMachine, // actor logic
|
|
25
|
+
nextSnapshot, // snapshot
|
|
26
|
+
{ type: 'TIMER' }); // event object
|
|
27
|
+
|
|
28
|
+
console.log(nextSnapshot2.value);
|
|
29
|
+
// =>'red'
|
|
30
|
+
```
|
|
31
|
+
*/
|
|
32
|
+
export declare function getNextSnapshot<T extends AnyActorLogic>(actorLogic: T, snapshot: SnapshotFrom<T>, event: EventFromLogic<T>): SnapshotFrom<T>;
|
|
@@ -26,12 +26,98 @@ export type UnknownGuard = UnknownReferencedGuard | UnknownInlineGuard;
|
|
|
26
26
|
type UnknownReferencedGuard = Guard<MachineContext, EventObject, ParameterizedObject['params'], ParameterizedObject>;
|
|
27
27
|
type UnknownInlineGuard = Guard<MachineContext, EventObject, undefined, ParameterizedObject>;
|
|
28
28
|
export declare function stateIn<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined>(stateValue: StateValue): GuardPredicate<TContext, TExpressionEvent, TParams, any>;
|
|
29
|
+
/**
|
|
30
|
+
* Higher-order guard that evaluates to `true` if the `guard` passed to it evaluates to `false`.
|
|
31
|
+
*
|
|
32
|
+
* @category Guards
|
|
33
|
+
* @example
|
|
34
|
+
```ts
|
|
35
|
+
import { setup, not } from 'xstate';
|
|
36
|
+
|
|
37
|
+
const machine = setup({
|
|
38
|
+
guards: {
|
|
39
|
+
someNamedGuard: () => false
|
|
40
|
+
}
|
|
41
|
+
}).createMachine({
|
|
42
|
+
on: {
|
|
43
|
+
someEvent: {
|
|
44
|
+
guard: not('someNamedGuard'),
|
|
45
|
+
actions: () => {
|
|
46
|
+
// will be executed if guard in `not(...)`
|
|
47
|
+
// evaluates to `false`
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
* @returns A guard
|
|
54
|
+
*/
|
|
29
55
|
export declare function not<TContext extends MachineContext, TExpressionEvent extends EventObject, TArg>(guard: SingleGuardArg<TContext, TExpressionEvent, unknown, TArg>): GuardPredicate<TContext, TExpressionEvent, unknown, NormalizeGuardArg<NoInfer<TArg>>>;
|
|
56
|
+
/**
|
|
57
|
+
* Higher-order guard that evaluates to `true` if all `guards` passed to it
|
|
58
|
+
* evaluate to `true`.
|
|
59
|
+
*
|
|
60
|
+
* @category Guards
|
|
61
|
+
* @example
|
|
62
|
+
```ts
|
|
63
|
+
import { setup, and } from 'xstate';
|
|
64
|
+
|
|
65
|
+
const machine = setup({
|
|
66
|
+
guards: {
|
|
67
|
+
someNamedGuard: () => true
|
|
68
|
+
}
|
|
69
|
+
}).createMachine({
|
|
70
|
+
on: {
|
|
71
|
+
someEvent: {
|
|
72
|
+
guard: and([
|
|
73
|
+
({ context }) => context.value > 0,
|
|
74
|
+
'someNamedGuard'
|
|
75
|
+
]),
|
|
76
|
+
actions: () => {
|
|
77
|
+
// will be executed if all guards in `and(...)`
|
|
78
|
+
// evaluate to true
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
* @returns A guard action object
|
|
85
|
+
*/
|
|
30
86
|
export declare function and<TContext extends MachineContext, TExpressionEvent extends EventObject, TArg extends unknown[]>(guards: readonly [
|
|
31
87
|
...{
|
|
32
88
|
[K in keyof TArg]: SingleGuardArg<TContext, TExpressionEvent, unknown, TArg[K]>;
|
|
33
89
|
}
|
|
34
90
|
]): GuardPredicate<TContext, TExpressionEvent, unknown, NormalizeGuardArgArray<NoInfer<TArg>>>;
|
|
91
|
+
/**
|
|
92
|
+
* Higher-order guard that evaluates to `true` if any of the `guards` passed to it
|
|
93
|
+
* evaluate to `true`.
|
|
94
|
+
*
|
|
95
|
+
* @category Guards
|
|
96
|
+
* @example
|
|
97
|
+
```ts
|
|
98
|
+
import { setup, or } from 'xstate';
|
|
99
|
+
|
|
100
|
+
const machine = setup({
|
|
101
|
+
guards: {
|
|
102
|
+
someNamedGuard: () => true
|
|
103
|
+
}
|
|
104
|
+
}).createMachine({
|
|
105
|
+
on: {
|
|
106
|
+
someEvent: {
|
|
107
|
+
guard: or([
|
|
108
|
+
({ context }) => context.value > 0,
|
|
109
|
+
'someNamedGuard'
|
|
110
|
+
]),
|
|
111
|
+
actions: () => {
|
|
112
|
+
// will be executed if any of the guards in `or(...)`
|
|
113
|
+
// evaluate to true
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
* @returns A guard action object
|
|
120
|
+
*/
|
|
35
121
|
export declare function or<TContext extends MachineContext, TExpressionEvent extends EventObject, TArg extends unknown[]>(guards: readonly [
|
|
36
122
|
...{
|
|
37
123
|
[K in keyof TArg]: SingleGuardArg<TContext, TExpressionEvent, unknown, TArg[K]>;
|
|
@@ -9,7 +9,8 @@ export * from "./typegenTypes.js";
|
|
|
9
9
|
export * from "./types.js";
|
|
10
10
|
export { waitFor } from "./waitFor.js";
|
|
11
11
|
import { createMachine } from "./createMachine.js";
|
|
12
|
-
|
|
12
|
+
export { getNextSnapshot } from "./getNextSnapshot.js";
|
|
13
|
+
import { Actor, createActor, interpret, Interpreter } from "./createActor.js";
|
|
13
14
|
import { StateNode } from "./StateNode.js";
|
|
14
15
|
export { and, not, or, stateIn } from "./guards.js";
|
|
15
16
|
export { setup } from "./setup.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ActorRefFrom,
|
|
1
|
+
import { ActorRefFrom, AnyActorLogic, AnyActorRef, AnyActorScope, AnyEventObject, AnyMachineSnapshot, ConditionalRequired, InputFrom, IsLiteralString, IsNotNever, ProvidedActor, RequiredActorOptions } from "./types.js";
|
|
2
2
|
type SpawnOptions<TActor extends ProvidedActor, TSrc extends TActor['src']> = TActor extends {
|
|
3
3
|
src: TSrc;
|
|
4
4
|
} ? ConditionalRequired<[
|
|
@@ -17,7 +17,7 @@ type GetConcreteLogic<TActor extends ProvidedActor, TSrc extends TActor['src']>
|
|
|
17
17
|
export type Spawner<TActor extends ProvidedActor> = IsLiteralString<TActor['src']> extends true ? <TSrc extends TActor['src']>(logic: TSrc, ...[options]: SpawnOptions<TActor, TSrc>) => ActorRefFrom<GetConcreteLogic<TActor, TSrc>> : <TLogic extends AnyActorLogic | string>(src: TLogic, options?: {
|
|
18
18
|
id?: string;
|
|
19
19
|
systemId?: string;
|
|
20
|
-
input?: unknown
|
|
20
|
+
input?: TLogic extends string ? unknown : InputFrom<TLogic>;
|
|
21
21
|
syncSnapshot?: boolean;
|
|
22
22
|
}) => TLogic extends string ? AnyActorRef : ActorRefFrom<TLogic>;
|
|
23
23
|
export declare function createSpawner(actorScope: AnyActorScope, { machine, context }: AnyMachineSnapshot, event: AnyEventObject, spawnedChildren: Record<string, AnyActorRef>): Spawner<any>;
|
|
@@ -1,10 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EventObject, IndexByType, IsNever, Prop, ParameterizedObject, ProvidedActor, OutputFrom, AnyActorLogic, IndexByProp } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated
|
|
4
|
+
*/
|
|
2
5
|
export interface TypegenDisabled {
|
|
3
6
|
'@@xstate/typegen': false;
|
|
4
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* @deprecated
|
|
10
|
+
*/
|
|
5
11
|
export interface TypegenEnabled {
|
|
6
12
|
'@@xstate/typegen': true;
|
|
7
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* @deprecated
|
|
16
|
+
*/
|
|
8
17
|
export interface TypegenMeta extends TypegenEnabled {
|
|
9
18
|
/**
|
|
10
19
|
* Allows you to specify all the results of state.matches
|
|
@@ -77,6 +86,9 @@ export interface TypegenMeta extends TypegenEnabled {
|
|
|
77
86
|
*/
|
|
78
87
|
eventsCausingActors: Record<string, string>;
|
|
79
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* @deprecated
|
|
91
|
+
*/
|
|
80
92
|
export interface ResolvedTypegenMeta extends TypegenMeta {
|
|
81
93
|
resolved: TypegenMeta & {
|
|
82
94
|
indexedActors: Record<string, ProvidedActor>;
|
|
@@ -86,16 +98,17 @@ export interface ResolvedTypegenMeta extends TypegenMeta {
|
|
|
86
98
|
indexedDelays: Record<string, ParameterizedObject>;
|
|
87
99
|
};
|
|
88
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* @deprecated
|
|
103
|
+
*/
|
|
89
104
|
export type TypegenConstraint = TypegenEnabled | TypegenDisabled;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
export type
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}>
|
|
98
|
-
]>;
|
|
105
|
+
/**
|
|
106
|
+
* @deprecated Always resolves to `true`
|
|
107
|
+
*/
|
|
108
|
+
export type AreAllImplementationsAssumedToBeProvided<_TResolvedTypesMeta, _TMissingImplementations> = true;
|
|
109
|
+
/**
|
|
110
|
+
* @deprecated
|
|
111
|
+
*/
|
|
99
112
|
interface AllImplementationsProvided {
|
|
100
113
|
missingImplementations: {
|
|
101
114
|
actions: never;
|
|
@@ -104,10 +117,6 @@ interface AllImplementationsProvided {
|
|
|
104
117
|
guards: never;
|
|
105
118
|
};
|
|
106
119
|
}
|
|
107
|
-
export interface MarkAllImplementationsAsProvided<TResolvedTypesMeta> {
|
|
108
|
-
'@@xstate/typegen': Prop<TResolvedTypesMeta, '@@xstate/typegen'>;
|
|
109
|
-
resolved: Prop<TResolvedTypesMeta, 'resolved'> & AllImplementationsProvided;
|
|
110
|
-
}
|
|
111
120
|
type GenerateActorEvents<TActor extends ProvidedActor, TInvokeSrcNameMap> = string extends TActor['src'] ? never : TActor extends any ? {
|
|
112
121
|
type: TActor['id'] extends string ? `xstate.done.actor.${TActor['id']}` : TActor['src'] extends keyof TInvokeSrcNameMap ? `xstate.done.actor.${TInvokeSrcNameMap[TActor['src']] & string}` : `xstate.done.actor.${string}`;
|
|
113
122
|
output: OutputFrom<TActor['logic']>;
|
|
@@ -123,6 +132,9 @@ type IndexParameterizedImplementation<TParameterizedImplementation extends Param
|
|
|
123
132
|
type WrapIntoParameterizedObject<T extends string> = T extends any ? {
|
|
124
133
|
type: T;
|
|
125
134
|
} : never;
|
|
135
|
+
/**
|
|
136
|
+
* @deprecated
|
|
137
|
+
*/
|
|
126
138
|
export interface ResolveTypegenMeta<TTypesMeta extends TypegenConstraint, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TTag extends string> {
|
|
127
139
|
'@@xstate/typegen': TTypesMeta['@@xstate/typegen'];
|
|
128
140
|
resolved: {
|