forked from kaelzhang/neuron.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
284 lines (219 loc) · 6.71 KB
/
queue.js
File metadata and controls
284 lines (219 loc) · 6.71 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* module asqueue
* author Kael Zhang
Asynchronous and Synchronous Queue:
- Converter: put all specified methods into an executing queue before initialization methods completed
- Runner: execute a specified list of methods
which could:
- keep the executing ORDER even if the queue is mixed with both asynchronous and synchronous methods
- make sure method A will be executed before method B if assigned to
- make sure a method will be executed only once if specified
<code>
// .plugin method is an asynchronous method, but .init method relies on the effect which the .plugin method caused
new Switch().plugin('carousel').init({...});
</code>
example:
<code>
var ASQ = require('until/asqueue'),
asq_r,
module = {
a: function(){
log('a');
},
b: function(){
log('b');
asyncMethod(function(){
log('b callback');
asq_r.resume();
});
}
}
asq_r = new ASQ.Runner([
{
method: 'b',
auto: false
},
'a'
], module);
asq_r.run();
</code>
result:
b
b callback
a
*/
function NOOP(){};
var
Queue = {};
// meta prototypes
Queue_meta = {
Runner: {
/**
* run the list of configured methods
*/
run: function(queue){
var self = this;
self._stack = self._sd( queue ? NR.makeArray(queue) : self._presetItems );
self.resume();
},
stop: function(){
this._stack.length = 0;
},
// santitize data
_sd: function(queue){
var self = this,
ret = [];
queue.forEach(function(q){
ret.push(self._santitize(q));
});
return ret;
}
},
Converter: {
/**
* make all specified method queue-supported
*/
on: function(){
var self = this, host = self.host;
self._items = {};
self._history = [];
if(host){
self._presetItems.forEach(function(i){
i && self._add(i);
});
}
self._converted = true;
return self;
},
/**
* recover the converted methods
*/
off: function(){
var identifier,
self = this,
host = self.host;
for(name in self._items){
delete host[name];
host[name] = self._items[name];
}
self._clean();
self._converted = false;
return self;
},
_clean: function(){
var self = this;
self._items = {};
self._stack.length = self._history.length = 0;
},
_add: function(obj){
var self = this,
host = self.host,
identifier;
if(self._converted || !host) return self;
obj = self._santitize(obj);
identifier = obj.id;
delete obj.id;
if(identifier){
self._items[identifier] = obj.method;
host[identifier] = function(){
var id = identifier, clone, mix;
if(
self._history.indexOf(obj.before) === -1 &&
(!obj.once ||
self._history.indexOf(id) === -1
)
){
mix = NR.mix;
clone = mix({}, obj);
clone.args = arguments;
self._stack.push(clone);
self._history.push(id);
}
if(obj.once){
self._items[id] = NOOP;
}
// avoid recursive invocation
setTimeout(function(){ self._next(); }, 0);
return host; // chain
}
}
}
}
},
Queue_proto = {
_stack: [],
/**
* @param {Array.<function(),string,Object>} items
{function()}
{string}
{Object} detail configuration for each method
{
auto: {boolean}, default to true
once: {boolean}, default to false
method: {function()|string}
args: {Array} arguments
}
* @param {Object} host
*/
initialize: function(items, host){
var self = this;
self._presetItems = NR.makeArray(items);
self.host = host || null;
},
/**
* resume the paused executing queue
*/
resume: function(){
var self = this;
self.processing = false;
return self._next();
},
// apply default settings
// 'method' -> {name: 'method'}
_santitize: function(obj, undef){
var self = this,
host = self.host,
mix = NR.mix;
if(NR.isFunction(obj)){
obj = {
method: obj
};
}else if(NR.isPlainObject(obj)){
obj = mix(obj, self._santitize(obj.method), true, ['method', 'bind', 'id']);
host && !('bind' in obj) && (obj.bind = host);
}else if(NR.isString(obj) && host){
obj = {
method: host[obj],
bind: host,
id: obj
};
}else{
obj = {};
}
return mix({
auto: true,
once: false
}, obj);
},
_next: function(){
var self = this,
current, fn;
if(!self.processing && (current = self._stack.shift()) && (fn = current.method)){
self.processing = true;
fn.apply(current.bind || null, current.args || []);
return current.auto && self.resume();
}
}
};
// Queue.Runner
// Queue.Converter
['Runner', 'Converter'].forEach(function(type){
Queue[type] = NR.Class( NR.mix(Queue_meta[type], Queue_proto) );
});
module.exports = Queue;
/**
change log:
TODO:
A. add judger instead of property 'once', and also improve it.
B. refractor, splitting basic functionalities of queue
*/