xstate 4.27.0 → 4.30.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.
Files changed (59) hide show
  1. package/CHANGELOG.md +227 -90
  2. package/README.md +5 -5
  3. package/dist/xstate.interpreter.js +1 -1
  4. package/dist/xstate.js +1 -1
  5. package/dist/xstate.web.js +2 -2
  6. package/es/Actor.d.ts +1 -2
  7. package/es/Actor.js +5 -5
  8. package/es/Machine.d.ts +5 -4
  9. package/es/State.d.ts +17 -14
  10. package/es/State.js +4 -4
  11. package/es/StateNode.d.ts +22 -17
  12. package/es/StateNode.js +33 -36
  13. package/es/actions.d.ts +3 -4
  14. package/es/actions.js +22 -3
  15. package/es/behaviors.d.ts +1 -1
  16. package/es/devTools.d.ts +3 -4
  17. package/es/each.d.ts +1 -1
  18. package/es/index.d.ts +10 -21
  19. package/es/index.js +11 -22
  20. package/es/interpreter.d.ts +34 -31
  21. package/es/interpreter.js +21 -13
  22. package/es/model.d.ts +2 -2
  23. package/es/model.types.d.ts +8 -9
  24. package/es/schema.d.ts +1 -0
  25. package/es/schema.js +2 -1
  26. package/es/scxml.d.ts +2 -2
  27. package/es/stateUtils.d.ts +6 -5
  28. package/es/typegenTypes.d.ts +121 -0
  29. package/es/types.d.ts +118 -57
  30. package/es/utils.d.ts +7 -2
  31. package/es/utils.js +9 -2
  32. package/lib/Actor.d.ts +1 -2
  33. package/lib/Actor.js +4 -4
  34. package/lib/Machine.d.ts +5 -4
  35. package/lib/State.d.ts +17 -14
  36. package/lib/State.js +4 -4
  37. package/lib/StateNode.d.ts +22 -17
  38. package/lib/StateNode.js +32 -35
  39. package/lib/actions.d.ts +3 -4
  40. package/lib/actions.js +20 -0
  41. package/lib/behaviors.d.ts +1 -1
  42. package/lib/devTools.d.ts +3 -4
  43. package/lib/each.d.ts +1 -1
  44. package/lib/index.d.ts +10 -21
  45. package/lib/index.js +15 -26
  46. package/lib/interpreter.d.ts +34 -31
  47. package/lib/interpreter.js +18 -10
  48. package/lib/model.d.ts +2 -2
  49. package/lib/model.types.d.ts +8 -9
  50. package/lib/schema.d.ts +1 -0
  51. package/lib/schema.js +2 -0
  52. package/lib/scxml.d.ts +2 -2
  53. package/lib/stateUtils.d.ts +6 -5
  54. package/lib/typegenTypes.d.ts +121 -0
  55. package/lib/typegenTypes.js +2 -0
  56. package/lib/types.d.ts +118 -57
  57. package/lib/utils.d.ts +7 -2
  58. package/lib/utils.js +10 -1
  59. package/package.json +5 -5
package/es/interpreter.js CHANGED
@@ -1,11 +1,11 @@
1
- import { __values, __assign, __spreadArray, __read } from './_virtual/_tslib.js';
1
+ import { __values, __spreadArray, __read, __assign } from './_virtual/_tslib.js';
2
2
  import { IS_PRODUCTION } from './environment.js';
3
- import { warn, mapContext, isFunction, keys, toSCXMLEvent, toInvokeSource, isMachine, isPromiseLike, isObservable, isBehavior, reportUnhandledExceptionOnInvocation, symbolObservable, isArray, toEventObject, isString, isActor, uniqueId, toObserver } from './utils.js';
3
+ import { warn, mapContext, isFunction, keys, toSCXMLEvent, toInvokeSource, isMachine, isPromiseLike, isObservable, isBehavior, reportUnhandledExceptionOnInvocation, interopSymbols, symbolObservable, isArray, toEventObject, isString, isActor, toObserver, uniqueId } from './utils.js';
4
4
  import { ActionTypes, SpecialTargets } from './types.js';
5
5
  import { isInFinalState } from './stateUtils.js';
6
6
  import { errorPlatform, log, stop, start, cancel, send, update, error as error$1 } from './actionTypes.js';
7
7
  import { doneInvoke, initEvent, getActionFunction, error } from './actions.js';
8
- import { isState, State, bindActionToState } from './State.js';
8
+ import { isStateConfig, State, bindActionToState } from './State.js';
9
9
  import { provide, consume } from './serviceScope.js';
