-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
430 lines (376 loc) · 13.1 KB
/
main.js
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/**
* @title YAFF (Yet Another Flow Framework)
* @author Daniil Sobol
* @license MIT
* @overview This library is intended to replace unsupported and abandoned node-seq (https://github.com/substack/node-seq/). It tries to be as compatible as possible. But it doesn't copy some weird behaviour of original library. So, in some complex cases it can't be drop-in replacement and requires you to rewrite implementation-dependent code. This library is much more simple and optimised compared to original one. Piece of pure awesomeness, I'd say.
* YAFF is an asynchronous flow control library with a chainable interface for sequential and parallel actions. Even the error handling is chainable. Each action in the chain operates on a stack of values. Unlike Seq YAFF doesn't have variables hash and operates on plain old arguments stack only, if you need to modify something please use map/filter methods.
* Example:
*
var fs = require('fs');
YAFF(['./', '../'])
.par(function (path) {
fs.readdir(path, this);
})
.par(function (path) {
fs.readdir(path, this);
})
.flatten()
.parMap(function (file) {
fs.stat(__dirname + '/' + file, this);
})
.map(function (stat) {
return stat.size;
})
.unflatten()
.finally(function (e, sizes) {
log(sizes);
});
**/
var ins = require('util').inspect;
var log = console.log;
var slice = Array.prototype.slice.call.bind(Array.prototype.slice);
const PAR = 'par';
const SEQ = 'seq';
const FIN = 'fin';
/**
* @module
*
* Each method executes callbacks with a context (its ```this```) described in the next section. Every method returns ```this```.
* Whenever ```this()``` is called with a non-falsy first argument, the error value propagates down to the ```finally``` block, skipping over all actions in between. There is an implicit ```finally``` at the end of all chains that just throws error away.
*/
/**
* @function YAFF
* @param {Array} initialStack=[] Initial stack of arguments
* The constructor function creates a new ```YAFF``` chain with the methods described below. The optional array argument becomes the new context stack.
*/
var YAFF = module.exports = function YAFF(initialStack) {
if (!(this instanceof YAFF))
return new YAFF(initialStack);
var self = this;
this.stack = [];
this.queue = [];
this.fin;
this.lastError;
this.concurrencyLevel = 0;
this.args = initialStack instanceof Array ? initialStack:[];
this.hash = {};
process.nextTick(function waitForStack() {
if (self.stack.length)
self.conveyor();
else
setImmediate(waitForStack);
});
};
//Handlers-----------------------------------------------------------------
YAFF.prototype.handlersMap = {};
YAFF.prototype.handlersMap[SEQ] = function (self, currItem) {
currItem = self.stack.shift();
executor(currItem, self);
};
YAFF.prototype.handlersMap[PAR] = function (self, currItem) {
while (self.stack.length && self.stack[0].type == PAR) {
currItem = self.stack.shift();
if (self.concurrencyLevel >= currItem.limit)
self.queue.push(currItem);
else
executor(currItem, self, true);
}
self.args = [];
};
YAFF.prototype.handlersMap[FIN] = function (self) {
self.stack.shift();
self.finHandler();
};
//System methods-------------------------------------------------------------------------
YAFF.prototype.conveyor = function () {
var currItem = this.stack[0];
if (currItem)
this.handlersMap[currItem.type](this, currItem);
};
var executor = function (currItem, self, merge) {
self.concurrencyLevel++;
self.lastError = undefined;
var cb = function (e) {
self.concurrencyLevel--;
if ((e || self.lastError) && !self.concurrencyLevel && currItem.type == 'par')
return self.errHandler([[].concat(self.lastError, e)]);
if (e) {
if (self.concurrencyLevel) {
if (!self.lastError)
self.lastError = [];
return self.lastError.push(e);
}
if (!self.lastError)
self.lastError = e;
return self.errHandler([e]);
} else {
var ret = slice(arguments, 1);
if (merge)
self.args[currItem.position] = ret.length > 1 ? ret:ret[0];
else
self.args = ret;
}
if (self.queue.length) {
var newItem = self.queue.pop();
return executor(newItem, self, true);
}
if (!self.concurrencyLevel)
process.nextTick(function () {
self.conveyor();
});
};
process.nextTick((function (cb, args) {
cb.args = args;
cb.hash = self.hash;
return function () {
currItem.fn.apply(cb, args);
};
})(cb, self.args.slice()));
};
YAFF.prototype.errHandler = function (e) {
this.stack = [];
if (this.fin)
this.finHandler(e);
else
throw e;
};
YAFF.prototype.finHandler = function (e) {
e = e || this.lastError;
this.fin.apply(this.fin, [].concat(e, e ? undefined:this.args));
};
//Interface methods--------------------------------------------------------------------------------------------------------------------------
/**
* This eponymous function executes actions sequentially. Once all running parallel actions are finished executing, the supplied callback is ```apply()```'d with the context stack.
* To execute the next action in the chain, call ```this()```. The first argument must be the error value. The rest of the values will become the stack for the next action in the chain and are also available at ```this.args```.
* @param {function} callback Function to be executed sequentially
* */
YAFF.prototype.seq = function (fn) {
this.stack.push({fn: fn, type: SEQ});
return this;
};
YAFF.prototype.seq_ = function (fn) {
var args = slice(arguments, 1);
return this.seq(function () { fn.apply(fn, (args.length ? args:this.args).concat(this)); });
};
YAFF.prototype.mseq = function (arr) {
var fn;
while (fn = arr.pop()) //jshint ignore:line
this.seq(fn);
return this;
};
/**
* Use par to execute actions in parallel. Chain multiple parallel actions together and collect all the responses on the stack with a sequential operation like seq.
* Each par sets one element in the stack with the second argument to ```this()``` in the order in which it appears, so multiple pars can be chained together.
* Like with seq, the first argument to ```this()``` should be the error value and the second will get pushed to the stack.
* @param {function} callback Function to be executed in parallel
* */
YAFF.prototype.par = function (fn) {
if (this.stack.length && this.stack[this.stack.length - 1].type == PAR)
this.stack.push({fn: fn, type: PAR, position: this.stack[this.stack.length - 1].position + 1});
else
this.stack.push({fn: fn, type: PAR, position: 0});
return this;
};
YAFF.prototype.par_ = function (fn) {
var args = slice(arguments, 1);
return this.par(function () { fn.apply(fn, (args.length ? args:this.args).concat(this)); });
};
YAFF.prototype.mpar = function (arr) {
var fn;
while (fn = arr.pop()) //jshint ignore:line
this.par(fn);
return this;
};
/**
* Finalizes the chain. Handles errors as well as results and fires provided callback in nodejs manner, so first argument becomes error (may be ```undefined``` if everything is ok) and the rest arguments are results (may be ```undefined``` too if there is an error). ```finally``` is a syncronous sequential action. You can only have one ```finally``` block per chain and it should be in the very end of it.
*
* It's handly if you use it inside asyncronous functions like that:
* ```javascript
* var myAsyncFunction = funtion(callback) {
* YAFF()
* .par(...)
* .par(...)
* .seq(...)
* .finally(callback)
* }
* ```
* @param {function} callback Function to be executed at the very end of the chain
*/
YAFF.prototype.finally = function (fn) {
this.stack.push({fn: this.fin = fn, type: FIN});
return undefined;
};
YAFF.prototype.limit = function (limit) {
if (this.stack.length)
this.stack[this.stack.length - 1].limit = limit;
return this;
};
YAFF.prototype.iterate = function (method, fn, limit) {
limit = typeof limit == 'number' ? limit:Infinity;
this.args.forEach(function (item, index, args) {
method.call(this, function () { fn.call(this, item, index, args, this); }).limit(limit);
}, this);
return this;
};
YAFF.prototype.forEach = function (fn, limit) {
return this.seq(function () {
YAFF(this.args).iterate(YAFF.prototype.par, fn, limit).finally(this);//TODO pass errors to main chain
this.apply(this, [null].concat(this.args));
});
};
YAFF.prototype.seqEach = function (fn) {
return this.seq(function () {
YAFF(this.args).iterate(YAFF.prototype.seq, fn).set(this.args).finally(this);
});
};
YAFF.prototype.parEach = function (fn, limit) {
return this.seq(function () {
YAFF(this.args).iterate(YAFF.prototype.par, fn, limit).set(this.args).finally(this);
});
};
YAFF.prototype.seqMap = function (fn) {
return this.seq(function () {
var stack = [];
YAFF(this.args).iterate(YAFF.prototype.seq, function (item, index, args, cb) {
fn.call(function (e, ret) {
cb(e, stack.push(ret));
}, item, index, args);
}).set(stack).finally(this);
});
};
YAFF.prototype.parMap = function (fn, limit) {
return this.seq(function () {
YAFF(this.args).iterate(YAFF.prototype.par, fn, limit).finally(this);
});
};
YAFF.prototype.seqFilter = function (fn) {
return this.seq(function () {
var stack = [];
YAFF(this.args).iterate(YAFF.prototype.seq, function (item, index, args, cb) {
fn.call(function (e, ret) {
if (ret && !e)
stack.push(item);
cb();
}, item, index, args);
}).set(stack).finally(this);
});
};
YAFF.prototype.parFilter = function (fn, limit) {
return this.seq(function () {
var stack = [];
YAFF(this.args).iterate(YAFF.prototype.par, function (item, index, args, cb) {
fn.call(function (e, ret) {
if (ret && !e)
stack.push(item);
cb();
}, item, index, args);
}, limit).set(stack).finally(this);
});
};
YAFF.prototype.map = function (fn, thisArg) {
return this.seq(function () {
this.apply(this, [null].concat(this.args.map(fn, thisArg || this)));
});
};
YAFF.prototype.filter = function (fn, thisArg) {
return this.seq(function () {
this.apply(this, [null].concat(this.args.filter(fn, thisArg || this)));
});
};
YAFF.prototype.reduce = function (fn, initialValue) {
return this.seq(function () {
this.apply(this, [null].concat(this.args.reduce(fn, initialValue)));
});
};
YAFF.prototype.flatten = function (fully) {
return this.seq(function () {
this.apply(this, [null].concat(this.args.reduce(function reducer(a, b) {
return a.concat(fully && b instanceof Array ? b.reduce(reducer, []):b);
}, [])));
});
};
YAFF.prototype.unflatten = function () {
return this.seq(function () {
this.apply(this, [null, this.args]);
});
};
YAFF.prototype.extend = function (arr) {
return this.seq(function () {
this.apply(this, [null].concat(this.args, arr));
});
};
YAFF.prototype.set = function (arr) {
return this.seq(function () {
this.apply(this, [null].concat(arr));
});
};
YAFF.prototype.empty = function () {
return this.seq(function () {
this();
});
};
YAFF.prototype.push = function (/*args*/) {
return this.seq(function () {//TODO seq -> seq_
this.apply(this, [null].concat(this.args, slice(arguments)));
});
};
YAFF.prototype.pop = function () {
return this.seq(function () {
this.apply(this, [null].concat(this.args.slice(0, -1)));
});
};
YAFF.prototype.shift = function () {
return this.seq(function () {
this.apply(this, [null].concat(this.args.slice(1)));
});
};
YAFF.prototype.unshift = function (/*args*/) {
return this.seq(function () {
this.apply(this, [null].concat(slice(arguments), this.args));
});
};
YAFF.prototype.splice = function (index, howMany, toAppend) {
return this.seq(function () {
Array.prototype.splice.apply(this.args, [index, howMany].concat(toAppend instanceof Array ? toAppend:[toAppend]));
this.apply(this, [null].concat(this.args));
});
};
YAFF.prototype.reverse = function () {
return this.seq(function () {
this.apply(this, [null].concat(this.args.reverse()));
});
};
YAFF.prototype.save = function (name) {
return this.seq(function () {
this.hash[name || '_'] = this.args;
this.apply(this, [null].concat(this.args));
});
};
YAFF.prototype.load = function (name) {
return this.seq(function () {
this.apply(this, [null].concat(this.hash[name || '_']));
});
};
YAFF.prototype.dummy = function () {
return this.seq(function () {
this.apply(this, [null].concat(this.args));
});
};
YAFF.prototype.apply = function (fn) {
return this.seq(function () {
this.apply(this, [null].concat(fn.apply(fn, this.args)));
});
};
YAFF.prototype.debug = function (fn) {
var self = this;
var defaultDebug = function () {
this.apply(this, [null].concat(this.args));
log('........................................');
log('->FUN STACK:');
log(ins(self.stack));
log('->ARG STACK:');
log(ins(self.args));
log('........................................');
};
return this.seq(fn || defaultDebug);
};