mq-flow
Version:
A lightweight, simple queue system designed for small tasks that need to be executed in a queued manner
2 lines • 2.62 kB
JavaScript
var f=h=>{throw TypeError(h)};var E=(h,u,e)=>u.has(h)||f("Cannot "+e);var s=(h,u,e)=>(E(h,u,"read from private field"),e?e.call(h):u.get(h)),I=(h,u,e)=>u.has(h)?f("Cannot add the same private member more than once"):u instanceof WeakSet?u.add(h):u.set(h,e),d=(h,u,e,r)=>(E(h,u,"write to private field"),r?r.call(h,e):u.set(h,e),e),P=(h,u,e)=>(E(h,u,"access private method"),e);var y=(h,u,e)=>new Promise((r,n)=>{var w=o=>{try{m(e.next(o))}catch(g){n(g)}},b=o=>{try{m(e.throw(o))}catch(g){n(g)}},m=o=>o.done?r(o.value):Promise.resolve(o.value).then(w,b);m((e=e.apply(h,u)).next())});import{EventEmitter as x}from"stream";var t,i,c,l,a,C=class extends x{constructor(){super(...arguments);I(this,l);I(this,t,[]);I(this,i,!1);I(this,c,1);this.CurrentProcessId=null}mqPush(e,r){let n=(r==null?void 0:r.ProcessId)!==void 0?r==null?void 0:r.ProcessId:s(this,t).length+1;if(r!=null&&r.Priority&&(r==null?void 0:r.Priority)!==0&&(r==null?void 0:r.Priority)!==1)throw new Error("Priority must be HIGH(1) or LOW(0)");return(r==null?void 0:r.Priority)===1?s(this,t).unshift({CallableFunction:e,ProcessId:n}):s(this,t).push({CallableFunction:e,ProcessId:n}),s(this,i)&&P(this,l,a).call(this),{ProcessId:n,QueueLength:s(this,t).length}}mqPop(){var r;if(s(this,t).length===0)throw new Error("Queue is empty: No processes available to execute.");if(this.CurrentProcessId===s(this,t)[s(this,t).length-1].ProcessId)throw new Error(`Process already under execution: The process with ID ${this.CurrentProcessId} is currently running.`);return{ProcessId:(r=s(this,t).pop())==null?void 0:r.ProcessId,QueueLength:s(this,t).length}}mqRemove(e){if(s(this,t).length===0)throw new Error("Queue is empty: No processes available to execute.");if(this.CurrentProcessId===e)throw new Error(`Process already under execution: The process with ID ${this.CurrentProcessId} is currently running.`);return{ProcessId:s(this,t).splice(s(this,t).findIndex(n=>n.ProcessId===e),1)[0].ProcessId,QueueLength:s(this,t).length}}mqStart(){d(this,i,!0),P(this,l,a).call(this)}mqEnd(){d(this,i,!1)}getCurrentProcessId(){return this.CurrentProcessId}getQueueLength(){return s(this,t).length}};t=new WeakMap,i=new WeakMap,c=new WeakMap,l=new WeakSet,a=function(){return y(this,null,function*(){if(!s(this,i)||s(this,t).length===0||s(this,c)==0)return;d(this,c,0),this.CurrentProcessId=s(this,t)[0].ProcessId||null;let e=s(this,t).shift(),n={value:yield e==null?void 0:e.CallableFunction(),processId:e==null?void 0:e.ProcessId,queueLength:s(this,t).length};this.emit("getResult",n),d(this,c,1),P(this,l,a).call(this)})};export{C as Queue};
//# sourceMappingURL=index.mjs.map