xstate 3.3.2 → 3.3.3
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/README.md +21 -1
- package/dist/xstate.js +1 -1
- package/dist/xstate.utils.js +1 -1
- package/es/State.d.ts +8 -7
- package/es/State.js +3 -2
- package/es/StateNode.d.ts +35 -20
- package/es/StateNode.js +119 -46
- package/es/graph.d.ts +4 -1
- package/es/graph.js +5 -4
- package/es/patterns.js +1 -1
- package/es/scxml.d.ts +2 -1
- package/es/scxml.js +9 -8
- package/es/types.d.ts +4 -7
- package/es/utils.d.ts +6 -1
- package/es/utils.js +17 -1
- package/lib/State.d.ts +8 -7
- package/lib/State.js +3 -2
- package/lib/StateNode.d.ts +35 -20
- package/lib/StateNode.js +118 -45
- package/lib/graph.d.ts +4 -1
- package/lib/graph.js +5 -3
- package/lib/patterns.js +1 -1
- package/lib/scxml.d.ts +2 -1
- package/lib/scxml.js +9 -8
- package/lib/types.d.ts +4 -7
- package/lib/utils.d.ts +6 -1
- package/lib/utils.js +18 -1
- package/package.json +3 -3
- package/src/State.ts +4 -1
- package/src/StateNode.ts +221 -89
- package/src/graph.ts +14 -12
- package/src/scxml.ts +52 -49
- package/src/types.ts +6 -9
- package/src/utils.ts +25 -4
- package/test/activities.test.ts +28 -1
- package/test/deterministic.test.ts +1 -1
- package/test/history.test.ts +139 -0
- package/test/invalid.test.ts +48 -0
- package/test/parallel.test.ts +229 -1
package/es/StateNode.js
CHANGED
|
@@ -6,7 +6,7 @@ var __assign = (this && this.__assign) || Object.assign || function(t) {
|
|
|
6
6
|
}
|
|
7
7
|
return t;
|
|
8
8
|
};
|
|
9
|
-
import { getEventType, toStatePath, toStateValue, mapValues, path, toStatePaths, pathsToStateValue, pathToStateValue, getActionType, flatMap, mapFilterValues } from './utils';
|
|
9
|
+
import { getEventType, toStatePath, toStateValue, mapValues, path, toStatePaths, pathsToStateValue, pathToStateValue, getActionType, flatMap, mapFilterValues, nestedPath } from './utils';
|
|
10
10
|
import { matchesState } from './matchesState';
|
|
11
11
|
import { State } from './State';
|
|
12
12
|
import { start, stop, toEventObject, actionTypes } from './actions';
|
|
@@ -15,11 +15,6 @@ var HISTORY_KEY = '$history';
|
|
|
15
15
|
var NULL_EVENT = '';
|
|
16
16
|
var STATE_IDENTIFIER = '#';
|
|
17
17
|
var isStateId = function (str) { return str[0] === STATE_IDENTIFIER; };
|
|
18
|
-
// const emptyActions: ActionMap = Object.freeze({
|
|
19
|
-
// onEntry: [],
|
|
20
|
-
// onExit: [],
|
|
21
|
-
// actions: []
|
|
22
|
-
// });
|
|
23
18
|
var defaultOptions = {
|
|
24
19
|
guards: {}
|
|
25
20
|
};
|
|
@@ -51,10 +46,10 @@ var StateNode = /** @class */ (function () {
|
|
|
51
46
|
this.parallel = !!config.parallel;
|
|
52
47
|
this.states = (config.states
|
|
53
48
|
? mapValues(config.states, function (stateConfig, key) {
|
|
49
|
+
var _a;
|
|
54
50
|
var stateNode = new StateNode(__assign({}, stateConfig, { key: key, parent: _this }));
|
|
55
51
|
Object.assign(_this.idMap, __assign((_a = {}, _a[stateNode.id] = stateNode, _a), stateNode.idMap));
|
|
56
52
|
return stateNode;
|
|
57
|
-
var _a;
|
|
58
53
|
})
|
|
59
54
|
: {});
|
|
60
55
|
// History config
|
|
@@ -72,6 +67,7 @@ var StateNode = /** @class */ (function () {
|
|
|
72
67
|
}
|
|
73
68
|
StateNode.prototype.getStateNodes = function (state) {
|
|
74
69
|
var _this = this;
|
|
70
|
+
var _a;
|
|
75
71
|
if (!state) {
|
|
76
72
|
return [];
|
|
77
73
|
}
|
|
@@ -92,7 +88,6 @@ var StateNode = /** @class */ (function () {
|
|
|
92
88
|
var subStateNode = _this.getStateNode(subStateKey).getStateNodes(stateValue[subStateKey]);
|
|
93
89
|
return allSubStateNodes.concat(subStateNode);
|
|
94
90
|
}, []));
|
|
95
|
-
var _a;
|
|
96
91
|
};
|
|
97
92
|
StateNode.prototype.handles = function (event) {
|
|
98
93
|
var eventType = getEventType(event);
|
|
@@ -194,10 +189,10 @@ var StateNode = /** @class */ (function () {
|
|
|
194
189
|
}
|
|
195
190
|
var allResolvedPaths = flatMap(Object.keys(transitionMap).map(function (key) {
|
|
196
191
|
var transition = transitionMap[key];
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
192
|
+
var value = transition.value || state.value;
|
|
193
|
+
return toStatePaths(path(_this.path)(value)[key]).map(function (statePath) {
|
|
194
|
+
return _this.path.concat(key, statePath);
|
|
195
|
+
});
|
|
201
196
|
}));
|
|
202
197
|
var nextStateValue = this.machine.resolve(pathsToStateValue(allResolvedPaths));
|
|
203
198
|
return {
|
|
@@ -280,7 +275,7 @@ var StateNode = /** @class */ (function () {
|
|
|
280
275
|
};
|
|
281
276
|
}
|
|
282
277
|
var nextStateNodes = flatMap(nextStateStrings.map(function (str) {
|
|
283
|
-
return _this.getRelativeStateNodes(str, state.
|
|
278
|
+
return _this.getRelativeStateNodes(str, state.historyValue);
|
|
284
279
|
}));
|
|
285
280
|
var nextStatePaths = nextStateNodes.map(function (stateNode) { return stateNode.path; });
|
|
286
281
|
var entryExitStates = nextStateNodes.reduce(function (allEntryExitStates, nextStateNode) {
|
|
@@ -292,7 +287,7 @@ var StateNode = /** @class */ (function () {
|
|
|
292
287
|
}, { entry: new Set(), exit: new Set() });
|
|
293
288
|
return {
|
|
294
289
|
value: this.machine.resolve(pathsToStateValue(flatMap(nextStateStrings.map(function (str) {
|
|
295
|
-
return _this.getRelativeStateNodes(str, state.
|
|
290
|
+
return _this.getRelativeStateNodes(str, state.historyValue).map(function (s) { return s.path; });
|
|
296
291
|
})))),
|
|
297
292
|
entryExitStates: entryExitStates,
|
|
298
293
|
actions: actions,
|
|
@@ -346,8 +341,7 @@ var StateNode = /** @class */ (function () {
|
|
|
346
341
|
var condFn;
|
|
347
342
|
if (typeof condition === 'string') {
|
|
348
343
|
if (!this.machine.options.guards[condition]) {
|
|
349
|
-
throw new Error("String condition '" + condition + "' is not defined on machine '" + this
|
|
350
|
-
.machine.id + "'");
|
|
344
|
+
throw new Error("String condition '" + condition + "' is not defined on machine '" + this.machine.id + "'");
|
|
351
345
|
}
|
|
352
346
|
condFn = this.machine.options.guards[condition];
|
|
353
347
|
}
|
|
@@ -379,28 +373,31 @@ var StateNode = /** @class */ (function () {
|
|
|
379
373
|
return {};
|
|
380
374
|
}
|
|
381
375
|
var activityMap = __assign({}, state.activities);
|
|
382
|
-
Array.from(transition.entryExitStates.
|
|
376
|
+
Array.from(transition.entryExitStates.exit).forEach(function (stateNode) {
|
|
383
377
|
if (!stateNode.activities) {
|
|
384
378
|
return; // TODO: fixme
|
|
385
379
|
}
|
|
386
380
|
stateNode.activities.forEach(function (activity) {
|
|
387
|
-
activityMap[getActionType(activity)] =
|
|
381
|
+
activityMap[getActionType(activity)] = false;
|
|
388
382
|
});
|
|
389
383
|
});
|
|
390
|
-
Array.from(transition.entryExitStates.
|
|
384
|
+
Array.from(transition.entryExitStates.entry).forEach(function (stateNode) {
|
|
391
385
|
if (!stateNode.activities) {
|
|
392
386
|
return; // TODO: fixme
|
|
393
387
|
}
|
|
394
388
|
stateNode.activities.forEach(function (activity) {
|
|
395
|
-
activityMap[getActionType(activity)] =
|
|
389
|
+
activityMap[getActionType(activity)] = true;
|
|
396
390
|
});
|
|
397
391
|
});
|
|
398
392
|
return activityMap;
|
|
399
393
|
};
|
|
400
394
|
StateNode.prototype.transition = function (state, event, extendedState) {
|
|
395
|
+
var _a;
|
|
401
396
|
var resolvedStateValue = typeof state === 'string'
|
|
402
397
|
? this.resolve(pathToStateValue(this.getResolvedPath(state)))
|
|
403
|
-
: state instanceof State
|
|
398
|
+
: state instanceof State
|
|
399
|
+
? state
|
|
400
|
+
: this.resolve(state);
|
|
404
401
|
var eventType = getEventType(event);
|
|
405
402
|
if (this.strict) {
|
|
406
403
|
if (this.events.indexOf(eventType) === -1) {
|
|
@@ -408,6 +405,11 @@ var StateNode = /** @class */ (function () {
|
|
|
408
405
|
}
|
|
409
406
|
}
|
|
410
407
|
var currentState = State.from(resolvedStateValue);
|
|
408
|
+
var historyValue = resolvedStateValue instanceof State
|
|
409
|
+
? resolvedStateValue.historyValue
|
|
410
|
+
? resolvedStateValue.historyValue
|
|
411
|
+
: this.machine.historyValue(resolvedStateValue.value)
|
|
412
|
+
: this.machine.historyValue(resolvedStateValue);
|
|
411
413
|
var stateTransition = this._transition(currentState.value, currentState, event, extendedState);
|
|
412
414
|
try {
|
|
413
415
|
this.ensureValidPaths(stateTransition.paths);
|
|
@@ -436,15 +438,15 @@ var StateNode = /** @class */ (function () {
|
|
|
436
438
|
stateNodes.forEach(function (stateNode) {
|
|
437
439
|
data[stateNode.id] = stateNode.data;
|
|
438
440
|
});
|
|
439
|
-
// Dispose of previous histories to prevent memory leaks
|
|
440
|
-
delete currentState.history;
|
|
441
441
|
var nextState = stateTransition.value
|
|
442
|
-
? new State(stateTransition.value, currentState, nonEventActions, activities, data, raisedEvents)
|
|
442
|
+
? new State(stateTransition.value, StateNode.updateHistoryValue(historyValue, stateTransition.value), currentState, nonEventActions, activities, data, raisedEvents)
|
|
443
443
|
: undefined;
|
|
444
444
|
if (!nextState) {
|
|
445
445
|
// Unchanged state should be returned with no actions
|
|
446
446
|
return State.inert(currentState);
|
|
447
447
|
}
|
|
448
|
+
// Dispose of previous histories to prevent memory leaks
|
|
449
|
+
delete currentState.history;
|
|
448
450
|
var maybeNextState = nextState;
|
|
449
451
|
while (raisedEvents.length) {
|
|
450
452
|
var currentActions = maybeNextState.actions;
|
|
@@ -453,7 +455,6 @@ var StateNode = /** @class */ (function () {
|
|
|
453
455
|
(_a = maybeNextState.actions).unshift.apply(_a, currentActions);
|
|
454
456
|
}
|
|
455
457
|
return maybeNextState;
|
|
456
|
-
var _a;
|
|
457
458
|
};
|
|
458
459
|
StateNode.prototype.ensureValidPaths = function (paths) {
|
|
459
460
|
var _this = this;
|
|
@@ -467,8 +468,9 @@ var StateNode = /** @class */ (function () {
|
|
|
467
468
|
if (marker.parent.parallel) {
|
|
468
469
|
continue outer;
|
|
469
470
|
}
|
|
470
|
-
throw new Error("State node '" + stateNode.id + "' shares parent '" + marker.parent
|
|
471
|
-
.
|
|
471
|
+
throw new Error("State node '" + stateNode.id + "' shares parent '" + marker.parent.id + "' with state node '" + visitedParents
|
|
472
|
+
.get(marker.parent)
|
|
473
|
+
.map(function (a) { return a.id; }) + "'");
|
|
472
474
|
}
|
|
473
475
|
if (!visitedParents.get(marker.parent)) {
|
|
474
476
|
visitedParents.set(marker.parent, [stateNode]);
|
|
@@ -485,8 +487,7 @@ var StateNode = /** @class */ (function () {
|
|
|
485
487
|
return this.machine.getStateNodeById(stateKey);
|
|
486
488
|
}
|
|
487
489
|
if (!this.states) {
|
|
488
|
-
throw new Error("Unable to retrieve child state '" + stateKey + "' from '" + this
|
|
489
|
-
.id + "'; no child states exist.");
|
|
490
|
+
throw new Error("Unable to retrieve child state '" + stateKey + "' from '" + this.id + "'; no child states exist.");
|
|
490
491
|
}
|
|
491
492
|
var result = this.states[stateKey];
|
|
492
493
|
if (!result) {
|
|
@@ -504,8 +505,18 @@ var StateNode = /** @class */ (function () {
|
|
|
504
505
|
}
|
|
505
506
|
return stateNode;
|
|
506
507
|
};
|
|
508
|
+
StateNode.prototype.getStateNodeByPath = function (statePath) {
|
|
509
|
+
var arrayStatePath = toStatePath(statePath, this.delimiter);
|
|
510
|
+
var currentStateNode = this;
|
|
511
|
+
while (arrayStatePath.length) {
|
|
512
|
+
var key = arrayStatePath.shift();
|
|
513
|
+
currentStateNode = currentStateNode.getStateNode(key);
|
|
514
|
+
}
|
|
515
|
+
return currentStateNode;
|
|
516
|
+
};
|
|
507
517
|
StateNode.prototype.resolve = function (stateValue) {
|
|
508
518
|
var _this = this;
|
|
519
|
+
var _a;
|
|
509
520
|
if (typeof stateValue === 'string') {
|
|
510
521
|
var subStateNode = this.getStateNode(stateValue);
|
|
511
522
|
return subStateNode.initial
|
|
@@ -523,10 +534,10 @@ var StateNode = /** @class */ (function () {
|
|
|
523
534
|
? _this.getStateNode(subStateKey).resolve(subStateValue)
|
|
524
535
|
: {};
|
|
525
536
|
});
|
|
526
|
-
var _a;
|
|
527
537
|
};
|
|
528
538
|
Object.defineProperty(StateNode.prototype, "resolvedStateValue", {
|
|
529
539
|
get: function () {
|
|
540
|
+
var _a, _b;
|
|
530
541
|
var key = this.key;
|
|
531
542
|
if (this.parallel) {
|
|
532
543
|
return _a = {},
|
|
@@ -540,7 +551,6 @@ var StateNode = /** @class */ (function () {
|
|
|
540
551
|
return _b = {},
|
|
541
552
|
_b[key] = this.states[this.initial].resolvedStateValue,
|
|
542
553
|
_b;
|
|
543
|
-
var _a, _b;
|
|
544
554
|
},
|
|
545
555
|
enumerable: true,
|
|
546
556
|
configurable: true
|
|
@@ -573,6 +583,7 @@ var StateNode = /** @class */ (function () {
|
|
|
573
583
|
});
|
|
574
584
|
Object.defineProperty(StateNode.prototype, "initialState", {
|
|
575
585
|
get: function () {
|
|
586
|
+
var _a;
|
|
576
587
|
var initialStateValue = this.initialStateValue;
|
|
577
588
|
if (!initialStateValue) {
|
|
578
589
|
throw new Error("Cannot retrieve initial state from simple state '" + this.id + ".'");
|
|
@@ -590,7 +601,21 @@ var StateNode = /** @class */ (function () {
|
|
|
590
601
|
});
|
|
591
602
|
}
|
|
592
603
|
});
|
|
593
|
-
|
|
604
|
+
// TODO: deduplicate - DRY (from this.transition())
|
|
605
|
+
var raisedEvents = actions.filter(function (action) {
|
|
606
|
+
return typeof action === 'object' &&
|
|
607
|
+
(action.type === actionTypes.raise || action.type === actionTypes.null);
|
|
608
|
+
});
|
|
609
|
+
var initialState = new State(initialStateValue, undefined, undefined, actions, activityMap);
|
|
610
|
+
var maybeNextState = initialState;
|
|
611
|
+
while (raisedEvents.length) {
|
|
612
|
+
var currentActions = maybeNextState.actions;
|
|
613
|
+
var raisedEvent = raisedEvents.shift();
|
|
614
|
+
maybeNextState = this.transition(maybeNextState, raisedEvent.type === actionTypes.null ? NULL_EVENT : raisedEvent.event, undefined // TODO: consider initial state given external state
|
|
615
|
+
);
|
|
616
|
+
(_a = maybeNextState.actions).unshift.apply(_a, currentActions);
|
|
617
|
+
}
|
|
618
|
+
return maybeNextState;
|
|
594
619
|
},
|
|
595
620
|
enumerable: true,
|
|
596
621
|
configurable: true
|
|
@@ -634,9 +659,8 @@ var StateNode = /** @class */ (function () {
|
|
|
634
659
|
* @param history The previous state to retrieve history
|
|
635
660
|
* @param resolve Whether state nodes should resolve to initial child state nodes
|
|
636
661
|
*/
|
|
637
|
-
StateNode.prototype.getRelativeStateNodes = function (relativeStateId,
|
|
662
|
+
StateNode.prototype.getRelativeStateNodes = function (relativeStateId, historyValue, resolve) {
|
|
638
663
|
if (resolve === void 0) { resolve = true; }
|
|
639
|
-
var historyValue = history ? history.value : undefined;
|
|
640
664
|
if (typeof relativeStateId === 'string' && isStateId(relativeStateId)) {
|
|
641
665
|
var unresolvedStateNode = this.getStateNodeById(relativeStateId);
|
|
642
666
|
return resolve
|
|
@@ -669,6 +693,12 @@ var StateNode = /** @class */ (function () {
|
|
|
669
693
|
enumerable: true,
|
|
670
694
|
configurable: true
|
|
671
695
|
});
|
|
696
|
+
/**
|
|
697
|
+
* Retrieves state nodes from a relative path to this state node.
|
|
698
|
+
*
|
|
699
|
+
* @param relativePath The relative path from this state node
|
|
700
|
+
* @param historyValue
|
|
701
|
+
*/
|
|
672
702
|
StateNode.prototype.getFromRelativePath = function (relativePath, historyValue) {
|
|
673
703
|
var _this = this;
|
|
674
704
|
if (!relativePath.length) {
|
|
@@ -683,7 +713,7 @@ var StateNode = /** @class */ (function () {
|
|
|
683
713
|
if (!historyValue) {
|
|
684
714
|
return [this];
|
|
685
715
|
}
|
|
686
|
-
var subHistoryValue =
|
|
716
|
+
var subHistoryValue = nestedPath(this.path, 'states')(historyValue).current;
|
|
687
717
|
if (typeof subHistoryValue === 'string') {
|
|
688
718
|
return this.states[subHistoryValue].getFromRelativePath(xs, historyValue);
|
|
689
719
|
}
|
|
@@ -700,6 +730,51 @@ var StateNode = /** @class */ (function () {
|
|
|
700
730
|
}
|
|
701
731
|
return this.states[x].getFromRelativePath(xs, historyValue);
|
|
702
732
|
};
|
|
733
|
+
StateNode.updateHistoryValue = function (hist, stateValue) {
|
|
734
|
+
function update(_hist, _sv) {
|
|
735
|
+
return mapValues(_hist.states, function (subHist, key) {
|
|
736
|
+
if (!subHist) {
|
|
737
|
+
return undefined;
|
|
738
|
+
}
|
|
739
|
+
var subStateValue = (typeof _sv === 'string' ? undefined : _sv[key]) ||
|
|
740
|
+
(subHist ? subHist.current : undefined);
|
|
741
|
+
if (!subStateValue) {
|
|
742
|
+
return undefined;
|
|
743
|
+
}
|
|
744
|
+
return {
|
|
745
|
+
current: subStateValue,
|
|
746
|
+
states: update(subHist, subStateValue)
|
|
747
|
+
};
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
return {
|
|
751
|
+
current: stateValue,
|
|
752
|
+
states: update(hist, stateValue)
|
|
753
|
+
};
|
|
754
|
+
};
|
|
755
|
+
StateNode.prototype.historyValue = function (relativeStateValue) {
|
|
756
|
+
if (!Object.keys(this.states).length) {
|
|
757
|
+
return undefined;
|
|
758
|
+
}
|
|
759
|
+
return {
|
|
760
|
+
current: relativeStateValue || this.initialStateValue,
|
|
761
|
+
states: mapFilterValues(this.states, function (stateNode, key) {
|
|
762
|
+
if (!relativeStateValue) {
|
|
763
|
+
return stateNode.historyValue();
|
|
764
|
+
}
|
|
765
|
+
var subStateValue = typeof relativeStateValue === 'string'
|
|
766
|
+
? undefined
|
|
767
|
+
: relativeStateValue[key];
|
|
768
|
+
return stateNode.historyValue(subStateValue || stateNode.initialStateValue);
|
|
769
|
+
}, function (stateNode) { return !stateNode.history; })
|
|
770
|
+
};
|
|
771
|
+
};
|
|
772
|
+
/**
|
|
773
|
+
* Resolves to the historical value(s) of the parent state node,
|
|
774
|
+
* represented by state nodes.
|
|
775
|
+
*
|
|
776
|
+
* @param historyValue
|
|
777
|
+
*/
|
|
703
778
|
StateNode.prototype.resolveHistory = function (historyValue) {
|
|
704
779
|
var _this = this;
|
|
705
780
|
if (!this.history) {
|
|
@@ -709,21 +784,19 @@ var StateNode = /** @class */ (function () {
|
|
|
709
784
|
if (!historyValue) {
|
|
710
785
|
return this.target
|
|
711
786
|
? flatMap(toStatePaths(this.target).map(function (relativeChildPath) {
|
|
712
|
-
return parent.getFromRelativePath(relativeChildPath
|
|
787
|
+
return parent.getFromRelativePath(relativeChildPath);
|
|
713
788
|
}))
|
|
714
789
|
: this.parent.initialStateNodes;
|
|
715
790
|
}
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
return [parent.getStateNode(subHistoryValue)];
|
|
720
|
-
}
|
|
721
|
-
return flatMap(toStatePaths(subHistoryValue).map(function (subStatePath) {
|
|
722
|
-
return _this.history === 'deep'
|
|
723
|
-
? parent.getFromRelativePath(subStatePath)
|
|
724
|
-
: [parent.states[subStatePath[0]]];
|
|
725
|
-
}));
|
|
791
|
+
var subHistoryValue = nestedPath(parent.path, 'states')(historyValue).current;
|
|
792
|
+
if (typeof subHistoryValue === 'string') {
|
|
793
|
+
return [parent.getStateNode(subHistoryValue)];
|
|
726
794
|
}
|
|
795
|
+
return flatMap(toStatePaths(subHistoryValue).map(function (subStatePath) {
|
|
796
|
+
return _this.history === 'deep'
|
|
797
|
+
? parent.getFromRelativePath(subStatePath)
|
|
798
|
+
: [parent.states[subStatePath[0]]];
|
|
799
|
+
}));
|
|
727
800
|
};
|
|
728
801
|
Object.defineProperty(StateNode.prototype, "events", {
|
|
729
802
|
get: function () {
|
package/es/graph.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { StateNode } from './index';
|
|
2
2
|
import { Machine, Edge, PathMap, PathItem, PathsItem, PathsMap, AdjacencyMap } from './types';
|
|
3
3
|
export declare function getNodes(node: StateNode): StateNode[];
|
|
4
|
-
export declare function
|
|
4
|
+
export declare function getEventEdges(node: StateNode, event: string): Edge[];
|
|
5
|
+
export declare function getEdges(node: StateNode, options?: {
|
|
6
|
+
deep: boolean;
|
|
7
|
+
}): Edge[];
|
|
5
8
|
export declare function getAdjacencyMap(node: Machine, extendedState?: any): AdjacencyMap;
|
|
6
9
|
export declare function getShortestPaths(machine: Machine, extendedState?: any): PathMap;
|
|
7
10
|
export declare function getShortestPathsAsArray(machine: Machine, extendedState?: any): PathItem[];
|
package/es/graph.js
CHANGED
|
@@ -10,7 +10,7 @@ export function getNodes(node) {
|
|
|
10
10
|
}, []);
|
|
11
11
|
return nodes;
|
|
12
12
|
}
|
|
13
|
-
function getEventEdges(node, event) {
|
|
13
|
+
export function getEventEdges(node, event) {
|
|
14
14
|
var transitions = node.on[event];
|
|
15
15
|
return flatMap(transitions.map(function (transition) {
|
|
16
16
|
var targets = [].concat(transition.target);
|
|
@@ -28,9 +28,10 @@ function getEventEdges(node, event) {
|
|
|
28
28
|
});
|
|
29
29
|
}));
|
|
30
30
|
}
|
|
31
|
-
export function getEdges(node) {
|
|
31
|
+
export function getEdges(node, options) {
|
|
32
|
+
var _a = (options || {}).deep, deep = _a === void 0 ? true : _a;
|
|
32
33
|
var edges = [];
|
|
33
|
-
if (node.states) {
|
|
34
|
+
if (node.states && deep) {
|
|
34
35
|
Object.keys(node.states).forEach(function (stateKey) {
|
|
35
36
|
edges.push.apply(edges, getEdges(node.states[stateKey]));
|
|
36
37
|
});
|
|
@@ -60,6 +61,7 @@ export function getAdjacencyMap(node, extendedState) {
|
|
|
60
61
|
return adjacency;
|
|
61
62
|
}
|
|
62
63
|
export function getShortestPaths(machine, extendedState) {
|
|
64
|
+
var _a;
|
|
63
65
|
if (!machine.states) {
|
|
64
66
|
return EMPTY_MAP;
|
|
65
67
|
}
|
|
@@ -103,7 +105,6 @@ export function getShortestPaths(machine, extendedState) {
|
|
|
103
105
|
}
|
|
104
106
|
util(machine.initialState.value);
|
|
105
107
|
return pathMap;
|
|
106
|
-
var _a;
|
|
107
108
|
}
|
|
108
109
|
export function getShortestPathsAsArray(machine, extendedState) {
|
|
109
110
|
var result = getShortestPaths(machine, extendedState);
|
package/es/patterns.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export function toggle(onState, offState, eventType) {
|
|
2
|
+
var _a, _b, _c;
|
|
2
3
|
return _a = {},
|
|
3
4
|
_a[onState] = {
|
|
4
5
|
on: (_b = {}, _b[eventType] = offState, _b)
|
|
@@ -7,5 +8,4 @@ export function toggle(onState, offState, eventType) {
|
|
|
7
8
|
on: (_c = {}, _c[eventType] = onState, _c)
|
|
8
9
|
},
|
|
9
10
|
_a;
|
|
10
|
-
var _a, _b, _c;
|
|
11
11
|
}
|
package/es/scxml.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Machine, EventObject } from './types';
|
|
2
2
|
export declare function fromMachine(machine: Machine): string;
|
|
3
3
|
export interface ScxmlToMachineOptions {
|
|
4
|
-
evalCond: (expr: string) =>
|
|
4
|
+
evalCond: (expr: string) => // tslint:disable-next-line:ban-types
|
|
5
|
+
((extState: any, event: EventObject) => boolean) | Function;
|
|
5
6
|
delimiter?: string;
|
|
6
7
|
}
|
|
7
8
|
export declare function toMachine(xml: string, options: ScxmlToMachineOptions): any;
|
package/es/scxml.js
CHANGED
|
@@ -73,9 +73,9 @@ function stateNodeToSCXML(stateNode) {
|
|
|
73
73
|
return {
|
|
74
74
|
type: 'element',
|
|
75
75
|
name: 'transition',
|
|
76
|
-
attributes: __assign({}, event ? { event: event } : undefined, { target: stateNode.parent.getRelativeStateNodes(targetTransition.target)[0].id }, targetTransition.cond
|
|
76
|
+
attributes: __assign({}, (event ? { event: event } : undefined), { target: stateNode.parent.getRelativeStateNodes(targetTransition.target)[0].id }, (targetTransition.cond
|
|
77
77
|
? { cond: targetTransition.cond.toString() }
|
|
78
|
-
: undefined),
|
|
78
|
+
: undefined)),
|
|
79
79
|
elements: targetTransition.actions
|
|
80
80
|
? targetTransition.actions.map(function (action) { return ({
|
|
81
81
|
type: 'element',
|
|
@@ -93,9 +93,10 @@ function stateNodeToSCXML(stateNode) {
|
|
|
93
93
|
return {
|
|
94
94
|
type: 'element',
|
|
95
95
|
name: 'transition',
|
|
96
|
-
attributes: __assign({}, event ? { event: event } : undefined, { target: stateNode.parent.getRelativeStateNodes(target)[0]
|
|
96
|
+
attributes: __assign({}, (event ? { event: event } : undefined), { target: stateNode.parent.getRelativeStateNodes(target)[0]
|
|
97
|
+
.id }, (targetTransition.cond
|
|
97
98
|
? { cond: targetTransition.cond.toString() }
|
|
98
|
-
: undefined),
|
|
99
|
+
: undefined)),
|
|
99
100
|
elements: targetTransition.actions
|
|
100
101
|
? targetTransition.actions.map(function (action) { return ({
|
|
101
102
|
type: 'element',
|
|
@@ -204,11 +205,11 @@ function toConfig(nodeJson, id, options) {
|
|
|
204
205
|
}
|
|
205
206
|
states = indexedRecord(stateElements, function (item) { return "" + item.attributes.id; });
|
|
206
207
|
on = mapValues(indexedAggregateRecord(transitionElements, function (item) { return item.attributes.event || ''; }), function (values) {
|
|
207
|
-
return values.map(function (value) { return (__assign({ target: "#" + value.attributes.target }, value.elements ? executableContent(value.elements) : undefined, value.attributes.cond
|
|
208
|
+
return values.map(function (value) { return (__assign({ target: "#" + value.attributes.target }, (value.elements ? executableContent(value.elements) : undefined), (value.attributes.cond
|
|
208
209
|
? {
|
|
209
210
|
cond: evalCond(value.attributes.cond)
|
|
210
211
|
}
|
|
211
|
-
: undefined)); });
|
|
212
|
+
: undefined))); });
|
|
212
213
|
});
|
|
213
214
|
var onEntry = onEntryElement
|
|
214
215
|
? onEntryElement.elements.map(function (element) {
|
|
@@ -230,13 +231,13 @@ function toConfig(nodeJson, id, options) {
|
|
|
230
231
|
}
|
|
231
232
|
})
|
|
232
233
|
: undefined;
|
|
233
|
-
return __assign({ id: id, delimiter: options.delimiter }, initial ? { initial: initial } : undefined, parallel ? { parallel: parallel } : undefined, stateElements.length
|
|
234
|
+
return __assign({ id: id, delimiter: options.delimiter }, (initial ? { initial: initial } : undefined), (parallel ? { parallel: parallel } : undefined), (stateElements.length
|
|
234
235
|
? {
|
|
235
236
|
states: mapValues(states, function (state, key) {
|
|
236
237
|
return toConfig(state, key, options);
|
|
237
238
|
})
|
|
238
239
|
}
|
|
239
|
-
: undefined, transitionElements.length ? { on: on } : undefined, onEntry ? { onEntry: onEntry } : undefined, onExit ? { onExit: onExit } : undefined);
|
|
240
|
+
: undefined), (transitionElements.length ? { on: on } : undefined), (onEntry ? { onEntry: onEntry } : undefined), (onExit ? { onExit: onExit } : undefined));
|
|
240
241
|
}
|
|
241
242
|
return { id: id };
|
|
242
243
|
}
|
package/es/types.d.ts
CHANGED
|
@@ -19,6 +19,10 @@ export interface StateValueMap {
|
|
|
19
19
|
[key: string]: StateValue;
|
|
20
20
|
}
|
|
21
21
|
export declare type StateValue = string | StateValueMap;
|
|
22
|
+
export interface HistoryValue {
|
|
23
|
+
states: Record<string, HistoryValue | undefined>;
|
|
24
|
+
current: StateValue | undefined;
|
|
25
|
+
}
|
|
22
26
|
export declare type ConditionPredicate = (extendedState: any, event: EventObject, microstepState: StateValue) => boolean;
|
|
23
27
|
export declare type Condition = string | ConditionPredicate;
|
|
24
28
|
export interface TransitionConfig {
|
|
@@ -85,7 +89,6 @@ export interface StandardMachineConfig extends MachineConfig {
|
|
|
85
89
|
export interface ParallelMachineConfig extends MachineConfig {
|
|
86
90
|
initial?: undefined;
|
|
87
91
|
parallel: true;
|
|
88
|
-
states: Record<string, CompoundStateNodeConfig>;
|
|
89
92
|
}
|
|
90
93
|
export interface EntryExitEffectMap {
|
|
91
94
|
entry: Action[];
|
|
@@ -149,12 +152,6 @@ export interface ActivityMap {
|
|
|
149
152
|
}
|
|
150
153
|
export declare type MaybeStateValueActionsTuple = [StateValue | undefined, ActionMap, ActivityMap | undefined];
|
|
151
154
|
export interface StateTransition {
|
|
152
|
-
statePaths: string[][];
|
|
153
|
-
actions: ActionMap;
|
|
154
|
-
activities: ActivityMap | undefined;
|
|
155
|
-
events: EventObject[];
|
|
156
|
-
}
|
|
157
|
-
export interface _StateTransition {
|
|
158
155
|
value: StateValue | undefined;
|
|
159
156
|
entryExitStates: EntryExitStates | undefined;
|
|
160
157
|
actions: Action[];
|
package/es/utils.d.ts
CHANGED
|
@@ -23,7 +23,12 @@ export declare function mapFilterValues<T, P>(collection: {
|
|
|
23
23
|
* Retrieves a value at the given path.
|
|
24
24
|
* @param props The deep path to the prop of the desired value
|
|
25
25
|
*/
|
|
26
|
-
export declare const path: (props: string[]) => any;
|
|
26
|
+
export declare const path: <T extends Record<string, any>>(props: string[]) => any;
|
|
27
|
+
/**
|
|
28
|
+
* Retrieves a value at the given path via the nested accessor prop.
|
|
29
|
+
* @param props The deep path to the prop of the desired value
|
|
30
|
+
*/
|
|
31
|
+
export declare function nestedPath<T extends Record<string, any>>(props: string[], accessorProp: keyof T): (object: T) => T;
|
|
27
32
|
export declare const toStatePaths: (stateValue: StateValue) => string[][];
|
|
28
33
|
export declare const pathsToStateValue: (paths: string[][]) => StateValue;
|
|
29
34
|
export declare const flatMap: <T>(array: T[][]) => T[];
|
package/es/utils.js
CHANGED
|
@@ -13,7 +13,9 @@ export function getActionType(action) {
|
|
|
13
13
|
try {
|
|
14
14
|
return typeof action === 'string' || typeof action === 'number'
|
|
15
15
|
? "" + action
|
|
16
|
-
: typeof action === 'function'
|
|
16
|
+
: typeof action === 'function'
|
|
17
|
+
? action.name
|
|
18
|
+
: action.type;
|
|
17
19
|
}
|
|
18
20
|
catch (e) {
|
|
19
21
|
throw new Error('Events must be strings or objects with a string event.type property.');
|
|
@@ -87,6 +89,20 @@ export var path = function (props) { return function (object) {
|
|
|
87
89
|
}
|
|
88
90
|
return result;
|
|
89
91
|
}; };
|
|
92
|
+
/**
|
|
93
|
+
* Retrieves a value at the given path via the nested accessor prop.
|
|
94
|
+
* @param props The deep path to the prop of the desired value
|
|
95
|
+
*/
|
|
96
|
+
export function nestedPath(props, accessorProp) {
|
|
97
|
+
return function (object) {
|
|
98
|
+
var result = object;
|
|
99
|
+
for (var _i = 0, props_2 = props; _i < props_2.length; _i++) {
|
|
100
|
+
var prop = props_2[_i];
|
|
101
|
+
result = result[accessorProp][prop];
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
90
106
|
export var toStatePaths = function (stateValue) {
|
|
91
107
|
if (typeof stateValue === 'string') {
|
|
92
108
|
return [[stateValue]];
|
package/lib/State.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { StateValue, ActivityMap, EventObject, Action, StateInterface } from './types';
|
|
1
|
+
import { StateValue, ActivityMap, EventObject, Action, StateInterface, HistoryValue } from './types';
|
|
2
2
|
export declare class State implements StateInterface {
|
|
3
3
|
value: StateValue;
|
|
4
|
-
|
|
4
|
+
historyValue?: HistoryValue | undefined;
|
|
5
|
+
history?: State | undefined;
|
|
5
6
|
actions: Action[];
|
|
6
7
|
activities: ActivityMap;
|
|
7
8
|
data: Record<string, any>;
|
|
@@ -11,10 +12,10 @@ export declare class State implements StateInterface {
|
|
|
11
12
|
events: EventObject[];
|
|
12
13
|
static from(stateValue: State | StateValue): State;
|
|
13
14
|
static inert(stateValue: State | StateValue): State;
|
|
14
|
-
constructor(value: StateValue, history?: State | undefined, actions?: Action[], activities?: ActivityMap, data?: Record<string, any>,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
constructor(value: StateValue, historyValue?: HistoryValue | undefined, history?: State | undefined, actions?: Action[], activities?: ActivityMap, data?: Record<string, any>,
|
|
16
|
+
/**
|
|
17
|
+
* Internal event queue
|
|
18
|
+
*/
|
|
19
|
+
events?: EventObject[]);
|
|
19
20
|
toString(): string | undefined;
|
|
20
21
|
}
|
package/lib/State.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var constants_1 = require("./constants");
|
|
4
4
|
var State = /** @class */ (function () {
|
|
5
|
-
function State(value, history, actions, activities, data,
|
|
5
|
+
function State(value, historyValue, history, actions, activities, data,
|
|
6
6
|
/**
|
|
7
7
|
* Internal event queue
|
|
8
8
|
*/
|
|
@@ -12,6 +12,7 @@ var State = /** @class */ (function () {
|
|
|
12
12
|
if (data === void 0) { data = {}; }
|
|
13
13
|
if (events === void 0) { events = []; }
|
|
14
14
|
this.value = value;
|
|
15
|
+
this.historyValue = historyValue;
|
|
15
16
|
this.history = history;
|
|
16
17
|
this.actions = actions;
|
|
17
18
|
this.activities = activities;
|
|
@@ -29,7 +30,7 @@ var State = /** @class */ (function () {
|
|
|
29
30
|
if (!stateValue.actions.length) {
|
|
30
31
|
return stateValue;
|
|
31
32
|
}
|
|
32
|
-
return new State(stateValue.value, stateValue.history, [], stateValue.activities);
|
|
33
|
+
return new State(stateValue.value, stateValue.historyValue, stateValue.history, [], stateValue.activities);
|
|
33
34
|
}
|
|
34
35
|
return State.from(stateValue);
|
|
35
36
|
};
|