-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue.js
More file actions
48 lines (44 loc) · 790 Bytes
/
queue.js
File metadata and controls
48 lines (44 loc) · 790 Bytes
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
var queue = function(){
var keepAlive = false;
var keepAliveDelay = 5*1000;
var stack = [];
function _add(task){
stack.push(task);
}
function _clear(){
stack = [];
}
function _next(){
return stack.shift();
}
function async(arg, callback) {
setTimeout(function(){ arg(callback);},0);
}
function _asyncCallback(result) {
_process();
}
function _process(){
var item = stack.shift();
if(item) {
async( item, _asyncCallback);
} else {
if(keepAlive) {
_keepAlive();
} else {
_final();
}
}
}
function _final(){
console.log('queue empty.');
}
function _keepAlive(){
setTimeout(function(){_asyncCallback('waiting');},keepAliveDelay);
}
return {
add : _add,
clear: _clear,
process: _process
}
}
module.exports = new queue();