xstate 4.30.3 → 4.30.6

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/interpreter.js CHANGED
@@ -1,16 +1,16 @@
1
1
  import { __values, __spreadArray, __read, __assign } from './_virtual/_tslib.js';
2
2
  import { ActionTypes, SpecialTargets } from './types.js';
3
+ import { isStateConfig, State, bindActionToState } from './State.js';
3
4
  import { errorPlatform, log, stop, start, cancel, send, update, error as error$1 } from './actionTypes.js';
5
+ import { doneInvoke, initEvent, getActionFunction, error } from './actions.js';
4
6
  import { IS_PRODUCTION } from './environment.js';
5
7
  import { warn, mapContext, isFunction, toSCXMLEvent, toInvokeSource, isMachine, isPromiseLike, isObservable, isBehavior, reportUnhandledExceptionOnInvocation, symbolObservable, isArray, toEventObject, isString, isActor, uniqueId, toObserver } from './utils.js';
6
- import { doneInvoke, initEvent, getActionFunction, error } from './actions.js';
7
- import { provide, consume } from './serviceScope.js';
8
+ import { Scheduler } from './scheduler.js';
8
9
  import { isSpawnedActor, createDeferredActor } from './Actor.js';
9
10
  import { isInFinalState } from './stateUtils.js';
10
- import { isStateConfig, State, bindActionToState } from './State.js';
11
- import { Scheduler } from './scheduler.js';
12
11
  import { registry } from './registry.js';
13
- import { registerService, getGlobal } from './devTools.js';
12
+ import { getGlobal, registerService } from './devTools.js';
13
+ import { provide, consume } from './serviceScope.js';
14
14
  import { spawnBehavior } from './behaviors.js';
15
15
 
16
16
  var DEFAULT_SPAWN_OPTIONS = {
@@ -24,8 +24,6 @@ var InterpreterStatus;
24
24
  InterpreterStatus[InterpreterStatus["Running"] = 1] = "Running";
25
25
  InterpreterStatus[InterpreterStatus["Stopped"] = 2] = "Stopped";
26
26
  })(InterpreterStatus || (InterpreterStatus = {}));
27
- /** @ts-ignore [symbolObservable] creates problems for people without `skipLibCheck` who are on older versions of TS, remove this comment when we drop support for TS@<4.3 */
28
-
29
27
 
30
28
  var Interpreter =
31
29
  /*#__PURE__*/
@@ -455,7 +453,13 @@ function () {
455
453
  if (this.status === InterpreterStatus.Running) {
456
454
  // Do not restart the service if it is already started
457
455
  return this;
458
- }
456
+ } // yes, it's a hack but we need the related cache to be populated for some things to work (like delayed transitions)
457
+ // this is usually called by `machine.getInitialState` but if we rehydrate from a state we might bypass this call
458
+ // we also don't want to call this method here as it resolves the full initial state which might involve calling assign actions
459
+ // and that could potentially lead to some unwanted side-effects (even such as creating some rogue actors)
460
+
461
+
462
+ this.machine._init();
459
463
 
460
464
  registry.register(this.sessionId, this);
461
465
  this.initialized = true;
@@ -1310,8 +1314,6 @@ function () {
1310
1314
  id: this.id
1311
1315
  };
1312
1316
  };
1313
- /** @ts-ignore this creates problems for people without `skipLibCheck` who are on older versions of TS, remove this comment when we drop support for TS@<4.3 */
1314
-
1315
1317
 
