xstate 4.7.8 → 4.10.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/es/actions.js CHANGED
@@ -1,10 +1,9 @@
1
- import { __assign, __rest } from './_virtual/_tslib.js';
2
- import { toSCXMLEvent, isFunction, toEventObject, getEventType, isString, isArray } from './utils.js';
1
+ import { __assign, __read, __rest } from './_virtual/_tslib.js';
2
+ import { IS_PRODUCTION } from './environment.js';
3
+ import { toSCXMLEvent, isFunction, toEventObject, getEventType, isString, partition, updateContext, flatten, toArray, toGuard, evaluateGuard, warn, isArray } from './utils.js';
3
4
  import { SpecialTargets, ActionTypes } from './types.js';
4
- import { send as send$1, update, assign as assign$1, raise as raise$1, init, log as log$1, cancel as cancel$1, error as error$1 } from './actionTypes.js';
5
- var initEvent =
6
- /*#__PURE__*/
7
- toSCXMLEvent({
5
+ import { send as send$1, update, assign as assign$1, init, raise as raise$1, log as log$1, cancel as cancel$1, error as error$1, pure as pure$1, choose as choose$1 } from './actionTypes.js';
6
+ var initEvent = /*#__PURE__*/toSCXMLEvent({
8
7
  type: init
9
8
  });
10
9
 
@@ -361,6 +360,13 @@ function error(id, data) {
361
360
 
362
361
  return eventObject;
363
362
  }
363
+
364
+ function pure(getActions) {
365
+ return {
366
+ type: ActionTypes.Pure,
367
+ get: getActions
368
+ };
369
+ }
364
370
  /**
365
371
  * Forwards (sends) an event to a specified service.
366
372
  *
@@ -396,4 +402,77 @@ function escalate(errorData, options) {
396
402
  }));
397
403
  }
398
404
 
399
- export { after, assign, cancel, done, doneInvoke, error, escalate, forwardTo, getActionFunction, initEvent, log, raise, resolveLog, resolveRaise, resolveSend, respond, send, sendParent, sendUpdate, start, stop, toActionObject, toActionObjects, toActivityDefinition };
405
+ function choose(conds) {
406
+ return {
407
+ type: ActionTypes.Choose,
408
+ conds: conds
409
+ };
410
+ }
411
+
412
+ function resolveActions(machine, currentState, currentContext, _event, actions) {
413
+ var _a = __read(partition(actions, function (action) {
414
+ return action.type === assign$1;
415
+ }), 2),
416
+ assignActions = _a[0],
417
+ otherActions = _a[1];
418
+
419
+ var updatedContext = assignActions.length ? updateContext(currentContext, _event, assignActions, currentState) : currentContext;
420
+ var resolvedActions = flatten(otherActions.map(function (actionObject) {
421
+ var _a;
422
+
423
+ switch (actionObject.type) {
424
+ case raise$1:
425
+ return resolveRaise(actionObject);
426
+
427
+ case send$1:
428
+ var sendAction = resolveSend(actionObject, updatedContext, _event, machine.options.delays); // TODO: fix ActionTypes.Init
429
+
430
+ if (!IS_PRODUCTION) {
431
+ // warn after resolving as we can create better contextual message here
432
+ warn(!isString(actionObject.delay) || typeof sendAction.delay === 'number', // tslint:disable-next-line:max-line-length
433
+ "No delay reference for delay expression '" + actionObject.delay + "' was found on machine '" + machine.id + "'");
434
+ }
435
+
436
+ return sendAction;
437
+
438
+ case log$1:
439
+ return resolveLog(actionObject, updatedContext, _event);
440
+
441
+ case choose$1:
442
+ {
443
+ var chooseAction = actionObject;
444
+ var matchedActions = (_a = chooseAction.conds.find(function (condition) {
445
+ var guard = toGuard(condition.cond, machine.options.guards);
446
+ return !guard || evaluateGuard(machine, guard, updatedContext, _event, currentState);
447
+ })) === null || _a === void 0 ? void 0 : _a.actions;
448
+
449
+ if (!matchedActions) {
450
+ return [];
451
+ }
452
+
453
+ var resolved = resolveActions(machine, currentState, updatedContext, _event, toActionObjects(toArray(matchedActions), machine.options.actions));
454
+ updatedContext = resolved[1];
455
+ return resolved[0];
456
+ }
457
+
458
+ case pure$1:
459
+ {
460
+ var matchedActions = actionObject.get(updatedContext, _event.data);
461
+
462
+ if (!matchedActions) {
463
+ return [];
464
+ }
465
+
466
+ var resolved = resolveActions(machine, currentState, updatedContext, _event, toActionObjects(toArray(matchedActions), machine.options.actions));
467
+ updatedContext = resolved[1];
468
+ return resolved[0];
469
+ }
470
+
471
+ default:
472
+ return toActionObject(actionObject, machine.options.actions);
473
+ }
474
+ }));
475
+ return [resolvedActions, updatedContext];
476
+ }
477
+
478
+ export { after, assign, cancel, choose, done, doneInvoke, error, escalate, forwardTo, getActionFunction, initEvent, log, pure, raise, resolveActions, resolveLog, resolveRaise, resolveSend, respond, send, sendParent, sendUpdate, start, stop, toActionObject, toActionObjects, toActivityDefinition };
package/es/index.d.ts CHANGED
@@ -4,7 +4,7 @@ import { StateNode } from './StateNode';
4
4
  import { State } from './State';
5
5
  import { Machine, createMachine } from './Machine';
6
6
  import { Actor } from './Actor';
7
- import { raise, send, sendParent, sendUpdate, log, start, stop, assign, after, done, respond, doneInvoke, forwardTo, escalate } from './actions';
7
+ import { raise, send, sendParent, sendUpdate, log, start, stop, assign, after, done, respond, doneInvoke, forwardTo, escalate, choose, pure } from './actions';
8
8
  import { interpret, Interpreter, spawn } from './interpreter';
9
9
  import { matchState } from './match';
10
10
  declare const actions: {
@@ -22,6 +22,8 @@ declare const actions: {
22
22
  respond: typeof respond;
23
23
  forwardTo: typeof forwardTo;
24
24
  escalate: typeof escalate;
25
+ choose: typeof choose;
26
+ pure: typeof pure;
25
27
  };
26
28
  export { Actor, Machine, StateNode, State, matchesState, mapState, actions, assign, send, sendParent, sendUpdate, forwardTo, interpret, Interpreter, matchState, spawn, doneInvoke, createMachine };
27
29
  export * from './types';
package/es/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  export { matchesState } from './utils.js';
2
2
  export { mapState } from './mapState.js';
3
3
  export { ActionTypes, SpecialTargets } from './types.js';
4
- import { raise, send, sendParent, sendUpdate, log, cancel, start, stop, assign, after, done, respond, forwardTo, escalate } from './actions.js';
4
+ import { raise, send, sendParent, sendUpdate, log, cancel, start, stop, assign, after, done, respond, forwardTo, escalate, choose, pure } from './actions.js';
5
5
  export { assign, doneInvoke, forwardTo, send, sendParent, sendUpdate } from './actions.js';
6
6
  export { State } from './State.js';
7
7
  export { StateNode } from './StateNode.js';
@@ -22,6 +22,8 @@ var actions = {
22
22
  done: done,
23
23
  respond: respond,
24
24
  forwardTo: forwardTo,
25
- escalate: escalate
25
+ escalate: escalate,
26
+ choose: choose,
27
+ pure: pure
26
28
  };
27
29
  export { actions };
@@ -1,7 +1,7 @@
1
1
  import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, DoneEvent, Unsubscribable, MachineOptions, SCXML, EventData, Observer, Spawnable, Typestate } from './types';
2
2
  import { State } from './State';
3
3
  import { Actor } from './Actor';
4
- export declare type StateListener<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext> = any> = (state: State<TContext, TEvent, any, TTypestate>, event: TEvent) => void;
4
+ export declare type StateListener<TContext, TEvent extends EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = any> = (state: State<TContext, TEvent, TStateSchema, TTypestate>, event: TEvent) => void;
5
5
  export declare type ContextListener<TContext = DefaultContext> = (context: TContext, prevContext: TContext | undefined) => void;
6
6
  export declare type EventListener<TEvent extends EventObject = EventObject> = (event: TEvent) => void;
7
7
  export declare type Listener = () => void;
@@ -14,7 +14,7 @@ interface SpawnOptions {
14
14
  autoForward?: boolean;
15
15
  sync?: boolean;
16
16
  }
17
- export declare class Interpreter<TContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = any> implements Actor<State<TContext, TEvent>, TEvent> {
17
+ export declare class Interpreter<TContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = any> implements Actor<State<TContext, TEvent, TStateSchema, TTypestate>, TEvent> {
18
18
  machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>;
19
19
  /**
20
20
  * The default interpreter options:
@@ -63,8 +63,8 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
63
63
  * @param options Interpreter options
64
64
  */
65
65
  constructor(machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>, options?: Partial<InterpreterOptions>);
66
- get initialState(): State<TContext, TEvent>;
67
- get state(): State<TContext, TEvent>;
66
+ get initialState(): State<TContext, TEvent, TStateSchema, TTypestate>;
67
+ get state(): State<TContext, TEvent, TStateSchema, TTypestate>;
68
68
  static interpret: typeof interpret;
69
69
  /**
70
70
  * Executes the actions of the given state, with that state's `context` and `event`.
@@ -72,9 +72,9 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
72
72
  * @param state The state whose actions will be executed
73
73
  * @param actionsConfig The action implementations to use
74
74
  */
75
- execute(state: State<TContext, TEvent>, actionsConfig?: MachineOptions<TContext, TEvent>['actions']): void;
75
+ execute(state: State<TContext, TEvent, TStateSchema, TTypestate>, actionsConfig?: MachineOptions<TContext, TEvent>['actions']): void;
76
76
  private update;
77
- onTransition(listener: StateListener<TContext, TEvent, TTypestate>): this;
77
+ onTransition(listener: StateListener<TContext, TEvent, TStateSchema, TTypestate>): this;
78
78
  subscribe(observer: Observer<State<TContext, TEvent, any, TTypestate>>): Unsubscribable;
79
79
  subscribe(nextListener?: (state: State<TContext, TEvent, any, TTypestate>) => void, errorListener?: (error: any) => void, completeListener?: () => void): Unsubscribable;
80
80
  /**
@@ -110,12 +110,12 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
110
110
  /**
111
111
  * Alias for Interpreter.prototype.start
112
112
  */
113
- init: (initialState?: string | State<TContext, TEvent, any, any> | import("./types").StateValueMap | undefined) => Interpreter<TContext, TStateSchema, TEvent, any>;
113
+ init: (initialState?: string | State<TContext, TEvent, TStateSchema, TTypestate> | import("./types").StateValueMap | undefined) => Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
114
114
  /**
115
115
  * Starts the interpreter from the given state, or the initial state.
116
116
  * @param initialState The state to start the statechart from
117
117
  */
118
- start(initialState?: State<TContext, TEvent> | StateValue): Interpreter<TContext, TStateSchema, TEvent>;
118
+ start(initialState?: State<TContext, TEvent, TStateSchema, TTypestate> | StateValue): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
119
119
  /**
120
120
  * Stops the interpreter and unsubscribe all listeners.
121
121
  *
@@ -131,14 +131,14 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
131
131
  *
132
132
  * @param event The event(s) to send
133
133
  */
134
- send: (event: TEvent | TEvent["type"] | Event<TEvent>[] | SCXML.Event<TEvent>, payload?: EventData | undefined) => State<TContext, TEvent, any, any>;
134
+ send: (event: TEvent | TEvent["type"] | Event<TEvent>[] | SCXML.Event<TEvent>, payload?: EventData | undefined) => State<TContext, TEvent, TStateSchema, TTypestate>;
135
135
  private batch;
136
136
  /**
137
137
  * Returns a send function bound to this interpreter instance.
138
138
  *
139
139
  * @param event The event to be sent by the sender.
140
140
  */
141
- sender(event: Event<TEvent>): () => State<TContext, TEvent>;
141
+ sender(event: Event<TEvent>): () => State<TContext, TEvent, TStateSchema, TTypestate>;
142
142
  private sendTo;
143
143
  /**
144
144
  * Returns the next state given the interpreter's current state and the event.
@@ -147,11 +147,12 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
147
147
  *
148
148
  * @param event The event to determine the next state
149
149
  */
150
- nextState(event: Event<TEvent> | SCXML.Event<TEvent>): State<TContext, TEvent>;
150
+ nextState(event: Event<TEvent> | SCXML.Event<TEvent>): State<TContext, TEvent, TStateSchema, TTypestate>;
151
151
  private forward;
152
152
  private defer;
153
153
  private cancel;
154
154
  private exec;
155
+ private removeChild;
155
156
  private stopChild;
156
157
  spawn(entity: Spawnable, name: string, options?: SpawnOptions): Actor;
157
158
  spawnMachine<TChildContext, TChildStateSchema, TChildEvent extends EventObject>(machine: StateMachine<TChildContext, TChildStateSchema, TChildEvent>, options?: {
package/es/interpreter.js CHANGED
@@ -21,9 +21,7 @@ var DEFAULT_SPAWN_OPTIONS = {
21
21
  * @private
22
22
  */
23
23
 
24
- var withServiceScope =
25
- /*#__PURE__*/
26
- function () {
24
+ var withServiceScope = /*#__PURE__*/function () {
27
25
  var serviceStack = [];
28
26
  return function (service, fn) {
29
27
  service && serviceStack.push(service);
@@ -738,9 +736,13 @@ function () {
738
736
  };
739
737
 
740
738
  Interpreter.prototype.exec = function (action, state, actionFunctionMap) {
739
+ if (actionFunctionMap === void 0) {
740
+ actionFunctionMap = this.machine.options.actions;
741
+ }
742
+
741
743
  var context = state.context,
742
744
  _event = state._event;
743
- var actionOrExec = getActionFunction(action.type, actionFunctionMap) || action.exec;
745
+ var actionOrExec = action.exec || getActionFunction(action.type, actionFunctionMap);
744
746
  var exec = isFunction(actionOrExec) ? actionOrExec : actionOrExec ? actionOrExec.exec : action.exec;
745
747
 
746
748
  if (exec) {
@@ -789,7 +791,7 @@ function () {
789
791
  // (such as in transient states)
790
792
  // don't bother starting the activity.
791
793
 
792
- if (!this.state.activities[activity.type]) {
794
+ if (!this.state.activities[activity.id || activity.type]) {
793
795
  break;
794
796
  } // Invoked services
795
797
 
@@ -866,6 +868,12 @@ function () {
866
868
  return undefined;
867
869
  };
868
870
 
871
+ Interpreter.prototype.removeChild = function (childId) {
872
+ this.children.delete(childId);
873
+ this.forwardTo.delete(childId);
874
+ delete this.state.children[childId];
875
+ };
876
+
869
877
  Interpreter.prototype.stopChild = function (childId) {
870
878
  var child = this.children.get(childId);
871
879
 
@@ -873,9 +881,7 @@ function () {
873
881
  return;
874
882
  }
875
883
 
876
- this.children.delete(childId);
877
- this.forwardTo.delete(childId);
878
- delete this.state.children[childId];
884
+ this.removeChild(childId);
879
885
 
880
886
  if (isFunction(child.stop)) {
881
887
  child.stop();
@@ -923,11 +929,6 @@ function () {
923
929
  });
924
930
  }
925
931
 
926
- childService.onDone(function (doneEvent) {
927
- _this.send(toSCXMLEvent(doneEvent, {
928
- origin: childService.id
929
- }));
930
- }).start();
931
932
  var actor = childService;
932
933
  this.children.set(childService.id, actor);
933
934
 
@@ -935,6 +936,13 @@ function () {
935
936
  this.forwardTo.add(childService.id);
936
937
  }
937
938
 
939
+ childService.onDone(function (doneEvent) {
940
+ _this.removeChild(childService.id);
941
+
942
+ _this.send(toSCXMLEvent(doneEvent, {
943
+ origin: childService.id
944
+ }));
945
+ }).start();
938
946
  return actor;
939
947
  };
940
948
 
@@ -944,12 +952,16 @@ function () {
944
952
  var canceled = false;
945
953
  promise.then(function (response) {
946
954
  if (!canceled) {
955
+ _this.removeChild(id);
956
+
947
957
  _this.send(toSCXMLEvent(doneInvoke(id, response), {
948
958
  origin: id
949
959
  }));
950
960
  }
951
961
  }, function (errorData) {
952
962
  if (!canceled) {
963
+ _this.removeChild(id);
964
+
953
965
  var errorEvent = error(id, errorData);
954
966
 
955
967
  try {
@@ -1094,10 +1106,14 @@ function () {
1094
1106
  origin: id
1095
1107
  }));
1096
1108
  }, function (err) {
1109
+ _this.removeChild(id);
1110
+
1097
1111
  _this.send(toSCXMLEvent(error(id, err), {
1098
1112
  origin: id
1099
1113
  }));
1100
1114
  }, function () {
1115
+ _this.removeChild(id);
1116
+
1101
1117
  _this.send(toSCXMLEvent(doneInvoke(id), {
1102
1118
  origin: id
1103
1119
  }));
@@ -1212,9 +1228,7 @@ function () {
1212
1228
  */
1213
1229
 
1214
1230
 
1215
- Interpreter.defaultOptions =
1216
- /*#__PURE__*/
1217
- function (global) {
1231
+ Interpreter.defaultOptions = /*#__PURE__*/function (global) {
1218
1232
  return {
1219
1233
  execute: true,
1220
1234
  deferEvents: true,
package/es/json.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ import { StateNode, ActionObject, Guard, InvokeDefinition } from './';
2
+ interface JSONFunction {
3
+ $function: string;
4
+ }
5
+ export declare function stringifyFunction(fn: Function): JSONFunction;
6
+ interface TransitionConfig {
7
+ target: string[];
8
+ source: string;
9
+ actions: Array<ActionObject<any, any>>;
10
+ cond: Guard<any, any> | undefined;
11
+ eventType: string;
12
+ }
13
+ interface StateNodeConfig {
14
+ type: StateNode['type'];
15
+ id: string;
16
+ key: string;
17
+ initial?: string;
18
+ entry: Array<ActionObject<any, any>>;
19
+ exit: Array<ActionObject<any, any>>;
20
+ on: {
21
+ [key: string]: TransitionConfig[];
22
+ };
23
+ invoke: Array<InvokeDefinition<any, any>>;
24
+ states: Record<string, StateNodeConfig>;
25
+ }
26
+ export declare function machineToJSON(stateNode: StateNode): StateNodeConfig;
27
+ export declare function stringify(machine: StateNode): string;
28
+ export declare function parse(machineString: string): StateNodeConfig;
29
+ export declare function jsonify<T extends Record<string, any>>(value: T): T;
30
+ export {};
31
+ //# sourceMappingURL=json.d.ts.map
package/es/registry.js CHANGED
@@ -1,6 +1,4 @@
1
- var children =
2
- /*#__PURE__*/
3
- new Map();
1
+ var children = /*#__PURE__*/new Map();
4
2
  var sessionIdIndex = 0;
5
3
  var registry = {
6
4
  bookId: function () {
package/es/types.d.ts CHANGED
@@ -50,7 +50,11 @@ export interface AssignMeta<TContext, TEvent extends EventObject> {
50
50
  _event: SCXML.Event<TEvent>;
51
51
  }
52
52
  export declare type ActionFunction<TContext, TEvent extends EventObject> = (context: TContext, event: TEvent, meta: ActionMeta<TContext, TEvent>) => any | void;
53
- export declare type Action<TContext, TEvent extends EventObject> = ActionType | ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent> | AssignAction<Required<TContext>, TEvent> | SendAction<TContext, TEvent> | RaiseAction<AnyEventObject>;
53
+ export interface ChooseConditon<TContext, TEvent extends EventObject> {
54
+ cond?: Condition<TContext, TEvent>;
55
+ actions: Actions<TContext, TEvent>;
56
+ }
57
+ export declare type Action<TContext, TEvent extends EventObject> = ActionType | ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent> | AssignAction<Required<TContext>, TEvent> | SendAction<TContext, TEvent, AnyEventObject> | RaiseAction<AnyEventObject> | ChooseAction<TContext, TEvent>;
54
58
  export declare type Actions<TContext, TEvent extends EventObject> = SingleOrArray<Action<TContext, TEvent>>;
55
59
  export declare type StateKey = string | State<any>;
56
60
  export interface StateValueMap {
@@ -171,21 +175,28 @@ export declare type StatesConfig<TContext, TStateSchema extends StateSchema, TEv
171
175
  export declare type StatesDefinition<TContext, TStateSchema extends StateSchema, TEvent extends EventObject> = {
172
176
  [K in keyof TStateSchema['states']]: StateNodeDefinition<TContext, TStateSchema['states'][K], TEvent>;
173
177
  };
174
- export declare type TransitionConfigTargetShortcut<TContext, TEvent extends EventObject> = string | undefined | StateNode<TContext, any, TEvent>;
178
+ export declare type TransitionConfigTarget<TContext, TEvent extends EventObject> = string | undefined | StateNode<TContext, any, TEvent>;
179
+ export declare type TransitionConfigOrTarget<TContext, TEvent extends EventObject> = SingleOrArray<TransitionConfigTarget<TContext, TEvent> | TransitionConfig<TContext, TEvent>>;
175
180
  declare type TransitionsConfigMap<TContext, TEvent extends EventObject> = {
176
- [K in TEvent['type'] | NullEvent['type'] | '*']?: SingleOrArray<TransitionConfigTargetShortcut<TContext, TEvent> | (TransitionConfig<TContext, K extends TEvent['type'] ? Extract<TEvent, {
181
+ [K in TEvent['type']]?: TransitionConfigOrTarget<TContext, TEvent extends {
177
182
  type: K;
178
- }> : EventObject> & {
179
- event?: undefined;
180
- })>;
183
+ } ? TEvent : never>;
184
+ } & {
185
+ ''?: TransitionConfigOrTarget<TContext, TEvent>;
186
+ } & {
187
+ '*'?: TransitionConfigOrTarget<TContext, TEvent>;
181
188
  };
182
189
  declare type TransitionsConfigArray<TContext, TEvent extends EventObject> = Array<{
183
- [K in TEvent['type'] | NullEvent['type'] | '*']: TransitionConfig<TContext, K extends TEvent['type'] ? Extract<TEvent, {
190
+ [K in TEvent['type']]: TransitionConfig<TContext, TEvent extends {
184
191
  type: K;
185
- }> : EventObject> & {
192
+ } ? TEvent : never> & {
186
193
  event: K;
187
194
  };
188
- }[TEvent['type'] | NullEvent['type'] | '*']>;
195
+ }[TEvent['type']] | (TransitionConfig<TContext, TEvent> & {
196
+ event: '';
197
+ }) | (TransitionConfig<TContext, TEvent> & {
198
+ event: '*';
199
+ })>;
189
200
  export declare type TransitionsConfig<TContext, TEvent extends EventObject> = TransitionsConfigMap<TContext, TEvent> | TransitionsConfigArray<TContext, TEvent>;
190
201
  export declare type InvokeConfig<TContext, TEvent extends EventObject> = {
191
202
  /**
@@ -345,14 +356,15 @@ export interface StateNodeDefinition<TContext, TStateSchema extends StateSchema,
345
356
  id: string;
346
357
  version: string | undefined;
347
358
  key: string;
359
+ context: TContext;
348
360
  type: 'atomic' | 'compound' | 'parallel' | 'final' | 'history';
349
361
  initial: StateNodeConfig<TContext, TStateSchema, TEvent>['initial'];
350
362
  history: boolean | 'shallow' | 'deep' | undefined;
351
363
  states: StatesDefinition<TContext, TStateSchema, TEvent>;
352
364
  on: TransitionDefinitionMap<TContext, TEvent>;
353
365
  transitions: Array<TransitionDefinition<TContext, TEvent>>;
354
- onEntry: Array<ActionObject<TContext, TEvent>>;
355
- onExit: Array<ActionObject<TContext, TEvent>>;
366
+ entry: Array<ActionObject<TContext, TEvent>>;
367
+ exit: Array<ActionObject<TContext, TEvent>>;
356
368
  activities: Array<ActivityDefinition<TContext, TEvent>>;
357
369
  meta: any;
358
370
  order: number;
@@ -478,7 +490,8 @@ export declare enum ActionTypes {
478
490
  ErrorPlatform = "error.platform",
479
491
  ErrorCustom = "xstate.error",
480
492
  Update = "xstate.update",
481
- Pure = "xstate.pure"
493
+ Pure = "xstate.pure",
494
+ Choose = "xstate.choose"
482
495
  }
483
496
  export interface RaiseAction<TEvent extends EventObject> {
484
497
  type: ActionTypes.Raise;
@@ -528,22 +541,22 @@ export interface LogAction<TContext, TEvent extends EventObject> extends ActionO
528
541
  export interface LogActionObject<TContext, TEvent extends EventObject> extends LogAction<TContext, TEvent> {
529
542
  value: any;
530
543
  }
531
- export interface SendAction<TContext, TEvent extends EventObject> extends ActionObject<TContext, TEvent> {
544
+ export interface SendAction<TContext, TEvent extends EventObject, TSentEvent extends EventObject> extends ActionObject<TContext, TEvent> {
532
545
  to: string | number | Actor | ExprWithMeta<TContext, TEvent, string | number | Actor> | undefined;
533
- event: TEvent | SendExpr<TContext, TEvent>;
546
+ event: TSentEvent | SendExpr<TContext, TEvent, TSentEvent>;
534
547
  delay?: number | string | DelayExpr<TContext, TEvent>;
535
548
  id: string | number;
536
549
  }
537
- export interface SendActionObject<TContext, TEvent extends EventObject> extends SendAction<TContext, TEvent> {
550
+ export interface SendActionObject<TContext, TEvent extends EventObject, TSentEvent extends EventObject = AnyEventObject> extends SendAction<TContext, TEvent, TSentEvent> {
538
551
  to: string | number | Actor | undefined;
539
- _event: SCXML.Event<TEvent>;
540
- event: TEvent;
552
+ _event: SCXML.Event<TSentEvent>;
553
+ event: TSentEvent;
541
554
  delay?: number;
542
555
  id: string | number;
543
556
  }
544
557
  export declare type Expr<TContext, TEvent extends EventObject, T> = (context: TContext, event: TEvent) => T;
545
558
  export declare type ExprWithMeta<TContext, TEvent extends EventObject, T> = (context: TContext, event: TEvent, meta: SCXMLEventMeta<TEvent>) => T;
546
- export declare type SendExpr<TContext, TEvent extends EventObject> = ExprWithMeta<TContext, TEvent, TEvent>;
559
+ export declare type SendExpr<TContext, TEvent extends EventObject, TSentEvent extends EventObject = AnyEventObject> = ExprWithMeta<TContext, TEvent, TSentEvent>;
547
560
  export declare enum SpecialTargets {
548
561
  Parent = "#_parent",
549
562
  Internal = "#_internal"
@@ -576,12 +589,23 @@ export interface PureAction<TContext, TEvent extends EventObject> extends Action
576
589
  type: ActionTypes.Pure;
577
590
  get: (context: TContext, event: TEvent) => SingleOrArray<ActionObject<TContext, TEvent>> | undefined;
578
591
  }
592
+ export interface ChooseAction<TContext, TEvent extends EventObject> extends ActionObject<TContext, TEvent> {
593
+ type: ActionTypes.Choose;
594
+ conds: Array<ChooseConditon<TContext, TEvent>>;
595
+ }
579
596
  export interface TransitionDefinition<TContext, TEvent extends EventObject> extends TransitionConfig<TContext, TEvent> {
580
597
  target: Array<StateNode<TContext, any, TEvent>> | undefined;
581
598
  source: StateNode<TContext, any, TEvent>;
582
599
  actions: Array<ActionObject<TContext, TEvent>>;
583
600
  cond?: Guard<TContext, TEvent>;
584
601
  eventType: TEvent['type'] | NullEvent['type'] | '*';
602
+ toJSON: () => {
603
+ target: string[] | undefined;
604
+ source: string;
605
+ actions: Array<ActionObject<TContext, TEvent>>;
606
+ cond?: Guard<TContext, TEvent>;
607
+ eventType: TEvent['type'] | NullEvent['type'] | '*';
608
+ };
585
609
  }
586
610
  export declare type TransitionDefinitionMap<TContext, TEvent extends EventObject> = {
587
611
  [K in TEvent['type'] | NullEvent['type'] | '*']: Array<TransitionDefinition<TContext, K extends TEvent['type'] ? Extract<TEvent, {
package/es/types.js CHANGED
@@ -20,6 +20,7 @@ var ActionTypes;
20
20
  ActionTypes["ErrorCustom"] = "xstate.error";
21
21
  ActionTypes["Update"] = "xstate.update";
22
22
  ActionTypes["Pure"] = "xstate.pure";
23
+ ActionTypes["Choose"] = "xstate.choose";
23
24
  })(ActionTypes || (ActionTypes = {}));
24
25
 
25
26
  var SpecialTargets;
package/es/utils.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Event, StateValue, ActionType, Action, EventObject, PropertyMapper, Mapper, EventType, HistoryValue, AssignAction, Condition, Guard, Subscribable, StateMachine, ConditionPredicate, SCXML, StateLike, EventData, TransitionConfig, TransitionConfigTargetShortcut, NullEvent, SingleOrArray } from './types';
1
+ import { Event, StateValue, ActionType, Action, EventObject, PropertyMapper, Mapper, EventType, HistoryValue, AssignAction, Condition, Subscribable, StateMachine, ConditionPredicate, SCXML, StateLike, EventData, TransitionConfig, TransitionConfigTarget, NullEvent, SingleOrArray, Guard } from './types';
2
2
  import { StateNode } from './StateNode';
3
3
  import { State } from '.';
4
4
  import { Actor } from './Actor';
@@ -59,9 +59,10 @@ export declare function isActor(value: any): value is Actor;
59
59
  export declare const uniqueId: () => string;
60
60
  export declare function toEventObject<TEvent extends EventObject>(event: Event<TEvent>, payload?: EventData): TEvent;
61
61
  export declare function toSCXMLEvent<TEvent extends EventObject>(event: Event<TEvent> | SCXML.Event<TEvent>, scxmlEvent?: Partial<SCXML.Event<TEvent>>): SCXML.Event<TEvent>;
62
- export declare function toTransitionConfigArray<TContext, TEvent extends EventObject>(event: TEvent['type'] | NullEvent['type'] | '*', configLike: SingleOrArray<TransitionConfig<TContext, TEvent> | TransitionConfigTargetShortcut<TContext, TEvent>>): Array<TransitionConfig<TContext, TEvent> & {
62
+ export declare function toTransitionConfigArray<TContext, TEvent extends EventObject>(event: TEvent['type'] | NullEvent['type'] | '*', configLike: SingleOrArray<TransitionConfig<TContext, TEvent> | TransitionConfigTarget<TContext, TEvent>>): Array<TransitionConfig<TContext, TEvent> & {
63
63
  event: TEvent['type'] | NullEvent['type'] | '*';
64
64
  }>;
65
65
  export declare function normalizeTarget<TContext, TEvent extends EventObject>(target: SingleOrArray<string | StateNode<TContext, any, TEvent>> | undefined): Array<string | StateNode<TContext, any, TEvent>> | undefined;
66
66
  export declare function reportUnhandledExceptionOnInvocation(originalError: any, currentError: any, id: string): void;
67
+ export declare function evaluateGuard<TContext, TEvent extends EventObject>(machine: StateNode<TContext, any, TEvent>, guard: Guard<TContext, TEvent>, context: TContext, _event: SCXML.Event<TEvent>, state: State<TContext, TEvent>): boolean;
67
68
  //# sourceMappingURL=utils.d.ts.map
package/es/utils.js CHANGED
@@ -482,9 +482,7 @@ function isObservable(value) {
482
482
  }
483
483
  }
484
484
 
485
- var symbolObservable =
486
- /*#__PURE__*/
487
- function () {
485
+ var symbolObservable = /*#__PURE__*/function () {
488
486
  return typeof Symbol === 'function' && Symbol.observable || '@@observable';
489
487
  }();
490
488
 
@@ -496,9 +494,7 @@ function isMachine(value) {
496
494
  }
497
495
  }
498
496
 
499
- var uniqueId =
500
- /*#__PURE__*/
501
- function () {
497
+ var uniqueId = /*#__PURE__*/function () {
502
498
  var currentId = 0;
503
499
  return function () {
504
500
  currentId++;
@@ -571,4 +567,25 @@ function reportUnhandledExceptionOnInvocation(originalError, currentError, id) {
571
567
  }
572
568
  }
573
569
 
574
- export { flatten, getEventType, isArray, isBuiltInEvent, isFunction, isMachine, isObservable, isPromiseLike, isStateLike, isString, keys, mapContext, mapFilterValues, mapValues, matchesState, nestedPath, normalizeTarget, partition, path, pathToStateValue, reportUnhandledExceptionOnInvocation, symbolObservable, toArray, toArrayStrict, toEventObject, toGuard, toSCXMLEvent, toStatePath, toStatePaths, toStateValue, toTransitionConfigArray, uniqueId, updateContext, updateHistoryStates, updateHistoryValue, warn };
570
+ function evaluateGuard(machine, guard, context, _event, state) {
571
+ var guards = machine.options.guards;
572
+ var guardMeta = {
573
+ state: state,
574
+ cond: guard,
575
+ _event: _event
576
+ }; // TODO: do not hardcode!
577
+
578
+ if (guard.type === DEFAULT_GUARD_TYPE) {
579
+ return guard.predicate(context, _event.data, guardMeta);
580
+ }
581
+
582
+ var condFn = guards[guard.type];
583
+
584
+ if (!condFn) {
585
+ throw new Error("Guard '" + guard.type + "' is not implemented on machine '" + machine.id + "'.");
586
+ }
587
+
588
+ return condFn(context, _event.data, guardMeta);
589
+ }
590
+
591
+ export { evaluateGuard, flatten, getEventType, isArray, isBuiltInEvent, isFunction, isMachine, isObservable, isPromiseLike, isStateLike, isString, keys, mapContext, mapFilterValues, mapValues, matchesState, nestedPath, normalizeTarget, partition, path, pathToStateValue, reportUnhandledExceptionOnInvocation, symbolObservable, toArray, toArrayStrict, toEventObject, toGuard, toSCXMLEvent, toStatePath, toStatePaths, toStateValue, toTransitionConfigArray, uniqueId, updateContext, updateHistoryStates, updateHistoryValue, warn };
package/lib/Machine.d.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  import { StateMachine, MachineOptions, DefaultContext, MachineConfig, StateSchema, EventObject, AnyEventObject, Typestate } from './types';
2
2
  export declare function Machine<TContext = any, TEvent extends EventObject = AnyEventObject>(config: MachineConfig<TContext, any, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>, initialContext?: TContext): StateMachine<TContext, any, TEvent>;
3
3
  export declare function Machine<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = AnyEventObject>(config: MachineConfig<TContext, TStateSchema, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>, initialContext?: TContext): StateMachine<TContext, TStateSchema, TEvent>;
4
- export declare function createMachine<TContext, TEvent extends EventObject = AnyEventObject, TTypestate extends Typestate<TContext> = any>(config: MachineConfig<TContext, any, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>): StateMachine<TContext, any, TEvent, TTypestate>;
4
+ export declare function createMachine<TContext, TEvent extends EventObject = AnyEventObject, TTypestate extends Typestate<TContext> = {
5
+ value: any;
6
+ context: TContext;
7
+ }>(config: MachineConfig<TContext, any, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>): StateMachine<TContext, any, TEvent, TTypestate>;
5
8
  //# sourceMappingURL=Machine.d.ts.map
package/lib/State.d.ts CHANGED
@@ -2,9 +2,12 @@ import { StateValue, ActivityMap, EventObject, HistoryValue, ActionObject, Event
2
2
  import { StateNode } from './StateNode';
3
3
  import { Actor } from './Actor';
4
4
  export declare function stateValuesEqual(a: StateValue | undefined, b: StateValue | undefined): boolean;
5
- export declare function isState<TContext, TEvent extends EventObject>(state: object | string): state is State<TContext, TEvent>;
5
+ export declare function isState<TContext, TEvent extends EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = any>(state: object | string): state is State<TContext, TEvent, TStateSchema, TTypestate>;
6
6
  export declare function bindActionToState<TC, TE extends EventObject>(action: ActionObject<TC, TE>, state: State<TC, TE>): ActionObject<TC, TE>;
7
- export declare class State<TContext, TEvent extends EventObject = EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = any> {
7
+ export declare class State<TContext, TEvent extends EventObject = EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = {
8
+ value: any;
9
+ context: TContext;
10
+ }> {
8
11
  value: StateValue;
9
12
  context: TContext;
10
13
  historyValue?: HistoryValue | undefined;
@@ -86,8 +89,10 @@ export declare class State<TContext, TEvent extends EventObject = EventObject, T
86
89
  * Whether the current state value is a subset of the given parent state value.
87
90
  * @param parentStateValue
88
91
  */
89
- matches<TSV extends TTypestate['value']>(parentStateValue: TSV): this is TTypestate extends {
92
+ matches<TSV extends TTypestate['value']>(parentStateValue: TSV): this is State<(TTypestate extends {
93
+ value: TSV;
94
+ } ? TTypestate : never)['context'], TEvent, TStateSchema, TTypestate> & {
90
95
  value: TSV;
91
- } ? State<TTypestate['context'], TEvent, TStateSchema, TTypestate> : never;
96
+ };
92
97
  }
93
98
  //# sourceMappingURL=State.d.ts.map