Comparing version 3.0.3 to 3.0.4
@@ -1,1 +0,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=4)}([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 Array.isArray(t)?t:t.toString().split(".")}catch(e){throw new Error("'"+t+"' is not a valid state path.")}}function a(t){if(t instanceof s.default)return t.value;if("object"==typeof t&&!(t instanceof s.default))return t;var e=i(t);if(1===e.length)return e[0];for(var n={},r=n,a=0;a<e.length-1;a++)a===e.length-2?r[e[a]]=e[a+1]:(r[e[a]]={},r=r[e[a]]);return n}function o(t,e){var n={};return Object.keys(t).forEach(function(r){n[r]=e(t[r],r,t)}),n}Object.defineProperty(e,"__esModule",{value:!0});var s=n(2);e.getEventType=r,e.toStatePath=i,e.toTrie=a,e.mapValues=o},function(t,e,n){"use strict";function r(t){return new f(t)}var i=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};Object.defineProperty(e,"__esModule",{value:!0});var a=n(0),o=n(3);e.matchesState=o.default;var s=n(6);e.mapState=s.default;var u=n(2);e.State=u.default;var c=".",f=function(){function t(e){var n=this;this.config=e,this.__cache={events:void 0,relativeValue:new Map,initialState:void 0},this.key=e.key||"(machine)",this.parent=e.parent,this.machine=this.parent?this.parent.machine:this,this.id=this.parent?this.parent.id+c+this.key:this.key,this.relativeId=this.parent&&this.parent.parent?this.parent.relativeId+c+this.key:this.key,this.initial=e.initial,this.parallel=!!e.parallel,this.states=e.states?a.mapValues(e.states,function(e,r){return new t(i({},e,{key:r,parent:n}))}):{},this.on=e.on,this.onEntry=e.onEntry?[].concat(e.onEntry):void 0,this.onExit=e.onExit?[].concat(e.onExit):void 0,this.strict=!!e.strict}return t.prototype.getStateNodes=function(t){var e=this,n=t instanceof u.default?t.value:a.toTrie(t);if("string"==typeof n){var r=this.states[n].initial;return r?this.getStateNodes((o={},o[n]=r,o)):[this.states[n]]}var i=Object.keys(n);return i.map(function(t){return e.states[t]}).concat(i.map(function(t){return e.states[t].getStateNodes(n[t])}).reduce(function(t,e){return t.concat(e)}));var o},t.prototype.handles=function(t){var e=a.getEventType(t);return this.on&&this.on[e]},t.prototype.transition=function(t,e,n){if(this.strict){var r=a.getEventType(e);if(-1===this.events.indexOf(r))throw new Error("Machine '"+this.id+"' does not accept event '"+r+"'")}var i=this.transitionStateValue(t,e,n),o=i[0],s=i[1];return o?new u.default(o,u.default.from(t),s.onExit.concat(s.actions).concat(s.onEntry)):u.default.inert(t)},t.prototype.transitionStateValue=function(t,e,n){var r=this,o=t instanceof u.default?t.history:void 0,s=a.toTrie(t);if("string"==typeof s){if(!this.states[s])throw new Error("State '"+s+"' does not exist on machine '"+this.id+"'");var c=this.states[s];if(!c.states||!c.initial)return c.next(e,o?o.value:void 0,n);var f=c.initialState.value;g={},g[s]=f,s=g}var h=a.mapValues(s,function(t,i){var a=o?o.value[i]:void 0,s=new u.default(t,a?u.default.from(a):void 0);return r.states[i].transitionStateValue(s,e,n)});if(Array.prototype.every.call(Object.keys(h),function(t){return void 0===h[t][0]})){if(this.parallel)return[void 0,{onEntry:[],onExit:[],actions:[]}];var l=Object.keys(h)[0],p=this.states[l].next(e,o?o.value:void 0),v=p[0],y=p[1],d=h[l][1];return[v,{onEntry:d.onEntry.concat(y.onEntry),actions:d.actions.concat(y.actions),onExit:d.onExit.concat(y.onExit)}]}this.parallel&&(h=i({},a.mapValues(this.initialState.value,function(t){return[t,{onEntry:[],onExit:[],actions:[]}]}),h));var E={onEntry:[],actions:[],onExit:[]};return[a.mapValues(h,function(t,e){var n=t[0],r=t[1];return r.onEntry&&(i=E.onEntry).push.apply(i,r.onEntry),r.actions&&(a=E.actions).push.apply(a,r.actions),r.onExit&&(o=E.onExit).push.apply(o,r.onExit),n||s[e];var i,a,o}),E];var g},t.prototype.next=function(t,e,n){var r=a.getEventType(t),i={onEntry:[],onExit:[],actions:[]};if(this.onExit&&(i.onExit=this.onExit),!this.on||!this.on[r])return[void 0,i];var o,s=this.on[r];if("string"==typeof s)o=s;else for(var u=0,c=Object.keys(s);u<c.length;u++){var f=c[u],h=s[f],l=h.cond,p=h.actions,v="string"==typeof t||"number"==typeof t?{type:t}:t;if(!l||l(n,v)){o=f,p&&(i.actions=i.actions.concat(p));break}}if(!o)return[void 0,i];var y=a.toStatePath(o),d=this.parent,E=e,g=this.key;if(y.forEach(function(e){if(!d||!d.states)throw new Error("Unable to read '"+e+"'");if("$history"===e)if(E)e="object"==typeof E?Object.keys(E)[0]:E;else{if(!d.initial)throw new Error("Cannot read '$history' from state '"+d.id+"': missing 'initial'");e=d.initial}if(void 0===(d=d.states[e]))throw new Error("Event '"+t+"' on state '"+g+"' leads to undefined state '"+y+"'.");d.onEntry&&(i.onEntry=i.onEntry.concat(d.onEntry)),g=e,E&&(E=E[e])}),!d)throw new Error("no state");for(;d.initial;){if(!d||!d.states)throw new Error("Invalid initial state");d=d.states[d.initial],d.onEntry&&(i.onEntry=i.onEntry.concat(d.onEntry))}return[d.getRelativeValue(this.parent),i]},Object.defineProperty(t.prototype,"initialStateValue",{get:function(){return this.__cache.initialState=this.__cache.initialState||(this.parallel?a.mapValues(this.states,function(t){return t.initialStateValue}):this.initial),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=this.getStateNodes(t).reduce(function(t,e){return e.onEntry?t.concat(e.onEntry):t},[]);return new u.default(t,void 0,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(r){n.push.apply(n,e.states[r].getStates(t[r]))}),n},t.prototype.getState=function(t){var e=a.toStatePath(t);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(this.on?Object.keys(this.on):void 0);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.getRelativeValue=function(t){var e=t?this.__cache.relativeValue.get(t):void 0;if(e)return e;for(var n=this.initialStateValue,r=n?(s={},s[this.key]=n,s):this.key,a=this.parent;a&&a!==t;){var o=a.initialStateValue;u={},u[a.key]="object"==typeof o&&"object"==typeof r?i({},o,r):r,r=u,a=a.parent}return this.__cache.relativeValue.set(t,r),r;var s,u},t}();e.StateNode=f,e.Machine=r},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var r=n(5),i=n(0),a=function(){function t(t,e,n){void 0===n&&(n=[]),this.value=t,this.history=e,this.actions=n}return t.from=function(e){return e instanceof t?e:new t(i.toTrie(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(r.STATE_DELIMITER)},t}();e.default=a},function(t,e,n){"use strict";function r(t,e){var n=i.toTrie(t),a=i.toTrie(e);return"string"==typeof a?"string"==typeof n?a===n:a in n:"string"==typeof n?n in a:Object.keys(n).every(function(t){return t in a&&r(n[t],a[t])})}Object.defineProperty(e,"__esModule",{value:!0});var i=n(0);e.default=r},function(t,e,n){n(1),t.exports=n(1)},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.STATE_DELIMITER="."},function(t,e,n){"use strict";function r(t,e){var n;return Object.keys(t).forEach(function(t){i.default(t,e)&&(!n||e.length>n.length)&&(n=t)}),t[n]}Object.defineProperty(e,"__esModule",{value:!0});var i=n(3);e.default=r}])}); | ||
!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=4)}([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){try{return Array.isArray(t)?t:t.toString().split(".")}catch(e){throw new Error("'"+t+"' is not a valid state path.")}}function a(t){if(t instanceof s.State)return t.value;if("object"==typeof t&&!(t instanceof s.State))return t;var e=r(t);if(1===e.length)return e[0];for(var n={},i=n,a=0;a<e.length-1;a++)a===e.length-2?i[e[a]]=e[a+1]:(i[e[a]]={},i=i[e[a]]);return n}function o(t,e){var n={};return Object.keys(t).forEach(function(i){n[i]=e(t[i],i,t)}),n}Object.defineProperty(e,"__esModule",{value:!0});var s=n(2);e.getEventType=i,e.toStatePath=r,e.toTrie=a,e.mapValues=o},function(t,e,n){"use strict";function i(t){return new f(t)}var r=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};Object.defineProperty(e,"__esModule",{value:!0});var a=n(0),o=n(3);e.matchesState=o.default;var s=n(6);e.mapState=s.default;var c=n(2);e.State=c.State;var u=".",f=function(){function t(e){var n=this;this.config=e,this.__cache={events:void 0,relativeValue:new Map,initialState:void 0},this.key=e.key||"(machine)",this.parent=e.parent,this.machine=this.parent?this.parent.machine:this,this.id=this.parent?this.parent.id+u+this.key:this.key,this.relativeId=this.parent&&this.parent.parent?this.parent.relativeId+u+this.key:this.key,this.initial=e.initial,this.parallel=!!e.parallel,this.states=e.states?a.mapValues(e.states,function(e,i){return new t(r({},e,{key:i,parent:n}))}):{},this.on=e.on,this.strict=!!e.strict,this.onEntry=e.onEntry?[].concat(e.onEntry):void 0,this.onExit=e.onExit?[].concat(e.onExit):void 0}return t.prototype.getStateNodes=function(t){var e=this,n=t instanceof c.State?t.value:a.toTrie(t);if("string"==typeof n){var i=this.states[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.states[t]}).concat(r.map(function(t){return e.states[t].getStateNodes(n[t])}).reduce(function(t,e){return t.concat(e)}));var o},t.prototype.handles=function(t){var e=a.getEventType(t);return!!this.on&&e in this.on},t.prototype.transition=function(t,e,n){if(this.strict){var i=a.getEventType(e);if(-1===this.events.indexOf(i))throw new Error("Machine '"+this.id+"' does not accept event '"+i+"'")}var r=this.transitionStateValue(t,e,n),o=r[0],s=r[1];return o?new c.State(o,c.State.from(t),s.onExit.concat(s.actions).concat(s.onEntry)):c.State.inert(t)},t.prototype.transitionStateValue=function(t,e,n){var i=this,o=t instanceof c.State?t.history:void 0,s=a.toTrie(t);if("string"==typeof s){if(!this.states[s])throw new Error("State '"+s+"' does not exist on machine '"+this.id+"'");var u=this.states[s];if(!u.states||!u.initial)return u.next(e,o?o.value:void 0,n);var f=u.initialState.value;g={},g[s]=f,s=g}var h=a.mapValues(s,function(t,r){var a=o?o.value[r]:void 0,s=new c.State(t,a?c.State.from(a):void 0);return i.states[r].transitionStateValue(s,e,n)});if(Array.prototype.every.call(Object.keys(h),function(t){return void 0===h[t][0]})){if(this.parallel)return[void 0,{onEntry:[],onExit:[],actions:[]}];var l=Object.keys(h)[0],p=this.states[l].next(e,o?o.value:void 0),v=p[0],y=p[1],d=h[l][1];return[v,{onEntry:d.onEntry.concat(y.onEntry),actions:d.actions.concat(y.actions),onExit:d.onExit.concat(y.onExit)}]}this.parallel&&(h=r({},a.mapValues(this.initialState.value,function(t){return[t,{onEntry:[],onExit:[],actions:[]}]}),h));var E={onEntry:[],actions:[],onExit:[]};return[a.mapValues(h,function(t,e){var n=t[0],i=t[1];return i.onEntry&&(r=E.onEntry).push.apply(r,i.onEntry),i.actions&&(a=E.actions).push.apply(a,i.actions),i.onExit&&(o=E.onExit).push.apply(o,i.onExit),n||s[e];var r,a,o}),E];var g},t.prototype.next=function(t,e,n){var i=a.getEventType(t),r={onEntry:[],onExit:[],actions:[]};if(this.onExit&&(r.onExit=this.onExit),!this.on||!this.on[i])return[void 0,r];var o,s=this.on[i];if("string"==typeof s)o=s;else for(var c=0,u=Object.keys(s);c<u.length;c++){var f=u[c],h=s[f],l=h.cond,p=h.actions,v="string"==typeof t||"number"==typeof t?{type:t}:t;if(!l||l(n,v)){o=f,p&&(r.actions=r.actions.concat(p));break}}if(!o)return[void 0,r];var y=a.toStatePath(o),d=this.parent,E=e,g=this.key;if(y.forEach(function(e){if(!d||!d.states)throw new Error("Unable to read '"+e+"'");if("$history"===e)if(E)e="object"==typeof E?Object.keys(E)[0]:E;else{if(!d.initial)throw new Error("Cannot read '$history' from state '"+d.id+"': missing 'initial'");e=d.initial}if(void 0===(d=d.states[e]))throw new Error("Event '"+t+"' on state '"+g+"' leads to undefined state '"+y+"'.");d.onEntry&&(r.onEntry=r.onEntry.concat(d.onEntry)),g=e,E&&(E=E[e])}),!d)throw new Error("no state");for(;d.initial;){if(!d||!d.states)throw new Error("Invalid initial state");d=d.states[d.initial],d.onEntry&&(r.onEntry=r.onEntry.concat(d.onEntry))}return[d.getRelativeValue(this.parent),r]},Object.defineProperty(t.prototype,"initialStateValue",{get:function(){return this.__cache.initialState=this.__cache.initialState||(this.parallel?a.mapValues(this.states,function(t){return t.initialStateValue}):this.initial),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=this.getStateNodes(t).reduce(function(t,e){return e.onEntry?t.concat(e.onEntry):t},[]);return new c.State(t,void 0,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){var e=a.toStatePath(t);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(this.on?Object.keys(this.on):void 0);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.getRelativeValue=function(t){var e=t?this.__cache.relativeValue.get(t):void 0;if(e)return e;for(var n=this.initialStateValue,i=n?(s={},s[this.key]=n,s):this.key,a=this.parent;a&&a!==t;){var o=a.initialStateValue;c={},c[a.key]="object"==typeof o&&"object"==typeof i?r({},o,i):i,i=c,a=a.parent}return this.__cache.relativeValue.set(t,i),i;var s,c},t}();e.StateNode=f,e.Machine=i},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var i=n(5),r=n(0),a=function(){function t(t,e,n){void 0===n&&(n=[]),this.value=t,this.history=e,this.actions=n}return t.from=function(e){return e instanceof t?e:new t(r.toTrie(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(i.STATE_DELIMITER)},t}();e.State=a},function(t,e,n){"use strict";function i(t,e){var n=r.toTrie(t),a=r.toTrie(e);return"string"==typeof a?"string"==typeof n?a===n:a in n:"string"==typeof n?n in a:Object.keys(n).every(function(t){return t in a&&i(n[t],a[t])})}Object.defineProperty(e,"__esModule",{value:!0});var r=n(0);e.default=i},function(t,e,n){n(1),t.exports=n(1)},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.STATE_DELIMITER="."},function(t,e,n){"use strict";function i(t,e){var n;return Object.keys(t).forEach(function(t){r.default(t,e)&&(!n||e.length>n.length)&&(n=t)}),t[n]}Object.defineProperty(e,"__esModule",{value:!0});var r=n(3);e.default=i}])}); |
@@ -1,7 +0,7 @@ | ||
import { Event, StateValue, Transition, Action, StandardMachine, ParallelMachine, StateOrMachineConfig, MachineConfig, ParallelMachineConfig, EventType } from './types'; | ||
import { Event, StateValue, Transition, Action, StandardMachine, ParallelMachine, SimpleOrCompoundStateNodeConfig, MachineConfig, ParallelMachineConfig, EventType, StandardMachineConfig } from './types'; | ||
import matchesState from './matchesState'; | ||
import mapState from './mapState'; | ||
import State from './State'; | ||
declare class StateNode<TStateKey extends string = string, TEventType extends string = string> { | ||
config: StateOrMachineConfig<TStateKey, TEventType>; | ||
import { State } from './State'; | ||
declare class StateNode { | ||
config: SimpleOrCompoundStateNodeConfig | StandardMachineConfig | ParallelMachineConfig; | ||
key: string; | ||
@@ -12,4 +12,4 @@ id: string; | ||
parallel?: boolean; | ||
states: Record<TStateKey, StateNode>; | ||
on?: Record<TEventType, Transition<TStateKey> | undefined>; | ||
states: Record<string, StateNode>; | ||
on?: Record<string, Transition | undefined>; | ||
onEntry?: Action[]; | ||
@@ -21,3 +21,3 @@ onExit?: Action[]; | ||
private __cache; | ||
constructor(config: StateOrMachineConfig<TStateKey, TEventType>); | ||
constructor(config: SimpleOrCompoundStateNodeConfig | StandardMachineConfig | ParallelMachineConfig); | ||
getStateNodes(state: StateValue | State): StateNode[]; | ||
@@ -24,0 +24,0 @@ handles(event: Event): boolean; |
@@ -17,3 +17,3 @@ "use strict"; | ||
var State_1 = require("./State"); | ||
exports.State = State_1.default; | ||
exports.State = State_1.State; | ||
var STATE_DELIMITER = '.'; | ||
@@ -48,2 +48,3 @@ var HISTORY_KEY = '$history'; | ||
this.on = config.on; | ||
this.strict = !!config.strict; | ||
this.onEntry = config.onEntry | ||
@@ -55,7 +56,6 @@ ? [].concat(config.onEntry) | ||
: undefined; | ||
this.strict = !!config.strict; | ||
} | ||
StateNode.prototype.getStateNodes = function (state) { | ||
var _this = this; | ||
var stateValue = state instanceof State_1.default ? state.value : utils_1.toTrie(state); | ||
var stateValue = state instanceof State_1.State ? state.value : utils_1.toTrie(state); | ||
if (typeof stateValue === 'string') { | ||
@@ -78,3 +78,6 @@ var initialStateValue = this.states[stateValue].initial; | ||
var eventType = utils_1.getEventType(event); | ||
return this.on && this.on[eventType]; | ||
if (this.on) { | ||
return eventType in this.on; | ||
} | ||
return false; | ||
}; | ||
@@ -91,9 +94,9 @@ StateNode.prototype.transition = function (state, event, extendedState) { | ||
if (!nextStateValue) { | ||
return State_1.default.inert(state); | ||
return State_1.State.inert(state); | ||
} | ||
return new State_1.default( | ||
return new State_1.State( | ||
// next state value | ||
nextStateValue, | ||
// history | ||
State_1.default.from(state), | ||
State_1.State.from(state), | ||
// effects | ||
@@ -104,3 +107,3 @@ nextActions.onExit.concat(nextActions.actions).concat(nextActions.onEntry)); | ||
var _this = this; | ||
var history = state instanceof State_1.default ? state.history : undefined; | ||
var history = state instanceof State_1.State ? state.history : undefined; | ||
var stateValue = utils_1.toTrie(state); | ||
@@ -122,3 +125,3 @@ if (typeof stateValue === 'string') { | ||
var subHistory = history ? history.value[subStateKey] : undefined; | ||
var subState = new State_1.default(subStateValue, subHistory ? State_1.default.from(subHistory) : undefined); | ||
var subState = new State_1.State(subStateValue, subHistory ? State_1.State.from(subHistory) : undefined); | ||
var subStateNode = _this.states[subStateKey]; | ||
@@ -195,2 +198,3 @@ var nextTuple = subStateNode.transitionStateValue(subState, event, extendedState); | ||
var candidate = _a[_i]; | ||
// if (Array.isArray(transition[candidate])) {break;} | ||
var _b = transition[candidate], cond = _b.cond, transitionActions = _b.actions; | ||
@@ -281,3 +285,3 @@ var eventObject = typeof event === 'string' || typeof event === 'number' | ||
}, []); | ||
return new State_1.default(initialStateValue, undefined, entryActions); | ||
return new State_1.State(initialStateValue, undefined, entryActions); | ||
}, | ||
@@ -325,3 +329,3 @@ enumerable: true, | ||
var event_1 = _a[_i]; | ||
events.add(event_1); | ||
events.add("" + event_1); | ||
} | ||
@@ -328,0 +332,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { StateNodeConfig } from './types'; | ||
export declare function toggle(onState: string, offState: string, eventType: string): Record<string, StateNodeConfig>; | ||
import { SimpleStateNodeConfig } from './types'; | ||
export declare function toggle(onState: string, offState: string, eventType: string): Record<string, SimpleStateNodeConfig>; |
import { StateValue, Action } from './types'; | ||
export default class State { | ||
export declare class State { | ||
value: StateValue; | ||
@@ -4,0 +4,0 @@ history: State | undefined; |
@@ -49,2 +49,2 @@ "use strict"; | ||
}()); | ||
exports.default = State; | ||
exports.State = State; |
import { StateNode } from './index'; | ||
import State from './State'; | ||
import { State } from './State'; | ||
export declare type EventType = string | number; | ||
@@ -24,34 +24,41 @@ export declare type ActionType = string | number; | ||
} | ||
export declare type Transition<TStateKey extends string = string> = TStateKey | Record<TStateKey, TransitionConfig>; | ||
export interface StateNodeConfig<TStateKey extends string = string, TEventType extends string = string> { | ||
initial?: string; | ||
states?: Record<TStateKey, StateOrMachineConfig>; | ||
parallel?: boolean; | ||
export interface TargetTransitionConfig extends TransitionConfig { | ||
target: string; | ||
} | ||
export declare type ConditionalTransitionConfig = TargetTransitionConfig[]; | ||
export declare type Transition = string | Record<string, TransitionConfig>; | ||
export interface StateNodeConfig { | ||
key?: string; | ||
on?: Record<TEventType, Transition<TStateKey> | undefined>; | ||
initial?: string | undefined; | ||
parallel?: boolean | undefined; | ||
states?: Record<string, SimpleOrCompoundStateNodeConfig> | undefined; | ||
on?: Record<string, Transition | undefined>; | ||
onEntry?: Action | Action[]; | ||
onExit?: Action | Action[]; | ||
parent?: StateNode; | ||
strict?: boolean; | ||
strict?: boolean | undefined; | ||
} | ||
export interface StateLeafNodeConfig<TStateKey extends string = string, TEventType extends string = string> extends StateNodeConfig<TStateKey, TEventType> { | ||
export interface SimpleStateNodeConfig extends StateNodeConfig { | ||
initial?: undefined; | ||
parallel?: undefined; | ||
parallel?: false | undefined; | ||
states?: undefined; | ||
} | ||
export interface BaseMachineConfig<TStateKey extends string = string, TEventType extends string = string> extends StateNodeConfig<TStateKey, TEventType> { | ||
key?: string; | ||
initial?: string | undefined; | ||
export interface CompoundStateNodeConfig extends StateNodeConfig { | ||
initial?: string; | ||
parallel?: boolean; | ||
states: Record<TStateKey, StateOrMachineConfig>; | ||
states: Record<string, SimpleOrCompoundStateNodeConfig>; | ||
} | ||
export interface MachineConfig<TStateKey extends string = string, TEventType extends string = string> extends BaseMachineConfig<TStateKey, TEventType> { | ||
export declare type SimpleOrCompoundStateNodeConfig = CompoundStateNodeConfig | SimpleStateNodeConfig; | ||
export interface MachineConfig extends CompoundStateNodeConfig { | ||
key?: string; | ||
strict?: boolean; | ||
} | ||
export interface StandardMachineConfig extends MachineConfig { | ||
initial: string; | ||
parallel?: undefined; | ||
parallel?: false | undefined; | ||
} | ||
export interface ParallelMachineConfig<TStateKey extends string = string, TEventType extends string = string> extends BaseMachineConfig<TStateKey, TEventType> { | ||
export interface ParallelMachineConfig extends MachineConfig { | ||
initial?: undefined; | ||
parallel: true; | ||
} | ||
export declare type StateOrMachineConfig<TStateKey extends string = string, TEventType extends string = string> = MachineConfig<TStateKey, TEventType> | ParallelMachineConfig<TStateKey, TEventType> | StateLeafNodeConfig<TStateKey, TEventType>; | ||
export declare type Action = string | ActionObject; | ||
@@ -62,3 +69,3 @@ export interface EntryExitEffectMap { | ||
} | ||
export interface StateNode<TStateKey extends string = string, TEventType extends string = string> { | ||
export interface StateNode { | ||
key: string; | ||
@@ -69,4 +76,4 @@ id: string; | ||
parallel: boolean; | ||
states: Record<TStateKey, StateNode>; | ||
on?: Record<TEventType, Transition<TStateKey>>; | ||
states: Record<string, StateNode>; | ||
on?: Record<string, Transition>; | ||
onEntry?: Action | Action[]; | ||
@@ -77,6 +84,6 @@ onExit?: Action | Action[]; | ||
} | ||
export interface ComplexStateNode<TStateKey extends string = string, TEventType extends string = string> extends StateNode<TStateKey, TEventType> { | ||
export interface ComplexStateNode extends StateNode { | ||
initial: string; | ||
} | ||
export interface LeafStateNode<TStateKey extends string = string, TEventType extends string = string> extends StateNode<TStateKey, TEventType> { | ||
export interface LeafStateNode extends StateNode { | ||
initial: never; | ||
@@ -83,0 +90,0 @@ parallel: never; |
@@ -1,2 +0,2 @@ | ||
import State from './State'; | ||
import { State } from './State'; | ||
import { Event, StateValue, EventType } from './types'; | ||
@@ -3,0 +3,0 @@ export declare function getEventType(event: Event): EventType; |
@@ -28,6 +28,6 @@ "use strict"; | ||
function toTrie(stateValue) { | ||
if (stateValue instanceof State_1.default) { | ||
if (stateValue instanceof State_1.State) { | ||
return stateValue.value; | ||
} | ||
if (typeof stateValue === 'object' && !(stateValue instanceof State_1.default)) { | ||
if (typeof stateValue === 'object' && !(stateValue instanceof State_1.State)) { | ||
return stateValue; | ||
@@ -34,0 +34,0 @@ } |
{ | ||
"name": "xstate", | ||
"version": "3.0.3", | ||
"version": "3.0.4", | ||
"description": "Simple JavaScript Finite State Machines and Statecharts", | ||
@@ -38,3 +38,3 @@ "main": "lib/index.js", | ||
"tslint": "^5.7.0", | ||
"typescript": "^2.6.2", | ||
"typescript": "^2.7.1", | ||
"webpack": "^3.5.6" | ||
@@ -41,0 +41,0 @@ }, |
@@ -5,2 +5,3 @@ # xstate | ||
[![npm](https://img.shields.io/npm/v/xstate.svg?style=flat-square)]() | ||
[![Statecharts gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/statecharts/statecharts) | ||
@@ -7,0 +8,0 @@ Functional, stateless JavaScript [finite state machines](https://en.wikipedia.org/wiki/Finite-state_machine) and [statecharts](http://www.inf.ed.ac.uk/teaching/courses/seoc/2005_2006/resources/statecharts.pdf). |
@@ -31,5 +31,3 @@ import { StateNode } from './index'; | ||
function getTransitionStateKeys<TStateKey extends string = string>( | ||
transition: Transition<TStateKey> | ||
): TStateKey[] { | ||
function getTransitionStateKeys(transition: Transition): string[] { | ||
if (typeof transition === 'string') { | ||
@@ -39,3 +37,3 @@ return [transition]; | ||
return Object.keys(transition) as TStateKey[]; | ||
return Object.keys(transition) as string[]; | ||
} | ||
@@ -79,3 +77,3 @@ | ||
subStateKeys.forEach(subStateKey => { | ||
const subNode = parent.getState(subStateKey) as StateNode; | ||
const subNode = parent!.getState(subStateKey) as StateNode; | ||
const edge: IEdge = { event, source: node, target: subNode }; | ||
@@ -82,0 +80,0 @@ |
@@ -10,3 +10,3 @@ import { getEventType, toStatePath, toTrie, mapValues } from './utils'; | ||
ParallelMachine, | ||
StateOrMachineConfig, | ||
SimpleOrCompoundStateNodeConfig, | ||
MachineConfig, | ||
@@ -17,7 +17,9 @@ ParallelMachineConfig, | ||
ActionMap, | ||
MaybeStateValueActionsTuple | ||
MaybeStateValueActionsTuple, | ||
StandardMachineConfig, | ||
TransitionConfig | ||
} from './types'; | ||
import matchesState from './matchesState'; | ||
import mapState from './mapState'; | ||
import State from './State'; | ||
import { State } from './State'; | ||
@@ -27,6 +29,3 @@ const STATE_DELIMITER = '.'; | ||
class StateNode< | ||
TStateKey extends string = string, | ||
TEventType extends string = string | ||
> { | ||
class StateNode { | ||
public key: string; | ||
@@ -37,4 +36,4 @@ public id: string; | ||
public parallel?: boolean; | ||
public states: Record<TStateKey, StateNode>; | ||
public on?: Record<TEventType, Transition<TStateKey> | undefined>; | ||
public states: Record<string, StateNode>; | ||
public on?: Record<string, Transition | undefined>; | ||
public onEntry?: Action[]; | ||
@@ -52,3 +51,8 @@ public onExit?: Action[]; | ||
constructor(public config: StateOrMachineConfig<TStateKey, TEventType>) { | ||
constructor( | ||
public config: | ||
| SimpleOrCompoundStateNodeConfig | ||
| StandardMachineConfig | ||
| ParallelMachineConfig | ||
) { | ||
this.key = config.key || '(machine)'; | ||
@@ -67,3 +71,3 @@ this.parent = config.parent; | ||
this.states = (config.states | ||
? mapValues<StateOrMachineConfig, StateNode>( | ||
? mapValues<SimpleOrCompoundStateNodeConfig, StateNode>( | ||
config.states, | ||
@@ -77,5 +81,6 @@ (stateConfig, key) => | ||
) | ||
: {}) as Record<TStateKey, StateNode<string, string>>; | ||
: {}) as Record<string, StateNode>; | ||
this.on = config.on; | ||
this.strict = !!config.strict; | ||
this.onEntry = config.onEntry | ||
@@ -87,3 +92,2 @@ ? ([] as Action[]).concat(config.onEntry) | ||
: undefined; | ||
this.strict = !!config.strict; | ||
} | ||
@@ -116,3 +120,8 @@ public getStateNodes(state: StateValue | State): StateNode[] { | ||
const eventType = getEventType(event); | ||
return this.on && this.on[eventType]; | ||
if (this.on) { | ||
return eventType in this.on; | ||
} | ||
return false; | ||
} | ||
@@ -287,3 +296,6 @@ public transition( | ||
for (const candidate of Object.keys(transition)) { | ||
const { cond, actions: transitionActions } = transition[candidate]; | ||
// if (Array.isArray(transition[candidate])) {break;} | ||
const { cond, actions: transitionActions } = transition[ | ||
candidate | ||
] as TransitionConfig; | ||
const eventObject: EventObject = | ||
@@ -443,3 +455,3 @@ typeof event === 'string' || typeof event === 'number' | ||
for (const event of state.events) { | ||
events.add(event); | ||
events.add(`${event}`); | ||
} | ||
@@ -446,0 +458,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { StateNodeConfig } from './types'; | ||
import { SimpleStateNodeConfig } from './types'; | ||
@@ -7,3 +7,3 @@ export function toggle( | ||
eventType: string | ||
): Record<string, StateNodeConfig> { | ||
): Record<string, SimpleStateNodeConfig> { | ||
return { | ||
@@ -10,0 +10,0 @@ [onState]: { |
@@ -5,3 +5,3 @@ import { StateValue, Action } from './types'; | ||
export default class State { | ||
export class State { | ||
public static from(stateValue: State | StateValue): State { | ||
@@ -8,0 +8,0 @@ if (stateValue instanceof State) { |
import { StateNode } from './index'; | ||
import State from './State'; | ||
import { State } from './State'; | ||
@@ -32,52 +32,47 @@ export type EventType = string | number; | ||
export type Transition<TStateKey extends string = string> = | ||
| TStateKey | ||
| Record<TStateKey, TransitionConfig>; | ||
export interface TargetTransitionConfig extends TransitionConfig { | ||
target: string; | ||
} | ||
export interface StateNodeConfig< | ||
TStateKey extends string = string, | ||
TEventType extends string = string | ||
> { | ||
initial?: string; | ||
states?: Record<TStateKey, StateOrMachineConfig>; | ||
parallel?: boolean; | ||
export type ConditionalTransitionConfig = TargetTransitionConfig[]; | ||
export type Transition = string | Record<string, TransitionConfig>; | ||
export interface StateNodeConfig { | ||
key?: string; | ||
on?: Record<TEventType, Transition<TStateKey> | undefined>; | ||
initial?: string | undefined; | ||
parallel?: boolean | undefined; | ||
states?: Record<string, SimpleOrCompoundStateNodeConfig> | undefined; | ||
on?: Record<string, Transition | undefined>; | ||
onEntry?: Action | Action[]; | ||
onExit?: Action | Action[]; | ||
parent?: StateNode; | ||
strict?: boolean; | ||
strict?: boolean | undefined; | ||
} | ||
export interface StateLeafNodeConfig< | ||
TStateKey extends string = string, | ||
TEventType extends string = string | ||
> extends StateNodeConfig<TStateKey, TEventType> { | ||
export interface SimpleStateNodeConfig extends StateNodeConfig { | ||
initial?: undefined; | ||
parallel?: undefined; | ||
parallel?: false | undefined; | ||
states?: undefined; | ||
} | ||
export interface BaseMachineConfig< | ||
TStateKey extends string = string, | ||
TEventType extends string = string | ||
> extends StateNodeConfig<TStateKey, TEventType> { | ||
key?: string; | ||
initial?: string | undefined; | ||
export interface CompoundStateNodeConfig extends StateNodeConfig { | ||
initial?: string; | ||
parallel?: boolean; | ||
states: Record<TStateKey, StateOrMachineConfig>; | ||
states: Record<string, SimpleOrCompoundStateNodeConfig>; | ||
} | ||
export interface MachineConfig< | ||
TStateKey extends string = string, | ||
TEventType extends string = string | ||
> extends BaseMachineConfig<TStateKey, TEventType> { | ||
export type SimpleOrCompoundStateNodeConfig = | ||
| CompoundStateNodeConfig | ||
| SimpleStateNodeConfig; | ||
export interface MachineConfig extends CompoundStateNodeConfig { | ||
key?: string; | ||
strict?: boolean; | ||
} | ||
export interface StandardMachineConfig extends MachineConfig { | ||
initial: string; | ||
parallel?: undefined; | ||
parallel?: false | undefined; | ||
} | ||
export interface ParallelMachineConfig< | ||
TStateKey extends string = string, | ||
TEventType extends string = string | ||
> extends BaseMachineConfig<TStateKey, TEventType> { | ||
export interface ParallelMachineConfig extends MachineConfig { | ||
initial?: undefined; | ||
@@ -87,10 +82,2 @@ parallel: true; | ||
export type StateOrMachineConfig< | ||
TStateKey extends string = string, | ||
TEventType extends string = string | ||
> = | ||
| MachineConfig<TStateKey, TEventType> | ||
| ParallelMachineConfig<TStateKey, TEventType> | ||
| StateLeafNodeConfig<TStateKey, TEventType>; | ||
export type Action = string | ActionObject; | ||
@@ -102,6 +89,3 @@ export interface EntryExitEffectMap { | ||
export interface StateNode< | ||
TStateKey extends string = string, | ||
TEventType extends string = string | ||
> { | ||
export interface StateNode { | ||
key: string; | ||
@@ -112,4 +96,4 @@ id: string; | ||
parallel: boolean; | ||
states: Record<TStateKey, StateNode>; | ||
on?: Record<TEventType, Transition<TStateKey>>; | ||
states: Record<string, StateNode>; | ||
on?: Record<string, Transition>; | ||
onEntry?: Action | Action[]; | ||
@@ -121,13 +105,7 @@ onExit?: Action | Action[]; | ||
export interface ComplexStateNode< | ||
TStateKey extends string = string, | ||
TEventType extends string = string | ||
> extends StateNode<TStateKey, TEventType> { | ||
export interface ComplexStateNode extends StateNode { | ||
initial: string; | ||
} | ||
export interface LeafStateNode< | ||
TStateKey extends string = string, | ||
TEventType extends string = string | ||
> extends StateNode<TStateKey, TEventType> { | ||
export interface LeafStateNode extends StateNode { | ||
initial: never; | ||
@@ -134,0 +112,0 @@ parallel: never; |
@@ -1,2 +0,2 @@ | ||
import State from './State'; | ||
import { State } from './State'; | ||
import { Event, StateValue, EventType } from './types'; | ||
@@ -3,0 +3,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
280
670435
8374