Skip to content

Instantly share code, notes, and snippets.

@jacobdam
Created January 22, 2013 15:01
Show Gist options
  • Save jacobdam/4595275 to your computer and use it in GitHub Desktop.
Save jacobdam/4595275 to your computer and use it in GitHub Desktop.
WorkQueue = {
MAX_CONCURRENT: 2,
init: function() {
this.num_working = 0;
this.items = [];
},
push: function(item) {
this.items.push(item);
this.update()
},
update: function() {
if (this.items.length > 0 && this.num_working < WorkQueue.MAX_CONCURRENT) {
var item = this.items.shift();
this.num_working++;
item.startWorking(this.itemFinishedHandler.bind(this));
}
},
itemFinishedHandler: function() {
this.num_working--;
this.update();
}.bind(this)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment