forked from vietor/node-func-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (134 loc) · 3.41 KB
/
Copy pathindex.js
File metadata and controls
157 lines (134 loc) · 3.41 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
function PersistentQueue(callback_error, callback_successed, callback_thisArg) {
var queue = [];
var self = this;
var aborted = false;
var executing = false;
var ptr = -1;
var len = 0;
this.add = function(callback) {
queue.push(callback);
len = queue.length;
ptr++;
};
function abort(error, args) {
aborted = true;
while(ptr < len) ptr++;
if(error)
callback_error.apply(callback_thisArg, args);
else
callback_successed.apply(callback_thisArg, args);
}
this.error = function() {
abort(true, arguments);
};
this.escape = function() {
abort(false, arguments);
};
function step(args) {
if(ptr == len-1) {
callback_successed.apply(callback_thisArg, args);
ptr++;
}
else {
ptr++;
queue[ptr].apply(self, args);
}
}
this.deliver = function() {
step(arguments);
};
this.execute = function() {
ptr=-1;
step(arguments);
};
}
function Queue(callback_error, callback_successed, callback_thisArg) {
var queue = [];
var self = this;
var aborted = false;
var executing = false;
this.add = function(callback) {
if(executing)
throw new Error("The Queue already executed");
queue.push(callback);
};
this.append = function(callback) {
if(!executing)
throw new Error("The Queue not executed");
queue.push(callback);
};
function abort(error, args) {
aborted = true;
while(queue.length > 0)
queue.shift();
if(error)
callback_error.apply(callback_thisArg, args);
else
callback_successed.apply(callback_thisArg, args);
}
this.error = function() {
abort(true, arguments);
};
this.escape = function() {
abort(false, arguments);
};
function step(args) {
if(queue.length < 1)
callback_successed.apply(callback_thisArg, args);
else
queue.shift().apply(self, args);
}
this.deliver = function() {
step(arguments);
};
this.execute = function() {
if(executing)
throw new Error("The Queue already executed");
executing = true;
step(arguments);
};
};
module.exports.createQueue = function(callback_error, callback_successed, callback_thisArg) {
return new Queue(callback_error, callback_successed, callback_thisArg);
};
function ConcurrentQueue(callback_done, callback_thisArg) {
var array = [];
var results = [];
var completed = 0;
var executing = false;
function checkAndProcessDone() {
++ completed;
if(completed >= array.length) {
callback_done.apply(callback_thisArg, [results]);
}
}
this.createQueue = function(key) {
if(executing)
throw new Error("The QueueArray already executed");
var result = {key: key, error: null, successed: null};
var queue = new Queue(function() {
result.error = arguments;
checkAndProcessDone();
}, function() {
result.successed = arguments;
checkAndProcessDone();
});
array.push(queue);
results.push(result);
return queue;
};
this.execute = function() {
if(executing)
throw new Error("The QueueArray already executed");
executing = true;
if(array.length < 1)
callback_done.apply(callback_thisArg, [results]);
else {
for(var i = 0; i < array.length; ++i)
array[i].execute();
}
};
}
module.exports.createConcurrentQueue = function(callback_done, callback_thisArg) {
return new ConcurrentQueue(callback_done, callback_thisArg);
};