xstate 4.30.2 → 4.30.5

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/utils.js CHANGED
@@ -1,4 +1,4 @@
1
- import { __spreadArray, __read, __values, __assign } 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
 
@@ -27,7 +27,7 @@ function matchesState(parentStateId, childStateId, delimiter) {
27
27
  return parentStateValue in childStateValue;
28
28
  }
29
29
 
30
- return keys(parentStateValue).every(function (key) {
30
+ return Object.keys(parentStateValue).every(function (key) {
31
31
  if (!(key in childStateValue)) {
32
32
  return false;
33
33
  }
@@ -42,6 +42,13 @@ function getEventType(event) {
42
42
  throw new Error('Events must be strings or objects with a string event.type property.');
43
43
  }
44
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
+ }
45
52
  function toStatePath(stateId, delimiter) {
46
53
  try {
47
54
  if (isArray(stateId)) {
@@ -93,7 +100,7 @@ function pathToStateValue(statePath) {
93
100
  }
94
101
  function mapValues(collection, iteratee) {
95
102
  var result = {};
96
- var collectionKeys = keys(collection);
103
+ var collectionKeys = Object.keys(collection);
97
104
 
98
105
  for (var i = 0; i < collectionKeys.length; i++) {
99
106
  var key = collectionKeys[i];
@@ -108,7 +115,7 @@ function mapFilterValues(collection, iteratee, predicate) {
108
115
  var result = {};
109
116
 
110
117
  try {
111
- for (var _b = __values(keys(collection)), _c = _b.next(); !_c.done; _c = _b.next()) {
118
+ for (var _b = __values(Object.keys(collection)), _c = _b.next(); !_c.done; _c = _b.next()) {
112
119
  var key = _c.value;
113
120
  var item = collection[key];
114
121
 
@@ -203,7 +210,7 @@ function toStatePaths(stateValue) {
203
210
  return [[stateValue]];
204
211
  }
205
212
 
206
- var result = flatten(keys(stateValue).map(function (key) {
213
+ var result = flatten(Object.keys(stateValue).map(function (key) {
207
214
  var subStateValue = stateValue[key];
208
215
 
209
216
  if (typeof subStateValue !== 'string' && (!subStateValue || !Object.keys(subStateValue).length)) {
@@ -216,6 +223,46 @@ function toStatePaths(stateValue) {
216
223
  }));
217
224
  return result;
218
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
+ }
219
266
  function flatten(array) {
220
267
  var _a;
221
268
 
@@ -362,7 +409,7 @@ function updateContext(context, _event, assignActions, state) {
362
409
  partialUpdate = assignment(acc, _event.data, meta);
363
410
  } else {
364
411
  try {
365
- for (var _b = __values(keys(assignment)), _c = _b.next(); !_c.done; _c = _b.next()) {
412
+ for (var _b = __values(Object.keys(assignment)), _c = _b.next(); !_c.done; _c = _b.next()) {
366
413
  var key = _c.value;
367
414
  var propAssignment = assignment[key];
368
415
  partialUpdate[key] = isFunction(propAssignment) ? propAssignment(acc, _event.data, meta) : propAssignment;
@@ -416,18 +463,7 @@ function isFunction(value) {
416
463
  }
417
464
  function isString(value) {
418
465
  return typeof value === 'string';
419
- } // export function memoizedGetter<T, TP extends { prototype: object }>(
420
- // o: TP,
421
- // property: string,
422
- // getter: () => T
423
- // ): void {
424
- // Object.defineProperty(o.prototype, property, {
425
- // get: getter,
426
- // enumerable: false,
427
- // configurable: false
428
- // });
429
- // }
430
-
466
+ }
431
467
  function toGuard(condition, guardMap) {
432
468
  if (!condition) {
433
469
  return undefined;
@@ -468,11 +504,7 @@ var interopSymbols = (_a = {}, _a[symbolObservable] = function () {
468
504
  return this;
469
505
  }, _a);
470
506
  function isMachine(value) {
471
- try {
472
- return '__xstatenode' in value;
473
- } catch (e) {
474
- return false;
475
- }
507
+ return !!value && '__xstatenode' in value;
476
508
  }
477
509
  function isActor(value) {
478
510
  return !!value && typeof value.send === 'function';
@@ -591,4 +623,4 @@ function createInvokeId(stateNodeId, index) {
591
623
  return "".concat(stateNodeId, ":invocation[").concat(index, "]");
592
624
  }
593
625
 
594
- export { createInvokeId, evaluateGuard, flatten, getEventType, interopSymbols, isActor, isArray, isBehavior, isBuiltInEvent, isFunction, isMachine, isObservable, isPromiseLike, isStateLike, isString, keys, 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 };
package/lib/Machine.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { Model } from './model.types';
2
1
  import { AnyEventObject, BaseActionObject, DefaultContext, EventObject, MachineConfig, InternalMachineOptions, StateMachine, StateSchema, Typestate, ServiceMap } from './types';
3
2
  import { TypegenConstraint, TypegenDisabled, ResolveTypegenMeta } from './typegenTypes';
4
3
  /**
@@ -9,5 +8,5 @@ export declare function Machine<TContext = DefaultContext, TStateSchema extends
9
8
  export declare function createMachine<TContext, TEvent extends EventObject = AnyEventObject, TTypestate extends Typestate<TContext> = {
10
9
  value: any;
11
10
  context: TContext;
12
- }, TServiceMap extends ServiceMap = ServiceMap, TTypesMeta extends TypegenConstraint = TypegenDisabled>(config: TContext extends Model<any, any, any, any> ? 'Model type no longer supported as generic type. Please use `model.createMachine(...)` instead.' : MachineConfig<TContext, any, TEvent, BaseActionObject, TServiceMap, TTypesMeta>, options?: InternalMachineOptions<TContext, TEvent, ResolveTypegenMeta<TTypesMeta, TEvent, BaseActionObject, TServiceMap>>): StateMachine<TContext, any, TEvent, TTypestate, BaseActionObject, TServiceMap, ResolveTypegenMeta<TTypesMeta, TEvent, BaseActionObject, TServiceMap>>;
11
+ }, TServiceMap extends ServiceMap = ServiceMap, TTypesMeta extends TypegenConstraint = TypegenDisabled>(config: MachineConfig<TContext, any, TEvent, BaseActionObject, TServiceMap, TTypesMeta>, options?: InternalMachineOptions<TContext, TEvent, ResolveTypegenMeta<TTypesMeta, TEvent, BaseActionObject, TServiceMap>>): StateMachine<TContext, any, TEvent, TTypestate, BaseActionObject, TServiceMap, ResolveTypegenMeta<TTypesMeta, TEvent, BaseActionObject, TServiceMap>>;
13
12
  //# sourceMappingURL=Machine.d.ts.map
@@ -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
8
  var stateUtils = require('./stateUtils.js');
10
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) {
@@ -22,8 +22,8 @@ function stateValuesEqual(a, b) {
22
22
  return a === b;
23
23
  }
24
24
 
25
- var aKeys = utils.keys(a);
26
- var bKeys = utils.keys(b);
25
+ var aKeys = Object.keys(a);
26
+ var bKeys = Object.keys(b);
27
27
  return aKeys.length === bKeys.length && aKeys.every(function (key) {
28
28
  return stateValuesEqual(a[key], b[key]);
29
29
  });
@@ -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
 
@@ -210,7 +215,7 @@ function () {
210
215
  return [stateValue];
211
216
  }
212
217
 
213
- var valueKeys = utils.keys(stateValue);
218
+ var valueKeys = Object.keys(stateValue);
214
219
  return valueKeys.concat.apply(valueKeys, _tslib.__spreadArray([], _tslib.__read(valueKeys.map(function (key) {
215
220
  return _this.toStrings(stateValue[key], delimiter).map(function (s) {
216
221
  return key + delimiter + s;
@@ -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;
package/lib/StateNode.js CHANGED
@@ -3,14 +3,14 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _tslib = require('./_virtual/_tslib.js');
6
- var constants = require('./constants.js');
7
- var environment = require('./environment.js');
8
6
  var utils = require('./utils.js');
9
7
  var types = require('./types.js');
10
- var stateUtils = require('./stateUtils.js');
8
+ var State = require('./State.js');
11
9
  var actionTypes = require('./actionTypes.js');
12
10
  var actions = require('./actions.js');
13
- var State = require('./State.js');
11
+ var environment = require('./environment.js');
12
+ var constants = require('./constants.js');
13
+ var stateUtils = require('./stateUtils.js');
14
14
  var Actor = require('./Actor.js');
15
15
  var invokeUtils = require('./invokeUtils.js');
16
16
 
@@ -92,7 +92,7 @@ function () {
92
92
  this.delimiter = this.config.delimiter || (this.parent ? this.parent.delimiter : constants.STATE_DELIMITER);
93
93
  this.id = this.config.id || _tslib.__spreadArray([this.machine.key], _tslib.__read(this.path), false).join(this.delimiter);
94
94
  this.version = this.parent ? this.parent.version : this.config.version;
95
- this.type = this.config.type || (this.config.parallel ? 'parallel' : this.config.states && utils.keys(this.config.states).length ? 'compound' : this.config.history ? 'history' : 'atomic');
95
+ this.type = this.config.type || (this.config.parallel ? 'parallel' : this.config.states && Object.keys(this.config.states).length ? 'compound' : this.config.history ? 'history' : 'atomic');
96
96
  this.schema = this.parent ? this.machine.schema : (_a = this.config.schema) !== null && _a !== void 0 ? _a : {};
97
97
  this.description = this.config.description;
98
98
 
@@ -369,7 +369,7 @@ function () {
369
369
  return _tslib.__assign(_tslib.__assign({}, transition), {
370
370
  event: eventType
371
371
  });
372
- }) : utils.flatten(utils.keys(afterConfig).map(function (delay, i) {
372
+ }) : utils.flatten(Object.keys(afterConfig).map(function (delay, i) {
373
373
  var configTransition = afterConfig[delay];
374
374
  var resolvedTransition = utils.isString(configTransition) ? {
375
375
  target: configTransition
@@ -413,7 +413,7 @@ function () {
413
413
  return initialStateValue !== undefined ? this.getStateNodes((_a = {}, _a[stateValue] = initialStateValue, _a)) : [this, this.states[stateValue]];
414
414
  }
415
415
 
416
- var subStateKeys = utils.keys(stateValue);
416
+ var subStateKeys = Object.keys(stateValue);
417
417
  var subStateNodes = [this];
418
418
  subStateNodes.push.apply(subStateNodes, _tslib.__spreadArray([], _tslib.__read(utils.flatten(subStateKeys.map(function (subStateKey) {
419
419
  return _this.getStateNode(subStateKey).getStateNodes(stateValue[subStateKey]);
@@ -463,7 +463,7 @@ function () {
463
463
  };
464
464
 
465
465
  StateNode.prototype.transitionCompoundNode = function (stateValue, state, _event) {
466
- var subStateKeys = utils.keys(stateValue);
466
+ var subStateKeys = Object.keys(stateValue);
467
467
  var stateNode = this.getStateNode(subStateKeys[0]);
468
468
 
469
469
  var next = stateNode._transition(stateValue[subStateKeys[0]], state, _event);
@@ -481,7 +481,7 @@ function () {
481
481
  var transitionMap = {};
482
482
 
483
483
  try {
484
- for (var _b = _tslib.__values(utils.keys(stateValue)), _c = _b.next(); !_c.done; _c = _b.next()) {
484
+ for (var _b = _tslib.__values(Object.keys(stateValue)), _c = _b.next(); !_c.done; _c = _b.next()) {
485
485
  var subStateKey = _c.value;
486
486
  var subStateValue = stateValue[subStateKey];
487
487
 
@@ -509,7 +509,7 @@ function () {
509
509
  }
510
510
  }
511
511
 
512
- var stateTransitions = utils.keys(transitionMap).map(function (key) {
512
+ var stateTransitions = Object.keys(transitionMap).map(function (key) {
513
513
  return transitionMap[key];
514
514
  });
515
515
  var enabledTransitions = utils.flatten(stateTransitions.map(function (st) {
@@ -526,7 +526,7 @@ function () {
526
526
  var entryNodes = utils.flatten(stateTransitions.map(function (t) {
527
527
  return t.entrySet;
528
528
  }));
529
- var configuration = utils.flatten(utils.keys(transitionMap).map(function (key) {
529
+ var configuration = utils.flatten(Object.keys(transitionMap).map(function (key) {
530
530
  return transitionMap[key].configuration;
531
531
  }));
532
532
  return {
@@ -537,7 +537,7 @@ function () {
537
537
  })),
538
538
  configuration: configuration,
539
539
  source: state,
540
- actions: utils.flatten(utils.keys(transitionMap).map(function (key) {
540
+ actions: utils.flatten(Object.keys(transitionMap).map(function (key) {
541
541
  return transitionMap[key].actions;
542
542
  }))
543
543
  };
@@ -550,7 +550,7 @@ function () {
550
550
  } // hierarchical node
551
551
 
552
552
 
553
- if (utils.keys(stateValue).length === 1) {
553
+ if (Object.keys(stateValue).length === 1) {
554
554
  return this.transitionCompoundNode(stateValue, state, _event);
555
555
  } // orthogonal node
556
556
 
@@ -1084,7 +1084,7 @@ function () {
1084
1084
  return stateValue;
1085
1085
  }
1086
1086
 
1087
- if (!utils.keys(stateValue).length) {
1087
+ if (!Object.keys(stateValue).length) {
1088
1088
  return this.initialStateValue || {};
1089
1089
  }
1090
1090
 
@@ -1275,7 +1275,7 @@ function () {
1275
1275
  };
1276
1276
 
1277
1277
  StateNode.prototype.historyValue = function (relativeStateValue) {
1278
- if (!utils.keys(this.states).length) {
1278
+ if (!Object.keys(this.states).length) {
1279
1279
  return undefined;
1280
1280
  }
1281
1281
 
@@ -1335,7 +1335,7 @@ function () {
1335
1335
  get: function () {
1336
1336
  var _this = this;
1337
1337
 
1338
- var childStateIds = utils.flatten(utils.keys(this.states).map(function (stateKey) {
1338
+ var childStateIds = utils.flatten(Object.keys(this.states).map(function (stateKey) {
1339
1339
  return _this.states[stateKey].stateIds;
1340
1340
  }));
1341
1341
  return [this.id].concat(childStateIds);
@@ -1359,7 +1359,7 @@ function () {
1359
1359
 
1360
1360
  if (states) {
1361
1361
  try {
1362
- for (var _c = _tslib.__values(utils.keys(states)), _d = _c.next(); !_d.done; _d = _c.next()) {
1362
+ for (var _c = _tslib.__values(Object.keys(states)), _d = _c.next(); !_d.done; _d = _c.next()) {
1363
1363
  var stateId = _d.value;
1364
1364
  var state = states[stateId];
1365
1365
 
@@ -1502,7 +1502,7 @@ function () {
1502
1502
  wildcardConfigs = _d === void 0 ? [] : _d,
1503
1503
  strictTransitionConfigs_1 = _tslib.__rest(_b, [typeof _c === "symbol" ? _c : _c + ""]);
1504
1504
 
1505
- onConfig = utils.flatten(utils.keys(strictTransitionConfigs_1).map(function (key) {
1505
+ onConfig = utils.flatten(Object.keys(strictTransitionConfigs_1).map(function (key) {
1506
1506
  if (!environment.IS_PRODUCTION && key === NULL_EVENT) {
1507
1507
  utils.warn(false, "Empty string transition configs (e.g., `{ on: { '': ... }}`) for transient transitions are deprecated. Specify the transition in the `{ always: ... }` property instead. " + "Please check the `on` configuration for \"#".concat(_this.id, "\"."));
1508
1508
  }
package/lib/actions.js CHANGED
@@ -3,10 +3,10 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _tslib = require('./_virtual/_tslib.js');
6
- var environment = require('./environment.js');
7
- var utils = require('./utils.js');
8
6
  var types = require('./types.js');
9
7
  var actionTypes = require('./actionTypes.js');
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('./environment.js');
6
- var utils = require('./utils.js');
7
- require('./types.js');
8
- require('./actionTypes.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/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;