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