10
10
  import { isSpawnedActor, createDeferredActor } from './Actor.js';
11
11
  import { Scheduler } from './scheduler.js';
@@ -459,7 +459,7 @@ function () {
459
459
  this.initialized = true;
460
460
  this.status = InterpreterStatus.Running;
461
461
  var resolvedState = initialState === undefined ? this.initialState : provide(this, function () {
462
- return isState(initialState) ? _this.machine.resolveState(initialState) : _this.machine.resolveState(State.from(initialState, _this.machine.context));
462
+ return isStateConfig(initialState) ? _this.machine.resolveState(initialState) : _this.machine.resolveState(State.from(initialState, _this.machine.context));
463
463
  });
464
464
 
465
465
  if (this.options.devTools) {
@@ -558,7 +558,9 @@ function () {
558
558
  return this;
559
559
  }
560
560
 
561
- this.state.configuration.forEach(function (stateNode) {
561
+ __spreadArray([], __read(this.state.configuration), false).sort(function (a, b) {
562
+ return b.order - a.order;
563
+ }).forEach(function (stateNode) {
562
564
  var e_11, _a;
563
565
 
564
566
  try {
@@ -580,6 +582,7 @@ function () {
580
582
  }
581
583
  }); // Stop all children
582
584
 
585
+
583
586
  this.children.forEach(function (child) {
584
587
  if (isFunction(child.stop)) {
585
588
  child.stop();
@@ -1036,7 +1039,8 @@ function () {
1036
1039
  }
1037
1040
  }
1038
1041
  });
1039
- var actor = {
1042
+
1043
+ var actor = __assign({
1040
1044
  id: id,
1041
1045
  send: function () {
1042
1046
  return void 0;
@@ -1080,7 +1084,8 @@ function () {
1080
1084
  getSnapshot: function () {
1081
1085
  return resolvedData;
1082
1086
  }
1083
- };
1087
+ }, interopSymbols);
1088
+
1084
1089
  this.children.set(id, actor);
1085
1090
  return actor;
1086
1091
  };
@@ -1124,7 +1129,7 @@ function () {
1124
1129
  return this.spawnPromise(callbackStop, id);
1125
1130
  }
1126
1131
 
1127
- var actor = {
1132
+ var actor = __assign({
1128
1133
  id: id,
1129
1134
  send: function (event) {
1130
1135
  return receivers.forEach(function (receiver) {
@@ -1154,7 +1159,8 @@ function () {
1154
1159
  getSnapshot: function () {
1155
1160
  return emitted;
1156
1161
  }
1157
- };
1162
+ }, interopSymbols);
1163
+
1158
1164
  this.children.set(id, actor);
1159
1165
  return actor;
1160
1166
  };
@@ -1182,7 +1188,8 @@ function () {
1182
1188
  origin: id
1183
1189
  }));
1184
1190
  });
1185
- var actor = {
1191
+
1192
+ var actor = __assign({
1186
1193
  id: id,
1187
1194
  send: function () {
1188
1195
  return void 0;
@@ -1201,7 +1208,8 @@ function () {
1201
1208
  id: id
1202
1209
  };
1203
1210
  }
1204
- };
1211
+ }, interopSymbols);
1212
+
1205
1213
  this.children.set(id, actor);
1206
1214
  return actor;
1207
1215
  };
@@ -1229,7 +1237,7 @@ function () {
1229
1237
  };
1230
1238
 
1231
1239
  Interpreter.prototype.spawnEffect = function (id, dispose) {
1232
- this.children.set(id, {
1240
+ this.children.set(id, __assign({
1233
1241
  id: id,
1234
1242
  send: function () {
1235
1243
  return void 0;
@@ -1250,7 +1258,7 @@ function () {
1250
1258
  id: id
1251
1259
  };
1252
1260
  }
1253
- });
1261
+ }, interopSymbols));
1254
1262
  };
1255
1263
 
1256
1264
  Interpreter.prototype.attachDev = function () {
package/es/model.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { EventObject, BaseActionObject } from './types';
2
- import { Cast, UnionFromCreatorsReturnTypes, FinalModelCreators, Model, ModelCreators, Prop, IsNever } from './model.types';
1
+ import type { Cast, EventObject, BaseActionObject, Prop, IsNever } from './types';
2
+ import { UnionFromCreatorsReturnTypes, FinalModelCreators, Model, ModelCreators } from './model.types';
3
3
  export declare function createModel<TContext, TEvent extends EventObject, TAction extends BaseActionObject = BaseActionObject>(initialContext: TContext): Model<TContext, TEvent, TAction, void>;
4
4
  export declare function createModel<TContext, TModelCreators extends ModelCreators<TModelCreators>, TFinalModelCreators = FinalModelCreators<TModelCreators>, TComputedEvent = UnionFromCreatorsReturnTypes<Prop<TFinalModelCreators, 'events'>>, TComputedAction = UnionFromCreatorsReturnTypes<Prop<TFinalModelCreators, 'actions'>>>(initialContext: TContext, creators: TModelCreators): Model<TContext, Cast<TComputedEvent, EventObject>, IsNever<TComputedAction> extends true ? BaseActionObject : Cast<TComputedAction, BaseActionObject>, TFinalModelCreators>;
5
5
  //# sourceMappingURL=model.d.ts.map
@@ -1,18 +1,17 @@
1
- import { EventObject, Assigner, ExtractEvent, PropertyAssigner, AssignAction, MachineConfig, MachineOptions, StateMachine, BaseActionObject } from './types';
2
- export declare type AnyFunction = (...args: any[]) => any;
3
- export declare type IsNever<T> = [T] extends [never] ? true : false;
4
- export declare type Cast<T extends any, TCastType extends any> = T extends TCastType ? T : TCastType;
5
- export declare type Compute<A extends any> = {
6
- [K in keyof A]: A[K];
7
- } & unknown;
8
- export declare type Prop<T, K> = K extends keyof T ? T[K] : never;
1
+ import { AnyFunction, AssignAction, Assigner, BaseActionObject, Compute, EventObject, ExtractEvent, MachineConfig, Prop, PropertyAssigner, StateMachine, InternalMachineOptions, ServiceMap } from './types';
2
+ import { ResolveTypegenMeta, TypegenConstraint, TypegenDisabled } from './typegenTypes';
9
3
  export interface Model<TContext, TEvent extends EventObject, TAction extends BaseActionObject = BaseActionObject, TModelCreators = void> {
10
4
  initialContext: TContext;
11
5
  assign: <TEventType extends TEvent['type'] = TEvent['type']>(assigner: Assigner<TContext, ExtractEvent<TEvent, TEventType>> | PropertyAssigner<TContext, ExtractEvent<TEvent, TEventType>>, eventType?: TEventType) => AssignAction<TContext, ExtractEvent<TEvent, TEventType>>;
12
6
  events: Prop<TModelCreators, 'events'>;
13
7
  actions: Prop<TModelCreators, 'actions'>;
14
8
  reset: () => AssignAction<TContext, any>;
15
- createMachine: (config: MachineConfig<TContext, any, TEvent, TAction>, implementations?: Partial<MachineOptions<TContext, TEvent, TAction>>) => StateMachine<TContext, any, TEvent>;
9
+ createMachine: {
10
+ <TServiceMap extends ServiceMap = ServiceMap, TTypesMeta extends TypegenConstraint = TypegenDisabled>(config: MachineConfig<TContext, any, TEvent, TAction, TServiceMap, TTypesMeta>, implementations?: InternalMachineOptions<TContext, TEvent, ResolveTypegenMeta<TTypesMeta, TEvent, TAction, TServiceMap>>): StateMachine<TContext, any, TEvent, {
11
+ value: any;
12
+ context: TContext;
13
+ }, TAction, TServiceMap, ResolveTypegenMeta<TTypesMeta, TEvent, TAction, TServiceMap>>;
14
+ };
16
15
  }
17
16
  export declare type ModelContextFrom<TModel extends Model<any, any, any, any>> = TModel extends Model<infer TContext, any, any, any> ? TContext : never;
18
17
  export declare type ModelEventsFrom<TModel extends Model<any, any, any, any> | undefined> = TModel extends Model<any, infer TEvent, any, any> ? TEvent : EventObject;
package/es/schema.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export declare function createSchema<T>(schema?: any): T;
2
+ export declare const t: typeof createSchema;
2
3
  //# sourceMappingURL=schema.d.ts.map
package/es/schema.js CHANGED
@@ -1,5 +1,6 @@
1
1
  function createSchema(schema) {
2
2
  return schema;
3
3
  }
4
+ var t = createSchema;
4
5
 
5
- export { createSchema };
6
+ export { createSchema, t };
package/es/scxml.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { StateNode } from './index';
1
+ import { StateMachine } from './index';
2
2
  export interface ScxmlToMachineOptions {
3
3
  delimiter?: string;
4
4
  }
5
- export declare function toMachine(xml: string, options: ScxmlToMachineOptions): StateNode;
5
+ export declare function toMachine(xml: string, options: ScxmlToMachineOptions): StateMachine<any, any, any, any, any, any, any>;
6
6
  //# sourceMappingURL=scxml.d.ts.map
@@ -1,15 +1,16 @@
1
- import { EventObject, StateNode, StateValue } from '.';
1
+ import { EventObject, StateValue } from './types';
2
+ import { StateNode } from './StateNode';
2
3
  declare type Configuration<TC, TE extends EventObject> = Iterable<StateNode<TC, any, TE>>;
3
4
  declare type AdjList<TC, TE extends EventObject> = Map<StateNode<TC, any, TE>, Array<StateNode<TC, any, TE>>>;
4
- export declare const isLeafNode: (stateNode: StateNode<any, any, any, any>) => boolean;
5
+ export declare const isLeafNode: (stateNode: StateNode<any, any, any, any, any, any>) => boolean;
5
6
  export declare function getChildren<TC, TE extends EventObject>(stateNode: StateNode<TC, any, TE>): Array<StateNode<TC, any, TE>>;
6
- export declare function getAllStateNodes<TC, TE extends EventObject>(stateNode: StateNode<TC, any, TE, any>): Array<StateNode<TC, any, TE, any>>;
7
- export declare function getConfiguration<TC, TE extends EventObject>(prevStateNodes: Iterable<StateNode<TC, any, TE, any>>, stateNodes: Iterable<StateNode<TC, any, TE, any>>): Set<StateNode<TC, any, TE, any>>;
7
+ export declare function getAllStateNodes<TC, TE extends EventObject>(stateNode: StateNode<TC, any, TE, any, any, any>): Array<StateNode<TC, any, TE, any, any, any>>;
8
+ export declare function getConfiguration<TC, TE extends EventObject>(prevStateNodes: Iterable<StateNode<TC, any, TE, any, any, any>>, stateNodes: Iterable<StateNode<TC, any, TE, any, any, any>>): Set<StateNode<TC, any, TE, any, any, any>>;
8
9
  export declare function getAdjList<TC, TE extends EventObject>(configuration: Configuration<TC, TE>): AdjList<TC, TE>;
9
10
  export declare function getValue<TC, TE extends EventObject>(rootNode: StateNode<TC, any, TE, any>, configuration: Configuration<TC, TE>): StateValue;
10
11
  export declare function has<T>(iterable: Iterable<T>, item: T): boolean;
11
12
  export declare function nextEvents<TC, TE extends EventObject>(configuration: Array<StateNode<TC, any, TE>>): Array<TE['type']>;
12
- export declare function isInFinalState<TC, TE extends EventObject>(configuration: Array<StateNode<TC, any, TE, any>>, stateNode: StateNode<TC, any, TE, any>): boolean;
13
+ export declare function isInFinalState<TC, TE extends EventObject>(configuration: Array<StateNode<TC, any, TE, any, any, any>>, stateNode: StateNode<TC, any, TE, any, any, any>): boolean;
13
14
  export declare function getMeta(configuration?: StateNode[]): Record<string, any>;
14
15
  export declare function getTagsFromConfiguration(configuration: StateNode<any, any, any, any>[]): Set<string>;
15
16
  export {};
@@ -0,0 +1,121 @@
1
+ import { BaseActionObject, EventObject, IndexByType, IsNever, Prop, Values, IsAny, ServiceMap, Cast } from './types';
2
+ export interface TypegenDisabled {
3
+ '@@xstate/typegen': false;
4
+ }
5
+ export interface TypegenEnabled {
6
+ '@@xstate/typegen': true;
7
+ }
8
+ export interface TypegenMeta extends TypegenEnabled {
9
+ /**
10
+ * Allows you to specify all the results of state.matches
11
+ */
12
+ matchesStates: string | {};
13
+ /**
14
+ * Allows you to specify all tags used by the machine
15
+ */
16
+ tags: string;
17
+ /**
18
+ * Allows you to specify all the missing implementations
19
+ * of the machine
20
+ */
21
+ missingImplementations: {
22
+ actions: string;
23
+ delays: string;
24
+ guards: string;
25
+ services: string;
26
+ };
27
+ /**
28
+ * A map for the internal events of the machine.
29
+ *
30
+ * key: 'done.invoke.myService'
31
+ * value: {
32
+ * type: 'done.invoke.myService';
33
+ * data: unknown;
34
+ * __tip: 'Declare the type in event types!';
35
+ * }
36
+ */
37
+ internalEvents: {};
38
+ /**
39
+ * Maps the name of the service to the event type
40
+ * of the done.invoke action
41
+ *
42
+ * key: 'invokeSrc'
43
+ * value: 'done.invoke.invokeName'
44
+ */
45
+ invokeSrcNameMap: Record<string, string>;
46
+ /**
47
+ * Keeps track of which events lead to which
48
+ * actions.
49
+ *
50
+ * Key: 'EVENT_NAME'
51
+ * Value: 'actionName' | 'otherActionName'
52
+ */
53
+ eventsCausingActions: Record<string, string>;
54
+ /**
55
+ * Keeps track of which events lead to which
56
+ * delays.
57
+ *
58
+ * Key: 'EVENT_NAME'
59
+ * Value: 'delayName' | 'otherDelayName'
60
+ */
61
+ eventsCausingDelays: Record<string, string>;
62
+ /**
63
+ * Keeps track of which events lead to which
64
+ * guards.
65
+ *
66
+ * Key: 'EVENT_NAME'
67
+ * Value: 'guardName' | 'otherGuardName'
68
+ */
69
+ eventsCausingGuards: Record<string, string>;
70
+ /**
71
+ * Keeps track of which events lead to which
72
+ * services.
73
+ *
74
+ * Key: 'EVENT_NAME'
75
+ * Value: 'serviceName' | 'otherServiceName'
76
+ */
77
+ eventsCausingServices: Record<string, string>;
78
+ }
79
+ export interface ResolvedTypegenMeta extends TypegenMeta {
80
+ indexedActions: Record<string, BaseActionObject>;
81
+ indexedEvents: Record<string, EventObject>;
82
+ }
83
+ export declare type TypegenConstraint = TypegenEnabled | TypegenDisabled;
84
+ export declare type AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta, TMissingImplementations = Prop<TResolvedTypesMeta, 'missingImplementations'>> = IsAny<TResolvedTypesMeta> extends true ? true : TResolvedTypesMeta extends TypegenEnabled ? IsNever<Values<{
85
+ [K in keyof TMissingImplementations]: TMissingImplementations[K];
86
+ }>> extends true ? true : false : true;
87
+ export declare type MarkAllImplementationsAsProvided<TResolvedTypesMeta> = TResolvedTypesMeta & {
88
+ missingImplementations: {
89
+ actions: never;
90
+ delays: never;
91
+ guards: never;
92
+ services: never;
93
+ };
94
+ };
95
+ declare type GenerateServiceEvent<TServiceName, TEventType, TServiceMap extends ServiceMap> = TEventType extends any ? {
96
+ type: TEventType;
97
+ } & Prop<TServiceMap, TServiceName> : never;
98
+ declare type GenerateServiceEvents<TServiceMap extends ServiceMap, TInvokeSrcNameMap> = string extends keyof TServiceMap ? never : Cast<{
99
+ [K in keyof TInvokeSrcNameMap]: GenerateServiceEvent<K, TInvokeSrcNameMap[K], TServiceMap>;
100
+ }[keyof TInvokeSrcNameMap], EventObject>;
101
+ declare type MergeWithInternalEvents<TIndexedEvents, TInternalEvents> = TIndexedEvents & Pick<TInternalEvents, Exclude<keyof TInternalEvents, keyof TIndexedEvents>>;
102
+ declare type AllowAllEvents = {
103
+ eventsCausingActions: Record<string, string>;
104
+ eventsCausingDelays: Record<string, string>;
105
+ eventsCausingGuards: Record<string, string>;
106
+ eventsCausingServices: Record<string, string>;
107
+ };
108
+ export declare type ResolveTypegenMeta<TTypesMeta extends TypegenConstraint, TEvent extends EventObject, TAction extends BaseActionObject, TServiceMap extends ServiceMap> = TTypesMeta extends TypegenEnabled ? TTypesMeta & {
109
+ indexedActions: IndexByType<TAction>;
110
+ indexedEvents: MergeWithInternalEvents<IndexByType<(string extends TEvent['type'] ? never : TEvent) | GenerateServiceEvents<TServiceMap, Prop<TTypesMeta, 'invokeSrcNameMap'>>>, Prop<TTypesMeta, 'internalEvents'>>;
111
+ } : MarkAllImplementationsAsProvided<TypegenDisabled> & AllowAllEvents & {
112
+ indexedActions: IndexByType<TAction>;
113
+ indexedEvents: Record<string, TEvent> & {
114
+ __XSTATE_ALLOW_ANY_INVOKE_DATA_HACK__: {
115
+ data: any;
116
+ };
117
+ };
118
+ invokeSrcNameMap: Record<string, '__XSTATE_ALLOW_ANY_INVOKE_DATA_HACK__'>;
119
+ };
120
+ export {};
121
+ //# sourceMappingURL=typegenTypes.d.ts.map