xstate 5.19.3 → 5.20.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/README.md +0 -1
- package/actions/dist/xstate-actions.cjs.d.ts +1 -1
- package/actions/dist/xstate-actions.cjs.js +9 -8
- package/actions/dist/xstate-actions.development.cjs.js +9 -8
- package/actions/dist/xstate-actions.development.esm.js +3 -2
- package/actions/dist/xstate-actions.esm.js +3 -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.d.ts +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/dev/dist/xstate-dev.cjs.d.ts +1 -1
- package/dev/dist/xstate-dev.umd.min.js.map +1 -1
- package/dist/StateMachine-1cda96d3.cjs.js +560 -0
- package/dist/StateMachine-38b5bb3f.development.cjs.js +566 -0
- package/dist/StateMachine-b4e94439.development.esm.js +563 -0
- package/dist/StateMachine-c88ea5dd.esm.js +557 -0
- package/dist/assign-6313ccb3.development.esm.js +133 -0
- package/dist/assign-c3259787.esm.js +127 -0
- package/dist/assign-c84786ab.development.cjs.js +135 -0
- package/dist/assign-e9c344ea.cjs.js +129 -0
- package/dist/declarations/src/StateMachine.d.ts +3 -4
- package/dist/declarations/src/graph/TestModel.d.ts +71 -0
- package/dist/declarations/src/graph/adjacency.d.ts +8 -0
- package/dist/declarations/src/graph/graph.d.ts +14 -0
- package/dist/declarations/src/graph/index.d.ts +9 -0
- package/dist/declarations/src/graph/pathFromEvents.d.ts +3 -0
- package/dist/declarations/src/graph/pathGenerators.d.ts +4 -0
- package/dist/declarations/src/graph/shortestPaths.d.ts +3 -0
- package/dist/declarations/src/graph/simplePaths.d.ts +3 -0
- package/dist/declarations/src/graph/types.d.ts +159 -0
- package/dist/declarations/src/types.d.ts +1 -1
- package/dist/{log-12aa30c9.esm.js → log-1c257a58.esm.js} +5 -126
- package/dist/{log-179b5431.cjs.js → log-215998b6.cjs.js} +5 -127
- package/dist/{log-210b28c2.development.cjs.js → log-2c8d7f98.development.cjs.js} +5 -133
- package/dist/{log-b47a3833.development.esm.js → log-ef959da6.development.esm.js} +5 -132
- package/dist/{raise-cbaedcda.cjs.js → raise-5872b9e8.cjs.js} +4 -2
- package/dist/{raise-527db318.development.esm.js → raise-78b8dcb8.development.esm.js} +5 -3
- package/dist/{raise-32fab6fb.development.cjs.js → raise-7a84f9f0.development.cjs.js} +4 -2
- package/dist/{raise-59f80ebb.esm.js → raise-b0a4e862.esm.js} +5 -3
- package/dist/xstate.cjs.d.ts +1 -1
- package/dist/xstate.cjs.js +8 -563
- package/dist/xstate.development.cjs.js +8 -569
- package/dist/xstate.development.esm.js +7 -568
- package/dist/xstate.esm.js +7 -562
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/graph/dist/xstate-graph.cjs.d.mts +2 -0
- package/graph/dist/xstate-graph.cjs.d.ts +2 -0
- package/graph/dist/xstate-graph.cjs.js +901 -0
- package/graph/dist/xstate-graph.cjs.mjs +15 -0
- package/graph/dist/xstate-graph.development.cjs.js +901 -0
- package/graph/dist/xstate-graph.development.cjs.mjs +15 -0
- package/graph/dist/xstate-graph.development.esm.js +885 -0
- package/graph/dist/xstate-graph.esm.js +885 -0
- package/graph/dist/xstate-graph.umd.min.js +2 -0
- package/graph/dist/xstate-graph.umd.min.js.map +1 -0
- package/graph/package.json +8 -0
- package/guards/dist/xstate-guards.cjs.d.ts +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 +1 -1
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +22 -6
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { EventObject, StateNode, TransitionDefinition, Snapshot, MachineContext, ActorLogic, MachineSnapshot } from "../index.js";
|
|
2
|
+
export type AnyStateNode = StateNode<any, any>;
|
|
3
|
+
type JSONSerializable<T extends object, U> = T & {
|
|
4
|
+
toJSON: () => U;
|
|
5
|
+
};
|
|
6
|
+
type DirectedGraphLabel = JSONSerializable<{
|
|
7
|
+
text: string;
|
|
8
|
+
}, {
|
|
9
|
+
text: string;
|
|
10
|
+
}>;
|
|
11
|
+
export type DirectedGraphEdge = JSONSerializable<{
|
|
12
|
+
id: string;
|
|
13
|
+
source: AnyStateNode;
|
|
14
|
+
target: AnyStateNode;
|
|
15
|
+
label: DirectedGraphLabel;
|
|
16
|
+
transition: TransitionDefinition<any, any>;
|
|
17
|
+
}, {
|
|
18
|
+
source: string;
|
|
19
|
+
target: string;
|
|
20
|
+
label: ReturnType<DirectedGraphLabel['toJSON']>;
|
|
21
|
+
}>;
|
|
22
|
+
export type DirectedGraphNode = JSONSerializable<{
|
|
23
|
+
id: string;
|
|
24
|
+
stateNode: StateNode;
|
|
25
|
+
children: DirectedGraphNode[];
|
|
26
|
+
/** The edges representing all transitions from this `stateNode`. */
|
|
27
|
+
edges: DirectedGraphEdge[];
|
|
28
|
+
}, {
|
|
29
|
+
id: string;
|
|
30
|
+
children: DirectedGraphNode[];
|
|
31
|
+
}>;
|
|
32
|
+
interface StatePlan<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject> {
|
|
33
|
+
/** The target state. */
|
|
34
|
+
state: TSnapshot;
|
|
35
|
+
/** The paths that reach the target state. */
|
|
36
|
+
paths: Array<StatePath<TSnapshot, TEvent>>;
|
|
37
|
+
}
|
|
38
|
+
export interface StatePath<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject> {
|
|
39
|
+
/** The ending state of the path. */
|
|
40
|
+
state: TSnapshot;
|
|
41
|
+
/**
|
|
42
|
+
* The ordered array of state-event pairs (steps) which reach the ending
|
|
43
|
+
* `state`.
|
|
44
|
+
*/
|
|
45
|
+
steps: Steps<TSnapshot, TEvent>;
|
|
46
|
+
/** The combined weight of all steps in the path. */
|
|
47
|
+
weight: number;
|
|
48
|
+
}
|
|
49
|
+
export interface StatePlanMap<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject> {
|
|
50
|
+
[key: string]: StatePlan<TSnapshot, TEvent>;
|
|
51
|
+
}
|
|
52
|
+
export interface Step<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject> {
|
|
53
|
+
/** The event that resulted in the current state */
|
|
54
|
+
event: TEvent;
|
|
55
|
+
/** The current state after taking the event. */
|
|
56
|
+
state: TSnapshot;
|
|
57
|
+
}
|
|
58
|
+
export type Steps<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject> = Array<Step<TSnapshot, TEvent>>;
|
|
59
|
+
type ExtractEvent<TEvent extends EventObject, TType extends TEvent['type']> = TEvent extends {
|
|
60
|
+
type: TType;
|
|
61
|
+
} ? TEvent : never;
|
|
62
|
+
export interface VisitedContext<TState, TEvent> {
|
|
63
|
+
vertices: Set<SerializedSnapshot>;
|
|
64
|
+
edges: Set<SerializedEvent>;
|
|
65
|
+
a?: TState | TEvent;
|
|
66
|
+
}
|
|
67
|
+
export interface SerializationConfig<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject> {
|
|
68
|
+
serializeState: (state: TSnapshot, event: TEvent | undefined, prevState?: TSnapshot) => string;
|
|
69
|
+
serializeEvent: (event: TEvent) => string;
|
|
70
|
+
}
|
|
71
|
+
type SerializationOptions<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject> = Partial<Pick<SerializationConfig<TSnapshot, TEvent>, 'serializeState' | 'serializeEvent'>>;
|
|
72
|
+
export type TraversalOptions<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject, TInput> = {
|
|
73
|
+
input?: TInput;
|
|
74
|
+
} & SerializationOptions<TSnapshot, TEvent> & Partial<Pick<TraversalConfig<TSnapshot, TEvent>, 'events' | 'limit' | 'fromState' | 'stopWhen' | 'toState'>>;
|
|
75
|
+
export interface TraversalConfig<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject> extends SerializationConfig<TSnapshot, TEvent> {
|
|
76
|
+
events: readonly TEvent[] | ((state: TSnapshot) => readonly TEvent[]);
|
|
77
|
+
/**
|
|
78
|
+
* The maximum number of traversals to perform when calculating the state
|
|
79
|
+
* transition adjacency map.
|
|
80
|
+
*
|
|
81
|
+
* @default `Infinity`
|
|
82
|
+
*/
|
|
83
|
+
limit: number;
|
|
84
|
+
fromState: TSnapshot | undefined;
|
|
85
|
+
/** When true, traversal of the adjacency map will stop for that current state. */
|
|
86
|
+
stopWhen: ((state: TSnapshot) => boolean) | undefined;
|
|
87
|
+
toState: ((state: TSnapshot) => boolean) | undefined;
|
|
88
|
+
}
|
|
89
|
+
type Brand<T, Tag extends string> = T & {
|
|
90
|
+
__tag: Tag;
|
|
91
|
+
};
|
|
92
|
+
export type SerializedSnapshot = Brand<string, 'state'>;
|
|
93
|
+
export type SerializedEvent = Brand<string, 'event'>;
|
|
94
|
+
export interface TestMeta<T, TContext extends MachineContext> {
|
|
95
|
+
test?: (testContext: T, state: MachineSnapshot<TContext, any, any, any, any, any, any, // TMeta
|
|
96
|
+
any>) => Promise<void> | void;
|
|
97
|
+
description?: string | ((state: MachineSnapshot<TContext, any, any, any, any, any, any, // TMeta
|
|
98
|
+
any>) => string);
|
|
99
|
+
skip?: boolean;
|
|
100
|
+
}
|
|
101
|
+
interface TestStateResult {
|
|
102
|
+
error: null | Error;
|
|
103
|
+
}
|
|
104
|
+
export interface TestStepResult {
|
|
105
|
+
step: Step<any, any>;
|
|
106
|
+
state: TestStateResult;
|
|
107
|
+
event: {
|
|
108
|
+
error: null | Error;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
export interface TestParam<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject> {
|
|
112
|
+
states?: {
|
|
113
|
+
[key: string]: (state: TSnapshot) => void | Promise<void>;
|
|
114
|
+
};
|
|
115
|
+
events?: {
|
|
116
|
+
[TEventType in TEvent['type']]?: EventExecutor<TSnapshot, {
|
|
117
|
+
type: ExtractEvent<TEvent, TEventType>['type'];
|
|
118
|
+
}>;
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
export interface TestPath<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject> extends StatePath<TSnapshot, TEvent> {
|
|
122
|
+
description: string;
|
|
123
|
+
/**
|
|
124
|
+
* Tests and executes each step in `steps` sequentially, and then tests the
|
|
125
|
+
* postcondition that the `state` is reached.
|
|
126
|
+
*/
|
|
127
|
+
test: (params: TestParam<TSnapshot, TEvent>) => Promise<TestPathResult>;
|
|
128
|
+
}
|
|
129
|
+
export interface TestPathResult {
|
|
130
|
+
steps: TestStepResult[];
|
|
131
|
+
state: TestStateResult;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Executes an effect using the `testContext` and `event` that triggers the
|
|
135
|
+
* represented `event`.
|
|
136
|
+
*/
|
|
137
|
+
export type EventExecutor<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject> = (step: Step<TSnapshot, TEvent>) => Promise<any> | void;
|
|
138
|
+
export interface TestModelOptions<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject, TInput> extends TraversalOptions<TSnapshot, TEvent, TInput> {
|
|
139
|
+
stateMatcher: (state: TSnapshot, stateKey: string) => boolean;
|
|
140
|
+
logger: {
|
|
141
|
+
log: (msg: string) => void;
|
|
142
|
+
error: (msg: string) => void;
|
|
143
|
+
};
|
|
144
|
+
serializeTransition: (state: TSnapshot, event: TEvent | undefined, prevState?: TSnapshot) => string;
|
|
145
|
+
}
|
|
146
|
+
export type PathGenerator<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject, TInput> = (behavior: ActorLogic<TSnapshot, TEvent, TInput>, options: TraversalOptions<TSnapshot, TEvent, TInput>) => Array<StatePath<TSnapshot, TEvent>>;
|
|
147
|
+
export interface AdjacencyValue<TState, TEvent> {
|
|
148
|
+
state: TState;
|
|
149
|
+
transitions: {
|
|
150
|
+
[key: SerializedEvent]: {
|
|
151
|
+
event: TEvent;
|
|
152
|
+
state: TState;
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
export interface AdjacencyMap<TState, TEvent> {
|
|
157
|
+
[key: SerializedSnapshot]: AdjacencyValue<TState, TEvent>;
|
|
158
|
+
}
|
|
159
|
+
export {};
|
|
@@ -9,7 +9,7 @@ import type { Actor } from "./createActor.js";
|
|
|
9
9
|
import { Guard, GuardPredicate, UnknownGuard } from "./guards.js";
|
|
10
10
|
import { InspectionEvent } from "./inspection.js";
|
|
11
11
|
import { Spawner } from "./spawn.js";
|
|
12
|
-
import { AnyActorSystem, Clock } from
|
|
12
|
+
import { AnyActorSystem, Clock } from "./system.js";
|
|
13
13
|
export type Identity<T> = {
|
|
14
14
|
[K in keyof T]: T[K];
|
|
15
15
|
};
|
|
@@ -1,128 +1,5 @@
|
|
|
1
|
-
import { T as
|
|
2
|
-
|
|
3
|
-
function createSpawner(actorScope, {
|
|
4
|
-
machine,
|
|
5
|
-
context
|
|
6
|
-
}, event, spawnedChildren) {
|
|
7
|
-
const spawn = (src, options) => {
|
|
8
|
-
if (typeof src === 'string') {
|
|
9
|
-
const logic = resolveReferencedActor(machine, src);
|
|
10
|
-
if (!logic) {
|
|
11
|
-
throw new Error(`Actor logic '${src}' not implemented in machine '${machine.id}'`);
|
|
12
|
-
}
|
|
13
|
-
const actorRef = createActor(logic, {
|
|
14
|
-
id: options?.id,
|
|
15
|
-
parent: actorScope.self,
|
|
16
|
-
syncSnapshot: options?.syncSnapshot,
|
|
17
|
-
input: typeof options?.input === 'function' ? options.input({
|
|
18
|
-
context,
|
|
19
|
-
event,
|
|
20
|
-
self: actorScope.self
|
|
21
|
-
}) : options?.input,
|
|
22
|
-
src,
|
|
23
|
-
systemId: options?.systemId
|
|
24
|
-
});
|
|
25
|
-
spawnedChildren[actorRef.id] = actorRef;
|
|
26
|
-
return actorRef;
|
|
27
|
-
} else {
|
|
28
|
-
const actorRef = createActor(src, {
|
|
29
|
-
id: options?.id,
|
|
30
|
-
parent: actorScope.self,
|
|
31
|
-
syncSnapshot: options?.syncSnapshot,
|
|
32
|
-
input: options?.input,
|
|
33
|
-
src,
|
|
34
|
-
systemId: options?.systemId
|
|
35
|
-
});
|
|
36
|
-
return actorRef;
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
return (src, options) => {
|
|
40
|
-
const actorRef = spawn(src, options); // TODO: fix types
|
|
41
|
-
spawnedChildren[actorRef.id] = actorRef;
|
|
42
|
-
actorScope.defer(() => {
|
|
43
|
-
if (actorRef._processingStatus === ProcessingStatus.Stopped) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
actorRef.start();
|
|
47
|
-
});
|
|
48
|
-
return actorRef;
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function resolveAssign(actorScope, snapshot, actionArgs, actionParams, {
|
|
53
|
-
assignment
|
|
54
|
-
}) {
|
|
55
|
-
if (!snapshot.context) {
|
|
56
|
-
throw new Error('Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.');
|
|
57
|
-
}
|
|
58
|
-
const spawnedChildren = {};
|
|
59
|
-
const assignArgs = {
|
|
60
|
-
context: snapshot.context,
|
|
61
|
-
event: actionArgs.event,
|
|
62
|
-
spawn: createSpawner(actorScope, snapshot, actionArgs.event, spawnedChildren),
|
|
63
|
-
self: actorScope.self,
|
|
64
|
-
system: actorScope.system
|
|
65
|
-
};
|
|
66
|
-
let partialUpdate = {};
|
|
67
|
-
if (typeof assignment === 'function') {
|
|
68
|
-
partialUpdate = assignment(assignArgs, actionParams);
|
|
69
|
-
} else {
|
|
70
|
-
for (const key of Object.keys(assignment)) {
|
|
71
|
-
const propAssignment = assignment[key];
|
|
72
|
-
partialUpdate[key] = typeof propAssignment === 'function' ? propAssignment(assignArgs, actionParams) : propAssignment;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
const updatedContext = Object.assign({}, snapshot.context, partialUpdate);
|
|
76
|
-
return [cloneMachineSnapshot(snapshot, {
|
|
77
|
-
context: updatedContext,
|
|
78
|
-
children: Object.keys(spawnedChildren).length ? {
|
|
79
|
-
...snapshot.children,
|
|
80
|
-
...spawnedChildren
|
|
81
|
-
} : snapshot.children
|
|
82
|
-
}), undefined, undefined];
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Updates the current context of the machine.
|
|
86
|
-
*
|
|
87
|
-
* @example
|
|
88
|
-
*
|
|
89
|
-
* ```ts
|
|
90
|
-
* import { createMachine, assign } from 'xstate';
|
|
91
|
-
*
|
|
92
|
-
* const countMachine = createMachine({
|
|
93
|
-
* context: {
|
|
94
|
-
* count: 0,
|
|
95
|
-
* message: ''
|
|
96
|
-
* },
|
|
97
|
-
* on: {
|
|
98
|
-
* inc: {
|
|
99
|
-
* actions: assign({
|
|
100
|
-
* count: ({ context }) => context.count + 1
|
|
101
|
-
* })
|
|
102
|
-
* },
|
|
103
|
-
* updateMessage: {
|
|
104
|
-
* actions: assign(({ context, event }) => {
|
|
105
|
-
* return {
|
|
106
|
-
* message: event.message.trim()
|
|
107
|
-
* };
|
|
108
|
-
* })
|
|
109
|
-
* }
|
|
110
|
-
* }
|
|
111
|
-
* });
|
|
112
|
-
* ```
|
|
113
|
-
*
|
|
114
|
-
* @param assignment An object that represents the partial context to update, or
|
|
115
|
-
* a function that returns an object that represents the partial context to
|
|
116
|
-
* update.
|
|
117
|
-
*/
|
|
118
|
-
function assign(assignment) {
|
|
119
|
-
function assign(_args, _params) {
|
|
120
|
-
}
|
|
121
|
-
assign.type = 'xstate.assign';
|
|
122
|
-
assign.assignment = assignment;
|
|
123
|
-
assign.resolve = resolveAssign;
|
|
124
|
-
return assign;
|
|
125
|
-
}
|
|
1
|
+
import { T as XSTATE_ERROR, U as createErrorActorEvent, w as evaluateGuard, f as cancel, r as raise, h as spawnChild, k as stopChild } from './raise-b0a4e862.esm.js';
|
|
2
|
+
import { a as assign } from './assign-c3259787.esm.js';
|
|
126
3
|
|
|
127
4
|
function resolveEmit(_, snapshot, args, actionParams, {
|
|
128
5
|
event: eventOrExpr
|
|
@@ -184,6 +61,8 @@ eventOrExpr) {
|
|
|
184
61
|
return emit;
|
|
185
62
|
}
|
|
186
63
|
|
|
64
|
+
// this is needed to make JSDoc `@link` work properly
|
|
65
|
+
|
|
187
66
|
/**
|
|
188
67
|
* @remarks
|
|
189
68
|
* `T | unknown` reduces to `unknown` and that can be problematic when it comes
|
|
@@ -499,4 +378,4 @@ function log(value = ({
|
|
|
499
378
|
return log;
|
|
500
379
|
}
|
|
501
380
|
|
|
502
|
-
export { SpecialTargets as S,
|
|
381
|
+
export { SpecialTargets as S, enqueueActions as a, sendTo as b, emit as e, forwardTo as f, log as l, sendParent as s };
|
|
@@ -1,130 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var guards_dist_xstateGuards = require('./raise-
|
|
4
|
-
|
|
5
|
-
function createSpawner(actorScope, {
|
|
6
|
-
machine,
|
|
7
|
-
context
|
|
8
|
-
}, event, spawnedChildren) {
|
|
9
|
-
const spawn = (src, options) => {
|
|
10
|
-
if (typeof src === 'string') {
|
|
11
|
-
const logic = guards_dist_xstateGuards.resolveReferencedActor(machine, src);
|
|
12
|
-
if (!logic) {
|
|
13
|
-
throw new Error(`Actor logic '${src}' not implemented in machine '${machine.id}'`);
|
|
14
|
-
}
|
|
15
|
-
const actorRef = guards_dist_xstateGuards.createActor(logic, {
|
|
16
|
-
id: options?.id,
|
|
17
|
-
parent: actorScope.self,
|
|
18
|
-
syncSnapshot: options?.syncSnapshot,
|
|
19
|
-
input: typeof options?.input === 'function' ? options.input({
|
|
20
|
-
context,
|
|
21
|
-
event,
|
|
22
|
-
self: actorScope.self
|
|
23
|
-
}) : options?.input,
|
|
24
|
-
src,
|
|
25
|
-
systemId: options?.systemId
|
|
26
|
-
});
|
|
27
|
-
spawnedChildren[actorRef.id] = actorRef;
|
|
28
|
-
return actorRef;
|
|
29
|
-
} else {
|
|
30
|
-
const actorRef = guards_dist_xstateGuards.createActor(src, {
|
|
31
|
-
id: options?.id,
|
|
32
|
-
parent: actorScope.self,
|
|
33
|
-
syncSnapshot: options?.syncSnapshot,
|
|
34
|
-
input: options?.input,
|
|
35
|
-
src,
|
|
36
|
-
systemId: options?.systemId
|
|
37
|
-
});
|
|
38
|
-
return actorRef;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
return (src, options) => {
|
|
42
|
-
const actorRef = spawn(src, options); // TODO: fix types
|
|
43
|
-
spawnedChildren[actorRef.id] = actorRef;
|
|
44
|
-
actorScope.defer(() => {
|
|
45
|
-
if (actorRef._processingStatus === guards_dist_xstateGuards.ProcessingStatus.Stopped) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
actorRef.start();
|
|
49
|
-
});
|
|
50
|
-
return actorRef;
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function resolveAssign(actorScope, snapshot, actionArgs, actionParams, {
|
|
55
|
-
assignment
|
|
56
|
-
}) {
|
|
57
|
-
if (!snapshot.context) {
|
|
58
|
-
throw new Error('Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.');
|
|
59
|
-
}
|
|
60
|
-
const spawnedChildren = {};
|
|
61
|
-
const assignArgs = {
|
|
62
|
-
context: snapshot.context,
|
|
63
|
-
event: actionArgs.event,
|
|
64
|
-
spawn: createSpawner(actorScope, snapshot, actionArgs.event, spawnedChildren),
|
|
65
|
-
self: actorScope.self,
|
|
66
|
-
system: actorScope.system
|
|
67
|
-
};
|
|
68
|
-
let partialUpdate = {};
|
|
69
|
-
if (typeof assignment === 'function') {
|
|
70
|
-
partialUpdate = assignment(assignArgs, actionParams);
|
|
71
|
-
} else {
|
|
72
|
-
for (const key of Object.keys(assignment)) {
|
|
73
|
-
const propAssignment = assignment[key];
|
|
74
|
-
partialUpdate[key] = typeof propAssignment === 'function' ? propAssignment(assignArgs, actionParams) : propAssignment;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const updatedContext = Object.assign({}, snapshot.context, partialUpdate);
|
|
78
|
-
return [guards_dist_xstateGuards.cloneMachineSnapshot(snapshot, {
|
|
79
|
-
context: updatedContext,
|
|
80
|
-
children: Object.keys(spawnedChildren).length ? {
|
|
81
|
-
...snapshot.children,
|
|
82
|
-
...spawnedChildren
|
|
83
|
-
} : snapshot.children
|
|
84
|
-
}), undefined, undefined];
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Updates the current context of the machine.
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
*
|
|
91
|
-
* ```ts
|
|
92
|
-
* import { createMachine, assign } from 'xstate';
|
|
93
|
-
*
|
|
94
|
-
* const countMachine = createMachine({
|
|
95
|
-
* context: {
|
|
96
|
-
* count: 0,
|
|
97
|
-
* message: ''
|
|
98
|
-
* },
|
|
99
|
-
* on: {
|
|
100
|
-
* inc: {
|
|
101
|
-
* actions: assign({
|
|
102
|
-
* count: ({ context }) => context.count + 1
|
|
103
|
-
* })
|
|
104
|
-
* },
|
|
105
|
-
* updateMessage: {
|
|
106
|
-
* actions: assign(({ context, event }) => {
|
|
107
|
-
* return {
|
|
108
|
-
* message: event.message.trim()
|
|
109
|
-
* };
|
|
110
|
-
* })
|
|
111
|
-
* }
|
|
112
|
-
* }
|
|
113
|
-
* });
|
|
114
|
-
* ```
|
|
115
|
-
*
|
|
116
|
-
* @param assignment An object that represents the partial context to update, or
|
|
117
|
-
* a function that returns an object that represents the partial context to
|
|
118
|
-
* update.
|
|
119
|
-
*/
|
|
120
|
-
function assign(assignment) {
|
|
121
|
-
function assign(_args, _params) {
|
|
122
|
-
}
|
|
123
|
-
assign.type = 'xstate.assign';
|
|
124
|
-
assign.assignment = assignment;
|
|
125
|
-
assign.resolve = resolveAssign;
|
|
126
|
-
return assign;
|
|
127
|
-
}
|
|
3
|
+
var guards_dist_xstateGuards = require('./raise-5872b9e8.cjs.js');
|
|
4
|
+
var assign = require('./assign-e9c344ea.cjs.js');
|
|
128
5
|
|
|
129
6
|
function resolveEmit(_, snapshot, args, actionParams, {
|
|
130
7
|
event: eventOrExpr
|
|
@@ -186,6 +63,8 @@ eventOrExpr) {
|
|
|
186
63
|
return emit;
|
|
187
64
|
}
|
|
188
65
|
|
|
66
|
+
// this is needed to make JSDoc `@link` work properly
|
|
67
|
+
|
|
189
68
|
/**
|
|
190
69
|
* @remarks
|
|
191
70
|
* `T | unknown` reduces to `unknown` and that can be problematic when it comes
|
|
@@ -386,7 +265,7 @@ function resolveEnqueueActions(actorScope, snapshot, args, actionParams, {
|
|
|
386
265
|
actions.push(action);
|
|
387
266
|
};
|
|
388
267
|
enqueue.assign = (...args) => {
|
|
389
|
-
actions.push(assign(...args));
|
|
268
|
+
actions.push(assign.assign(...args));
|
|
390
269
|
};
|
|
391
270
|
enqueue.cancel = (...args) => {
|
|
392
271
|
actions.push(guards_dist_xstateGuards.cancel(...args));
|
|
@@ -502,7 +381,6 @@ function log(value = ({
|
|
|
502
381
|
}
|
|
503
382
|
|
|
504
383
|
exports.SpecialTargets = SpecialTargets;
|
|
505
|
-
exports.assign = assign;
|
|
506
384
|
exports.emit = emit;
|
|
507
385
|
exports.enqueueActions = enqueueActions;
|
|
508
386
|
exports.forwardTo = forwardTo;
|
|
@@ -1,136 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var guards_dist_xstateGuards = require('./raise-
|
|
4
|
-
|
|
5
|
-
function createSpawner(actorScope, {
|
|
6
|
-
machine,
|
|
7
|
-
context
|
|
8
|
-
}, event, spawnedChildren) {
|
|
9
|
-
const spawn = (src, options) => {
|
|
10
|
-
if (typeof src === 'string') {
|
|
11
|
-
const logic = guards_dist_xstateGuards.resolveReferencedActor(machine, src);
|
|
12
|
-
if (!logic) {
|
|
13
|
-
throw new Error(`Actor logic '${src}' not implemented in machine '${machine.id}'`);
|
|
14
|
-
}
|
|
15
|
-
const actorRef = guards_dist_xstateGuards.createActor(logic, {
|
|
16
|
-
id: options?.id,
|
|
17
|
-
parent: actorScope.self,
|
|
18
|
-
syncSnapshot: options?.syncSnapshot,
|
|
19
|
-
input: typeof options?.input === 'function' ? options.input({
|
|
20
|
-
context,
|
|
21
|
-
event,
|
|
22
|
-
self: actorScope.self
|
|
23
|
-
}) : options?.input,
|
|
24
|
-
src,
|
|
25
|
-
systemId: options?.systemId
|
|
26
|
-
});
|
|
27
|
-
spawnedChildren[actorRef.id] = actorRef;
|
|
28
|
-
return actorRef;
|
|
29
|
-
} else {
|
|
30
|
-
const actorRef = guards_dist_xstateGuards.createActor(src, {
|
|
31
|
-
id: options?.id,
|
|
32
|
-
parent: actorScope.self,
|
|
33
|
-
syncSnapshot: options?.syncSnapshot,
|
|
34
|
-
input: options?.input,
|
|
35
|
-
src,
|
|
36
|
-
systemId: options?.systemId
|
|
37
|
-
});
|
|
38
|
-
return actorRef;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
return (src, options) => {
|
|
42
|
-
const actorRef = spawn(src, options); // TODO: fix types
|
|
43
|
-
spawnedChildren[actorRef.id] = actorRef;
|
|
44
|
-
actorScope.defer(() => {
|
|
45
|
-
if (actorRef._processingStatus === guards_dist_xstateGuards.ProcessingStatus.Stopped) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
actorRef.start();
|
|
49
|
-
});
|
|
50
|
-
return actorRef;
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function resolveAssign(actorScope, snapshot, actionArgs, actionParams, {
|
|
55
|
-
assignment
|
|
56
|
-
}) {
|
|
57
|
-
if (!snapshot.context) {
|
|
58
|
-
throw new Error('Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.');
|
|
59
|
-
}
|
|
60
|
-
const spawnedChildren = {};
|
|
61
|
-
const assignArgs = {
|
|
62
|
-
context: snapshot.context,
|
|
63
|
-
event: actionArgs.event,
|
|
64
|
-
spawn: createSpawner(actorScope, snapshot, actionArgs.event, spawnedChildren),
|
|
65
|
-
self: actorScope.self,
|
|
66
|
-
system: actorScope.system
|
|
67
|
-
};
|
|
68
|
-
let partialUpdate = {};
|
|
69
|
-
if (typeof assignment === 'function') {
|
|
70
|
-
partialUpdate = assignment(assignArgs, actionParams);
|
|
71
|
-
} else {
|
|
72
|
-
for (const key of Object.keys(assignment)) {
|
|
73
|
-
const propAssignment = assignment[key];
|
|
74
|
-
partialUpdate[key] = typeof propAssignment === 'function' ? propAssignment(assignArgs, actionParams) : propAssignment;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const updatedContext = Object.assign({}, snapshot.context, partialUpdate);
|
|
78
|
-
return [guards_dist_xstateGuards.cloneMachineSnapshot(snapshot, {
|
|
79
|
-
context: updatedContext,
|
|
80
|
-
children: Object.keys(spawnedChildren).length ? {
|
|
81
|
-
...snapshot.children,
|
|
82
|
-
...spawnedChildren
|
|
83
|
-
} : snapshot.children
|
|
84
|
-
}), undefined, undefined];
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Updates the current context of the machine.
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
*
|
|
91
|
-
* ```ts
|
|
92
|
-
* import { createMachine, assign } from 'xstate';
|
|
93
|
-
*
|
|
94
|
-
* const countMachine = createMachine({
|
|
95
|
-
* context: {
|
|
96
|
-
* count: 0,
|
|
97
|
-
* message: ''
|
|
98
|
-
* },
|
|
99
|
-
* on: {
|
|
100
|
-
* inc: {
|
|
101
|
-
* actions: assign({
|
|
102
|
-
* count: ({ context }) => context.count + 1
|
|
103
|
-
* })
|
|
104
|
-
* },
|
|
105
|
-
* updateMessage: {
|
|
106
|
-
* actions: assign(({ context, event }) => {
|
|
107
|
-
* return {
|
|
108
|
-
* message: event.message.trim()
|
|
109
|
-
* };
|
|
110
|
-
* })
|
|
111
|
-
* }
|
|
112
|
-
* }
|
|
113
|
-
* });
|
|
114
|
-
* ```
|
|
115
|
-
*
|
|
116
|
-
* @param assignment An object that represents the partial context to update, or
|
|
117
|
-
* a function that returns an object that represents the partial context to
|
|
118
|
-
* update.
|
|
119
|
-
*/
|
|
120
|
-
function assign(assignment) {
|
|
121
|
-
if (guards_dist_xstateGuards.executingCustomAction) {
|
|
122
|
-
console.warn('Custom actions should not call `assign()` directly, as it is not imperative. See https://stately.ai/docs/actions#built-in-actions for more details.');
|
|
123
|
-
}
|
|
124
|
-
function assign(_args, _params) {
|
|
125
|
-
{
|
|
126
|
-
throw new Error(`This isn't supposed to be called`);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
assign.type = 'xstate.assign';
|
|
130
|
-
assign.assignment = assignment;
|
|
131
|
-
assign.resolve = resolveAssign;
|
|
132
|
-
return assign;
|
|
133
|
-
}
|
|
3
|
+
var guards_dist_xstateGuards = require('./raise-7a84f9f0.development.cjs.js');
|
|
4
|
+
var assign = require('./assign-c84786ab.development.cjs.js');
|
|
134
5
|
|
|
135
6
|
function resolveEmit(_, snapshot, args, actionParams, {
|
|
136
7
|
event: eventOrExpr
|
|
@@ -198,6 +69,8 @@ eventOrExpr) {
|
|
|
198
69
|
return emit;
|
|
199
70
|
}
|
|
200
71
|
|
|
72
|
+
// this is needed to make JSDoc `@link` work properly
|
|
73
|
+
|
|
201
74
|
/**
|
|
202
75
|
* @remarks
|
|
203
76
|
* `T | unknown` reduces to `unknown` and that can be problematic when it comes
|
|
@@ -414,7 +287,7 @@ function resolveEnqueueActions(actorScope, snapshot, args, actionParams, {
|
|
|
414
287
|
actions.push(action);
|
|
415
288
|
};
|
|
416
289
|
enqueue.assign = (...args) => {
|
|
417
|
-
actions.push(assign(...args));
|
|
290
|
+
actions.push(assign.assign(...args));
|
|
418
291
|
};
|
|
419
292
|
enqueue.cancel = (...args) => {
|
|
420
293
|
actions.push(guards_dist_xstateGuards.cancel(...args));
|
|
@@ -536,7 +409,6 @@ function log(value = ({
|
|
|
536
409
|
}
|
|
537
410
|
|
|
538
411
|
exports.SpecialTargets = SpecialTargets;
|
|
539
|
-
exports.assign = assign;
|
|
540
412
|
exports.emit = emit;
|
|
541
413
|
exports.enqueueActions = enqueueActions;
|
|
542
414
|
exports.forwardTo = forwardTo;
|