UNPKG

pouchdb-all-dbs

Version:
39 lines (31 loc) 862 B
'use strict'; var argsarray = require('argsarray'); var Queue = require('tiny-queue'); // see http://stackoverflow.com/a/15349865/680742 /* istanbul ignore next */ var nextTick = global.setImmediate || process.nextTick; function TaskQueue() { this.queue = new Queue(); this.running = false; } TaskQueue.prototype.add = function (fun, callback) { callback = callback || function () {}; this.queue.push({fun: fun, callback: callback}); this.processNext(); }; TaskQueue.prototype.processNext = function () { var self = this; if (self.running || !self.queue.length) { return; } self.running = true; var task = self.queue.shift(); nextTick(function () { task.fun(argsarray(function (args) { task.callback.apply(null, args); self.running = false; self.processNext(); })); }); }; module.exports = TaskQueue;