xstate 3.2.1 → 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.
Files changed (55) hide show
  1. package/.vscode/launch.json +15 -13
  2. package/README.md +37 -9
  3. package/dist/xstate.js +1 -1
  4. package/dist/xstate.utils.js +1 -1
  5. package/es/Machine.d.ts +2 -2
  6. package/es/Machine.js +2 -2
  7. package/es/State.d.ts +8 -7
  8. package/es/State.js +3 -2
  9. package/es/StateNode.d.ts +50 -13
  10. package/es/StateNode.js +617 -412
  11. package/es/graph.d.ts +9 -6
  12. package/es/graph.js +31 -24
  13. package/es/patterns.js +1 -1
  14. package/es/scxml.d.ts +2 -1
  15. package/es/scxml.js +33 -10
  16. package/es/types.d.ts +38 -7
  17. package/es/utils.d.ts +14 -1
  18. package/es/utils.js +33 -5
  19. package/lib/Machine.d.ts +2 -2
  20. package/lib/Machine.js +2 -2
  21. package/lib/State.d.ts +8 -7
  22. package/lib/State.js +3 -2
  23. package/lib/StateNode.d.ts +50 -13
  24. package/lib/StateNode.js +616 -411
  25. package/lib/graph.d.ts +9 -6
  26. package/lib/graph.js +30 -22
  27. package/lib/patterns.js +1 -1
  28. package/lib/scxml.d.ts +2 -1
  29. package/lib/scxml.js +33 -10
  30. package/lib/types.d.ts +38 -7
  31. package/lib/utils.d.ts +14 -1
  32. package/lib/utils.js +35 -5
  33. package/package.json +3 -3
  34. package/src/Machine.ts +5 -3
  35. package/src/State.ts +10 -2
  36. package/src/StateNode.ts +966 -590
  37. package/src/graph.ts +60 -31
  38. package/src/scxml.ts +80 -49
  39. package/src/types.ts +48 -7
  40. package/src/utils.ts +52 -7
  41. package/test/actions.test.ts +24 -1
  42. package/test/activities.test.ts +165 -0
  43. package/test/deep.test.ts +14 -16
  44. package/test/deterministic.test.ts +26 -5
  45. package/test/examples/6.17.test.ts +64 -0
  46. package/test/fixtures/id.ts +1 -1
  47. package/test/graph.test.ts +39 -16
  48. package/test/guards.test.ts +172 -15
  49. package/test/history.test.ts +193 -58
  50. package/test/invalid.test.ts +48 -0
  51. package/test/multiple.test.ts +12 -18
  52. package/test/parallel.test.ts +472 -1
  53. package/test/scxml.test.ts +13 -4
  54. package/test/stateIn.test.ts +1 -1
  55. package/test/transient.test.ts +183 -1
@@ -4,32 +4,34 @@
4
4
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
5
  "version": "0.2.0",
6
6
  "configurations": [
7
+
7
8
  {
8
9
  "type": "node",
9
10
  "request": "launch",
10
11
  "name": "Launch Program",
11
12
  "program": "${workspaceFolder}/lib/index.js",
12
13
  "preLaunchTask": "tsc: build - tsconfig.json",
13
- "outFiles": [
14
- "${workspaceFolder}/lib/**/*.js"
15
- ]
14
+ "outFiles": ["${workspaceFolder}/lib/**/*.js"]
16
15
  },
17
16
  {
17
+ "name": "Run Mocha Tests",
18
18
  "type": "node",
19
19
  "request": "launch",
20
- "name": "Mocha All",
21
- "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
20
+ "cwd": "${workspaceRoot}",
21
+ "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
22
22
  "args": [
23
- "--timeout",
24
- "999999",
25
- "--colors",
26
- "--require",
27
- "ts-node/register",
28
- "${workspaceFolder}/test/**.ts",
29
- "${workspaceFolder}/test/**.test.ts"
23
+ "-r",
24
+ "ts-node/register",
25
+ "--timeout",
26
+ "999999",
27
+ "--colors",
28
+ // "${workspaceFolder}/test/**.test.ts",
29
+ "${workspaceFolder}/test/scxml.test.ts",
30
+
30
31
  ],
31
32
  "console": "integratedTerminal",
32
- "internalConsoleOptions": "neverOpen"
33
+ "internalConsoleOptions": "neverOpen",
34
+ "protocol": "inspector"
33
35
  }
34
36
  ]
35
37
  }