1316
1318
  Interpreter.prototype[symbolObservable] = function () {
1317
1319
  return this;
@@ -1332,23 +1334,20 @@ function () {
1332
1334
  */
1333
1335
 
1334
1336
 
1335
- Interpreter.defaultOptions = /*#__PURE__*/function (global) {
1336
- return {
1337
- execute: true,
1338
- deferEvents: true,
1339
- clock: {
1340
- setTimeout: function (fn, ms) {
1341
- return setTimeout(fn, ms);
1342
- },
1343
- clearTimeout: function (id) {
1344
- return clearTimeout(id);
1345
- }
1337
+ Interpreter.defaultOptions = {
1338
+ execute: true,
1339
+ deferEvents: true,
1340
+ clock: {
1341
+ setTimeout: function (fn, ms) {
1342
+ return setTimeout(fn, ms);
1346
1343
  },
1347
- logger: global.console.log.bind(console),
1348
- devTools: false
1349
- };
1350
- }(typeof self !== 'undefined' ? self : global);
1351
-
1344
+ clearTimeout: function (id) {
1345
+ return clearTimeout(id);
1346
+ }
1347
+ },
1348
+ logger: /*#__PURE__*/console.log.bind(console),
1349
+ devTools: false
1350
+ };
1352
1351
  Interpreter.interpret = interpret;
1353
1352
  return Interpreter;
1354
1353
  }();
package/es/invokeUtils.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { __assign, __rest } from './_virtual/_tslib.js';
2
2
  import './types.js';
3
3
  import { invoke } from './actionTypes.js';
4
- import './environment.js';
5
4
  import './utils.js';
5
+ import './environment.js';
6
6
 
7
7
  function toInvokeSource(src) {
8
8
  if (typeof src === 'string') {
@@ -25,9 +25,9 @@ function toInvokeDefinition(invokeConfig) {
25
25
  type: invoke
26
26
  }, invokeConfig), {
27
27
  toJSON: function () {
28
- var onDone = invokeConfig.onDone,
29
- onError = invokeConfig.onError,
30
- invokeDef = __rest(invokeConfig, ["onDone", "onError"]);
28
+ invokeConfig.onDone;
29
+ invokeConfig.onError;
30
+ var invokeDef = __rest(invokeConfig, ["onDone", "onError"]);
31
31
 
32
32
  return __assign(__assign({}, invokeDef), {
33
33
  type: invoke,
package/es/json.js ADDED
@@ -0,0 +1,86 @@
1
+ import { mapValues, isFunction } from './utils.js';
2
+
3
+ function stringifyFunction(fn) {
4
+ return {
5
+ $function: fn.toString()
6
+ };
7
+ }
8
+
9
+ function getStateNodeId(stateNode) {
10
+ return "#".concat(stateNode.id);
11
+ } // derive config from machine
12
+
13
+
14
+ function machineToJSON(stateNode) {
15
+ var config = {
16
+ type: stateNode.type,
17
+ initial: stateNode.initial === undefined ? undefined : String(stateNode.initial),
18
+ id: stateNode.id,
19
+ key: stateNode.key,
20
+ entry: stateNode.onEntry,
21
+ exit: stateNode.onExit,
22
+ on: mapValues(stateNode.on, function (transition) {
23
+ return transition.map(function (t) {
24
+ return {
25
+ target: t.target ? t.target.map(getStateNodeId) : [],
26
+ source: getStateNodeId(t.source),
27
+ actions: t.actions,
28
+ cond: t.cond,
29
+ eventType: t.eventType
30
+ };
31
+ });
32
+ }),
33
+ invoke: stateNode.invoke,
34
+ states: {}
35
+ };
36
+ Object.values(stateNode.states).forEach(function (sn) {
37
+ config.states[sn.key] = machineToJSON(sn);
38
+ });
39
+ return config;
40
+ }
41
+ function stringify(machine) {
42
+ return JSON.stringify(machineToJSON(machine), function (_, value) {
43
+ if (isFunction(value)) {
44
+ return {
45
+ $function: value.toString()
46
+ };
47
+ }
48
+
49
+ return value;
50
+ });
51
+ }
52
+ function parse(machineString) {
53
+ var config = JSON.parse(machineString, function (_, value) {
54
+ if (typeof value === 'object' && '$function' in value) {
55
+ return new Function(value.value);
56
+ }
57
+
58
+ return value;
59
+ });
60
+ return config;
61
+ }
62
+ function jsonify(value) {
63
+ Object.defineProperty(value, 'toJSON', {
64
+ value: function () {
65
+ return mapValues(value, function (subValue) {
66
+ if (isFunction(subValue)) {
67
+ return stringifyFunction(subValue);
68
+ } else if (typeof subValue === 'object' && !Array.isArray(subValue)) {
69
+ // mostly for assignments
70
+ return mapValues(subValue, function (subSubValue) {
71
+ if (isFunction(subSubValue)) {
72
+ return stringifyFunction(subSubValue);
73
+ }
74
+
75
+ return subSubValue;
76
+ });
77
+ }
78
+
79
+ return subValue;
80
+ });
81
+ }
82
+ });
83
+ return value;
84
+ }
85
+
86
+ export { jsonify, machineToJSON, parse, stringify, stringifyFunction };
package/es/model.js ADDED
@@ -0,0 +1,50 @@
1
+ import { __assign, __spreadArray, __read } from './_virtual/_tslib.js';
2
+ import { assign } from './actions.js';
3
+ import { createMachine } from './Machine.js';
4
+ import { mapValues } from './utils.js';
5
+
6
+ function createModel(initialContext, creators) {
7
+ var eventCreators = creators === null || creators === void 0 ? void 0 : creators.events;
8
+ var actionCreators = creators === null || creators === void 0 ? void 0 : creators.actions;
9
+ var model = {
10
+ initialContext: initialContext,
11
+ assign: assign,
12
+ events: eventCreators ? mapValues(eventCreators, function (fn, eventType) {
13
+ return function () {
14
+ var args = [];
15
+
16
+ for (var _i = 0; _i < arguments.length; _i++) {
17
+ args[_i] = arguments[_i];
18
+ }
19
+
20
+ return __assign(__assign({}, fn.apply(void 0, __spreadArray([], __read(args), false))), {
21
+ type: eventType
22
+ });
23
+ };
24
+ }) : undefined,
25
+ actions: actionCreators ? mapValues(actionCreators, function (fn, actionType) {
26
+ return function () {
27
+ var args = [];
28
+
29
+ for (var _i = 0; _i < arguments.length; _i++) {
30
+ args[_i] = arguments[_i];
31
+ }
32
+
33
+ return __assign(__assign({}, fn.apply(void 0, __spreadArray([], __read(args), false))), {
34
+ type: actionType
35
+ });
36
+ };
37
+ }) : undefined,
38
+ reset: function () {
39
+ return assign(initialContext);
40
+ },
41
+ createMachine: function (config, implementations) {
42
+ return createMachine('context' in config ? config : __assign(__assign({}, config), {
43
+ context: initialContext
44
+ }), implementations);
45
+ }
46
+ };
47
+ return model;
48
+ }
49
+
50
+ export { createModel };
package/es/patterns.js ADDED
@@ -0,0 +1,48 @@
1
+ import { __assign } from './_virtual/_tslib.js';
2
+ import { toEventObject } from './utils.js';
3
+
4
+ function toggle(onState, offState, eventType) {
5
+ var _a, _b, _c;
6
+
7
+ return _a = {}, _a[onState] = {
8
+ on: (_b = {}, _b[eventType] = offState, _b)
9
+ }, _a[offState] = {
10
+ on: (_c = {}, _c[eventType] = onState, _c)
11
+ }, _a;
12
+ }
13
+ var defaultSequencePatternOptions = {
14
+ nextEvent: 'NEXT',
15
+ prevEvent: 'PREV'
16
+ };
17
+ function sequence(items, options) {
18
+ var resolvedOptions = __assign(__assign({}, defaultSequencePatternOptions), options);
19
+
20
+ var states = {};
21
+ var nextEventObject = resolvedOptions.nextEvent === undefined ? undefined : toEventObject(resolvedOptions.nextEvent);
22
+ var prevEventObject = resolvedOptions.prevEvent === undefined ? undefined : toEventObject(resolvedOptions.prevEvent);
23
+ items.forEach(function (item, i) {
24
+ var state = {
25
+ on: {}
26
+ };
27
+
28
+ if (i + 1 === items.length) {
29
+ state.type = 'final';
30
+ }
31
+
32
+ if (nextEventObject && i + 1 < items.length) {
33
+ state.on[nextEventObject.type] = items[i + 1];
34
+ }
35
+
36
+ if (prevEventObject && i > 0) {
37
+ state.on[prevEventObject.type] = items[i - 1];
38
+ }
39
+
40
+ states[item] = state;
41
+ });
42
+ return {
43
+ initial: items[0],
44
+ states: states
45
+ };
46
+ }
47
+
48
+ export { sequence, toggle };
package/es/types.d.ts CHANGED
@@ -526,7 +526,7 @@ export declare type InternalMachineOptions<TContext, TEvent extends EventObject,
526
526
  activities?: Record<string, ActivityConfig<TContext, TEvent>>;
527
527
  };
528
528
  export declare type MachineOptions<TContext, TEvent extends EventObject, TAction extends BaseActionObject = BaseActionObject, TServiceMap extends ServiceMap = ServiceMap, TTypesMeta extends TypegenConstraint = TypegenDisabled> = InternalMachineOptions<TContext, TEvent, ResolveTypegenMeta<TTypesMeta, TEvent, TAction, TServiceMap>>;
529
- export interface MachineConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject, TAction extends BaseActionObject = BaseActionObject, TServiceMap extends ServiceMap = ServiceMap, TTypesMeta = TypegenDisabled> extends StateNodeConfig<NoInfer<TContext>, TStateSchema, TEvent, TAction> {
529
+ export interface MachineConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject, TAction extends BaseActionObject = BaseActionObject, TServiceMap extends ServiceMap = ServiceMap, TTypesMeta = TypegenDisabled> extends StateNodeConfig<NoInfer<TContext>, TStateSchema, NoInfer<TEvent>, TAction> {
530
530
  /**
531
531
  * The initial context (extended state)
532
532
  */
@@ -572,7 +572,7 @@ export interface HistoryStateNode<TContext> extends StateNode<TContext> {
572
572
  export interface StateMachine<TContext, TStateSchema extends StateSchema, TEvent extends EventObject, TTypestate extends Typestate<TContext> = {
573
573
  value: any;
574
574
  context: TContext;
575
- }, TAction extends BaseActionObject = BaseActionObject, TServiceMap extends ServiceMap = ServiceMap, TResolvedTypesMeta = ResolveTypegenMeta<TypegenDisabled, TEvent, TAction, TServiceMap>> extends StateNode<TContext, TStateSchema, TEvent, TTypestate, TServiceMap, TResolvedTypesMeta> {
575
+ }, TAction extends BaseActionObject = BaseActionObject, TServiceMap extends ServiceMap = ServiceMap, TResolvedTypesMeta = ResolveTypegenMeta<TypegenDisabled, NoInfer<TEvent>, TAction, TServiceMap>> extends StateNode<TContext, TStateSchema, TEvent, TTypestate, TServiceMap, TResolvedTypesMeta> {
576
576
  id: string;
577
577
  states: StateNode<TContext, TStateSchema, TEvent, TTypestate, TServiceMap, TResolvedTypesMeta>['states'];
578
578
  withConfig(options: InternalMachineOptions<TContext, TEvent, TResolvedTypesMeta, true>, context?: TContext | (() => TContext)): StateMachine<TContext, TStateSchema, TEvent, TTypestate, TAction, TServiceMap, AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends false ? MarkAllImplementationsAsProvided<TResolvedTypesMeta> : TResolvedTypesMeta>;
@@ -1011,9 +1011,9 @@ export declare type ActorRefWithDeprecatedState<TContext, TEvent extends EventOb
1011
1011
  */
1012
1012
  state: State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>;
1013
1013
  };
1014
- export declare type ActorRefFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer TContext, any, infer TEvent, infer TTypestate, any, any, infer TResolvedTypesMeta> ? ActorRefWithDeprecatedState<TContext, TEvent, TTypestate, TResolvedTypesMeta> : R extends Promise<infer U> ? ActorRef<never, U> : R extends Behavior<infer TEvent, infer TEmitted> ? ActorRef<TEvent, TEmitted> : never : never;
1014
+ export declare type ActorRefFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer TContext, any, infer TEvent, infer TTypestate, any, any, infer TResolvedTypesMeta> ? ActorRefWithDeprecatedState<TContext, TEvent, TTypestate, MarkAllImplementationsAsProvided<TResolvedTypesMeta>> : R extends Promise<infer U> ? ActorRef<never, U> : R extends Behavior<infer TEvent, infer TEmitted> ? ActorRef<TEvent, TEmitted> : never : never;
1015
1015
  export declare type AnyInterpreter = Interpreter<any, any, any, any, any>;
1016
- export declare type InterpreterFrom<T extends AnyStateMachine | ((...args: any[]) => AnyStateMachine)> = T extends StateMachine<infer TContext, infer TStateSchema, infer TEvent, infer TTypestate, any, any, infer TResolvedTypesMeta> ? Interpreter<TContext, TStateSchema, TEvent, TTypestate, TResolvedTypesMeta> : T extends (...args: any[]) => StateMachine<infer TContext, infer TStateSchema, infer TEvent, infer TTypestate, any, any, infer TResolvedTypesMeta> ? Interpreter<TContext, TStateSchema, TEvent, TTypestate, TResolvedTypesMeta> : never;
1016
+ export declare type InterpreterFrom<T extends AnyStateMachine | ((...args: any[]) => AnyStateMachine)> = ReturnTypeOrValue<T> extends StateMachine<infer TContext, infer TStateSchema, infer TEvent, infer TTypestate, any, any, infer TResolvedTypesMeta> ? Interpreter<TContext, TStateSchema, TEvent, TTypestate, MarkAllImplementationsAsProvided<TResolvedTypesMeta>> : never;
1017
1017
  export declare type MachineOptionsFrom<T extends AnyStateMachine | ((...args: any[]) => AnyStateMachine), TRequireMissingImplementations extends boolean = false> = ReturnTypeOrValue<T> extends StateMachine<infer TContext, any, infer TEvent, any, any, any, infer TResolvedTypesMeta> ? InternalMachineOptions<TContext, TEvent, TResolvedTypesMeta, TRequireMissingImplementations> : never;
1018
1018
  export declare type __ResolvedTypesMetaFrom<T> = T extends StateMachine<any, any, any, any, any, any, infer TResolvedTypesMeta> ? TResolvedTypesMeta : never;
1019
1019
  export interface ActorContext<TEvent extends EventObject, TEmitted> {
@@ -1028,10 +1028,10 @@ export interface Behavior<TEvent extends EventObject, TEmitted = any> {
1028
1028
  start?: (actorCtx: ActorContext<TEvent, TEmitted>) => TEmitted;
1029
1029
  }
1030
1030
  export declare type EmittedFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends ActorRef<infer _, infer TEmitted> ? TEmitted : R extends Behavior<infer _, infer TEmitted> ? TEmitted : R extends ActorContext<infer _, infer TEmitted> ? TEmitted : never : never;
1031
- declare type ResolveEventType<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer _, infer __, infer TEvent, infer ____> ? TEvent : R extends Model<infer _, infer TEvent, infer ___, infer ____> ? TEvent : R extends State<infer _, infer TEvent, infer ___, infer ____> ? TEvent : R extends Interpreter<infer _, infer __, infer TEvent, infer ____> ? TEvent : R extends ActorRef<infer TEvent, infer _> ? TEvent : never : never;
1031
+ declare type ResolveEventType<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer _, infer __, infer TEvent, infer ___, infer ____, infer _____, infer ______> ? TEvent : R extends Model<infer _, infer TEvent, infer __, infer ___> ? TEvent : R extends State<infer _, infer TEvent, infer __, infer ___, infer ____> ? TEvent : R extends Interpreter<infer _, infer __, infer TEvent, infer ___, infer ____> ? TEvent : R extends ActorRef<infer TEvent, infer _> ? TEvent : never : never;
1032
1032
  export declare type EventFrom<T, K extends Prop<TEvent, 'type'> = never, TEvent = ResolveEventType<T>> = IsNever<K> extends true ? TEvent : Extract<TEvent, {
1033
1033
  type: K;
1034
1034
  }>;
1035
- export declare type ContextFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer TContext, infer _, infer __, infer ___> ? TContext : R extends Model<infer TContext, infer _, infer __, infer ___> ? TContext : R extends State<infer TContext, infer _, infer __, infer ___> ? TContext : R extends Interpreter<infer TContext, infer _, infer __, infer ___> ? TContext : never : never;
1035
+ export declare type ContextFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer TContext, infer _, infer __, infer ___, infer ____, infer _____, infer ______> ? TContext : R extends Model<infer TContext, infer _, infer __, infer ___> ? TContext : R extends State<infer TContext, infer _, infer __, infer ___, infer ____> ? TContext : R extends Interpreter<infer TContext, infer _, infer __, infer ___, infer ____> ? TContext : never : never;
1036
1036
  export {};
1037
1037
  //# sourceMappingURL=types.d.ts.map
package/es/utils.d.ts CHANGED
@@ -3,6 +3,7 @@ import { StateNode } from './StateNode';
3
3
  import { State } from './State';
4
4
  import { Actor } from './Actor';
5
5
  import { AnyStateMachine } from '.';
6
+ export declare function keys<T extends object>(value: T): Array<keyof T & string>;
6
7
  export declare function matchesState(parentStateId: StateValue, childStateId: StateValue, delimiter?: string): boolean;
7
8
  export declare function getEventType<TEvent extends EventObject = EventObject>(event: Event<TEvent>): TEvent['type'];
8
9
  export declare function getActionType(action: Action<any, any>): ActionType;
package/es/utils.js CHANGED
@@ -1,8 +1,11 @@
1
- import { __assign, __spreadArray, __read, __values } from './_virtual/_tslib.js';
1
+ import { __values, __spreadArray, __read, __assign } from './_virtual/_tslib.js';
2
2
  import { DEFAULT_GUARD_TYPE, TARGETLESS_KEY, STATE_DELIMITER } from './constants.js';
3
3
  import { IS_PRODUCTION } from './environment.js';
4
4
 
5
5
  var _a;
6
+ function keys(value) {
7
+ return Object.keys(value);
8
+ }
6
9
  function matchesState(parentStateId, childStateId, delimiter) {
7
10
  if (delimiter === void 0) {
8
11
  delimiter = STATE_DELIMITER;
@@ -39,6 +42,13 @@ function getEventType(event) {
39
42
  throw new Error('Events must be strings or objects with a string event.type property.');
40
43
  }
41
44
  }
45
+ function getActionType(action) {
46
+ try {
47
+ return isString(action) || typeof action === 'number' ? "".concat(action) : isFunction(action) ? action.name : action.type;
48
+ } catch (e) {
49
+ throw new Error('Actions must be strings or objects with a string action.type property.');
50
+ }
51
+ }
42
52
  function toStatePath(stateId, delimiter) {
43
53
  try {
44
54
  if (isArray(stateId)) {
@@ -213,6 +223,46 @@ function toStatePaths(stateValue) {
213
223
  }));
214
224
  return result;
215
225
  }
226
+ function pathsToStateValue(paths) {
227
+ var e_4, _a;
228
+
229
+ var result = {};
230
+
231
+ if (paths && paths.length === 1 && paths[0].length === 1) {
232
+ return paths[0][0];
233
+ }
234
+
235
+ try {
236
+ for (var paths_1 = __values(paths), paths_1_1 = paths_1.next(); !paths_1_1.done; paths_1_1 = paths_1.next()) {
237
+ var currentPath = paths_1_1.value;
238
+ var marker = result; // tslint:disable-next-line:prefer-for-of
239
+
240
+ for (var i = 0; i < currentPath.length; i++) {
241
+ var subPath = currentPath[i];
242
+
243
+ if (i === currentPath.length - 2) {
244
+ marker[subPath] = currentPath[i + 1];
245
+ break;
246
+ }
247
+
248
+ marker[subPath] = marker[subPath] || {};
249
+ marker = marker[subPath];
250
+ }
251
+ }
252
+ } catch (e_4_1) {
253
+ e_4 = {
254
+ error: e_4_1
255
+ };
256
+ } finally {
257
+ try {
258
+ if (paths_1_1 && !paths_1_1.done && (_a = paths_1.return)) _a.call(paths_1);
259
+ } finally {
260
+ if (e_4) throw e_4.error;
261
+ }
262
+ }
263
+
264
+ return result;
265
+ }
216
266
  function flatten(array) {
217
267
  var _a;
218
268
 
@@ -573,4 +623,4 @@ function createInvokeId(stateNodeId, index) {
573
623
  return "".concat(stateNodeId, ":invocation[").concat(index, "]");
574
624
  }
575
625
 
576
- export { createInvokeId, evaluateGuard, flatten, getEventType, interopSymbols, isActor, isArray, isBehavior, isBuiltInEvent, isFunction, isMachine, isObservable, isPromiseLike, isStateLike, isString, mapContext, mapFilterValues, mapValues, matchesState, nestedPath, normalizeTarget, partition, path, pathToStateValue, reportUnhandledExceptionOnInvocation, symbolObservable, toArray, toArrayStrict, toEventObject, toGuard, toInvokeSource, toObserver, toSCXMLEvent, toStatePath, toStatePaths, toStateValue, toTransitionConfigArray, uniqueId, updateContext, updateHistoryStates, updateHistoryValue, warn };
626
+ export { createInvokeId, evaluateGuard, flatten, getActionType, getEventType, interopSymbols, isActor, isArray, isBehavior, isBuiltInEvent, isFunction, isMachine, isObservable, isPromiseLike, isStateLike, isString, keys, mapContext, mapFilterValues, mapValues, matchesState, nestedPath, normalizeTarget, partition, path, pathToStateValue, pathsToStateValue, reportUnhandledExceptionOnInvocation, symbolObservable, toArray, toArrayStrict, toEventObject, toGuard, toInvokeSource, toObserver, toSCXMLEvent, toStatePath, toStatePaths, toStateValue, toTransitionConfigArray, uniqueId, updateContext, updateHistoryStates, updateHistoryValue, warn };
@@ -1,83 +1,85 @@
1
- "use strict";
2
- var __read = (this && this.__read) || function (o, n) {
3
- var m = typeof Symbol === "function" && o[Symbol.iterator];
4
- if (!m) return o;
5
- var i = m.call(o), r, ar = [], e;
6
- try {
7
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _tslib = require('./_virtual/_tslib.js');
6
+
7
+ var SimulatedClock =
8
+ /*#__PURE__*/
9
+
10
+ /** @class */
11
+ function () {
12
+ function SimulatedClock() {
13
+ this.timeouts = new Map();
14
+ this._now = 0;
15
+ this._id = 0;
16
+ }
17
+
18
+ SimulatedClock.prototype.now = function () {
19
+ return this._now;
20
+ };
21
+
22
+ SimulatedClock.prototype.getId = function () {
23
+ return this._id++;
24
+ };
25
+
26
+ SimulatedClock.prototype.setTimeout = function (fn, timeout) {
27
+ var id = this.getId();
28
+ this.timeouts.set(id, {
29
+ start: this.now(),
30
+ timeout: timeout,
31
+ fn: fn
32
+ });
33
+ return id;
34
+ };
35
+
36
+ SimulatedClock.prototype.clearTimeout = function (id) {
37
+ this.timeouts.delete(id);
38
+ };
39
+
40
+ SimulatedClock.prototype.set = function (time) {
41
+ if (this._now > time) {
42
+ throw new Error('Unable to travel back in time');
8
43
  }
9
- catch (error) { e = { error: error }; }
10
- finally {
11
- try {
12
- if (r && !r.done && (m = i["return"])) m.call(i);
13
- }
14
- finally { if (e) throw e.error; }
15
- }
16
- return ar;
17
- };
18
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20
- if (ar || !(i in from)) {
21
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22
- ar[i] = from[i];
23
- }
24
- }
25
- return to.concat(ar || Array.prototype.slice.call(from));
26
- };
27
- Object.defineProperty(exports, "__esModule", { value: true });
28
- exports.SimulatedClock = void 0;
29
- var SimulatedClock = /** @class */ (function () {
30
- function SimulatedClock() {
31
- this.timeouts = new Map();
32
- this._now = 0;
33
- this._id = 0;
34
- }
35
- SimulatedClock.prototype.now = function () {
36
- return this._now;
37
- };
38
- SimulatedClock.prototype.getId = function () {
39
- return this._id++;
40
- };
41
- SimulatedClock.prototype.setTimeout = function (fn, timeout) {
42
- var id = this.getId();
43
- this.timeouts.set(id, {
44
- start: this.now(),
45
- timeout: timeout,
46
- fn: fn
47
- });
48
- return id;
49
- };
50
- SimulatedClock.prototype.clearTimeout = function (id) {
51
- this.timeouts.delete(id);
52
- };
53
- SimulatedClock.prototype.set = function (time) {
54
- if (this._now > time) {
55
- throw new Error('Unable to travel back in time');
56
- }
57
- this._now = time;
58
- this.flushTimeouts();
59
- };
60
- SimulatedClock.prototype.flushTimeouts = function () {
61
- var _this = this;
62
- __spreadArray([], __read(this.timeouts), false).sort(function (_a, _b) {
63
- var _c = __read(_a, 2), _idA = _c[0], timeoutA = _c[1];
64
- var _d = __read(_b, 2), _idB = _d[0], timeoutB = _d[1];
65
- var endA = timeoutA.start + timeoutA.timeout;
66
- var endB = timeoutB.start + timeoutB.timeout;
67
- return endB > endA ? -1 : 1;
68
- })
69
- .forEach(function (_a) {
70
- var _b = __read(_a, 2), id = _b[0], timeout = _b[1];
71
- if (_this.now() - timeout.start >= timeout.timeout) {
72
- _this.timeouts.delete(id);
73
- timeout.fn.call(null);
74
- }
75
- });
76
- };
77
- SimulatedClock.prototype.increment = function (ms) {
78
- this._now += ms;
79
- this.flushTimeouts();
80
- };
81
- return SimulatedClock;
82
- }());
44
+
45
+ this._now = time;
46
+ this.flushTimeouts();
47
+ };
48
+
49
+ SimulatedClock.prototype.flushTimeouts = function () {
50
+ var _this = this;
51
+
52
+ _tslib.__spreadArray([], _tslib.__read(this.timeouts), false).sort(function (_a, _b) {
53
+ var _c = _tslib.__read(_a, 2);
54
+ _c[0];
55
+ var timeoutA = _c[1];
56
+
57
+ var _d = _tslib.__read(_b, 2);
58
+ _d[0];
59
+ var timeoutB = _d[1];
60
+
61
+ var endA = timeoutA.start + timeoutA.timeout;
62
+ var endB = timeoutB.start + timeoutB.timeout;
63
+ return endB > endA ? -1 : 1;
64
+ }).forEach(function (_a) {
65
+ var _b = _tslib.__read(_a, 2),
66
+ id = _b[0],
67
+ timeout = _b[1];
68
+
69
+ if (_this.now() - timeout.start >= timeout.timeout) {
70
+ _this.timeouts.delete(id);
71
+
72
+ timeout.fn.call(null);
73
+ }
74
+ });
75
+ };
76
+
77
+ SimulatedClock.prototype.increment = function (ms) {
78
+ this._now += ms;
79
+ this.flushTimeouts();
80
+ };
81
+
82
+ return SimulatedClock;
83
+ }();
84
+
83
85
  exports.SimulatedClock = SimulatedClock;
package/lib/State.js CHANGED
@@ -4,10 +4,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _tslib = require('./_virtual/_tslib.js');
6
6
  var constants = require('./constants.js');
7
- var environment = require('./environment.js');
8
7
  var utils = require('./utils.js');
9
- var actions = require('./actions.js');
10
8
  var stateUtils = require('./stateUtils.js');
9
+ var actions = require('./actions.js');
10
+ var environment = require('./environment.js');
11
11
 
12
12
  function stateValuesEqual(a, b) {
13
13
  if (a === b) {
@@ -35,6 +35,11 @@ function isStateConfig(state) {
35
35
 
36
36
  return 'value' in state && '_event' in state;
37
37
  }
38
+ /**
39
+ * @deprecated Use `isStateConfig(object)` or `state instanceof State` instead.
40
+ */
41
+
42
+ var isState = isStateConfig;
38
43
  function bindActionToState(action, state) {
39
44
  var exec = action.exec;
40
45
 
@@ -219,12 +224,12 @@ function () {
219
224
  };
220
225
 
221
226
  State.prototype.toJSON = function () {
222
- var _a = this,
223
- configuration = _a.configuration,
224
- transitions = _a.transitions,
225
- tags = _a.tags,
226
- machine = _a.machine,
227
- jsonValues = _tslib.__rest(_a, ["configuration", "transitions", "tags", "machine"]);
227
+ var _a = this;
228
+ _a.configuration;
229
+ _a.transitions;
230
+ var tags = _a.tags;
231
+ _a.machine;
232
+ var jsonValues = _tslib.__rest(_a, ["configuration", "transitions", "tags", "machine"]);
228
233
 
229
234
  return _tslib.__assign(_tslib.__assign({}, jsonValues), {
230
235
  tags: Array.from(tags)
@@ -272,5 +277,6 @@ function () {
272
277
 
273
278
  exports.State = State;
274
279
  exports.bindActionToState = bindActionToState;
280
+ exports.isState = isState;
275
281
  exports.isStateConfig = isStateConfig;
276
282
  exports.stateValuesEqual = stateValuesEqual;