UNPKG

asyncstorage-down

Version:

A leveldown API implementation that maps to AsyncStorage in React Native, originally a fork of no9/localstorage-down

37 lines (29 loc) 794 B
'use strict'; var argsarray = require('argsarray'); var Queue = require('tiny-queue'); // see http://stackoverflow.com/a/15349865/680742 var nextTick = global.setImmediate || process.nextTick; function TaskQueue() { this.queue = new Queue(); this.running = false; } TaskQueue.prototype.add = function (fun, callback) { 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;