package/README.md CHANGED
@@ -8,8 +8,31 @@ Functional, stateless JavaScript [finite state machines](https://en.wikipedia.or
8
8
 
9
9
  📖 [Read the documentation!](http://davidkpiano.github.io/xstate/docs)
10
10
 
11
+ - [Visualizer](#visualizer)
12
+ - [3rd-Party Usage](#3rd-party-usage)
13
+ - [Why? (info about statecharts)](#why)
14
+ - [Installation](#installation)
15
+ - [Finite State Machines](#finite-state-machines)
16
+ - [Hierarchical (Nested) State Machines](#hierarchical-nested-state-machines)
17
+ - [Parallel State Machines](#parallel-state-machines)
18
+ - [History States](#history-states)
19
+ - [Interpreters](#interpreters)
20
+
21
+ ## Visualizer
22
+
23
+ **[:new: Preview and simulate your statecharts in the xstate visualizer (beta)!](https://bit.ly/xstate-viz)**
24
+
25
+ <a href="https://bit.ly/xstate-viz" title="xstate visualizer"><img src="https://i.imgur.com/fOMJKDZ.png" alt="xstate visualizer" width="300" /></a>
26
+
27
+ ## 3rd-Party Usage
28
+
29
+ With [sketch.systems](https://sketch.systems), you can now copy-paste your state machine sketches as `xstate`-compatible JSON!
30
+ 1. Create your sketch (example: https://sketch.systems/anon/sketch/new)
31
+ 2. Click **Export to clipboard...**
32
+ 3. Select `XState JSON`
33
+
11
34
  ## Why?
12
- In short, statecharts are a formalism for modeling stateful, reactive systems. This is useful for declaratively describing the _behavior_ of your application, from the individual components to the overall application logic.
35
+ Statecharts are a formalism for modeling stateful, reactive systems. This is useful for declaratively describing the _behavior_ of your application, from the individual components to the overall application logic.
13
36
 
14
37
  Read [📽 the slides](http://slides.com/davidkhourshid/finite-state-machines) ([🎥 video](https://www.youtube.com/watch?v=VU1NKX6Qkxc)) or check out these resources for learning about the importance of finite state machines and statecharts in user interfaces:
15
38
 
@@ -17,6 +40,7 @@ Read [📽 the slides](http://slides.com/davidkhourshid/finite-state-machines) (
17
40
  - [The World of Statecharts](https://statecharts.github.io/) by Erik Mogensen
18
41
  - [Pure UI](https://rauchg.com/2015/pure-ui) by Guillermo Rauch
19
42
  - [Pure UI Control](https://medium.com/@asolove/pure-ui-control-ac8d1be97a8d) by Adam Solove
43
+ - [Spectrum - Statecharts Community](https://spectrum.chat/statecharts)
20
44
 
21
45
  ## Installation
22
46
  1. `npm install xstate --save`
@@ -24,7 +48,7 @@ Read [📽 the slides](http://slides.com/davidkhourshid/finite-state-machines) (
24
48
 
25
49
  ## Finite State Machines
26
50
 
27
- <img src="http://i.imgur.com/KNUL5X8.png" alt="Light Machine" width="300" />
51
+ <img src="https://imgur.com/rqqmkJh.png" alt="Light Machine" width="300" />
28
52
 
29
53
  ```js
30
54
  import { Machine } from 'xstate';
@@ -62,7 +86,7 @@ const nextState = lightMachine
62
86
 
63
87
  ## Hierarchical (Nested) State Machines
64
88
 
65
- <img src="http://imgur.com/OuZ1nn8.png" alt="Hierarchical Light Machine" width="300" />
89
+ <img src="https://imgur.com/GDZAeB9.png" alt="Hierarchical Light Machine" width="300" />
66
90
 
67
91
  ```js
68
92
  import { Machine } from 'xstate';
@@ -147,7 +171,9 @@ lightMachine
147
171
  // => 'green'
148
172
  ```
149
173
 
150
- ## Parallel States
174
+ ## Parallel State Machines
175
+
176
+ <img src="https://imgur.com/GKd4HwR.png" width="300" alt="Parallel state machine" />
151
177
 
152
178
  ```js
153
179
  const wordMachine = Machine({
@@ -233,9 +259,7 @@ const nextState = wordMachine
233
259
 
234
260
  ## History States
235
261
 
236
- To provide full flexibility, history states are more arbitrarily defined than the original statechart specification. To go to a history state, use the special key `$history`.
237
-
238
- <img src="http://imgur.com/sjTlr6j.png" width="300" alt="Payment Machine" />
262
+ <img src="https://imgur.com/I4QsQsz.png" width="300" alt="Machine with history state" />
239
263
 
240
264
  ```js
241
265
  const paymentMachine = Machine({
@@ -245,12 +269,13 @@ const paymentMachine = Machine({
245
269
  initial: 'cash',
246
270
  states: {
247
271
  cash: { on: { SWITCH_CHECK: 'check' } },
248
- check: { on: { SWITCH_CASH: 'cash' } }
272
+ check: { on: { SWITCH_CASH: 'cash' } },
273
+ hist: { history: true }
249
274
  },
250
275
  on: { NEXT: 'review' }
251
276
  },
252
277
  review: {
253
- on: { PREVIOUS: 'method.$history' }
278
+ on: { PREVIOUS: 'method.hist' }
254
279
  }
255
280
  }
256
281
  });
@@ -277,3 +302,6 @@ const previousState = paymentMachine
277
302
 
278
303
  // => { method: 'check' }
279
304
  ```
305
+
306
+ ## Interpreters
307
+ - [`xstateful` by @avaragado](https://www.npmjs.com/package/@avaragado/xstateful)
package/dist/xstate.js CHANGED
@@ -1 +1 @@
1
- !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.xstate=e():t.xstate=e()}(this,function(){return function(t){function e(i){if(n[i])return n[i].exports;var r=n[i]={i:i,l:!1,exports:{}};return t[i].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,i){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:i})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,n){"use strict";function i(t){try{return"string"==typeof t||"number"==typeof t?""+t:t.type}catch(t){throw new Error("Events must be strings or objects with a string event.type property.")}}function r(t,e){try{return Array.isArray(t)?t:t.toString().split(e)}catch(e){throw new Error("'"+t+"' is not a valid state path.")}}function a(t,e){return t instanceof p?t.value:"object"!=typeof t||t instanceof p?o(r(t,e)):t}function o(t){if(1===t.length)return t[0];for(var e={},n=e,i=0;i<t.length-1;i++)i===t.length-2?n[t[i]]=t[i+1]:(n[t[i]]={},n=n[t[i]]);return e}function s(t,e){var n={};return Object.keys(t).forEach(function(i){n[i]=e(t[i],i,t)}),n}function c(t,e,n){void 0===n&&(n=l);var i=a(t,n),r=a(e,n);return"string"==typeof r?"string"==typeof i&&r===i:"string"==typeof i?i in r:Object.keys(i).every(function(t){return t in r&&c(i[t],r[t])})}function u(t,e){var n;return Object.keys(t).forEach(function(t){c(t,e)&&(!n||e.length>n.length)&&(n=t)}),t[n]}function f(t){return new I(t)}Object.defineProperty(e,"__esModule",{value:!0});var h={};n.d(h,"actionTypes",function(){return E}),n.d(h,"toEventObject",function(){return m}),n.d(h,"toActionObject",function(){return S}),n.d(h,"toActionObjects",function(){return x}),n.d(h,"raise",function(){return w}),n.d(h,"send",function(){return j}),n.d(h,"cancel",function(){return O}),n.d(h,"start",function(){return k}),n.d(h,"stop",function(){return P});var l=".",v={},p=function(){function t(t,e,n,i,r,a){void 0===n&&(n=[]),void 0===i&&(i=v),void 0===r&&(r={}),void 0===a&&(a=[]),this.value=t,this.history=e,this.actions=n,this.activities=i,this.data=r,this.events=a}return t.from=function(e){return e instanceof t?e:new t(e)},t.inert=function(e){return e instanceof t?e.actions.length?new t(e.value,e.history,[]):e:t.from(e)},t.prototype.toString=function(){if("string"==typeof this.value)return this.value;for(var t=[],e=this.value;;){if("string"==typeof e){t.push(e);break}var n=Object.keys(e),i=n[0];if(n.slice(1).length)return;t.push(i),e=e[i]}return t.join(l)},t}(),d=function(t){return function(e){for(var n=e,i=0,r=t;i<r.length;i++){n=n[r[i]]}return n}},y=function(t){return"string"==typeof t?[[t]]:Object.keys(t).map(function(e){return y(t[e]).map(function(t){return[e].concat(t)})}).reduce(function(t,e){return t.concat(e)},[])},g=function(t){var e={};if(t&&1===t.length&&1===t[0].length)return t[0][0];for(var n=0,i=t;n<i.length;n++)for(var r=i[n],a=e,o=0;o<r.length;o++){var s=r[o];if(o===r.length-2){a[s]=r[o+1];break}a[s]=a[s]||{},a=a[s]}return e},E={start:"xstate.start",stop:"xstate.stop",raise:"xstate.raise",send:"xstate.send",cancel:"xstate.cancel",null:"xstate.null"},b=function(t){return function(e){var n="string"==typeof e||"number"==typeof e?{type:e}:e;return{type:t,activity:i(e),data:n}}},m=function(t){return"string"==typeof t||"number"==typeof t?{type:t}:t},S=function(t){var e;if("string"==typeof t||"number"==typeof t)e={type:t};else{if("function"!=typeof t)return t;e={type:t.name}}return Object.defineProperty(e,"toString",{value:function(){return e.type}}),e},x=function(t){return t?(Array.isArray(t)?t:[t]).map(S):[]},w=function(t){return{type:E.raise,event:t}},j=function(t,e){return{type:E.send,event:m(t),delay:e?e.delay:void 0,id:e&&void 0!==e.id?e.id:i(t)}},O=function(t){return{type:E.cancel,sendId:t}},k=b(E.start),P=b(E.stop),N=this&&this.__assign||Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++){e=arguments[n];for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])}return t},_=".",V=function(t){return"#"===t[0]},A=Object.freeze({onEntry:[],onExit:[],actions:[]}),M=function(t){for(;t.parent&&!t.parent.parallel;)t=t.parent;return t},T=function(t,e,n,i){var r=M(i),a=r.parent,o=a?a.id:"";if(n[o]=n[o]||{},n[o][r.id]&&n[o][r.id]!==i)throw new Error("Event '"+e+"' on state '"+t.id+"' leads to an invalid configuration: Two or more states in the orthogonal region '"+r.id+"'.");return n[o][r.id]=i,r},I=function(){function t(e){var n=this;this.config=e,this.__cache={events:void 0,relativeValue:new Map,initialState:void 0},this.idMap={},this.key=e.key||"(machine)",this.parent=e.parent,this.machine=this.parent?this.parent.machine:this,this.path=this.parent?this.parent.path.concat(this.key):[],this.delimiter=e.delimiter||(this.parent?this.parent.delimiter:_),this.id=e.id||(this.machine?[this.machine.key].concat(this.path).join(this.delimiter):this.key),this.initial=e.initial,this.parallel=!!e.parallel,this.states=e.states?s(e.states,function(e,i){var r=new t(N({},e,{key:i,parent:n}));return Object.assign(n.idMap,N((a={},a[r.id]=r,a),r.idMap)),r;var a}):{},this.on=e.on?this.formatTransitions(e.on):{},this.strict=!!e.strict,this.onEntry=e.onEntry?[].concat(e.onEntry):void 0,this.onExit=e.onExit?[].concat(e.onExit):void 0,this.data=e.data,this.activities=e.activities}return t.prototype.getStateNodes=function(t){var e=this,n=t instanceof p?t.value:a(t,this.delimiter);if("string"==typeof n){var i=this.getStateNode(n).initial;return i?this.getStateNodes((o={},o[n]=i,o)):[this.states[n]]}var r=Object.keys(n);return r.map(function(t){return e.getStateNode(t)}).concat(r.reduce(function(t,i){var r=e.getStateNode(i).getStateNodes(n[i]);return t.concat(r)},[]));var o},t.prototype.handles=function(t){var e=i(t);return-1!==this.events.indexOf(e)},t.prototype.transition=function(t,e,n){var r="string"==typeof t?this.resolve(o(this.getResolvedPath(t))):t instanceof p?t:this.resolve(t);if(this.strict){var a=i(e);if(-1===this.events.indexOf(a))throw new Error("Machine '"+this.id+"' does not accept event '"+a+"'")}var s=p.from(r),c=this.transitionStateValue(s,e,s,n),u=this.stateTransitionToState(c,s);if(!u)return p.inert(s);var f=u,h=u.actions.filter(function(t){return"object"==typeof t&&t.type===E.raise});if(h.length){var l=h[0].event;return u=this.transition(u,l,n),(y=u.actions).unshift.apply(y,u.actions),u}if(c.events.length){var v=c.events[0].type===E.raise?c.events[0].event:void 0,d=c.events[0].type===E.null;if(v||d)return f=this.transition(u,d?"":v,n),(g=f.actions).unshift.apply(g,u.actions),f}return u;var y,g},t.prototype.stateTransitionToState=function(t,e){var n=t.statePaths,i=t.actions,r=t.activities,a=t.events;if(n.length){var o=e instanceof p?e.activities:void 0,s=N({},o,r),c=this.resolve(g(n));return new p(c,p.from(e),i?i.onExit.concat(i.actions).concat(i.onEntry):[],s,this.getStateNodes(c).reduce(function(t,e){return void 0!==e.data&&(t[e.id]=e.data),t},{}),a)}},t.prototype.getStateNode=function(t){if(V(t))return this.machine.getStateNodeById(t);if(!this.states)throw new Error("Unable to retrieve child state '"+t+"' from '"+this.id+"'; no child states exist.");var e=this.states[t];if(!e)throw new Error("Child state '"+t+"' does not exist on '"+this.id+"'");return e},t.prototype.getStateNodeById=function(t){var e=V(t)?t.slice("#".length):t,n=this.idMap[e];if(!n)throw new Error("Substate '#"+e+"' does not exist on '"+this.id+"'");return n},t.prototype.resolve=function(t){var e=this;if("string"==typeof t){var n=this.getStateNode(t);return n.initial?(i={},i[t]=n.initialStateValue,i):t}return this.parallel?s(this.initialStateValue,function(n,i){return e.getStateNode(i).resolve(t[i]||n)}):s(t,function(t,n){return e.getStateNode(n).resolve(t)});var i},t.prototype.transitionStateValue=function(t,e,n,i){var r=this,a=t.history,o=t.value;if("string"==typeof o){var c=this.getStateNode(o),u=c.next(e,n,a?a.value:void 0,i);return u.statePaths.length||this.parent?u:this.next(e,n,a?a.value:void 0,i)}var f=[],h=!1,l=s(o,function(t,o){var s=r.getStateNode(o),c=a?a.value[o]:void 0,u=new p(t,c?p.from(c):void 0),l=s.transitionStateValue(u,e,n,i);return l.statePaths.length?h=!0:f.push(s.next(e,n,a?a.value:void 0,i)),l});if(!h){if(this.parallel)return f.length?f[0]:{statePaths:[],actions:A,activities:void 0,events:[]};var v=Object.keys(l)[0],d=this.getStateNode(v).next(e,n,a?a.value:void 0,i),g=d.statePaths,E=d.actions,b=d.activities,m=l[v].actions,S=l[v].activities,x=N({},S,b);return{statePaths:g,actions:E?m?{onEntry:m.onEntry.concat(E.onEntry),actions:m.actions.concat(E.actions),onExit:m.onExit.concat(E.onExit)}:E:m,activities:x,events:[]}}this.parallel&&(l=N({},s(this.initialState.value,function(t,e){var n=l[e];return{statePaths:n&&n.statePaths.length?n.statePaths:y(o[e]||t).map(function(t){return r.getStateNode(e).path.concat(t)}),actions:n&&n.actions?n.actions:{onEntry:[],onExit:[],actions:[]},activities:void 0,events:[]}})));var w={onEntry:[],actions:[],onExit:[]},j={};return s(l,function(t){var e=t.actions,n=t.activities;e&&(e.onEntry&&(i=w.onEntry).push.apply(i,e.onEntry),e.actions&&(r=w.actions).push.apply(r,e.actions),e.onExit&&(a=w.onExit).push.apply(a,e.onExit)),n&&Object.assign(j,n);var i,r,a}),{statePaths:Object.keys(l).map(function(t){return l[t].statePaths}).reduce(function(t,e){return t.concat(e)},[]),actions:w,activities:j,events:[]}},t.prototype.next=function(t,e,n,r){var o=this,s=i(t),u={onEntry:[],onExit:[],actions:[]},f={},h=this.on[s];if(this.onExit&&(u.onExit=this.onExit),this.activities&&this.activities.forEach(function(t){f[i(t)]=!1,u.onExit=u.onExit.concat(P(t))}),!h)return{statePaths:[],actions:u,activities:f,events:[]};for(var l=[],v=0,p=h;v<p.length;v++){var g=p[v],b=g,S=b.cond,x=b.in,w=b.actions,j=r||{},O=m(t),N=!x||c(a(x,this.delimiter),d(this.path.slice(0,-2))(e.value));if((!S||S(j,O))&&(!x||N)){l=Array.isArray(g.target)?g.target:[g.target],w&&(u.actions=u.actions.concat(w));break}}if(0===l.length)return{statePaths:[],actions:u,activities:f,events:[]};var _=[],A=[],M={};return l.forEach(function(e){var r=o.getResolvedPath(e),a=V(e)?o.machine:o.parent,s=n,c=o.key;if(r.forEach(function(e){if(""===e)return u.onExit=[],void(a=o);if(!a||!a.states)throw new Error("Unable to read '"+e+"' from '"+o.id+"'");if("$history"===e)if(Object.keys(a.states).length)if(s)e="object"==typeof s?Object.keys(s)[0]:s;else{if(!a.initial)throw new Error("Cannot read '$history' from state '"+a.id+"': missing 'initial'");e=a.initial}else e="";try{""!==e&&(a=a.getStateNode(e))}catch(e){throw new Error("Event '"+t+"' on state '"+c+"' leads to undefined state '"+r.join(o.delimiter)+"'.")}a.onEntry&&(u.onEntry=u.onEntry.concat(a.onEntry)),a.activities&&a.activities.forEach(function(t){f[i(t)]=!0,u.onEntry=u.onEntry.concat(k(t))}),c=e,s&&(s=s[e])}),!a)throw new Error("no state");for(var h=T(o,t,M,a);h.parent;)h=T(o,t,M,h.parent);var l=[a.path];if(a.initial||a.parallel){var v=a.initialState;u.onEntry=u.onEntry.concat(v.actions),l=y(v.value).map(function(t){return a.path.concat(t)})}for(_.push.apply(_,l);a.initial;){if(!a||!a.states)throw new Error("Invalid initial state");a=a.states[a.initial],a.activities&&a.activities.forEach(function(t){f[i(t)]=!0,u.onEntry=u.onEntry.concat(k(t))})}(a.onEntry?a.onEntry.filter(function(t){return"object"==typeof t&&t.type===E.raise}):[]).concat(a.on[""]?{type:E.null}:[]).forEach(function(t){return A.push(t)})}),{statePaths:_,actions:u,activities:f,events:A}},Object.defineProperty(t.prototype,"resolvedStateValue",{get:function(){var t=this.key;return this.parallel?(e={},e[t]=s(this.states,function(t){return t.resolvedStateValue[t.key]}),e):this.initial?(n={},n[t]=this.states[this.initial].resolvedStateValue,n):t;var e,n},enumerable:!0,configurable:!0}),t.prototype.getResolvedPath=function(t){if(V(t)){var e=this.machine.idMap[t.slice("#".length)];if(!e)throw new Error("Unable to find state node '"+t+"'");return e.path}return r(t,this.delimiter)},Object.defineProperty(t.prototype,"initialStateValue",{get:function(){var t=this.__cache.initialState||(this.parallel?s(this.states,function(t){return t.initialStateValue}):"string"==typeof this.resolvedStateValue?void 0:this.resolvedStateValue[this.key]);return this.__cache.initialState=t,this.__cache.initialState},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"initialState",{get:function(){var t=this.initialStateValue;if(!t)throw new Error("Cannot retrieve initial state from simple state '"+this.id+".'");var e={},n=[];return this.getStateNodes(t).forEach(function(t){t.onEntry&&n.push.apply(n,t.onEntry),t.activities&&t.activities.forEach(function(t){e[i(t)]=!0,n.push(k(t))})}),new p(t,void 0,n,e)},enumerable:!0,configurable:!0}),t.prototype.getStates=function(t){var e=this;if("string"==typeof t)return[this.states[t]];var n=[];return Object.keys(t).forEach(function(i){n.push.apply(n,e.states[i].getStates(t[i]))}),n},t.prototype.getState=function(t){if("string"==typeof t&&V(t))return this.getStateNodeById(t);var e=r(t,this.delimiter);try{return e.reduce(function(t,e){if(!t.states)throw new Error("Cannot retrieve subPath '"+e+"' from node with no states");return t.states[e]},this)}catch(e){throw new Error("State '"+t+" does not exist on machine '"+this.id+"'")}},Object.defineProperty(t.prototype,"events",{get:function(){if(this.__cache.events)return this.__cache.events;var t=this.states,e=new Set(Object.keys(this.on));return t&&Object.keys(t).forEach(function(n){var i=t[n];if(i.states)for(var r=0,a=i.events;r<a.length;r++){var o=a[r];e.add(""+o)}}),this.__cache.events=Array.from(e)},enumerable:!0,configurable:!0}),t.prototype.formatTransitions=function(t){return s(t,function(t){return void 0===t?[]:Array.isArray(t)?t:"string"==typeof t?[{target:t}]:Object.keys(t).map(function(e){return N({target:e},t[e])})})},t}();n.d(e,"Machine",function(){return f}),n.d(e,"StateNode",function(){return I}),n.d(e,"State",function(){return p}),n.d(e,"matchesState",function(){return c}),n.d(e,"mapState",function(){return u}),n.d(e,"actions",function(){return h})}])});
1
+ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.xstate=e():t.xstate=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,e),i.l=!0,i.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,n){"use strict";function r(t){try{return"string"==typeof t||"number"==typeof t?""+t:t.type}catch(t){throw new Error("Events must be strings or objects with a string event.type property.")}}function i(t){try{return"string"==typeof t||"number"==typeof t?""+t:"function"==typeof t?t.name:t.type}catch(t){throw new Error("Events must be strings or objects with a string event.type property.")}}function a(t,e){try{return Array.isArray(t)?t:t.toString().split(e)}catch(e){throw new Error("'"+t+"' is not a valid state path.")}}function o(t,e){return t instanceof g?t.value:"object"!=typeof t||t instanceof g?s(a(t,e)):t}function s(t){if(1===t.length)return t[0];for(var e={},n=e,r=0;r<t.length-1;r++)r===t.length-2?n[t[r]]=t[r+1]:(n[t[r]]={},n=n[t[r]]);return e}function u(t,e){var n={};return Object.keys(t).forEach(function(r){n[r]=e(t[r],r,t)}),n}function c(t,e,n){var r={};return Object.keys(t).forEach(function(i){var a=t[i];n(a)&&(r[i]=e(a,i,t))}),r}function h(t,e){return function(n){for(var r=n,i=0,a=t;i<a.length;i++){var o=a[i];r=r[e][o]}return r}}function f(t,e,n){void 0===n&&(n=v);var r=o(t,n),i=o(e,n);return"string"==typeof i?"string"==typeof r&&i===r:"string"==typeof r?r in i:Object.keys(r).every(function(t){return t in i&&f(r[t],i[t])})}function p(t,e){var n;return Object.keys(t).forEach(function(t){f(t,e)&&(!n||e.length>n.length)&&(n=t)}),t[n]}function l(t,e){return new C(t,e)}Object.defineProperty(e,"__esModule",{value:!0});var y={};n.d(y,"actionTypes",function(){return E}),n.d(y,"toEventObject",function(){return j}),n.d(y,"toActionObject",function(){return O}),n.d(y,"toActionObjects",function(){return _}),n.d(y,"raise",function(){return N}),n.d(y,"send",function(){return k}),n.d(y,"cancel",function(){return A}),n.d(y,"start",function(){return V}),n.d(y,"stop",function(){return P});var v=".",d={},g=function(){function t(t,e,n,r,i,a,o){void 0===r&&(r=[]),void 0===i&&(i=d),void 0===a&&(a={}),void 0===o&&(o=[]),this.value=t,this.historyValue=e,this.history=n,this.actions=r,this.activities=i,this.data=a,this.events=o}return t.from=function(e){return e instanceof t?e:new t(e)},t.inert=function(e){return e instanceof t?e.actions.length?new t(e.value,e.historyValue,e.history,[],e.activities):e:t.from(e)},t.prototype.toString=function(){if("string"==typeof this.value)return this.value;for(var t=[],e=this.value;;){if("string"==typeof e){t.push(e);break}var n=Object.keys(e),r=n[0];if(n.slice(1).length)return;t.push(r),e=e[r]}return t.join(v)},t}(),m=function(t){return function(e){for(var n=e,r=0,i=t;r<i.length;r++){n=n[i[r]]}return n}},S=function(t){return"string"==typeof t?[[t]]:b(Object.keys(t).map(function(e){return S(t[e]).map(function(t){return[e].concat(t)})}))},x=function(t){var e={};if(t&&1===t.length&&1===t[0].length)return t[0][0];for(var n=0,r=t;n<r.length;n++)for(var i=r[n],a=e,o=0;o<i.length;o++){var s=i[o];if(o===i.length-2){a[s]=i[o+1];break}a[s]=a[s]||{},a=a[s]}return e},b=function(t){return t.reduce(function(t,e){return t.concat(e)},[])},E={start:"xstate.start",stop:"xstate.stop",raise:"xstate.raise",send:"xstate.send",cancel:"xstate.cancel",null:"xstate.null"},w=function(t){return function(e){var n="string"==typeof e||"number"==typeof e?{type:e}:e;return{type:t,activity:r(e),data:n}}},j=function(t){return"string"==typeof t||"number"==typeof t?{type:t}:t},O=function(t){var e;if("string"==typeof t||"number"==typeof t)e={type:t};else{if("function"!=typeof t)return t;e={type:t.name}}return Object.defineProperty(e,"toString",{value:function(){return e.type}}),e},_=function(t){return t?(Array.isArray(t)?t:[t]).map(O):[]},N=function(t){return{type:E.raise,event:t}},k=function(t,e){return{type:E.send,event:j(t),delay:e?e.delay:void 0,id:e&&void 0!==e.id?e.id:r(t)}},A=function(t){return{type:E.cancel,sendId:t}},V=w(E.start),P=w(E.stop),R=this&&this.__assign||Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++){e=arguments[n];for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])}return t},M=".",F="",H=function(t){return"#"===t[0]},T={guards:{}},C=function(){function t(e,n){void 0===n&&(n=T);var r=this;this.config=e,this.options=n,this.__cache={events:void 0,relativeValue:new Map,initialState:void 0},this.idMap={},this.key=e.key||"(machine)",this.parent=e.parent,this.machine=this.parent?this.parent.machine:this,this.path=this.parent?this.parent.path.concat(this.key):[],this.delimiter=e.delimiter||(this.parent?this.parent.delimiter:M),this.id=e.id||(this.machine?[this.machine.key].concat(this.path).join(this.delimiter):this.key),this.initial=e.initial,this.parallel=!!e.parallel,this.states=e.states?u(e.states,function(e,n){var i,a=new t(R({},e,{key:n,parent:r}));return Object.assign(r.idMap,R((i={},i[a.id]=a,i),a.idMap)),a}):{},this.history=!0===e.history?"shallow":e.history||!1,this.on=e.on?this.formatTransitions(e.on):{},this.transient=!!this.on[F],this.strict=!!e.strict,this.onEntry=e.onEntry?[].concat(e.onEntry):[],this.onExit=e.onExit?[].concat(e.onExit):[],this.data=e.data,this.activities=e.activities}return t.prototype.getStateNodes=function(t){var e,n=this;if(!t)return[];var r=t instanceof g?t.value:o(t,this.delimiter);if("string"==typeof r){var i=this.getStateNode(r).initial;return i?this.getStateNodes((e={},e[r]=i,e)):[this.states[r]]}var a=Object.keys(r);return a.map(function(t){return n.getStateNode(t)}).concat(a.reduce(function(t,e){var i=n.getStateNode(e).getStateNodes(r[e]);return t.concat(i)},[]))},t.prototype.handles=function(t){var e=r(t);return-1!==this.events.indexOf(e)},t.prototype._transitionLeafNode=function(t,e,n,r){var i=this.getStateNode(t),a=i._next(e,n,r);if(!a.value){var o=this._next(e,n,r),s=o.value,u=o.entryExitStates,c=o.actions,h=o.paths;return{value:s,entryExitStates:{entry:u?u.entry:new Set,exit:new Set([i].concat(u?Array.from(u.exit):[]))},actions:c,paths:h}}return a},t.prototype._transitionHierarchicalNode=function(t,e,n,r){var i=Object.keys(t),a=this.getStateNode(i[0]),o=a._transition(t[i[0]],e,n,r);if(!o.value){var s=this._next(e,n,r),u=s.value,c=s.entryExitStates,h=s.actions,f=s.paths;return{value:u,entryExitStates:{entry:c?c.entry:new Set,exit:new Set((o.entryExitStates?Array.from(o.entryExitStates.exit):[]).concat([a],c?Array.from(c.exit):[]))},actions:h,paths:f}}return o},t.prototype._transitionOrthogonalNode=function(t,e,n,r){var i=this,a=[],o={};if(Object.keys(t).forEach(function(s){var u=t[s];if(u){var c=i.getStateNode(s)._transition(u,e,n,r);c.value||a.push(s),o[s]=c}}),!Object.keys(o).some(function(t){return void 0!==o[t].value})){var u=this._next(e,n,r),c=u.value,h=u.entryExitStates,p=u.actions,l=u.paths;return{value:c,entryExitStates:{entry:h?h.entry:new Set,exit:new Set(Object.keys(this.states).map(function(t){return i.states[t]}).concat(h?Array.from(h.exit):[]))},actions:p,paths:l}}var y=b(Object.keys(o).map(function(t){return o[t].paths}));if(1===y.length&&!f(s(this.path),s(y[0])))return{value:this.machine.resolve(x(y)),entryExitStates:Object.keys(o).map(function(t){return o[t].entryExitStates}).reduce(function(t,e){var n=e,r=n.entry,i=n.exit;return{entry:new Set(Array.from(t.entry).concat(Array.from(r))),exit:new Set(Array.from(t.exit).concat(Array.from(i)))}},{entry:new Set,exit:new Set}),actions:b(Object.keys(o).map(function(t){return o[t].actions})),paths:y};var v=b(Object.keys(o).map(function(t){var n=o[t],r=n.value||e.value;return S(m(i.path)(r)[t]).map(function(e){return i.path.concat(t,e)})})),d=this.machine.resolve(x(v));return{value:d,entryExitStates:Object.keys(o).reduce(function(t,e){var n=o[e],r=n.value,i=n.entryExitStates;if(!r||!i)return t;var a=i.entry,s=i.exit;return{entry:new Set(Array.from(t.entry).concat(Array.from(a))),exit:new Set(Array.from(t.exit).concat(Array.from(s)))}},{entry:new Set,exit:new Set}),actions:b(Object.keys(o).map(function(t){return o[t].actions})),paths:S(d)}},t.prototype._transition=function(t,e,n,r){return"string"==typeof t?this._transitionLeafNode(t,e,n,r):1===Object.keys(t).length?this._transitionHierarchicalNode(t,e,n,r):this._transitionOrthogonalNode(t,e,n,r)},t.prototype._next=function(t,e,n){var i=this,a=r(e),s=this.on[a],u=this.transient?[{type:E.null}]:[];if(!s||!s.length)return{value:void 0,entryExitStates:void 0,actions:u,paths:[]};for(var c,h=[],p=0,l=s;p<l.length;p++){var y=l[p],v=y,d=v.cond,g=v.in,S=n||{},w=j(e),O=!g||f(o(g,this.delimiter),m(this.path.slice(0,-2))(t.value));if((!d||this._evaluateCond(d,S,w,t.value))&&(!g||O)){h=Array.isArray(y.target)?y.target:[y.target],u.push.apply(u,y.actions?y.actions:[]),c=y;break}}if(0===h.length)return{value:void 0,entryExitStates:void 0,actions:u,paths:[]};var _=b(h.map(function(e){return i.getRelativeStateNodes(e,t.historyValue)})),N=_.map(function(t){return t.path}),k=_.reduce(function(t,e){var n=i._getEntryExitStates(e,!!c.internal),r=n.entry,a=n.exit;return{entry:new Set(Array.from(t.entry).concat(Array.from(r))),exit:new Set(Array.from(t.exit).concat(Array.from(a)))}},{entry:new Set,exit:new Set});return{value:this.machine.resolve(x(b(h.map(function(e){return i.getRelativeStateNodes(e,t.historyValue).map(function(t){return t.path})})))),entryExitStates:k,actions:u,paths:N}},t.prototype._getEntryExitStates=function(t,e){for(var n={entry:[],exit:[]},r=this.path,i=t.path,a=this.machine,o=0;o<Math.min(r.length,i.length);o++){var s=r[o];if(s!==i[o])break;a=a.getStateNode(s)}for(var u=a.path,c=a,h=0,f=r.slice(u.length);h<f.length;h++){var p=f[h];c=c.getStateNode(p),n.exit.unshift(c)}a===this&&(e||(n.exit.push(this),n.entry.push(this))),c=a;for(var l=0,y=i.slice(u.length);l<y.length;l++){var p=y[l];c=c.getStateNode(p),n.entry.push(c)}return{entry:new Set(n.entry),exit:new Set(n.exit)}},t.prototype._evaluateCond=function(t,e,n,r){var i;if("string"==typeof t){if(!this.machine.options.guards[t])throw new Error("String condition '"+t+"' is not defined on machine '"+this.machine.id+"'");i=this.machine.options.guards[t]}else i=t;return i(e,n,r)},t.prototype._getActions=function(t){var e={entry:t.entryExitStates?b(Array.from(t.entryExitStates.entry).map(function(t){return t.onEntry.concat(t.activities?t.activities.map(function(t){return V(t)}):[])})):[],exit:t.entryExitStates?b(Array.from(t.entryExitStates.exit).map(function(t){return t.onExit.concat(t.activities?t.activities.map(function(t){return P(t)}):[])})):[]};return(e.exit||[]).concat(t.actions||[]).concat(e.entry||[])},t.prototype._getActivities=function(t,e){if(!e.entryExitStates)return{};var n=R({},t.activities);return Array.from(e.entryExitStates.exit).forEach(function(t){t.activities&&t.activities.forEach(function(t){n[i(t)]=!1})}),Array.from(e.entryExitStates.entry).forEach(function(t){t.activities&&t.activities.forEach(function(t){n[i(t)]=!0})}),n},t.prototype.transition=function(e,n,i){var a,o="string"==typeof e?this.resolve(s(this.getResolvedPath(e))):e instanceof g?e:this.resolve(e),u=r(n);if(this.strict&&-1===this.events.indexOf(u))throw new Error("Machine '"+this.id+"' does not accept event '"+u+"'");var c=g.from(o),h=o instanceof g?o.historyValue?o.historyValue:this.machine.historyValue(o.value):this.machine.historyValue(o),f=this._transition(c.value,c,n,i);try{this.ensureValidPaths(f.paths)}catch(t){throw new Error("Event '"+u+"' leads to an invalid configuration: "+t.message)}var p=this._getActions(f),l=this._getActivities(c,f),y=p.filter(function(t){return"object"==typeof t&&(t.type===E.raise||t.type===E.null)}),v=p.filter(function(t){return"object"!=typeof t||t.type!==E.raise&&t.type!==E.null}),d=f.value?this.getStateNodes(f.value):[];d.some(function(t){return t.transient})&&y.push({type:E.null});var m={};d.forEach(function(t){m[t.id]=t.data});var S=f.value?new g(f.value,t.updateHistoryValue(h,f.value),c,v,l,m,y):void 0;if(!S)return g.inert(c);delete c.history;for(var x=S;y.length;){var b=x.actions,w=y.shift();x=this.transition(x,w.type===E.null?F:w.event,i),(a=x.actions).unshift.apply(a,b)}return x},t.prototype.ensureValidPaths=function(t){var e=this,n=new Map,r=b(t.map(function(t){return e.getRelativeStateNodes(t)}));t:for(var i=0,a=r;i<a.length;i++)for(var o=a[i],s=o;s.parent;){if(n.has(s.parent)){if(s.parent.parallel)continue t;throw new Error("State node '"+o.id+"' shares parent '"+s.parent.id+"' with state node '"+n.get(s.parent).map(function(t){return t.id})+"'")}n.get(s.parent)?n.get(s.parent).push(o):n.set(s.parent,[o]),s=s.parent}},t.prototype.getStateNode=function(t){if(H(t))return this.machine.getStateNodeById(t);if(!this.states)throw new Error("Unable to retrieve child state '"+t+"' from '"+this.id+"'; no child states exist.");var e=this.states[t];if(!e)throw new Error("Child state '"+t+"' does not exist on '"+this.id+"'");return e},t.prototype.getStateNodeById=function(t){var e=H(t)?t.slice("#".length):t,n=this.machine.idMap[e];if(!n)throw new Error("Substate '#"+e+"' does not exist on '"+this.id+"'");return n},t.prototype.getStateNodeByPath=function(t){for(var e=a(t,this.delimiter),n=this;e.length;){var r=e.shift();n=n.getStateNode(r)}return n},t.prototype.resolve=function(t){var e,n=this;if("string"==typeof t){var r=this.getStateNode(t);return r.initial?(e={},e[t]=r.initialStateValue,e):t}return this.parallel?u(this.initialStateValue,function(e,r){return e?n.getStateNode(r).resolve(t[r]||e):{}}):u(t,function(t,e){return t?n.getStateNode(e).resolve(t):{}})},Object.defineProperty(t.prototype,"resolvedStateValue",{get:function(){var t,e,n=this.key;return this.parallel?(t={},t[n]=c(this.states,function(t){return t.resolvedStateValue[t.key]},function(t){return!t.history}),t):this.initial?(e={},e[n]=this.states[this.initial].resolvedStateValue,e):n},enumerable:!0,configurable:!0}),t.prototype.getResolvedPath=function(t){if(H(t)){var e=this.machine.idMap[t.slice("#".length)];if(!e)throw new Error("Unable to find state node '"+t+"'");return e.path}return a(t,this.delimiter)},Object.defineProperty(t.prototype,"initialStateValue",{get:function(){if(this.__cache.initialState)return this.__cache.initialState;var t=this.parallel?c(this.states,function(t){return t.initialStateValue||{}},function(t){return!t.history}):"string"==typeof this.resolvedStateValue?void 0:this.resolvedStateValue[this.key];return this.__cache.initialState=t,this.__cache.initialState},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"initialState",{get:function(){var t,e=this.initialStateValue;if(!e)throw new Error("Cannot retrieve initial state from simple state '"+this.id+".'");var n={},i=[];this.getStateNodes(e).forEach(function(t){t.onEntry&&i.push.apply(i,t.onEntry),t.activities&&t.activities.forEach(function(t){n[r(t)]=!0,i.push(V(t))})});for(var a=i.filter(function(t){return"object"==typeof t&&(t.type===E.raise||t.type===E.null)}),o=new g(e,void 0,void 0,i,n),s=o;a.length;){var u=s.actions,c=a.shift();s=this.transition(s,c.type===E.null?F:c.event,void 0),(t=s.actions).unshift.apply(t,u)}return s},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"target",{get:function(){var t;if(this.history){var e=this.config;t=e.target&&"string"==typeof e.target&&H(e.target)?s(this.machine.getStateNodeById(e.target).path.slice(this.path.length-1)):e.target}return t},enumerable:!0,configurable:!0}),t.prototype.getStates=function(t){var e=this;if("string"==typeof t)return[this.states[t]];var n=[];return Object.keys(t).forEach(function(r){n.push.apply(n,e.states[r].getStates(t[r]))}),n},t.prototype.getRelativeStateNodes=function(t,e,n){if(void 0===n&&(n=!0),"string"==typeof t&&H(t)){var r=this.getStateNodeById(t);return n?r.history?r.resolveHistory(e):r.initialStateNodes:[r]}var i=a(t,this.delimiter),o=this.parent||this,s=o.getFromRelativePath(i,e);return n?b(s.map(function(t){return t.initialStateNodes})):s},Object.defineProperty(t.prototype,"initialStateNodes",{get:function(){var t=this;if(!this.parallel&&!this.initial)return[this];var e=this.initialState,n=S(e.value);return b(n.map(function(e){return t.getFromRelativePath(e)}))},enumerable:!0,configurable:!0}),t.prototype.getFromRelativePath=function(t,e){var n=this;if(!t.length)return[this];var r=t[0],i=t.slice(1);if(!this.states)throw new Error("Cannot retrieve subPath '"+r+"' from node with no states");if("$history"===r){if(!e)return[this];var a=h(this.path,"states")(e).current;return"string"==typeof a?this.states[a].getFromRelativePath(i,e):b(Object.keys(a).map(function(t){return n.states[t].getFromRelativePath(i,e)}))}var o=this.getStateNode(r);if(o.history)return o.resolveHistory(e);if(!this.states[r])throw new Error("Child state '"+r+"' does not exist on '"+this.id+"'");return this.states[r].getFromRelativePath(i,e)},t.updateHistoryValue=function(t,e){function n(t,e){return u(t.states,function(t,r){if(t){var i=("string"==typeof e?void 0:e[r])||(t?t.current:void 0);if(i)return{current:i,states:n(t,i)}}})}return{current:e,states:n(t,e)}},t.prototype.historyValue=function(t){if(Object.keys(this.states).length)return{current:t||this.initialStateValue,states:c(this.states,function(e,n){if(!t)return e.historyValue();var r="string"==typeof t?void 0:t[n];return e.historyValue(r||e.initialStateValue)},function(t){return!t.history})}},t.prototype.resolveHistory=function(t){var e=this;if(!this.history)return[this];var n=this.parent;if(!t)return this.target?b(S(this.target).map(function(t){return n.getFromRelativePath(t)})):this.parent.initialStateNodes;var r=h(n.path,"states")(t).current;return"string"==typeof r?[n.getStateNode(r)]:b(S(r).map(function(t){return"deep"===e.history?n.getFromRelativePath(t):[n.states[t[0]]]}))},Object.defineProperty(t.prototype,"events",{get:function(){if(this.__cache.events)return this.__cache.events;var t=this.states,e=new Set(Object.keys(this.on));return t&&Object.keys(t).forEach(function(n){var r=t[n];if(r.states)for(var i=0,a=r.events;i<a.length;i++){var o=a[i];e.add(""+o)}}),this.__cache.events=Array.from(e)},enumerable:!0,configurable:!0}),t.prototype.formatTransition=function(t,e){var n=this,r=!!e&&e.internal,i=t.map(function(t){var e="string"==typeof t&&t[0]===n.delimiter;return r=r||e,e&&!n.parent?t.slice(1):e?n.key+t:t});return R({},e,{target:i,internal:r})},t.prototype.formatTransitions=function(t){var e=this;return u(t,function(t){return void 0===t?[]:Array.isArray(t)?t.map(function(t){return e.formatTransition([].concat(t.target),t)}):"string"==typeof t?[e.formatTransition([t])]:Object.keys(t).map(function(n){return e.formatTransition([n],t[n])})})},t}();n.d(e,"Machine",function(){return l}),n.d(e,"StateNode",function(){return C}),n.d(e,"State",function(){return g}),n.d(e,"matchesState",function(){return f}),n.d(e,"mapState",function(){return p}),n.d(e,"actions",function(){return y})}])});
@@ -1 +1 @@
1
- !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.xstateUtils=e():t.xstateUtils=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,e),i.l=!0,i.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,n){"use strict";function r(t){try{return"string"==typeof t||"number"==typeof t?""+t:"function"==typeof t?t.name:t.type}catch(t){throw new Error("Events must be strings or objects with a string event.type property.")}}function i(t,e){try{return Array.isArray(t)?t:t.toString().split(e)}catch(e){throw new Error("'"+t+"' is not a valid state path.")}}function a(t,e){return t instanceof g?t.value:"object"!=typeof t||t instanceof g?o(i(t,e)):t}function o(t){if(1===t.length)return t[0];for(var e={},n=e,r=0;r<t.length-1;r++)r===t.length-2?n[t[r]]=t[r+1]:(n[t[r]]={},n=n[t[r]]);return e}function s(t){var e=t.states;return Object.keys(e).reduce(function(t,n){var r=e[n],i=s(e[n]);return t.push.apply(t,[r].concat(i)),t},[])}function u(t,e){return t.on[e].map(function(n){return{source:t,target:t.parent.getState(n.target),event:e,actions:n.actions?n.actions.map(r):[],cond:n.cond}})}function c(t){var e=[];return t.states&&Object.keys(t.states).forEach(function(n){e.push.apply(e,c(t.states[n]))}),Object.keys(t.on).forEach(function(n){e.push.apply(e,u(t,n))}),e}function f(t){function e(i){var a=JSON.stringify(i);if(!n[a]){n[a]={};for(var o=0,s=r;o<s.length;o++){var u=s[o],c=t.transition(i,u);n[a][u]={state:c.value},e(c.value)}}}var n={},r=t.events;return e(t.initialState.value),n}function p(t){function e(r){var s=JSON.stringify(r);o.add(s);for(var u=n[s],c=0,f=Object.keys(u);c<f.length;c++){var p=f[c],v=u[p].state;if(v){var l=JSON.stringify(a(v,t.delimiter));(!i[l]||i[l].length>i[s].length+1)&&(i[l]=(i[s]||[]).concat([{state:r,event:p}]))}}for(var h=0,y=Object.keys(u);h<y.length;h++){var g=y[h],v=u[g].state;if(v){var l=JSON.stringify(v);o.has(l)||e(v)}}return i}if(!t.states)return d;var n=f(t),r=JSON.stringify(t.initialState.value),i=(s={},s[r]=[],s),o=new Set;return e(t.initialState.value),i;var s}function v(t){var e=p(t);return Object.keys(e).map(function(t){return{state:JSON.parse(t),path:e[t]}})}function l(t){function e(t,o){if(r.add(t),t===o)a[o]=a[o]||[],a[o].push(i.slice());else for(var s=0,u=Object.keys(n[t]);s<u.length;s++){var c=u[s],f=n[t][c].state;if(f){var p=JSON.stringify(f);r.has(p)||(i.push({state:JSON.parse(t),event:c}),e(p,o))}}i.pop(),r.delete(t)}if(!t.states)return d;var n=f(t),r=new Set,i=[],a={},o=JSON.stringify(t.initialState.value);return Object.keys(n).forEach(function(t){e(o,t)}),a}function h(t){var e=l(t);return Object.keys(e).map(function(t){return{state:JSON.parse(t),paths:e[t]}})}Object.defineProperty(e,"__esModule",{value:!0});var y={},g=function(){function t(t,e,n,r,i,a){void 0===n&&(n=[]),void 0===r&&(r=y),void 0===i&&(i={}),void 0===a&&(a=[]),this.value=t,this.history=e,this.actions=n,this.activities=r,this.data=i,this.events=a}return t.from=function(e){return e instanceof t?e:new t(e)},t.inert=function(e){return e instanceof t?e.actions.length?new t(e.value,e.history,[]):e:t.from(e)},t.prototype.toString=function(){if("string"==typeof this.value)return this.value;for(var t=[],e=this.value;;){if("string"==typeof e){t.push(e);break}var n=Object.keys(e),r=n[0];if(n.slice(1).length)return;t.push(r),e=e[r]}return t.join(".")},t}();e.getNodes=s,e.getEdges=c,e.getAdjacencyMap=f,e.getShortestPaths=p,e.getShortestPathsAsArray=v,e.getSimplePaths=l,e.getSimplePathsAsArray=h;var d={}}])});
1
+ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.xstateUtils=e():t.xstateUtils=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,e),i.l=!0,i.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,n){"use strict";function r(t){try{return"string"==typeof t||"number"==typeof t?""+t:"function"==typeof t?t.name:t.type}catch(t){throw new Error("Events must be strings or objects with a string event.type property.")}}function i(t,e){try{return Array.isArray(t)?t:t.toString().split(e)}catch(e){throw new Error("'"+t+"' is not a valid state path.")}}function a(t,e){return t instanceof g?t.value:"object"!=typeof t||t instanceof g?o(i(t,e)):t}function o(t){if(1===t.length)return t[0];for(var e={},n=e,r=0;r<t.length-1;r++)r===t.length-2?n[t[r]]=t[r+1]:(n[t[r]]={},n=n[t[r]]);return e}function s(t){var e=t.states;return Object.keys(e).reduce(function(t,n){var r=e[n],i=s(e[n]);return t.push.apply(t,[r].concat(i)),t},[])}function u(t,e){var n=t.on[e];return d(n.map(function(n){return[].concat(n.target).map(function(i){var a=t.getRelativeStateNodes(i,void 0,!1)[0];return{source:t,target:a,event:e,actions:n.actions?n.actions.map(r):[],cond:n.cond}})}))}function c(t,e){var n=(e||{}).deep,r=void 0===n||n,i=[];return t.states&&r&&Object.keys(t.states).forEach(function(e){i.push.apply(i,c(t.states[e]))}),Object.keys(t.on).forEach(function(e){i.push.apply(i,u(t,e))}),i}function f(t,e){function n(a){var o=JSON.stringify(a);if(!r[o]){r[o]={};for(var s=0,u=i;s<u.length;s++){var c=u[s],f=t.transition(a,c,e);r[o][c]={state:f.value},n(f.value)}}}var r={},i=t.events;return n(t.initialState.value),r}function v(t,e){function n(e){var r=JSON.stringify(e);u.add(r);for(var o=i[r],c=0,f=Object.keys(o);c<f.length;c++){var v=f[c],p=o[v].state;if(p){var l=JSON.stringify(a(p,t.delimiter));(!s[l]||s[l].length>s[r].length+1)&&(s[l]=(s[r]||[]).concat([{state:e,event:v}]))}}for(var h=0,y=Object.keys(o);h<y.length;h++){var g=y[h],p=o[g].state;if(p){var l=JSON.stringify(p);u.has(l)||n(p)}}return s}var r;if(!t.states)return O;var i=f(t,e),o=JSON.stringify(t.initialState.value),s=(r={},r[o]=[],r),u=new Set;return n(t.initialState.value),s}function p(t,e){var n=v(t,e);return Object.keys(n).map(function(t){return{state:JSON.parse(t),path:n[t]}})}function l(t,e){function n(t,e){if(i.add(t),t===e)o[e]=o[e]||[],o[e].push(a.slice());else for(var s=0,u=Object.keys(r[t]);s<u.length;s++){var c=u[s],f=r[t][c].state;if(f){var v=JSON.stringify(f);i.has(v)||(a.push({state:JSON.parse(t),event:c}),n(v,e))}}a.pop(),i.delete(t)}if(!t.states)return O;var r=f(t,e),i=new Set,a=[],o={},s=JSON.stringify(t.initialState.value);return Object.keys(r).forEach(function(t){n(s,t)}),o}function h(t,e){var n=l(t,e);return Object.keys(n).map(function(t){return{state:JSON.parse(t),paths:n[t]}})}Object.defineProperty(e,"__esModule",{value:!0});var y={},g=function(){function t(t,e,n,r,i,a,o){void 0===r&&(r=[]),void 0===i&&(i=y),void 0===a&&(a={}),void 0===o&&(o=[]),this.value=t,this.historyValue=e,this.history=n,this.actions=r,this.activities=i,this.data=a,this.events=o}return t.from=function(e){return e instanceof t?e:new t(e)},t.inert=function(e){return e instanceof t?e.actions.length?new t(e.value,e.historyValue,e.history,[],e.activities):e:t.from(e)},t.prototype.toString=function(){if("string"==typeof this.value)return this.value;for(var t=[],e=this.value;;){if("string"==typeof e){t.push(e);break}var n=Object.keys(e),r=n[0];if(n.slice(1).length)return;t.push(r),e=e[r]}return t.join(".")},t}(),d=function(t){return t.reduce(function(t,e){return t.concat(e)},[])};e.getNodes=s,e.getEventEdges=u,e.getEdges=c,e.getAdjacencyMap=f,e.getShortestPaths=v,e.getShortestPathsAsArray=p,e.getSimplePaths=l,e.getSimplePathsAsArray=h;var O={}}])});
package/es/Machine.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { StandardMachine, ParallelMachine, MachineConfig, ParallelMachineConfig } from './types';
2
- export declare function Machine(config: MachineConfig | ParallelMachineConfig): StandardMachine | ParallelMachine;
1
+ import { StandardMachine, ParallelMachine, MachineConfig, ParallelMachineConfig, MachineOptions } from './types';
2
+ export declare function Machine(config: MachineConfig | ParallelMachineConfig, options?: MachineOptions): StandardMachine | ParallelMachine;
package/es/Machine.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import { StateNode } from './StateNode';
2
- export function Machine(config) {
3
- return new StateNode(config);
2
+ export function Machine(config, options) {
3
+ return new StateNode(config, options);
4
4
  }
package/es/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
- history: State | undefined;
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
- * Internal event queue
17
- */
18
- events?: EventObject[]);
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/es/State.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { STATE_DELIMITER, EMPTY_ACTIVITY_MAP } from './constants';
2
2
  var State = /** @class */ (function () {
3
- function State(value, history, actions, activities, data,
3
+ function State(value, historyValue, history, actions, activities, data,
4
4
  /**
5
5
  * Internal event queue
6
6
  */
@@ -10,6 +10,7 @@ var State = /** @class */ (function () {
10
10
  if (data === void 0) { data = {}; }
11
11
  if (events === void 0) { events = []; }
12
12
  this.value = value;
13
+ this.historyValue = historyValue;
13
14
  this.history = history;
14
15
  this.actions = actions;
15
16
  this.activities = activities;
@@ -27,7 +28,7 @@ var State = /** @class */ (function () {
27
28
  if (!stateValue.actions.length) {
28
29
  return stateValue;
29
30
  }
30
- return new State(stateValue.value, stateValue.history, []);
31
+ return new State(stateValue.value, stateValue.historyValue, stateValue.history, [], stateValue.activities);
31
32
  }
32
33
  return State.from(stateValue);
33
34
  };
package/es/StateNode.d.ts CHANGED
@@ -1,16 +1,19 @@
1
- import { Event, StateValue, Action, StandardMachine, ParallelMachine, SimpleOrCompoundStateNodeConfig, MachineConfig, ParallelMachineConfig, EventType, StandardMachineConfig, StateNodeConfig, Activity, ConditionalTransitionConfig } from './types';
1
+ import { Event, StateValue, Action, StandardMachine, ParallelMachine, SimpleOrCompoundStateNodeConfig, ParallelMachineConfig, EventType, StandardMachineConfig, Activity, ConditionalTransitionConfig, StateTransition, MachineOptions, HistoryValue } from './types';
2
2
  import { State } from './State';
3
- declare class StateNode implements StateNodeConfig {
3
+ declare class StateNode {
4
4
  config: SimpleOrCompoundStateNodeConfig | StandardMachineConfig | ParallelMachineConfig;
5
+ options: MachineOptions;
5
6
  key: string;
6
7
  id: string;
7
8
  path: string[];
8
9
  initial?: string;
9
10
  parallel?: boolean;
11
+ transient: boolean;
10
12
  states: Record<string, StateNode>;
13
+ history: false | 'shallow' | 'deep';
11
14
  on: Record<string, ConditionalTransitionConfig>;
12
- onEntry?: Action[];
13
- onExit?: Action[];
15
+ onEntry: Action[];
16
+ onExit: Action[];
14
17
  activities?: Activity[];
15
18
  strict: boolean;
16
19
  parent?: StateNode;
@@ -19,24 +22,58 @@ declare class StateNode implements StateNodeConfig {
19
22
  delimiter: string;
20
23
  private __cache;
21
24
  private idMap;
22
- constructor(config: SimpleOrCompoundStateNodeConfig | StandardMachineConfig | ParallelMachineConfig);
25
+ constructor(config: SimpleOrCompoundStateNodeConfig | StandardMachineConfig | ParallelMachineConfig, options?: MachineOptions);
23
26
  getStateNodes(state: StateValue | State): StateNode[];
24
27
  handles(event: Event): boolean;
28
+ private _transitionLeafNode;
29
+ private _transitionHierarchicalNode;
30
+ private _transitionOrthogonalNode;
31
+ _transition(stateValue: StateValue, state: State, event: Event, extendedState?: any): StateTransition;
32
+ private _next;
33
+ private _getEntryExitStates;
34
+ private _evaluateCond;
35
+ private _getActions;
36
+ private _getActivities;
25
37
  transition(state: StateValue | State, event: Event, extendedState?: any): State;
26
- private stateTransitionToState(stateTransition, prevState);
38
+ private ensureValidPaths;
27
39
  getStateNode(stateKey: string): StateNode;
28
40
  getStateNodeById(stateId: string): StateNode;
29
- private resolve(stateValue);
30
- private transitionStateValue(state, event, fullState, extendedState?);
31
- private next(event, fullState, history?, extendedState?);
41
+ getStateNodeByPath(statePath: string | string[]): StateNode;
42
+ private resolve;
32
43
  private readonly resolvedStateValue;
33
- private getResolvedPath(stateIdentifier);
44
+ private getResolvedPath;
34
45
  private readonly initialStateValue;
35
46
  readonly initialState: State;
47
+ readonly target: StateValue | undefined;
36
48
  getStates(stateValue: StateValue): StateNode[];
37
- getState(relativeStateId: string | string[]): StateNode | undefined;
49
+ /**
50
+ * Returns the leaf nodes from a state path relative to this state node.
51
+ *
52
+ * @param relativeStateId The relative state path to retrieve the state nodes
53
+ * @param history The previous state to retrieve history
54
+ * @param resolve Whether state nodes should resolve to initial child state nodes
55
+ */
56
+ getRelativeStateNodes(relativeStateId: string | string[], historyValue?: HistoryValue, resolve?: boolean): StateNode[];
57
+ readonly initialStateNodes: StateNode[];
58
+ /**
59
+ * Retrieves state nodes from a relative path to this state node.
60
+ *
61
+ * @param relativePath The relative path from this state node
62
+ * @param historyValue
63
+ */
64
+ getFromRelativePath(relativePath: string[], historyValue?: HistoryValue): StateNode[];
65
+ static updateHistoryValue(hist: HistoryValue, stateValue: StateValue): HistoryValue;
66
+ historyValue(relativeStateValue?: StateValue | undefined): HistoryValue | undefined;
67
+ /**
68
+ * Resolves to the historical value(s) of the parent state node,
69
+ * represented by state nodes.
70
+ *
71
+ * @param historyValue
72
+ */
73
+ private resolveHistory;
38
74
  readonly events: EventType[];
39
- private formatTransitions(onConfig);
75
+ private formatTransition;
76
+ private formatTransitions;
40
77
  }
41
- export declare function Machine(config: MachineConfig | ParallelMachineConfig): StandardMachine | ParallelMachine;
78
+ export declare function Machine<T extends StandardMachineConfig | ParallelMachineConfig>(config: T, options?: MachineOptions): T extends ParallelMachineConfig ? ParallelMachine : T extends StandardMachineConfig ? StandardMachine : never;
42
79
  export { StateNode };