@qiwi/cyclone
Version:
"State machine" for basic purposes
3 lines (2 loc) • 4.92 kB
JavaScript
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t||self).cyclone={})}(this,function(t){function n(){return n=Object.assign?Object.assign.bind():function(t){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])}return t},n.apply(this,arguments)}function e(t){return e=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},e(t)}function i(t,n){return i=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,n){return t.__proto__=n,t},i(t,n)}function r(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(t){return!1}}function o(t,n,e){return o=r()?Reflect.construct.bind():function(t,n,e){var r=[null];r.push.apply(r,n);var o=new(Function.bind.apply(t,r));return e&&i(o,e.prototype),o},o.apply(null,arguments)}function s(t){var n="function"==typeof Map?new Map:void 0;return s=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==n){if(n.has(t))return n.get(t);n.set(t,r)}function r(){return o(t,arguments,e(this).constructor)}return r.prototype=Object.create(t.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),i(r,t)},s(t)}var u="Transition violation",c="Invalid unlock key",a="Lock violation",f="Unreachable state",h=/*#__PURE__*/function(t){var n,e;function r(){return t.apply(this,arguments)||this}return e=t,(n=r).prototype=Object.create(e.prototype),n.prototype.constructor=n,i(n,e),r}(/*#__PURE__*/s(Error)),l=function(){return new Date},p=function(){return Math.random().toString()},y="[cyclone]",d={debug:console.debug.bind(console,y),info:console.info.bind(console,y),error:console.error.bind(console,y),warn:console.warn.bind(console,y)},g=function(){return[].slice.call(arguments).pop()},v={transitions:{},historySize:10,immutable:!1},T=/*#__PURE__*/function(){function t(t){return this.opts=void 0,this.history=void 0,this.key=void 0,this.id=void 0,this.transitions=void 0,this.DEFAULT_HANDLER=g,this.opts=n({},v,t),this.history=[],this.key=null,this.id=p(),this.transitions=t.transitions,"string"==typeof t.initialState&&this.history.push({state:t.initialState,data:t.initialData,id:p(),date:l()}),this}var e=t.prototype;return e.next=function(n){if(this.key)throw new h(a);var e=t.getHandler(n,this.history,this.transitions),i=this.current(),r=e.apply(void 0,[i.data].concat([].slice.call(arguments,1))),o=p(),s=l();return this.history.push({state:n,data:r,id:o,date:s}),this.history.length>t.getHistoryLimit(this.opts.historySize)&&(d.debug("history limit reached"),this.history.shift()),this},e.current=function(){return n({},this.history[this.history.length-1])},e.last=function(t){if(void 0===t)return this.current();var n="string"==typeof t?function(n){return n.state===t}:t;return[].concat(this.history).reverse().find(n)},e.prev=function(t){if(this.key)throw new h(a);if(this.history.length<2)throw new h(f);if(void 0===t)return this.history.pop(),this;var n=this.last(t);if(!n)throw new h(f);return this.history.length=this.history.indexOf(n)+1,this},e.lock=function(t){return this.key=t||"lock"+p(),this},e.unlock=function(t){if(this.key!==t)throw new h(c);return this.key=null,this},t.getHistoryLimit=function(t){return void 0===t?10:-1===t?Number.POSITIVE_INFINITY:t},t.getHandler=function(t,n,e){var i=this.getTargetTransition(t,n),r=this.getTransition(i,e);if(!r)throw new h(u);var o=e[r];return"function"==typeof o?o:g},t.getTransition=function(t,n){return Object.keys(n).filter(function(n){return t.length>n.length?new RegExp(".*"+n+"$").test(t):t===n}).sort(function(t,n){return n.length-t.length})[0]},t.getTargetTransition=function(t,n){return[].concat(n.map(function(t){return t.state}),[t]).join(">")},t}(),b=/*#__PURE__*/function(){function t(){this.store=void 0,this.store={}}var n=t.prototype;return n.get=function(t){return this.store[t]},n.add=function(t,n){this.store[t]=n},n.remove=function(t){delete this.store[t]},t}(),O=new b,E=new b,w=function(t,n){return"string"==typeof t?n.get(t):t};t.DEFAULT_HANDLER=g,t.DEFAULT_HISTORY_SIZE=10,t.DEFAULT_MACHINE_REGISTRY=E,t.DEFAULT_OPTS=v,t.DEFAULT_TEMPLATE={},t.DEFAULT_TEMPLATE_REGISTRY=O,t.DELIMITER=">",t.INVALID_UNLOCK_KEY=c,t.LOCK_VIOLATION=a,t.Machine=T,t.MachineError=h,t.TRANSITION_VIOLATION=u,t.UNREACHABLE_STATE=f,t.factory=function(t){var e=t.machine,i=t.templateRegistry,r=t.machineRegistry,o=void 0===r?E:r,s=w(t.template,void 0===i?O:i),u=n({},v,s,e),c=new T(u);return o.add(c.id,c),c},t.getTemplate=w});
//# sourceMappingURL=cyclone.umd.js.map