UNPKG

hkey-async-queue

Version:

Async Queue (Throttler). For rate limiting. For example, you use an external API and the number of requests per second is limited there.

41 lines 694 B
module.exports = class CommonStorage{ push(item){ this._push(item); item.isInStorage = true; } cmp(a,b){ return (b.priority!==a.priority) ? (b.priority - a.priority) : (a.id - b.id); } shift(){ while(true){ const item = this._shift(); if(!item){ return item; } else if(item.isInStorage){ item.isInStorage = false; return item; } } } shiftAll(){ const res = []; while(true){ const item = this.shift(); if(!item){ break; } else { res.push(item); } } return res; } remove(item){ item.isInStorage = false; } isEmpty(){ return this.length===0; } clear(){ this.shiftAll(); } };