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/lib/StateNode.js CHANGED
@@ -3,15 +3,15 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _tslib = require('./_virtual/_tslib.js');
6
+ var utils = require('./utils.js');
6
7
  var types = require('./types.js');
8
+ var State = require('./State.js');
7
9
  var actionTypes = require('./actionTypes.js');
8
- var constants = require('./constants.js');
9
- var environment = require('./environment.js');
10
- var utils = require('./utils.js');
11
10
  var actions = require('./actions.js');
12
- var Actor = require('./Actor.js');
11
+ var environment = require('./environment.js');
12
+ var constants = require('./constants.js');
13
13
  var stateUtils = require('./stateUtils.js');
14
- var State = require('./State.js');
14
+ var Actor = require('./Actor.js');
15
15
  var invokeUtils = require('./invokeUtils.js');
16
16
 
17
17
  var NULL_EVENT = '';
@@ -447,7 +447,8 @@ function () {
447
447
  value: this.resolve(stateFromConfig.value),
448
448
  configuration: configuration,
449
449
  done: stateUtils.isInFinalState(configuration, this),
450
- tags: stateUtils.getTagsFromConfiguration(configuration)
450
+ tags: stateUtils.getTagsFromConfiguration(configuration),
451
+ machine: this.machine
451
452
  }));
452
453
  };
453
454
 
package/lib/actions.js CHANGED
@@ -5,8 +5,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var _tslib = require('./_virtual/_tslib.js');
6
6
  var types = require('./types.js');
7
7
  var actionTypes = require('./actionTypes.js');
8
- var environment = require('./environment.js');
9
8
  var utils = require('./utils.js');
9
+ var environment = require('./environment.js');
10
10
 
