Created
March 17, 2016 07:25
-
-
Save alfredwesterveld/e8850fd311b13476ae2b to your computer and use it in GitHub Desktop.
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
| "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