UNPKG

mq-flow

Version:

A lightweight, simple queue system designed for small tasks that need to be executed in a queued manner

2 lines 3.1 kB
"use strict";var E=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var C=s=>{throw TypeError(s)};var N=(s,t)=>{for(var e in t)E(s,e,{get:t[e],enumerable:!0})},q=(s,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of T(t))!D.call(s,n)&&n!==e&&E(s,n,{get:()=>t[n],enumerable:!(r=R(t,n))||r.enumerable});return s};var H=s=>q(E({},"__esModule",{value:!0}),s);var f=(s,t,e)=>t.has(s)||C("Cannot "+e);var h=(s,t,e)=>(f(s,t,"read from private field"),e?e.call(s):t.get(s)),I=(s,t,e)=>t.has(s)?C("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(s):t.set(s,e),d=(s,t,e,r)=>(f(s,t,"write to private field"),r?r.call(s,e):t.set(s,e),e),P=(s,t,e)=>(f(s,t,"access private method"),e);var w=(s,t,e)=>new Promise((r,n)=>{var x=o=>{try{m(e.next(o))}catch(g){n(g)}},L=o=>{try{m(e.throw(o))}catch(g){n(g)}},m=o=>o.done?r(o.value):Promise.resolve(o.value).then(x,L);m((e=e.apply(s,t)).next())});var Q={};N(Q,{Queue:()=>y});module.exports=H(Q);var b=require("stream");var u,i,c,l,a,y=class extends b.EventEmitter{constructor(){super(...arguments);I(this,l);I(this,u,[]);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:h(this,u).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?h(this,u).unshift({CallableFunction:e,ProcessId:n}):h(this,u).push({CallableFunction:e,ProcessId:n}),h(this,i)&&P(this,l,a).call(this),{ProcessId:n,QueueLength:h(this,u).length}}mqPop(){var r;if(h(this,u).length===0)throw new Error("Queue is empty: No processes available to execute.");if(this.CurrentProcessId===h(this,u)[h(this,u).length-1].ProcessId)throw new Error(`Process already under execution: The process with ID ${this.CurrentProcessId} is currently running.`);return{ProcessId:(r=h(this,u).pop())==null?void 0:r.ProcessId,QueueLength:h(this,u).length}}mqRemove(e){if(h(this,u).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:h(this,u).splice(h(this,u).findIndex(n=>n.ProcessId===e),1)[0].ProcessId,QueueLength:h(this,u).length}}mqStart(){d(this,i,!0),P(this,l,a).call(this)}mqEnd(){d(this,i,!1)}getCurrentProcessId(){return this.CurrentProcessId}getQueueLength(){return h(this,u).length}};u=new WeakMap,i=new WeakMap,c=new WeakMap,l=new WeakSet,a=function(){return w(this,null,function*(){if(!h(this,i)||h(this,u).length===0||h(this,c)==0)return;d(this,c,0),this.CurrentProcessId=h(this,u)[0].ProcessId||null;let e=h(this,u).shift(),n={value:yield e==null?void 0:e.CallableFunction(),processId:e==null?void 0:e.ProcessId,queueLength:h(this,u).length};this.emit("getResult",n),d(this,c,1),P(this,l,a).call(this)})};0&&(module.exports={Queue}); //# sourceMappingURL=index.js.map