xstate 6.0.0-alpha.12 → 6.0.0-alpha.14
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/dist/{StateMachine-8a9e6cb4.development.esm.js → StateMachine-0fa9c094.development.esm.js} +42 -1
- package/dist/{StateMachine-97ef0e5e.esm.js → StateMachine-8dbd20ad.esm.js} +42 -1
- package/dist/{StateMachine-c7e1996c.development.cjs.js → StateMachine-98411dc5.development.cjs.js} +42 -1
- package/dist/{StateMachine-7af634f9.cjs.js → StateMachine-e3b1aa2d.cjs.js} +42 -1
- package/dist/declarations/src/State.d.ts +13 -2
- package/dist/declarations/src/StateMachine.d.ts +1 -0
- package/dist/declarations/src/fsm.d.ts +74 -0
- package/dist/declarations/src/index.d.ts +1 -0
- package/dist/declarations/src/setup.d.ts +59 -11
- package/dist/declarations/src/types.d.ts +27 -5
- package/dist/declarations/src/types.v6.d.ts +5 -5
- package/dist/{index-3d018b1e.development.cjs.js → index-07b19ed0.development.cjs.js} +23 -18
- package/dist/{index-698a3320.cjs.js → index-0d940f2c.cjs.js} +23 -18
- package/dist/{index-45856a94.esm.js → index-2635a437.esm.js} +20 -19
- package/dist/{index-be3d4c2d.development.esm.js → index-362125ce.development.esm.js} +20 -19
- package/dist/xstate-actors.cjs.js +1 -1
- package/dist/xstate-actors.development.cjs.js +1 -1
- package/dist/xstate-actors.development.esm.js +1 -1
- package/dist/xstate-actors.esm.js +1 -1
- package/dist/xstate-graph.cjs.js +2 -2
- package/dist/xstate-graph.development.cjs.js +2 -2
- package/dist/xstate-graph.development.esm.js +2 -2
- package/dist/xstate-graph.esm.js +2 -2
- package/dist/xstate-graph.umd.min.js +1 -1
- package/dist/xstate-graph.umd.min.js.map +1 -1
- package/dist/xstate.cjs.js +360 -4
- package/dist/xstate.cjs.mjs +1 -0
- package/dist/xstate.development.cjs.js +360 -4
- package/dist/xstate.development.cjs.mjs +1 -0
- package/dist/xstate.development.esm.js +363 -8
- package/dist/xstate.esm.js +363 -8
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/package.json +1 -1
package/dist/{StateMachine-8a9e6cb4.development.esm.js → StateMachine-0fa9c094.development.esm.js}
RENAMED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as ProcessingStatus,
|
|
1
|
+
import { P as ProcessingStatus, G as resolveReferencedActor, d as createActor, S as STATE_DELIMITER, H as mapValues, t as toArray, I as createInvokeId, J as getDelayedTransitions, K as evaluateCandidate, L as formatTransition, N as NULL_EVENT, M as toTransitionConfigArray, O as createInvokeTimeoutEvent, Q as getCandidates, R as formatRouteTransitions, U as resolveStateValue, V as getAllStateNodes, k as getStateNodes, W as createMachineSnapshot, Y as isInFinalState, e as macrostep, Z as cloneMachineSnapshot, _ as transitionNode, m as matchesEventDescriptor, r as resolveActionsWithContext, f as createInitEvent, g as initialMicrostep, $ as toStatePath, a0 as isStateId, a1 as getStateNodeByPath, a2 as getPersistedSnapshot, a3 as $$ACTOR_TYPE } from './index-362125ce.development.esm.js';
|
|
2
2
|
|
|
3
3
|
function createSpawner(actorScope, {
|
|
4
4
|
machine,
|
|
@@ -526,12 +526,53 @@ class StateMachine {
|
|
|
526
526
|
* @param event The received event
|
|
527
527
|
*/
|
|
528
528
|
transition(snapshot, event, actorScope) {
|
|
529
|
+
const fastSnapshot = this._transitionFast(snapshot, event, actorScope);
|
|
530
|
+
if (fastSnapshot) {
|
|
531
|
+
return [fastSnapshot, []];
|
|
532
|
+
}
|
|
529
533
|
const {
|
|
530
534
|
snapshot: nextSnapshot,
|
|
531
535
|
microsteps
|
|
532
536
|
} = macrostep(snapshot, event, actorScope, []);
|
|
533
537
|
return [nextSnapshot, microsteps.flatMap(([, actions]) => actions)];
|
|
534
538
|
}
|
|
539
|
+
_transitionFast(snapshot, event, actorScope) {
|
|
540
|
+
if (snapshot.status !== 'active' || typeof snapshot.value !== 'string' || this.root.always?.length) {
|
|
541
|
+
return undefined;
|
|
542
|
+
}
|
|
543
|
+
const sourceNode = this.root.states[snapshot.value];
|
|
544
|
+
if (!sourceNode || sourceNode.type !== 'atomic' || sourceNode.exit || sourceNode.invoke.length || sourceNode.always?.length || sourceNode.after?.length) {
|
|
545
|
+
return undefined;
|
|
546
|
+
}
|
|
547
|
+
const transitions = sourceNode.transitions.get(event.type);
|
|
548
|
+
if (transitions?.length !== 1) {
|
|
549
|
+
return undefined;
|
|
550
|
+
}
|
|
551
|
+
const selected = transitions[0];
|
|
552
|
+
if (selected.guard || selected.actions || selected.to || selected.reenter || selected.input || typeof selected.context === 'function' || selected.target && selected.target.length !== 1) {
|
|
553
|
+
return undefined;
|
|
554
|
+
}
|
|
555
|
+
const targetNode = selected.target?.[0] ?? sourceNode;
|
|
556
|
+
const stateChanged = targetNode !== sourceNode;
|
|
557
|
+
if (targetNode.parent !== this.root || targetNode.type !== 'atomic' || stateChanged && (targetNode.entry || targetNode.invoke.length || targetNode.always?.length || targetNode.after?.length)) {
|
|
558
|
+
return undefined;
|
|
559
|
+
}
|
|
560
|
+
const context = selected.context !== undefined ? {
|
|
561
|
+
...snapshot.context,
|
|
562
|
+
...selected.context
|
|
563
|
+
} : snapshot.context;
|
|
564
|
+
const collectedMicrosteps = actorScope.self._collectedMicrosteps || [];
|
|
565
|
+
collectedMicrosteps.push(selected);
|
|
566
|
+
actorScope.self._collectedMicrosteps = collectedMicrosteps;
|
|
567
|
+
return cloneMachineSnapshot(snapshot, {
|
|
568
|
+
...(context !== snapshot.context ? {
|
|
569
|
+
context
|
|
570
|
+
} : {}),
|
|
571
|
+
...(stateChanged ? {
|
|
572
|
+
_nodes: [this.root, targetNode]
|
|
573
|
+
} : {})
|
|
574
|
+
});
|
|
575
|
+
}
|
|
535
576
|
|
|
536
577
|
/**
|
|
537
578
|
* Determines the next state given the current `state` and `event`. Calculates
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as ProcessingStatus,
|
|
1
|
+
import { P as ProcessingStatus, G as resolveReferencedActor, d as createActor, S as STATE_DELIMITER, H as mapValues, t as toArray, I as createInvokeId, J as getDelayedTransitions, K as evaluateCandidate, L as formatTransition, N as NULL_EVENT, M as toTransitionConfigArray, O as createInvokeTimeoutEvent, Q as getCandidates, R as formatRouteTransitions, U as resolveStateValue, V as getAllStateNodes, k as getStateNodes, W as createMachineSnapshot, Y as isInFinalState, e as macrostep, Z as cloneMachineSnapshot, _ as transitionNode, m as matchesEventDescriptor, r as resolveActionsWithContext, f as createInitEvent, g as initialMicrostep, $ as toStatePath, a0 as isStateId, a1 as getStateNodeByPath, a2 as getPersistedSnapshot, a3 as $$ACTOR_TYPE } from './index-2635a437.esm.js';
|
|
2
2
|
|
|
3
3
|
function createSpawner(actorScope, {
|
|
4
4
|
machine,
|
|
@@ -512,12 +512,53 @@ class StateMachine {
|
|
|
512
512
|
* @param event The received event
|
|
513
513
|
*/
|
|
514
514
|
transition(snapshot, event, actorScope) {
|
|
515
|
+
const fastSnapshot = this._transitionFast(snapshot, event, actorScope);
|
|
516
|
+
if (fastSnapshot) {
|
|
517
|
+
return [fastSnapshot, []];
|
|
518
|
+
}
|
|
515
519
|
const {
|
|
516
520
|
snapshot: nextSnapshot,
|
|
517
521
|
microsteps
|
|
518
522
|
} = macrostep(snapshot, event, actorScope, []);
|
|
519
523
|
return [nextSnapshot, microsteps.flatMap(([, actions]) => actions)];
|
|
520
524
|
}
|
|
525
|
+
_transitionFast(snapshot, event, actorScope) {
|
|
526
|
+
if (snapshot.status !== 'active' || typeof snapshot.value !== 'string' || this.root.always?.length) {
|
|
527
|
+
return undefined;
|
|
528
|
+
}
|
|
529
|
+
const sourceNode = this.root.states[snapshot.value];
|
|
530
|
+
if (!sourceNode || sourceNode.type !== 'atomic' || sourceNode.exit || sourceNode.invoke.length || sourceNode.always?.length || sourceNode.after?.length) {
|
|
531
|
+
return undefined;
|
|
532
|
+
}
|
|
533
|
+
const transitions = sourceNode.transitions.get(event.type);
|
|
534
|
+
if (transitions?.length !== 1) {
|
|
535
|
+
return undefined;
|
|
536
|
+
}
|
|
537
|
+
const selected = transitions[0];
|
|
538
|
+
if (selected.guard || selected.actions || selected.to || selected.reenter || selected.input || typeof selected.context === 'function' || selected.target && selected.target.length !== 1) {
|
|
539
|
+
return undefined;
|
|
540
|
+
}
|
|
541
|
+
const targetNode = selected.target?.[0] ?? sourceNode;
|
|
542
|
+
const stateChanged = targetNode !== sourceNode;
|
|
543
|
+
if (targetNode.parent !== this.root || targetNode.type !== 'atomic' || stateChanged && (targetNode.entry || targetNode.invoke.length || targetNode.always?.length || targetNode.after?.length)) {
|
|
544
|
+
return undefined;
|
|
545
|
+
}
|
|
546
|
+
const context = selected.context !== undefined ? {
|
|
547
|
+
...snapshot.context,
|
|
548
|
+
...selected.context
|
|
549
|
+
} : snapshot.context;
|
|
550
|
+
const collectedMicrosteps = actorScope.self._collectedMicrosteps || [];
|
|
551
|
+
collectedMicrosteps.push(selected);
|
|
552
|
+
actorScope.self._collectedMicrosteps = collectedMicrosteps;
|
|
553
|
+
return cloneMachineSnapshot(snapshot, {
|
|
554
|
+
...(context !== snapshot.context ? {
|
|
555
|
+
context
|
|
556
|
+
} : {}),
|
|
557
|
+
...(stateChanged ? {
|
|
558
|
+
_nodes: [this.root, targetNode]
|
|
559
|
+
} : {})
|
|
560
|
+
});
|
|
561
|
+
}
|
|
521
562
|
|
|
522
563
|
/**
|
|
523
564
|
* Determines the next state given the current `state` and `event`. Calculates
|
package/dist/{StateMachine-c7e1996c.development.cjs.js → StateMachine-98411dc5.development.cjs.js}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var dist_xstateActors = require('./index-
|
|
3
|
+
var dist_xstateActors = require('./index-07b19ed0.development.cjs.js');
|
|
4
4
|
|
|
5
5
|
function createSpawner(actorScope, {
|
|
6
6
|
machine,
|
|
@@ -528,12 +528,53 @@ class StateMachine {
|
|
|
528
528
|
* @param event The received event
|
|
529
529
|
*/
|
|
530
530
|
transition(snapshot, event, actorScope) {
|
|
531
|
+
const fastSnapshot = this._transitionFast(snapshot, event, actorScope);
|
|
532
|
+
if (fastSnapshot) {
|
|
533
|
+
return [fastSnapshot, []];
|
|
534
|
+
}
|
|
531
535
|
const {
|
|
532
536
|
snapshot: nextSnapshot,
|
|
533
537
|
microsteps
|
|
534
538
|
} = dist_xstateActors.macrostep(snapshot, event, actorScope, []);
|
|
535
539
|
return [nextSnapshot, microsteps.flatMap(([, actions]) => actions)];
|
|
536
540
|
}
|
|
541
|
+
_transitionFast(snapshot, event, actorScope) {
|
|
542
|
+
if (snapshot.status !== 'active' || typeof snapshot.value !== 'string' || this.root.always?.length) {
|
|
543
|
+
return undefined;
|
|
544
|
+
}
|
|
545
|
+
const sourceNode = this.root.states[snapshot.value];
|
|
546
|
+
if (!sourceNode || sourceNode.type !== 'atomic' || sourceNode.exit || sourceNode.invoke.length || sourceNode.always?.length || sourceNode.after?.length) {
|
|
547
|
+
return undefined;
|
|
548
|
+
}
|
|
549
|
+
const transitions = sourceNode.transitions.get(event.type);
|
|
550
|
+
if (transitions?.length !== 1) {
|
|
551
|
+
return undefined;
|
|
552
|
+
}
|
|
553
|
+
const selected = transitions[0];
|
|
554
|
+
if (selected.guard || selected.actions || selected.to || selected.reenter || selected.input || typeof selected.context === 'function' || selected.target && selected.target.length !== 1) {
|
|
555
|
+
return undefined;
|
|
556
|
+
}
|
|
557
|
+
const targetNode = selected.target?.[0] ?? sourceNode;
|
|
558
|
+
const stateChanged = targetNode !== sourceNode;
|
|
559
|
+
if (targetNode.parent !== this.root || targetNode.type !== 'atomic' || stateChanged && (targetNode.entry || targetNode.invoke.length || targetNode.always?.length || targetNode.after?.length)) {
|
|
560
|
+
return undefined;
|
|
561
|
+
}
|
|
562
|
+
const context = selected.context !== undefined ? {
|
|
563
|
+
...snapshot.context,
|
|
564
|
+
...selected.context
|
|
565
|
+
} : snapshot.context;
|
|
566
|
+
const collectedMicrosteps = actorScope.self._collectedMicrosteps || [];
|
|
567
|
+
collectedMicrosteps.push(selected);
|
|
568
|
+
actorScope.self._collectedMicrosteps = collectedMicrosteps;
|
|
569
|
+
return dist_xstateActors.cloneMachineSnapshot(snapshot, {
|
|
570
|
+
...(context !== snapshot.context ? {
|
|
571
|
+
context
|
|
572
|
+
} : {}),
|
|
573
|
+
...(stateChanged ? {
|
|
574
|
+
_nodes: [this.root, targetNode]
|
|
575
|
+
} : {})
|
|
576
|
+
});
|
|
577
|
+
}
|
|
537
578
|
|
|
538
579
|
/**
|
|
539
580
|
* Determines the next state given the current `state` and `event`. Calculates
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var dist_xstateActors = require('./index-
|
|
3
|
+
var dist_xstateActors = require('./index-0d940f2c.cjs.js');
|
|
4
4
|
|
|
5
5
|
function createSpawner(actorScope, {
|
|
6
6
|
machine,
|
|
@@ -514,12 +514,53 @@ class StateMachine {
|
|
|
514
514
|
* @param event The received event
|
|
515
515
|
*/
|
|
516
516
|
transition(snapshot, event, actorScope) {
|
|
517
|
+
const fastSnapshot = this._transitionFast(snapshot, event, actorScope);
|
|
518
|
+
if (fastSnapshot) {
|
|
519
|
+
return [fastSnapshot, []];
|
|
520
|
+
}
|
|
517
521
|
const {
|
|
518
522
|
snapshot: nextSnapshot,
|
|
519
523
|
microsteps
|
|
520
524
|
} = dist_xstateActors.macrostep(snapshot, event, actorScope, []);
|
|
521
525
|
return [nextSnapshot, microsteps.flatMap(([, actions]) => actions)];
|
|
522
526
|
}
|
|
527
|
+
_transitionFast(snapshot, event, actorScope) {
|
|
528
|
+
if (snapshot.status !== 'active' || typeof snapshot.value !== 'string' || this.root.always?.length) {
|
|
529
|
+
return undefined;
|
|
530
|
+
}
|
|
531
|
+
const sourceNode = this.root.states[snapshot.value];
|
|
532
|
+
if (!sourceNode || sourceNode.type !== 'atomic' || sourceNode.exit || sourceNode.invoke.length || sourceNode.always?.length || sourceNode.after?.length) {
|
|
533
|
+
return undefined;
|
|
534
|
+
}
|
|
535
|
+
const transitions = sourceNode.transitions.get(event.type);
|
|
536
|
+
if (transitions?.length !== 1) {
|
|
537
|
+
return undefined;
|
|
538
|
+
}
|
|
539
|
+
const selected = transitions[0];
|
|
540
|
+
if (selected.guard || selected.actions || selected.to || selected.reenter || selected.input || typeof selected.context === 'function' || selected.target && selected.target.length !== 1) {
|
|
541
|
+
return undefined;
|
|
542
|
+
}
|
|
543
|
+
const targetNode = selected.target?.[0] ?? sourceNode;
|
|
544
|
+
const stateChanged = targetNode !== sourceNode;
|
|
545
|
+
if (targetNode.parent !== this.root || targetNode.type !== 'atomic' || stateChanged && (targetNode.entry || targetNode.invoke.length || targetNode.always?.length || targetNode.after?.length)) {
|
|
546
|
+
return undefined;
|
|
547
|
+
}
|
|
548
|
+
const context = selected.context !== undefined ? {
|
|
549
|
+
...snapshot.context,
|
|
550
|
+
...selected.context
|
|
551
|
+
} : snapshot.context;
|
|
552
|
+
const collectedMicrosteps = actorScope.self._collectedMicrosteps || [];
|
|
553
|
+
collectedMicrosteps.push(selected);
|
|
554
|
+
actorScope.self._collectedMicrosteps = collectedMicrosteps;
|
|
555
|
+
return dist_xstateActors.cloneMachineSnapshot(snapshot, {
|
|
556
|
+
...(context !== snapshot.context ? {
|
|
557
|
+
context
|
|
558
|
+
} : {}),
|
|
559
|
+
...(stateChanged ? {
|
|
560
|
+
_nodes: [this.root, targetNode]
|
|
561
|
+
} : {})
|
|
562
|
+
});
|
|
563
|
+
}
|
|
523
564
|
|
|
524
565
|
/**
|
|
525
566
|
* Determines the next state given the current `state` and `event`. Calculates
|
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
import type { AnyMachineSnapshot, AnyStateMachine, EventObject, HistoryValue, MachineContext, StateConfig, StateValue, AnyActorRef, Snapshot, MetaObject, StateSchema, StateId, StateIdInputs, StateContextFromStateValue, SnapshotStatus, AnyStateNode } from "./types.js";
|
|
1
|
+
import type { AnyMachineSnapshot, AnyStateMachine, EventObject, HistoryValue, MachineContext, StateConfig, StateValue, StateValueMap, AnyActorRef, Snapshot, MetaObject, StateSchema, StateId, StateIdInputs, StateContextFromStateValue, SnapshotStatus, AnyStateNode } from "./types.js";
|
|
2
2
|
export declare function isMachineSnapshot(value: unknown): value is AnyMachineSnapshot;
|
|
3
|
+
type Values<T> = T[keyof T];
|
|
4
|
+
type MatchingObjectStateValue<TStateValue extends Record<string, unknown>, TTestStateValue extends Record<string, unknown>> = false extends Values<{
|
|
5
|
+
[K in keyof TTestStateValue]: K extends keyof TStateValue ? NonNullable<TStateValue[K]> extends StateValue ? NonNullable<TTestStateValue[K]> extends StateValue ? [
|
|
6
|
+
MatchingStateValue<NonNullable<TStateValue[K]>, NonNullable<TTestStateValue[K]>>
|
|
7
|
+
] extends [never] ? false : true : false : false : false;
|
|
8
|
+
}> ? never : {
|
|
9
|
+
[K in keyof TStateValue]: K extends keyof TTestStateValue ? MatchingStateValue<NonNullable<TStateValue[K]>, NonNullable<TTestStateValue[K]>> : TStateValue[K];
|
|
10
|
+
};
|
|
11
|
+
type MatchingStateValue<TStateValue extends StateValue, TTestStateValue extends StateValue> = StateValue extends TTestStateValue ? TStateValue : string extends TTestStateValue ? TStateValue : TStateValue extends unknown ? TTestStateValue extends string ? TStateValue extends string ? TTestStateValue extends TStateValue ? TTestStateValue : Extract<TStateValue, TTestStateValue> : TStateValue extends Record<string, unknown> ? TStateValue & Record<TTestStateValue, StateValue | undefined> : never : TTestStateValue extends Record<string, unknown> ? TStateValue extends Record<string, unknown> ? MatchingObjectStateValue<TStateValue, TTestStateValue> : never : never : never;
|
|
3
12
|
interface MachineSnapshotBase<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, _TOutput, TMeta extends MetaObject, TStateSchema extends StateSchema = StateSchema> {
|
|
4
13
|
/** The state machine that produced this state snapshot. */
|
|
5
14
|
machine: AnyStateMachine;
|
|
@@ -42,7 +51,9 @@ interface MachineSnapshotBase<TContext extends MachineContext, TEvent extends Ev
|
|
|
42
51
|
*
|
|
43
52
|
* @param partialStateValue
|
|
44
53
|
*/
|
|
45
|
-
matches<const TTestStateValue extends
|
|
54
|
+
matches<const TTestStateValue extends string>(partialStateValue: TTestStateValue, ...args: string extends TTestStateValue ? [never] : []): this is MachineSnapshot<StateContextFromStateValue<TStateSchema, TContext, TTestStateValue>, TEvent, TChildren, MatchingStateValue<TStateValue, TTestStateValue>, TTag, _TOutput, TMeta, TStateSchema>;
|
|
55
|
+
matches<const TTestStateValue extends StateValueMap>(partialStateValue: TTestStateValue, ...args: string extends keyof TTestStateValue ? [never] : []): this is MachineSnapshot<StateContextFromStateValue<TStateSchema, TContext, TTestStateValue>, TEvent, TChildren, MatchingStateValue<TStateValue, TTestStateValue>, TTag, _TOutput, TMeta, TStateSchema>;
|
|
56
|
+
matches(partialStateValue: StateValue): boolean;
|
|
46
57
|
/**
|
|
47
58
|
* Whether the current state nodes has a state node with the specified `tag`.
|
|
48
59
|
*
|
|
@@ -63,6 +63,7 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
|
|
|
63
63
|
* @param event The received event
|
|
64
64
|
*/
|
|
65
65
|
transition(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, event: TEvent, actorScope: ActorScope<typeof snapshot, TEvent, AnyActorSystem, TEmitted>): ActorLogicTransitionResult<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, ExecutableActionObjectFromLogic<this>>;
|
|
66
|
+
private _transitionFast;
|
|
66
67
|
/**
|
|
67
68
|
* Determines the next state given the current `state` and `event`. Calculates
|
|
68
69
|
* a microstep.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { createTransitionEnqueue } from "./transitionActions.js";
|
|
2
|
+
import type { ActorLogic, EventObject, MachineContext, NonReducibleUnknown, Snapshot } from "./types.js";
|
|
3
|
+
export type FSMSnapshot<TContext extends MachineContext, TState extends string, TInput = unknown> = Snapshot<undefined> & {
|
|
4
|
+
value: TState;
|
|
5
|
+
context: TContext;
|
|
6
|
+
input: TInput | undefined;
|
|
7
|
+
children: {};
|
|
8
|
+
_stateInput: Record<string, unknown> | undefined;
|
|
9
|
+
machine: {
|
|
10
|
+
id: string;
|
|
11
|
+
implementations: {
|
|
12
|
+
actions: {};
|
|
13
|
+
actorSources: {};
|
|
14
|
+
guards: {};
|
|
15
|
+
delays: {};
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
type FSMArgs<TContext extends MachineContext, TEvent extends EventObject, TState extends string, TInput> = {
|
|
20
|
+
context: TContext;
|
|
21
|
+
event: TEvent;
|
|
22
|
+
input: TInput | undefined;
|
|
23
|
+
value: TState;
|
|
24
|
+
self: any;
|
|
25
|
+
system: any;
|
|
26
|
+
parent: any;
|
|
27
|
+
children: {};
|
|
28
|
+
};
|
|
29
|
+
type FSMAction<TContext extends MachineContext, TEvent extends EventObject, TState extends string, TInput> = (args: FSMArgs<TContext, TEvent, TState, TInput> & {
|
|
30
|
+
input: Record<string, unknown> | undefined;
|
|
31
|
+
}, enq: ReturnType<typeof createTransitionEnqueue>) => void | {
|
|
32
|
+
context?: FSMContextPatch<TContext>;
|
|
33
|
+
};
|
|
34
|
+
type FSMGuard<TContext extends MachineContext, TEvent extends EventObject, TState extends string, TInput> = (args: FSMArgs<TContext, TEvent, TState, TInput>) => boolean;
|
|
35
|
+
type FSMContextPatch<TContext extends MachineContext> = Partial<TContext> & {
|
|
36
|
+
call?: never;
|
|
37
|
+
apply?: never;
|
|
38
|
+
bind?: never;
|
|
39
|
+
};
|
|
40
|
+
type FSMContextMapper<TContext extends MachineContext, TEvent extends EventObject, TState extends string, TInput> = (args: FSMArgs<TContext, TEvent, TState, TInput>) => FSMContextPatch<TContext>;
|
|
41
|
+
type FSMTarget<TContext extends MachineContext> = {
|
|
42
|
+
target?: string;
|
|
43
|
+
context?: FSMContextPatch<TContext>;
|
|
44
|
+
input?: Record<string, unknown> | ((args: {
|
|
45
|
+
context: TContext;
|
|
46
|
+
event: EventObject;
|
|
47
|
+
}) => Record<string, unknown>);
|
|
48
|
+
};
|
|
49
|
+
type FSMObjectTarget<TContext extends MachineContext, TEvent extends EventObject, TState extends string, TInput> = Omit<FSMTarget<TContext>, 'context'> & {
|
|
50
|
+
context?: FSMContextPatch<TContext> | FSMContextMapper<TContext, TEvent, TState, TInput>;
|
|
51
|
+
};
|
|
52
|
+
type FSMTransitionFunction<TContext extends MachineContext, TEvent extends EventObject, TState extends string, TInput> = (args: FSMArgs<TContext, TEvent, TState, TInput>, enq: ReturnType<typeof createTransitionEnqueue>) => void | false | FSMTarget<TContext>;
|
|
53
|
+
type FSMTransition<TContext extends MachineContext, TEvent extends EventObject, TState extends string, TInput> = FSMObjectTarget<TContext, TEvent, TState, TInput> | (FSMObjectTarget<TContext, TEvent, TState, TInput> & {
|
|
54
|
+
guard?: FSMGuard<TContext, TEvent, TState, TInput>;
|
|
55
|
+
actions?: FSMAction<TContext, TEvent, TState, TInput> | Array<FSMAction<TContext, TEvent, TState, TInput>>;
|
|
56
|
+
}) | FSMTransitionFunction<TContext, TEvent, TState, TInput>;
|
|
57
|
+
type FSMStateConfig<TContext extends MachineContext, TEvent extends EventObject, TState extends string, TInput> = {
|
|
58
|
+
entry?: FSMAction<TContext, TEvent, TState, TInput> | Array<FSMAction<TContext, TEvent, TState, TInput>>;
|
|
59
|
+
exit?: FSMAction<TContext, TEvent, TState, TInput> | Array<FSMAction<TContext, TEvent, TState, TInput>>;
|
|
60
|
+
on?: Record<string, FSMTransition<TContext, TEvent, TState, TInput> | Array<FSMTransition<TContext, TEvent, TState, TInput>>>;
|
|
61
|
+
};
|
|
62
|
+
export type FSMConfig<TContext extends MachineContext, TEvent extends EventObject, TState extends string, TInput = NonReducibleUnknown> = {
|
|
63
|
+
id?: string;
|
|
64
|
+
initial: TState;
|
|
65
|
+
context?: TContext | ((args: {
|
|
66
|
+
input: TInput;
|
|
67
|
+
}) => TContext);
|
|
68
|
+
states: Record<TState, FSMStateConfig<TContext, TEvent, TState, TInput>>;
|
|
69
|
+
};
|
|
70
|
+
export type FSMActorLogic<TContext extends MachineContext, TEvent extends EventObject, TState extends string, TInput> = ActorLogic<FSMSnapshot<TContext, TState, TInput>, TEvent, TInput, any, EventObject> & {
|
|
71
|
+
id?: string;
|
|
72
|
+
};
|
|
73
|
+
export declare function createFSM<TContext extends MachineContext, TEvent extends EventObject, TInput = NonReducibleUnknown>(config: FSMConfig<TContext, TEvent, string, TInput>): FSMActorLogic<TContext, TEvent, string, TInput>;
|
|
74
|
+
export {};
|
|
@@ -2,6 +2,7 @@ export * from "./actors/index.js";
|
|
|
2
2
|
export { assertEvent } from "./assert.js";
|
|
3
3
|
export { Actor, createActor, type RequiredActorOptionsKeys as RequiredActorOptionsKeys } from "./createActor.js";
|
|
4
4
|
export { createMachine, createStateConfig } from "./createMachine.js";
|
|
5
|
+
export { createFSM, type FSMActorLogic, type FSMConfig, type FSMSnapshot } from "./fsm.js";
|
|
5
6
|
export { createMachineFromConfig } from "./createMachineFromConfig.js";
|
|
6
7
|
export type { ActionJSON, GuardJSON, InvokeJSON, MachineJSON, StateNodeJSON, TransitionJSON } from "./createMachineFromConfig.js";
|
|
7
8
|
export { machineConfigToJSON, serializeMachine, type CodeExpression } from "./serialize.js";
|
|
@@ -80,6 +80,29 @@ type SetupOrConfigSchemaMap<TSchemas, TKey extends 'events' | 'emitted' | 'child
|
|
|
80
80
|
type SetupStateKeys<TStateSchemas extends Record<string, SetupStateSchema>> = keyof TStateSchemas & string;
|
|
81
81
|
type SetupStateKey<TStateSchemas extends Record<string, SetupStateSchema>> = string extends SetupStateKeys<TStateSchemas> ? string : [SetupStateKeys<TStateSchemas>] extends [never] ? string : SetupStateKeys<TStateSchemas>;
|
|
82
82
|
type SetupStateTarget<TStateSchemas extends Record<string, SetupStateSchema>> = string extends SetupStateKeys<TStateSchemas> ? string : [SetupStateKeys<TStateSchemas>] extends [never] ? string : SetupStateKeys<TStateSchemas> | `.${string}` | `#${string}`;
|
|
83
|
+
/**
|
|
84
|
+
* The sibling state schemas of a dotted path: the children of the path's
|
|
85
|
+
* parent. A bare transition target resolves relative to the parent, so the
|
|
86
|
+
* valid targets for a path's config are its siblings. For a dotless (top-level)
|
|
87
|
+
* path the siblings are the root states.
|
|
88
|
+
*/
|
|
89
|
+
type ResolveStateSiblings<TStates extends Record<string, SetupStateSchema>, TPath extends string> = TPath extends `${infer Head}.${infer Rest}` ? Head extends keyof TStates ? TStates[Head]['states'] extends Record<string, SetupStateSchema> ? ResolveStateSiblings<TStates[Head]['states'], Rest> : never : never : TStates;
|
|
90
|
+
/**
|
|
91
|
+
* Resolves a dotted state path (e.g. `'parent.child'`) into the leaf
|
|
92
|
+
* `SetupStateSchema` it addresses, or `never` if any segment is missing.
|
|
93
|
+
*/
|
|
94
|
+
type ResolveStatePath<TStates extends Record<string, SetupStateSchema>, TPath extends string> = TPath extends `${infer Head}.${infer Rest}` ? Head extends keyof TStates ? TStates[Head]['states'] extends Record<string, SetupStateSchema> ? ResolveStatePath<TStates[Head]['states'], Rest> : never : never : TPath extends keyof TStates ? TStates[TPath] : never;
|
|
95
|
+
/** Union of every addressable dotted path into a setup states tree */
|
|
96
|
+
type StatePathsInner<TStates extends Record<string, SetupStateSchema>> = {
|
|
97
|
+
[K in SetupStateKeys<TStates>]: K | (TStates[K]['states'] extends Record<string, SetupStateSchema> ? `${K}.${StatePathsInner<TStates[K]['states']>}` : never);
|
|
98
|
+
}[SetupStateKeys<TStates>];
|
|
99
|
+
type StatePaths<TStates extends Record<string, SetupStateSchema>> = string extends SetupStateKeys<TStates> ? string : [SetupStateKeys<TStates>] extends [never] ? string : StatePathsInner<TStates>;
|
|
100
|
+
/**
|
|
101
|
+
* Shared body of both `createStateConfig` overloads: the
|
|
102
|
+
* `StateNodeConfigWithNestedInput` instantiation whose only varying parts are
|
|
103
|
+
* the leading sibling-schemas and state-schema arguments.
|
|
104
|
+
*/
|
|
105
|
+
type SetupStateNodeConfig<TStates extends Record<string, SetupStateSchema>, TStateSchema extends SetupStateSchema, TSchemas extends SetupSchemas, TSetupActionMap extends Implementations['actions'], TSetupActorMap extends Implementations['actorSources'], TSetupGuardMap extends Implementations['guards'], TSetupDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = StateNodeConfigWithNestedInput<TStates, TStateSchema, SetupContext<TSchemas, StandardSchemaV1>, SetupContextShape<TSchemas, StandardSchemaV1, SetupContext<TSchemas, StandardSchemaV1>>, SetupEvents<TSchemas, Record<string, StandardSchemaV1>>, Cast<SetupChildren<TSchemas, Record<string, StandardSchemaV1>>, Record<string, AnyActorRef | undefined>>, string, SetupTags<TSchemas, StandardSchemaV1>, SetupOutput<TSchemas, StandardSchemaV1>, SetupEmitted<TSchemas, Record<string, StandardSchemaV1>>, SetupMeta<TSchemas, StandardSchemaV1>, SetupActions<TSchemas, TSetupActionMap>, TSetupActorMap, SetupGuards<TSchemas, TSetupGuardMap>, TSetupDelayMap, TSystemRegistry>;
|
|
83
106
|
type InvalidSetupStateKeys<TConfig, TStateSchemas extends Record<string, SetupStateSchema>> = string extends SetupStateKeys<TStateSchemas> ? never : [SetupStateKeys<TStateSchemas>] extends [never] ? never : TConfig extends {
|
|
84
107
|
states: infer TStates;
|
|
85
108
|
} ? Exclude<keyof TStates & string, SetupStateKeys<TStateSchemas>> : never;
|
|
@@ -242,12 +265,12 @@ type StateNodeConfigWithNestedInput<TSiblingStateSchemas extends Record<string,
|
|
|
242
265
|
type StateTransitions<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TEvent extends EventObject, TEmitted extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TMeta extends MetaObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = {
|
|
243
266
|
[K in EventDescriptor<TEvent>]?: StateTransitionConfigOrTarget<TStateSchemas, TContext, TContextShape, ExtractEvent<TEvent, K>, TEvent, TEmitted, TChildren, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
|
|
244
267
|
};
|
|
245
|
-
type StateTransitionConfigOrTarget<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TExpressionEvent extends EventObject, TEvent extends EventObject, TEmitted extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TMeta extends MetaObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = undefined | StateTransitionObjectConfig<TStateSchemas, TContext, TContextShape, TExpressionEvent, TMeta> | StateTransitionFunction<TStateSchemas, TContext, TContextShape, TExpressionEvent, TEvent, TEmitted, TChildren, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
|
|
246
|
-
type StateTransitionObjectConfig<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TExpressionEvent extends EventObject, TMeta extends MetaObject> = (StateTransitionResult<TStateSchemas, TContext, TContextShape, TMeta> & {
|
|
268
|
+
type StateTransitionConfigOrTarget<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TExpressionEvent extends EventObject, TEvent extends EventObject, TEmitted extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TMeta extends MetaObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = undefined | StateTransitionObjectConfig<TStateSchemas, TContext, TContextShape, TExpressionEvent, TChildren, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry> | StateTransitionFunction<TStateSchemas, TContext, TContextShape, TExpressionEvent, TEvent, TEmitted, TChildren, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
|
|
269
|
+
type StateTransitionObjectConfig<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TExpressionEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TMeta extends MetaObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = (StateTransitionResult<TStateSchemas, TContext, TContextShape, TMeta, TExpressionEvent, TChildren, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry, true> & {
|
|
247
270
|
description?: string;
|
|
248
271
|
}) | {
|
|
249
272
|
target: SetupStateTarget<TStateSchemas>[];
|
|
250
|
-
context?:
|
|
273
|
+
context?: StateTransitionContext<true, TContext, TContextShape, TContextShape, TContext, TExpressionEvent, TChildren, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
|
|
251
274
|
description?: string;
|
|
252
275
|
reenter?: boolean;
|
|
253
276
|
meta?: TMeta;
|
|
@@ -256,6 +279,21 @@ type StateTransitionObjectConfig<TStateSchemas extends Record<string, SetupState
|
|
|
256
279
|
event: TExpressionEvent;
|
|
257
280
|
} & OutputArg<TExpressionEvent>) => Record<string, unknown>);
|
|
258
281
|
};
|
|
282
|
+
type StateTransitionContextMapper<TContext extends MachineContext, TContextShape, TTargetContextShape, TResolvedTargetContext extends MachineContext, TExpressionEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = (args: {
|
|
283
|
+
context: TContext;
|
|
284
|
+
event: TExpressionEvent;
|
|
285
|
+
self: AnyActorRef;
|
|
286
|
+
parent: AnyActorRef | undefined;
|
|
287
|
+
value: StateValue;
|
|
288
|
+
children: TChildren;
|
|
289
|
+
system: SystemRuntime<TSystemRegistry>;
|
|
290
|
+
actions: TActionMap;
|
|
291
|
+
actorSources: TActorMap;
|
|
292
|
+
guards: TGuardMap;
|
|
293
|
+
delays: TDelayMap;
|
|
294
|
+
} & OutputArg<TExpressionEvent>) => ContextPatch<TContextShape, TTargetContextShape, TResolvedTargetContext>;
|
|
295
|
+
type StateTransitionContextOrMapper<TContext extends MachineContext, TContextShape, TTargetContextShape, TResolvedTargetContext extends MachineContext, TExpressionEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = ContextPatch<TContextShape, TTargetContextShape, TResolvedTargetContext> | StateTransitionContextMapper<TContext, TContextShape, TTargetContextShape, TResolvedTargetContext, TExpressionEvent, TChildren, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
|
|
296
|
+
type StateTransitionContext<TAllowMapper extends boolean, TContext extends MachineContext, TContextShape, TTargetContextShape, TResolvedTargetContext extends MachineContext, TExpressionEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = TAllowMapper extends true ? StateTransitionContextOrMapper<TContext, TContextShape, TTargetContextShape, TResolvedTargetContext, TExpressionEvent, TChildren, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry> : ContextPatch<TContextShape, TTargetContextShape, TResolvedTargetContext>;
|
|
259
297
|
type StateTransitionFunction<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TExpressionEvent extends EventObject, TEvent extends EventObject, TEmitted extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TMeta extends MetaObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = (args: {
|
|
260
298
|
context: TContext;
|
|
261
299
|
event: TExpressionEvent;
|
|
@@ -268,10 +306,10 @@ type StateTransitionFunction<TStateSchemas extends Record<string, SetupStateSche
|
|
|
268
306
|
actorSources: TActorMap;
|
|
269
307
|
guards: TGuardMap;
|
|
270
308
|
delays: TDelayMap;
|
|
271
|
-
} & OutputArg<TExpressionEvent>, enq: EnqueueObject<TEvent, TEmitted, TSystemRegistry>) => StateTransitionResult<TStateSchemas, TContext, TContextShape, TMeta> | void;
|
|
272
|
-
type StateTransitionResult<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TMeta extends MetaObject> = {
|
|
309
|
+
} & OutputArg<TExpressionEvent>, enq: EnqueueObject<TEvent, TEmitted, TSystemRegistry>) => StateTransitionResult<TStateSchemas, TContext, TContextShape, TMeta, TExpressionEvent, TChildren, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry, false> | void;
|
|
310
|
+
type StateTransitionResult<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TMeta extends MetaObject, TExpressionEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry, TAllowContextMapper extends boolean> = {
|
|
273
311
|
target?: never;
|
|
274
|
-
context?:
|
|
312
|
+
context?: StateTransitionContext<TAllowContextMapper, TContext, TContextShape, TContextShape, TContext, TExpressionEvent, TChildren, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
|
|
275
313
|
reenter?: boolean;
|
|
276
314
|
meta?: TMeta;
|
|
277
315
|
} | {
|
|
@@ -286,17 +324,21 @@ type StateTransitionResult<TStateSchemas extends Record<string, SetupStateSchema
|
|
|
286
324
|
} & ([TContextShape] extends [
|
|
287
325
|
StateContextShape<TStateSchemas[K], TContextShape>
|
|
288
326
|
] ? {
|
|
289
|
-
context?:
|
|
327
|
+
context?: StateTransitionContext<TAllowContextMapper, TContext, TContextShape, StateContextShape<TStateSchemas[K], TContextShape>, StateContext<TStateSchemas[K], TContext>, TExpressionEvent, TChildren, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
|
|
290
328
|
} : {
|
|
291
|
-
context:
|
|
329
|
+
context: StateTransitionContext<TAllowContextMapper, TContext, TContextShape, StateContextShape<TStateSchemas[K], TContextShape>, StateContext<TStateSchemas[K], TContext>, TExpressionEvent, TChildren, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
|
|
292
330
|
});
|
|
293
331
|
}[keyof TStateSchemas & string] | {
|
|
294
332
|
target: Exclude<SetupStateTarget<TStateSchemas>, keyof TStateSchemas & string>;
|
|
295
|
-
context?:
|
|
333
|
+
context?: StateTransitionContext<TAllowContextMapper, TContext, TContextShape, TContextShape, TContext, TExpressionEvent, TChildren, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
|
|
296
334
|
reenter?: boolean;
|
|
297
335
|
meta?: TMeta;
|
|
298
336
|
};
|
|
299
|
-
type ContextPatch<TCurrentContext, TTargetContext, TResolvedTargetContext extends MachineContext> = Compute<Partial<TResolvedTargetContext> & Pick<TResolvedTargetContext, Extract<RequiredContextKeys<TCurrentContext, TTargetContext>, string
|
|
337
|
+
type ContextPatch<TCurrentContext, TTargetContext, TResolvedTargetContext extends MachineContext> = Compute<Partial<TResolvedTargetContext> & Pick<TResolvedTargetContext, Extract<RequiredContextKeys<TCurrentContext, TTargetContext>, string>> & {
|
|
338
|
+
call?: never;
|
|
339
|
+
apply?: never;
|
|
340
|
+
bind?: never;
|
|
341
|
+
}>;
|
|
300
342
|
type RequiredContextKeys<TCurrentContext, TTargetContext> = {
|
|
301
343
|
[K in keyof TTargetContext]-?: K extends keyof TCurrentContext ? [TCurrentContext[K]] extends [TTargetContext[K]] ? never : K : K;
|
|
302
344
|
}[keyof TTargetContext];
|
|
@@ -338,8 +380,14 @@ export interface SetupReturn<TStates extends Record<string, SetupStateSchema> =
|
|
|
338
380
|
}), Cast<MergeChildren<SetupChildren<TSchemas, TChildrenSchemaMap>, TActor>, Record<string, AnyActorRef | undefined>>, StateValue, TTag & string, [
|
|
339
381
|
SetupSchema<TSchemas, 'input'>
|
|
340
382
|
] extends [never] ? TInput : SetupInput<TSchemas, TInputSchema>, SetupOutput<TSchemas, TOutputSchema>, SetupEmitted<TSchemas, TEmittedSchemaMap>, SetupMeta<TSchemas, TMetaSchema>, MergeStateSchema<Cast<TConfig, StateSchema>, SetupStatesToStateSchema<TStates>>, MergeImplementationMaps<SetupActions<TSchemas, TSetupActionMap>, MergeImplementationMaps<InferActions<TActionSchemaMap>, TActionMap>>, MergeImplementationMaps<TSetupActorMap, TActorMap>, MergeImplementationMaps<SetupGuards<TSchemas, TSetupGuardMap>, MergeImplementationMaps<InferGuards<TGuardSchemaMap>, TGuardMap>>, DelayMapFromNames<TSetupDelays | TDelays, MergeImplementationMaps<TSetupDelayMap, TDelayMap>>>;
|
|
383
|
+
/**
|
|
384
|
+
* Creates a state node config bound to a specific setup-declared state,
|
|
385
|
+
* addressed by a dotted path (e.g. `'loading'` or `'parent.child'`). The
|
|
386
|
+
* addressed state's own `input` schema is typed inside `entry`/`exit` args.
|
|
387
|
+
*/
|
|
388
|
+
createStateConfig<const TPath extends StatePaths<TStates>, const TConfig extends SetupStateNodeConfig<ResolveStateSiblings<TStates, TPath>, ResolveStatePath<TStates, TPath>, TSchemas, TSetupActionMap, TSetupActorMap, TSetupGuardMap, TSetupDelayMap, TSystemRegistry>>(path: TPath, config: TConfig): TConfig;
|
|
341
389
|
/** Creates a state node config with the setup configuration */
|
|
342
|
-
createStateConfig<const TConfig extends
|
|
390
|
+
createStateConfig<const TConfig extends SetupStateNodeConfig<TStates, SetupStateSchema, TSchemas, TSetupActionMap, TSetupActorMap, TSetupGuardMap, TSetupDelayMap, TSystemRegistry>>(config: TConfig): TConfig;
|
|
343
391
|
/** State input schemas from setup config */
|
|
344
392
|
states: TStates;
|
|
345
393
|
}
|
|
@@ -130,12 +130,18 @@ export interface StateValueMap {
|
|
|
130
130
|
*/
|
|
131
131
|
export type StateValue = string | StateValueMap;
|
|
132
132
|
export type TransitionTarget = SingleOrArray<string>;
|
|
133
|
+
export type TransitionContextPatch<TContext extends MachineContext> = Partial<TContext> & {
|
|
134
|
+
call?: never;
|
|
135
|
+
apply?: never;
|
|
136
|
+
bind?: never;
|
|
137
|
+
};
|
|
138
|
+
export type TransitionContextMapper<TContext extends MachineContext, TCurrentEvent extends EventObject, TEvent extends EventObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], _TCtx extends MachineContext = [TContext] extends [never] ? any : TContext> = (args: TransitionFunctionArgs<_TCtx, TCurrentEvent, TEvent, TActionMap, TActorMap, TGuardMap, TDelayMap>) => TransitionContextPatch<_TCtx>;
|
|
133
139
|
export interface TransitionConfig<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TEmitted extends EventObject, TMeta extends MetaObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays']> {
|
|
134
140
|
actions?: never;
|
|
135
141
|
guard?: unknown;
|
|
136
142
|
reenter?: boolean;
|
|
137
143
|
target?: TransitionTarget | undefined;
|
|
138
|
-
context?:
|
|
144
|
+
context?: TransitionContextPatch<TContext> | TransitionContextMapper<TContext, TExpressionEvent, TEvent, TActionMap, TActorMap, TGuardMap, TDelayMap>;
|
|
139
145
|
to?: TransitionConfigFunction<TContext, TExpressionEvent, TEvent, TEmitted, TActionMap, TActorMap, TGuardMap, TDelayMap, TMeta>;
|
|
140
146
|
meta?: TMeta;
|
|
141
147
|
description?: string;
|
|
@@ -198,11 +204,12 @@ export type StateNodesConfig<TContext extends MachineContext, TEvent extends Eve
|
|
|
198
204
|
};
|
|
199
205
|
export type TransitionConfigTarget = string | undefined;
|
|
200
206
|
export type TransitionConfigOrTarget<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TEmitted extends EventObject, TMeta extends MetaObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays']> = SingleOrArray<TransitionConfigTarget | TransitionConfig<TContext, TExpressionEvent, TEvent, TEmitted, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap> | TransitionConfigFunction<TContext, TExpressionEvent, TEvent, TEmitted, TActionMap, TActorMap, TGuardMap, TDelayMap, TMeta>>;
|
|
201
|
-
export type TransitionConfigFunction<TContext extends MachineContext, TCurrentEvent extends EventObject, TEvent extends EventObject, TEmitted extends EventObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TMeta extends MetaObject, _TCtx = [TContext] extends [never] ? any : TContext> = (args: TransitionFunctionArgs<_TCtx, TCurrentEvent, TEvent, TActionMap, TActorMap, TGuardMap, TDelayMap>, enq: EnqueueObject<TEvent, TEmitted>) => {
|
|
207
|
+
export type TransitionConfigFunction<TContext extends MachineContext, TCurrentEvent extends EventObject, TEvent extends EventObject, TEmitted extends EventObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TMeta extends MetaObject, _TCtx extends MachineContext = [TContext] extends [never] ? any : TContext> = (args: TransitionFunctionArgs<_TCtx, TCurrentEvent, TEvent, TActionMap, TActorMap, TGuardMap, TDelayMap>, enq: EnqueueObject<TEvent, TEmitted>) => {
|
|
202
208
|
target?: string | string[];
|
|
203
|
-
context?:
|
|
209
|
+
context?: TransitionContextPatch<_TCtx>;
|
|
204
210
|
reenter?: boolean;
|
|
205
211
|
meta?: TMeta;
|
|
212
|
+
input?: Record<string, unknown>;
|
|
206
213
|
} | void;
|
|
207
214
|
type TransitionFunctionArgs<TContext, TCurrentEvent extends EventObject, TEvent extends EventObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays']> = {
|
|
208
215
|
context: TContext;
|
|
@@ -379,7 +386,20 @@ export type PersistedHistoryValue = Record<string, Array<{
|
|
|
379
386
|
id: string;
|
|
380
387
|
}>>;
|
|
381
388
|
export type AnyHistoryValue = HistoryValue;
|
|
382
|
-
export type StateFrom<T extends AnyStateMachine | ((...args: any[]) => AnyStateMachine)> = T extends AnyStateMachine ?
|
|
389
|
+
export type StateFrom<T extends AnyStateMachine | ((...args: any[]) => AnyStateMachine)> = T extends AnyStateMachine ? StateSnapshotFromMachine<T> : T extends (...args: any[]) => AnyStateMachine ? StateSnapshotFromMachine<ReturnType<T>> : never;
|
|
390
|
+
type StateValueFromStateSchema<T extends StateSchema> = StateSchema extends T ? StateValue : ToStateValue<T> extends infer TStateValue ? TStateValue extends StateValue ? TStateValue : StateValue : StateValue;
|
|
391
|
+
type MatchingStateValueForStateFrom<TStateValue extends StateValue, TTestStateValue extends StateValue> = TStateValue extends unknown ? TTestStateValue extends string ? TStateValue extends string ? TTestStateValue extends TStateValue ? TTestStateValue : Extract<TStateValue, TTestStateValue> : TStateValue extends StateValueMap ? TStateValue & Record<TTestStateValue, StateValue | undefined> : never : TTestStateValue extends StateValueMap ? TStateValue extends StateValueMap ? MatchingStateValueMapForStateFrom<TStateValue, TTestStateValue> : never : never : never;
|
|
392
|
+
type MatchingStateValueMapForStateFrom<TStateValue extends StateValueMap, TTestStateValue extends StateValueMap> = false extends {
|
|
393
|
+
[K in keyof TTestStateValue & string]: K extends keyof TStateValue ? IsNever<MatchingStateValueForStateFrom<NonNullable<TStateValue[K]>, NonNullable<TTestStateValue[K]>>> extends true ? false : true : false;
|
|
394
|
+
}[keyof TTestStateValue & string] ? never : {
|
|
395
|
+
[K in keyof TStateValue]: K extends keyof TTestStateValue ? MatchingStateValueForStateFrom<NonNullable<TStateValue[K]>, NonNullable<TTestStateValue[K]>> : TStateValue[K];
|
|
396
|
+
};
|
|
397
|
+
type StateSnapshotFromStateValue<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, TOutput, TMeta extends MetaObject, TStateSchema extends StateSchema, TAllStateValue extends StateValue = TStateValue> = TStateValue extends unknown ? Omit<MachineSnapshot<StateContextFromStateValue<TStateSchema, TContext, TStateValue>, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>, 'matches'> & {
|
|
398
|
+
matches<const TTestStateValue extends string>(partialStateValue: TTestStateValue, ...args: string extends TTestStateValue ? [never] : []): this is StateSnapshotFromStateValue<StateContextFromStateValue<TStateSchema, TContext, TTestStateValue>, TEvent, TChildren, MatchingStateValueForStateFrom<TAllStateValue, TTestStateValue>, TTag, TOutput, TMeta, TStateSchema, TAllStateValue>;
|
|
399
|
+
matches<const TTestStateValue extends StateValueMap>(partialStateValue: TTestStateValue, ...args: string extends keyof TTestStateValue ? [never] : []): this is StateSnapshotFromStateValue<StateContextFromStateValue<TStateSchema, TContext, TTestStateValue>, TEvent, TChildren, MatchingStateValueForStateFrom<TAllStateValue, TTestStateValue>, TTag, TOutput, TMeta, TStateSchema, TAllStateValue>;
|
|
400
|
+
matches(partialStateValue: StateValue): boolean;
|
|
401
|
+
} : never;
|
|
402
|
+
type StateSnapshotFromMachine<T extends AnyStateMachine> = SnapshotFrom<T> extends MachineSnapshot<infer TContext, infer TEvent, infer TChildren, infer _TStateValue, infer TTag, infer TOutput, infer TMeta, infer TStateSchema> ? StateSnapshotFromStateValue<TContext, TEvent, TChildren, StateValueFromStateSchema<TStateSchema>, TTag, TOutput, TMeta, TStateSchema, StateValueFromStateSchema<TStateSchema>> : SnapshotFrom<T>;
|
|
383
403
|
export interface DoneActorEvent<TOutput = unknown, TId extends string = string> extends EventObject {
|
|
384
404
|
type: `xstate.done.actor.${TId}`;
|
|
385
405
|
output: TOutput;
|
|
@@ -967,7 +987,9 @@ type _GroupStateKeys<T extends StateSchema, S extends keyof T['states']> = S ext
|
|
|
967
987
|
type: 'history';
|
|
968
988
|
} ? [never, never] : T extends {
|
|
969
989
|
type: 'parallel';
|
|
970
|
-
} ? [S, never] :
|
|
990
|
+
} ? [S, never] : T['states'][S] extends {
|
|
991
|
+
states: Record<string, StateSchema>;
|
|
992
|
+
} ? [S, never] : [never, S] : never;
|
|
971
993
|
type GroupStateKeys<T extends StateSchema, S extends keyof T['states']> = {
|
|
972
994
|
nonLeaf: _GroupStateKeys<T, S & string>[0];
|
|
973
995
|
leaf: _GroupStateKeys<T, S & string>[1];
|