Created
October 4, 2014 23:27
-
-
Save goliatone/fcaa76e9bc302c511d29 to your computer and use it in GitHub Desktop.
Simple queue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define('helpers/queue', function() { | |
var Queue = {}; | |
Queue.items = []; | |
Queue.enqueue = function(process) { | |
var id = Queue.items.push(process); | |
setTimeout(function() { | |
Queue.update(id); | |
}, 0); | |
}; | |
Queue.update = function update(id) { | |
if (Queue.items.length < 1 || this.id) return; | |
var process = Queue.items.shift(); | |
if (!process) return console.log('process no process'); | |
this.id = id; | |
process(function() { | |
this.id = null; | |
this.update(); | |
}.bind(this)); | |
}; | |
return Queue; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment