-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
52 lines (45 loc) · 1.21 KB
/
queue.js
File metadata and controls
52 lines (45 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Created by stevenzhu on 2016/5/3/003, Guangzhou, China
* MailTo:cnvoid@126.com
*/
module.exports = function () {
'use strict';
return {
queue: [],
doing: false,
event: {},
enqueue: function (data) {
if (!data) {
return false;
}
console.log('enqueue')
this.queue.push(data)
return this
},
dequeue: function () {
return this.queue.shift()
},
startLoop: function () {
if (!this.doing && this.queue.length > 0) {
let item = this.dequeue()
if (typeof item == 'function'){
item()
this.doing = true
}
}
return this
},
killFront: function () {
this.doing = false
this.startLoop()
console.log('dequeue', this.queue.length)
if(this.queue.length === 0) {
typeof this.event['empty'] == 'function' && this.event['empty']()
}
return this
},
on: function(event, cb) {
this.event[event] = cb
}
}
}