Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | export default class Queue { constructor(limit) { this.limit = limit; this.size = 0; this.awaiting = null; } /** * Creates a new "proxy" function associated with the current execution queue * instance. When the returned function is invoked, the queue limit is checked * to make sure the limit of scheduled tasks is respected (throwing an * exception when the limit has been reached and before calling the original * function). The original function is only invoked after all the previously * scheduled tasks have finished executing (their returned promises have * resolved/rejected); * * @param {function} task The function whose execution will be associated * with the current Queue instance; * @returns {function} The "proxy" function bound to the current Queue * instance; */ bind(task) { return bind(this, task); } bindSafe(task, onError) { const boundTask = bind(this, task); return async function safeTask(...args) { try { return await boundTask(...args); } catch (e) { onError(e); } }; } } /** * Utils */ function bind(queue, task) { const cleaner = clean.bind(null, queue); return async function boundTask(...args) { Iif (queue.size >= queue.limit) { throw new Error('Queue limit reached'); } const promise = chain(queue.awaiting, task, args); queue.awaiting = promise.then(cleaner, cleaner); queue.size++; return promise; }; } function clean(queue) { Iif (queue.size > 0 && --queue.size === 0) { queue.awaiting = null; } } async function chain(prev, task, args) { await prev; return task(...args); } |