Skip to content

Instantly share code, notes, and snippets.

@alfredwesterveld
Created March 17, 2016 07:25
Show Gist options
  • Select an option

  • Save alfredwesterveld/e8850fd311b13476ae2b to your computer and use it in GitHub Desktop.

Select an option

Save alfredwesterveld/e8850fd311b13476ae2b to your computer and use it in GitHub Desktop.
"use strict";
const Queue = module.exports = function () {
this.queue = []
this.awaiting = []
}
Queue.prototype = {
take: function () {
let queue = this.queue,
awaiting = this.awaiting
return function (cb) {
if (queue.length === 0) {
// no items in queue so wait
awaiting.push(cb)
} else {
let item = queue.shift()
cb(null, item)
}
}
},
poll: function () {
return this.queue.shift()
},
put: function (item) {
if (this.awaiting.length > 0) {
let cb = this.awaiting.shift()
cb(null, item)
} else {
this.queue.push(item)
}
},
size: function () {
return this.queue.length
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment