UNPKG

@qiwi/cyclone

Version:

"State machine" for basic purposes

3 lines (2 loc) 4.91 kB
function t(){return t=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},t.apply(this,arguments)}function n(t){return n=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},n(t)}function e(t,n){return e=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,n){return t.__proto__=n,t},e(t,n)}function i(){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 r(t,n,o){return r=i()?Reflect.construct.bind():function(t,n,i){var r=[null];r.push.apply(r,n);var o=new(Function.bind.apply(t,r));return i&&e(o,i.prototype),o},r.apply(null,arguments)}function o(t){var i="function"==typeof Map?new Map:void 0;return o=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!==i){if(i.has(t))return i.get(t);i.set(t,o)}function o(){return r(t,arguments,n(this).constructor)}return o.prototype=Object.create(t.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),e(o,t)},o(t)}var s="Transition violation",c="Invalid unlock key",u="Lock violation",a="Unreachable state",h=/*#__PURE__*/function(t){var n,i;function r(){return t.apply(this,arguments)||this}return i=t,(n=r).prototype=Object.create(i.prototype),n.prototype.constructor=n,e(n,i),r}(/*#__PURE__*/o(Error)),f=function(){return new Date},l=function(){return Math.random().toString()},p={debug:console.debug.bind(console,"[cyclone]"),info:console.info.bind(console,"[cyclone]"),error:console.error.bind(console,"[cyclone]"),warn:console.warn.bind(console,"[cyclone]")},y=">",d=function(){return[].slice.call(arguments).pop()},v=10,g={transitions:{},historySize:10,immutable:!1},b=/*#__PURE__*/function(){function n(n){return this.opts=void 0,this.history=void 0,this.key=void 0,this.id=void 0,this.transitions=void 0,this.DEFAULT_HANDLER=d,this.opts=t({},g,n),this.history=[],this.key=null,this.id=l(),this.transitions=n.transitions,"string"==typeof n.initialState&&this.history.push({state:n.initialState,data:n.initialData,id:l(),date:f()}),this}var e=n.prototype;return e.next=function(t){if(this.key)throw new h("Lock violation");var e=n.getHandler(t,this.history,this.transitions),i=this.current(),r=e.apply(void 0,[i.data].concat([].slice.call(arguments,1))),o=l(),s=f();return this.history.push({state:t,data:r,id:o,date:s}),this.history.length>n.getHistoryLimit(this.opts.historySize)&&(p.debug("history limit reached"),this.history.shift()),this},e.current=function(){return t({},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("Lock violation");if(this.history.length<2)throw new h("Unreachable state");if(void 0===t)return this.history.pop(),this;var n=this.last(t);if(!n)throw new h("Unreachable state");return this.history.length=this.history.indexOf(n)+1,this},e.lock=function(t){return this.key=t||"lock"+l(),this},e.unlock=function(t){if(this.key!==t)throw new h("Invalid unlock key");return this.key=null,this},n.getHistoryLimit=function(t){return void 0===t?10:-1===t?Number.POSITIVE_INFINITY:t},n.getHandler=function(t,n,e){var i=this.getTargetTransition(t,n),r=this.getTransition(i,e);if(!r)throw new h("Transition violation");var o=e[r];return"function"==typeof o?o:d},n.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]},n.getTargetTransition=function(t,n){return[].concat(n.map(function(t){return t.state}),[t]).join(">")},n}(),w=/*#__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={},k=new w,m=new w,j=function(n){var e=n.machine,i=n.templateRegistry,r=n.machineRegistry,o=void 0===r?m:r,s=T(n.template,void 0===i?k:i),c=t({},g,s,e),u=new b(c);return o.add(u.id,u),u},T=function(t,n){return"string"==typeof t?n.get(t):t};export{d as DEFAULT_HANDLER,v as DEFAULT_HISTORY_SIZE,m as DEFAULT_MACHINE_REGISTRY,g as DEFAULT_OPTS,O as DEFAULT_TEMPLATE,k as DEFAULT_TEMPLATE_REGISTRY,y as DELIMITER,c as INVALID_UNLOCK_KEY,u as LOCK_VIOLATION,b as Machine,h as MachineError,s as TRANSITION_VIOLATION,a as UNREACHABLE_STATE,j as factory,T as getTemplate}; //# sourceMappingURL=cyclone.esm.mjs.map