UNPKG

511 BJavaScriptView Raw
1module.exports = queue
2
3function queue (arr) {
4 return arr.reduce(function (obj, method) {
5 obj[method] = Queue()
6 return obj
7 }, {})
8}
9
10function Queue () {
11 if (!(this instanceof Queue)) return new Queue()
12 this._ready = false
13 this._arr = []
14}
15
16Queue.prototype.add = function (cb) {
17 if (!this._ready) this._arr.push(cb)
18 else cb()
19}
20
21Queue.prototype.ready = function () {
22 this._ready = true
23 this._arr.forEach(function (fn) {
24 fn()
25 })
26 this._arr.length = 0 // clean up internal array
27}