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