UNPKG

@qiwi/cyclone

Version:

"State machine" for basic purposes

3 lines (2 loc) 3.12 kB
const t="Transition violation",i="Invalid unlock key",s="Lock violation",e="Unreachable state";class n extends Error{}function o(){return o=Object.assign?Object.assign.bind():function(t){for(var i=1;i<arguments.length;i++){var s=arguments[i];for(var e in s)Object.prototype.hasOwnProperty.call(s,e)&&(t[e]=s[e])}return t},o.apply(this,arguments)}const r=()=>new Date,h=()=>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]")},c=">",l=(...t)=>t.pop(),y=10,d={transitions:{},historySize:10,immutable:!1};class u{constructor(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=l,this.opts=o({},d,t),this.history=[],this.key=null,this.id=h(),this.transitions=t.transitions,"string"==typeof t.initialState&&this.history.push({state:t.initialState,data:t.initialData,id:h(),date:r()}),this}next(t,...i){if(this.key)throw new n("Lock violation");const s=u.getHandler(t,this.history,this.transitions)(this.current().data,...i),e=h(),o=r();return this.history.push({state:t,data:s,id:e,date:o}),this.history.length>u.getHistoryLimit(this.opts.historySize)&&(a.debug("history limit reached"),this.history.shift()),this}current(){return o({},this.history[this.history.length-1])}last(t){if(void 0===t)return this.current();const i="string"==typeof t?({state:i})=>i===t:t;return[...this.history].reverse().find(i)}prev(t){if(this.key)throw new n("Lock violation");if(this.history.length<2)throw new n("Unreachable state");if(void 0===t)return this.history.pop(),this;const i=this.last(t);if(!i)throw new n("Unreachable state");return this.history.length=this.history.indexOf(i)+1,this}lock(t){return this.key=t||`lock${h()}`,this}unlock(t){if(this.key!==t)throw new n("Invalid unlock key");return this.key=null,this}static getHistoryLimit(t){return void 0===t?10:-1===t?Number.POSITIVE_INFINITY:t}static getHandler(t,i,s){const e=this.getTargetTransition(t,i),o=this.getTransition(e,s);if(!o)throw new n("Transition violation");const r=s[o];return"function"==typeof r?r:l}static getTransition(t,i){return Object.keys(i).filter(i=>t.length>i.length?new RegExp(`.*${i}$`).test(t):t===i).sort((t,i)=>i.length-t.length)[0]}static getTargetTransition(t,i){return[...i.map(({state:t})=>t),t].join(">")}}class g{constructor(){this.store=void 0,this.store={}}get(t){return this.store[t]}add(t,i){this.store[t]=i}remove(t){delete this.store[t]}}const p={},f=new g,v=new g,w=t=>{const{machine:i,template:s,templateRegistry:e=f,machineRegistry:n=v}=t,r=k(s,e),h=o({},d,r,i),a=new u(h);return n.add(a.id,a),a},k=(t,i)=>"string"==typeof t?i.get(t):t;export{l as DEFAULT_HANDLER,y as DEFAULT_HISTORY_SIZE,v as DEFAULT_MACHINE_REGISTRY,d as DEFAULT_OPTS,p as DEFAULT_TEMPLATE,f as DEFAULT_TEMPLATE_REGISTRY,c as DELIMITER,i as INVALID_UNLOCK_KEY,s as LOCK_VIOLATION,u as Machine,n as MachineError,t as TRANSITION_VIOLATION,e as UNREACHABLE_STATE,w as factory,k as getTemplate}; //# sourceMappingURL=cyclone.modern.mjs.map