11
11
  var initEvent = /*#__PURE__*/utils.toSCXMLEvent({
12
12
  type: actionTypes.init
package/lib/behaviors.js CHANGED
@@ -2,12 +2,78 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- require('./types.js');
6
- require('./actionTypes.js');
7
- require('./environment.js');
8
- var utils = require('./utils.js');
5
+ var actions = require('./actions.js');
9
6
  var Actor = require('./Actor.js');
7
+ var utils = require('./utils.js');
8
+
9
+ /**
10
+ * Returns an actor behavior from a reducer and its initial state.
11
+ *
12
+ * @param transition The pure reducer that returns the next state given the current state and event.
13
+ * @param initialState The initial state of the reducer.
14
+ * @returns An actor behavior
15
+ */
16
+
17
+ function fromReducer(transition, initialState) {
18
+ return {
19
+ transition: transition,
20
+ initialState: initialState
21
+ };
22
+ }
23
+ function fromPromise(promiseFn) {
24
+ var initialState = {
25
+ error: undefined,
26
+ data: undefined,
27
+ status: 'pending'
28
+ };
29
+ return {
30
+ transition: function (state, event, _a) {
31
+ var parent = _a.parent,
32
+ id = _a.id,
33
+ observers = _a.observers;
10
34
 
35
+ switch (event.type) {
36
+ case 'fulfill':
37
+ parent === null || parent === void 0 ? void 0 : parent.send(actions.doneInvoke(id, event.data));
38
+ return {
39
+ error: undefined,
40
+ data: event.data,
41
+ status: 'fulfilled'
42
+ };
43
+
44
+ case 'reject':
45
+ parent === null || parent === void 0 ? void 0 : parent.send(actions.error(id, event.error));
46
+ observers.forEach(function (observer) {
47
+ observer.error(event.error);
48
+ });
49
+ return {
50
+ error: event.error,
51
+ data: undefined,
52
+ status: 'rejected'
53
+ };
54
+
55
+ default:
56
+ return state;
57
+ }
58
+ },
59
+ initialState: initialState,
60
+ start: function (_a) {
61
+ var self = _a.self;
62
+ promiseFn().then(function (data) {
63
+ self.send({
64
+ type: 'fulfill',
65
+ data: data
66
+ });
67
+ }, function (reason) {
68
+ self.send({
69
+ type: 'reject',
70
+ error: reason
71
+ });
72
+ });
73
+ return initialState;
74
+ }
75
+ };
76
+ }
11
77
  function spawnBehavior(behavior, options) {
12
78
  if (options === void 0) {
13
79
  options = {};
@@ -66,4 +132,6 @@ function spawnBehavior(behavior, options) {
66
132
  return actor;
67
133
  }
68
134
 
135
+ exports.fromPromise = fromPromise;
136
+ exports.fromReducer = fromReducer;
69
137
  exports.spawnBehavior = spawnBehavior;
package/lib/devTools.js CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
5
+ var environment = require('./environment.js');
6
+
6
7
  function getGlobal() {
7
8
  if (typeof globalThis !== 'undefined') {
8
9
  return globalThis;
@@ -20,7 +21,9 @@ function getGlobal() {
20
21
  return global;
21
22
  }
22
23
 
23
- return undefined;
24
+ if (!environment.IS_PRODUCTION) {
25
+ console.warn('XState could not find a global object in this environment. Please let the maintainers know and raise an issue here: https://github.com/statelyai/xstate/issues');
26
+ }
24
27
  }
25
28
 
26
29
  function getDevTools() {
package/lib/each.js CHANGED
@@ -1,15 +1,17 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.each = void 0;
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
4
5
  function each(collection, item, indexOrActions, maybeActions) {
5
- var actions = maybeActions || indexOrActions;
6
- var index = maybeActions ? indexOrActions : undefined;
7
- return {
8
- type: 'xstate.foreach',
9
- collection: collection,
10
- item: item,
11
- index: index,
12
- actions: actions
13
- };
6
+ var actions = maybeActions || indexOrActions;
7
+ var index = maybeActions ? indexOrActions : undefined;
8
+ return {
9
+ type: 'xstate.foreach',
10
+ collection: collection,
11
+ item: item,
12
+ index: index,
13
+ actions: actions
14
+ };
14
15
  }
16
+
15
17
  exports.each = each;
package/lib/index.js CHANGED
@@ -2,18 +2,18 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var types = require('./types.js');
6
- var utils = require('./utils.js');
7
5
  var actions = require('./actions.js');
8
6
  var Actor = require('./Actor.js');
9
- var State = require('./State.js');
10
- var behaviors = require('./behaviors.js');
11
7
  var interpreter = require('./interpreter.js');
12
- var StateNode = require('./StateNode.js');
13
8
  var Machine = require('./Machine.js');
14
9
  var mapState = require('./mapState.js');
15
10
  var match = require('./match.js');
16
11
  var schema = require('./schema.js');
12
+ var State = require('./State.js');
13
+ var StateNode = require('./StateNode.js');
14
+ var behaviors = require('./behaviors.js');
15
+ var types = require('./types.js');
16
+ var utils = require('./utils.js');
17
17
 
18
18
  var assign = actions.assign,
19
19
  send = actions.send,
@@ -22,42 +22,36 @@ var assign = actions.assign,
22
22
  forwardTo = actions.forwardTo,
23
23
  doneInvoke = actions.doneInvoke;
24
24
 
25
- Object.defineProperty(exports, 'ActionTypes', {
26
- enumerable: true,
27
- get: function () {
28
- return types.ActionTypes;
29
- }
30
- });
31
- Object.defineProperty(exports, 'SpecialTargets', {
32
- enumerable: true,
33
- get: function () {
34
- return types.SpecialTargets;
35
- }
36
- });
37
- exports.matchesState = utils.matchesState;
38
- exports.toEventObject = utils.toEventObject;
39
- exports.toObserver = utils.toObserver;
40
- exports.toSCXMLEvent = utils.toSCXMLEvent;
41
25
  exports.actions = actions;
42
26
  exports.toActorRef = Actor.toActorRef;
43
- exports.State = State.State;
44
- exports.spawnBehavior = behaviors.spawnBehavior;
45
27
  exports.Interpreter = interpreter.Interpreter;
46
28
  Object.defineProperty(exports, 'InterpreterStatus', {
47
29
  enumerable: true,
48
- get: function () {
49
- return interpreter.InterpreterStatus;
50
- }
30
+ get: function () { return interpreter.InterpreterStatus; }
51
31
  });
52
32
  exports.interpret = interpreter.interpret;
53
33
  exports.spawn = interpreter.spawn;
54
- exports.StateNode = StateNode.StateNode;
55
34
  exports.Machine = Machine.Machine;
56
35
  exports.createMachine = Machine.createMachine;
57
36
  exports.mapState = mapState.mapState;
58
37
  exports.matchState = match.matchState;
59
38
  exports.createSchema = schema.createSchema;
60
39
  exports.t = schema.t;
40
+ exports.State = State.State;
41
+ exports.StateNode = StateNode.StateNode;
42
+ exports.spawnBehavior = behaviors.spawnBehavior;
43
+ Object.defineProperty(exports, 'ActionTypes', {
44
+ enumerable: true,
45
+ get: function () { return types.ActionTypes; }
46
+ });
47
+ Object.defineProperty(exports, 'SpecialTargets', {
48
+ enumerable: true,
49
+ get: function () { return types.SpecialTargets; }
50
+ });
51
+ exports.matchesState = utils.matchesState;
52
+ exports.toEventObject = utils.toEventObject;
53
+ exports.toObserver = utils.toObserver;
54
+ exports.toSCXMLEvent = utils.toSCXMLEvent;
61
55
  exports.assign = assign;
62
56
  exports.doneInvoke = doneInvoke;
63
57
  exports.forwardTo = forwardTo;
@@ -23,7 +23,6 @@ export declare enum InterpreterStatus {
23
23
  Running = 1,
24
24
  Stopped = 2
25
25
  }
26
- /** @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 */
27
26
  export declare class Interpreter<TContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
28
27
  value: any;
29
28
  context: TContext;
@@ -190,8 +189,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
190
189
  toJSON(): {
191
190
  id: string;
192
191
  };
193
- /** @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 */
194
- [symbolObservable](): InteropSubscribable<State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>>;
192
+ [Symbol.observable](): InteropSubscribable<State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>>;
195
193
  getSnapshot(): State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>;
196
194
  }
197
195
  export declare function spawn<T extends Behavior<any, any>>(entity: T, nameOrOptions?: string | SpawnOptions): ActorRefFrom<T>;
@@ -4,31 +4,30 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _tslib = require('./_virtual/_tslib.js');
6
6
  var types = require('./types.js');
7
+ var State = require('./State.js');
7
8
  var actionTypes = require('./actionTypes.js');
9
+ var actions = require('./actions.js');
8
10
  var environment = require('./environment.js');
9
11
  var utils = require('./utils.js');
10
- var actions = require('./actions.js');
11
- var serviceScope = require('./serviceScope.js');
12
+ var scheduler = require('./scheduler.js');
12
13
  var Actor = require('./Actor.js');
13
14
  var stateUtils = require('./stateUtils.js');
14
- var State = require('./State.js');
15
- var scheduler = require('./scheduler.js');
16
15
  var registry = require('./registry.js');
17
16
  var devTools = require('./devTools.js');
17
+ var serviceScope = require('./serviceScope.js');
18
18
  var behaviors = require('./behaviors.js');
19
19
 
20
20
  var DEFAULT_SPAWN_OPTIONS = {
21
21
  sync: false,
22
22
  autoForward: false
23
23
  };
24
+ exports.InterpreterStatus = void 0;
24
25
 
25
26
  (function (InterpreterStatus) {
26
27
  InterpreterStatus[InterpreterStatus["NotStarted"] = 0] = "NotStarted";
27
28
  InterpreterStatus[InterpreterStatus["Running"] = 1] = "Running";
28
29
  InterpreterStatus[InterpreterStatus["Stopped"] = 2] = "Stopped";
29
30
  })(exports.InterpreterStatus || (exports.InterpreterStatus = {}));
30
- /** @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 */
31
-
32
31
 
33
32
  var Interpreter =
34
33
  /*#__PURE__*/
@@ -458,7 +457,13 @@ function () {
458
457
  if (this.status === exports.InterpreterStatus.Running) {
459
458
  // Do not restart the service if it is already started
460
459
  return this;
461
- }
460
+ } // yes, it's a hack but we need the related cache to be populated for some things to work (like delayed transitions)
461
+ // this is usually called by `machine.getInitialState` but if we rehydrate from a state we might bypass this call
462
+ // we also don't want to call this method here as it resolves the full initial state which might involve calling assign actions
463
+ // and that could potentially lead to some unwanted side-effects (even such as creating some rogue actors)
464
+
465
+
466
+ this.machine._init();
462
467
 
463
468
  registry.registry.register(this.sessionId, this);
464
469
  this.initialized = true;
@@ -1313,8 +1318,6 @@ function () {
1313
1318
  id: this.id
1314
1319
  };
1315
1320
  };
1316
- /** @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 */
1317
-
1318
1321
 
1319
1322
  Interpreter.prototype[utils.symbolObservable] = function () {
1320
1323
  return this;
@@ -1335,23 +1338,20 @@ function () {
1335
1338
  */
1336
1339
 
1337
1340
 
1338
- Interpreter.defaultOptions = /*#__PURE__*/function (global) {
1339
- return {
1340
- execute: true,
1341
- deferEvents: true,
1342
- clock: {
1343
- setTimeout: function (fn, ms) {
1344
- return setTimeout(fn, ms);
1345
- },
1346
- clearTimeout: function (id) {
1347
- return clearTimeout(id);
1348
- }
1341
+ Interpreter.defaultOptions = {
1342
+ execute: true,
1343
+ deferEvents: true,
1344
+ clock: {
1345
+ setTimeout: function (fn, ms) {
1346
+ return setTimeout(fn, ms);
1349
1347
  },
1350
- logger: global.console.log.bind(console),
1351
- devTools: false
1352
- };
1353
- }(typeof self !== 'undefined' ? self : global);
1354
-
1348
+ clearTimeout: function (id) {
1349
+ return clearTimeout(id);
1350
+ }
1351
+ },
1352
+ logger: /*#__PURE__*/console.log.bind(console),
1353
+ devTools: false
1354
+ };
1355
1355
  Interpreter.interpret = interpret;
1356
1356
  return Interpreter;
1357
1357
  }();
@@ -5,8 +5,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var _tslib = require('./_virtual/_tslib.js');
6
6
  require('./types.js');
7
7
  var actionTypes = require('./actionTypes.js');
8
- require('./environment.js');
9
8
  require('./utils.js');
9
+ require('./environment.js');
10
10
 
11
11
  function toInvokeSource(src) {
12
12
  if (typeof src === 'string') {
@@ -29,9 +29,9 @@ function toInvokeDefinition(invokeConfig) {
29
29
  type: actionTypes.invoke
30
30
  }, invokeConfig), {
31
31
  toJSON: function () {
32
- var onDone = invokeConfig.onDone,
33
- onError = invokeConfig.onError,
34
- invokeDef = _tslib.__rest(invokeConfig, ["onDone", "onError"]);
32
+ invokeConfig.onDone;
33
+ invokeConfig.onError;
34
+ var invokeDef = _tslib.__rest(invokeConfig, ["onDone", "onError"]);
35
35
 
36
36
  return _tslib.__assign(_tslib.__assign({}, invokeDef), {
37
37
  type: actionTypes.invoke,
package/lib/json.js CHANGED
@@ -1,85 +1,94 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.jsonify = exports.parse = exports.stringify = exports.machineToJSON = exports.stringifyFunction = void 0;
4
- var utils_1 = require("./utils");
5
- // tslint:disable-next-line:ban-types
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var utils = require('./utils.js');
6
+
6
7
  function stringifyFunction(fn) {
7
- return {
8
- $function: fn.toString()
9
- };
8
+ return {
9
+ $function: fn.toString()
10
+ };
10
11
  }
11
- exports.stringifyFunction = stringifyFunction;
12
+
12
13
  function getStateNodeId(stateNode) {
13
- return "#".concat(stateNode.id);
14
- }
15
- // derive config from machine
14
+ return "#".concat(stateNode.id);
15
+ } // derive config from machine
16
+
17
+
16
18
  function machineToJSON(stateNode) {
17
- var config = {
18
- type: stateNode.type,
19
- initial: stateNode.initial === undefined ? undefined : String(stateNode.initial),
20
- id: stateNode.id,
21
- key: stateNode.key,
22
- entry: stateNode.onEntry,
23
- exit: stateNode.onExit,
24
- on: (0, utils_1.mapValues)(stateNode.on, function (transition) {
25
- return transition.map(function (t) {
26
- return {
27
- target: t.target ? t.target.map(getStateNodeId) : [],
28
- source: getStateNodeId(t.source),
29
- actions: t.actions,
30
- cond: t.cond,
31
- eventType: t.eventType
32
- };
33
- });
34
- }),
35
- invoke: stateNode.invoke,
36
- states: {}
37
- };
38
- Object.values(stateNode.states).forEach(function (sn) {
39
- config.states[sn.key] = machineToJSON(sn);
40
- });
41
- return config;
19
+ var config = {
20
+ type: stateNode.type,
21
+ initial: stateNode.initial === undefined ? undefined : String(stateNode.initial),
22
+ id: stateNode.id,
23
+ key: stateNode.key,
24
+ entry: stateNode.onEntry,
25
+ exit: stateNode.onExit,
26
+ on: utils.mapValues(stateNode.on, function (transition) {
27
+ return transition.map(function (t) {
28
+ return {
29
+ target: t.target ? t.target.map(getStateNodeId) : [],
30
+ source: getStateNodeId(t.source),
31
+ actions: t.actions,
32
+ cond: t.cond,
33
+ eventType: t.eventType
34
+ };
35
+ });
36
+ }),
37
+ invoke: stateNode.invoke,
38
+ states: {}
39
+ };
40
+ Object.values(stateNode.states).forEach(function (sn) {
41
+ config.states[sn.key] = machineToJSON(sn);
42
+ });
43
+ return config;
42
44
  }
43
- exports.machineToJSON = machineToJSON;
44
45
  function stringify(machine) {
45
- return JSON.stringify(machineToJSON(machine), function (_, value) {
46
- if ((0, utils_1.isFunction)(value)) {
47
- return { $function: value.toString() };
48
- }
49
- return value;
50
- });
46
+ return JSON.stringify(machineToJSON(machine), function (_, value) {
47
+ if (utils.isFunction(value)) {
48
+ return {
49
+ $function: value.toString()
50
+ };
51
+ }
52
+
53
+ return value;
54
+ });
51
55
  }
52
- exports.stringify = stringify;
53
56
  function parse(machineString) {
54
- var config = JSON.parse(machineString, function (_, value) {
55
- if (typeof value === 'object' && '$function' in value) {
56
- return new Function(value.value);
57
- }
58
- return value;
59
- });
60
- return config;
57
+ var config = JSON.parse(machineString, function (_, value) {
58
+ if (typeof value === 'object' && '$function' in value) {
59
+ return new Function(value.value);
60
+ }
61
+
62
+ return value;
63
+ });
64
+ return config;
61
65
  }
62
- exports.parse = parse;
63
66
  function jsonify(value) {
64
- Object.defineProperty(value, 'toJSON', {
65
- value: function () {
66
- return (0, utils_1.mapValues)(value, function (subValue) {
67
- if ((0, utils_1.isFunction)(subValue)) {
68
- return stringifyFunction(subValue);
69
- }
70
- else if (typeof subValue === 'object' && !Array.isArray(subValue)) {
71
- // mostly for assignments
72
- return (0, utils_1.mapValues)(subValue, function (subSubValue) {
73
- if ((0, utils_1.isFunction)(subSubValue)) {
74
- return stringifyFunction(subSubValue);
75
- }
76
- return subSubValue;
77
- });
78
- }
79
- return subValue;
80
- });
67
+ Object.defineProperty(value, 'toJSON', {
68
+ value: function () {
69
+ return utils.mapValues(value, function (subValue) {
70
+ if (utils.isFunction(subValue)) {
71
+ return stringifyFunction(subValue);
72
+ } else if (typeof subValue === 'object' && !Array.isArray(subValue)) {
73
+ // mostly for assignments
74
+ return utils.mapValues(subValue, function (subSubValue) {
75
+ if (utils.isFunction(subSubValue)) {
76
+ return stringifyFunction(subSubValue);
77
+ }
78
+
79
+ return subSubValue;
80
+ });
81
81
  }
82
- });
83
- return value;
82
+
83
+ return subValue;
84
+ });
85
+ }
86
+ });
87
+ return value;
84
88
  }
89
+
85
90
  exports.jsonify = jsonify;
91
+ exports.machineToJSON = machineToJSON;
92
+ exports.parse = parse;
93
+ exports.stringify = stringify;
94
+ exports.stringifyFunction = stringifyFunction;