This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flw.js
389 lines (332 loc) · 9.76 KB
/
flw.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
'use strict';
(function () {
const root = this;
// Globals
let callFn = _callSetTimeout;
const fnMap = {};
const ourContextKeys = [
// methods
'_store',
'_stop',
'_stopped',
'_clean',
// deprecated
'_flw_store'
];
// in NodeJS ?
if (typeof require === 'function') {
callFn = setImmediate;
}
/**
* Call functions in order of the array
* @param {function[]} fns Array of function to execute
* @param {Object} [context] The initial context object (optional)
* @param {String} [key] context key to return to done()
* @param {function} done callback
*/
fnMap.series = function series(fns, context, returnKey, done) {
if (typeof done !== 'function') {
if (typeof context === 'function') {
done = context; returnKey = undefined; context = {};
} else if (typeof returnKey === 'function') {
done = returnKey;
if (typeof context === 'object') {
returnKey = undefined;
} else {
returnKey = context; context = {};
}
}
}
let fnIterator = 0;
const numTodo = fns.length;
_checkContext(context);
if (numTodo <= 0) return seriesDone(null);
return callFunction();
function callFunction() {
if (context._stopped) return seriesDone(null, null);
callFn(fns[fnIterator], context, seriesDone);
}
function seriesDone(err) {
if (err) return done(err, context);
if (++fnIterator >= numTodo) {
return done(null, returnKey ? context[returnKey] : context);
}
return callFunction();
}
};
/**
* Call functions in parallel
* @param {function[]} fns Array of function to exectute
* @param {Object} [context] The initial context object (optional)
* @param {String} [key] context key to return to done()
* @param {function} done callback
*/
fnMap.parallel = function parallel(fns, context, returnKey, done) {
if (typeof done !== 'function') {
if (typeof context === 'function') {
done = context; returnKey = undefined; context = {};
} else if (typeof returnKey === 'function') {
done = returnKey;
if (typeof context === 'object') {
returnKey = undefined;
} else {
returnKey = context; context = {};
}
}
}
let numDone = 0;
const numTodo = fns.length;
let doneCalled = false;
_checkContext(context);
if (numTodo <= 0) return parallelDone(null);
fns.forEach(function (fn) {
return callFn(fn, context, onCallDone);
});
function onCallDone(err) {
if (err) return parallelDone(err);
if (++numDone >= numTodo) return parallelDone(err);
}
function parallelDone(err) {
// We cannot call done twice, a possible error would be lost here
if (doneCalled) return;
doneCalled = true;
return done(err || null, returnKey ? context[returnKey] : context);
}
};
/**
* Returns wrapped regular function that stores the result on the context key
* @param {function} fn function to wrap
* @param {any[]} [args] Array of arguments to pass (optional)
* @param {String} [key] name of context key to store the result in (optional)
*/
function wrap(fn, args, key) {
const self = this;
if (key === undefined && typeof(args) === 'string') {
key = args;
args = [];
}
if (!args) args = [];
return function wrapper(context, cb) {
const copyArgs = args.slice(args);
copyArgs.unshift(self);
copyArgs.push(onWrappedDone);
return fn.bind.apply(fn, copyArgs)();
function onWrappedDone(err, result) {
if (err) return cb(err);
if (key) context[key] = result;
return cb(null);
}
};
}
/**
* Calls fn with every item in the array
* @param {any[]} items Array items to process
* @param {Number} [numParallel] Limit parallelisation (default: 3)
* @param {function} fn function call for each item
* @param {function} done callback
*/
function each(items, numParralel, fn, done) {
if (done === undefined) {
done = fn; fn = numParralel;
numParralel = 3;
}
if (numParralel <= 0) numParralel = 1;
let doing = 0;
let numProcessing = 0;
let numDone = 0;
const numTotal = items.length;
const results = [];
return nextItem();
function nextItem() {
// We done-check first in case of emtpty array
if (numDone >= numTotal) return done(null, results);
// Batch (or call next item)
while (doing < numTotal && numProcessing < numParralel) {
callFn(fn, items[doing++], onDone);
numProcessing++;
}
return;
// All done
function onDone(err, result) {
if (err) return done(err);
results.push(result);
numProcessing--;
numDone++;
return nextItem();
}
}
}
/**
* Calls fn with every item in the array
* The items will be processed one at a time, preserving the result order
* @param {any[]} items Array items to process
* @param {function} fn function call for each item
* @param {function} done callback
*/
function eachSeries(items, fn, done) {
return each(items, 1, fn, done);
}
/**
* Calls fn x times (with index)
* @param {Number} num number of times to call fn
* @param {function} fn function call for each item
* @param {function} done callback
*/
function n(num, fn, done) {
const results = [];
return nextItem();
function nextItem() {
if (results.length >= num) return done(null, results);
callFn(fn, results.length, function (err, result) {
if (err) return done(err);
results.push(result);
return nextItem();
});
}
}
/**
* Calls fn x times (without index, deprecated: Use .n)
* @deprecated use .n()
* @param {Number} [num] number of times to call fn
* @param {function} fn function call for each item
* @param {function} done callback
*/
function times(num, fn, done) {
const results = [];
return nextItem();
function nextItem() {
if (results.length >= num) return done(null, results);
callFn(fn, function (err, result) {
if (err) return done(err);
results.push(result);
return nextItem();
});
}
}
/**
* build the list of exposed methods into the .make syntax
*/
function make() {
// create a map of all flow functions wrapped by _make
const makeFnMap = {};
Object.keys(fnMap).forEach(function (key) {
makeFnMap[key] = _make(fnMap[key]);
});
return makeFnMap;
// takes a function and wraps it so that execution is 'postponed'
function _make(fn) {
// the user calls this function, e.g. flw.make.series(...)
return function madeFunction(fns, context, returnKey) {
if (typeof context === 'string') {
returnKey = context; context = {};
}
// this function is consumed by flw
return function flowFunction(context, cb) {
// when passed from a flw flow it's called with a premade context
// if called directly, create a new context
if (cb === undefined && typeof context === 'function') {
cb = context;
context = {};
_checkContext(context);
}
if (typeof cb !== 'function') {
throw new Error('flw: .make - cb !== function');
}
return fn(fns, context, returnKey, cb);
};
};
}
}
/**
* Call functions with setTimeout (for browser support)
* @private
*/
function _callSetTimeout(fn, context, cb) {
return setTimeout(fn, 0, context, cb);
}
/**
* Ensures a enrichched flw context when a flow is starting
* @private
*/
function _checkContext(c) {
if (c.hasOwnProperty('_stopped')) return; // Already done?
c._stopped = null;
// Indicate that we gracefully stop
// if set, stops the flow until we are back to the main callback
function _flw_stop(reason, cb) {
if (!cb && typeof reason === 'function') {
cb = reason;
reason = 'stopped';
}
c._stopped = reason;
return cb();
}
Object.defineProperty(c, '_stop', {
enumerable: false,
configurable: false,
writable: false,
value: _flw_stop,
});
// Stores the data returned from the callback in the context with key 'key'
// then calls the callback
function _flw_store(key, cb) {
const self = this;
const fn = function (err, data) {
if (err) return cb(err);
self[key] = data;
return cb();
};
return fn;
}
Object.defineProperty(c, '_store', {
enumerable: false,
configurable: false,
value: _flw_store,
});
// Cleans all flw related properties from the context object
function _flw_clean() {
const self = this;
const contextCopy = {};
Object.keys(this).forEach(function (k) {
if (ourContextKeys.indexOf(k) !== -1) return;
contextCopy[k] = self[k];
});
return contextCopy;
}
Object.defineProperty(c, '_clean', {
enumerable: false,
configurable: false,
value: _flw_clean,
});
// compatibilty for a while
Object.defineProperty(c, '_flw_store', {
enumerable: false,
configurable: false,
writable: false,
value: _flw_store,
});
return c;
}
/**
* Export
*/
const flw = {};
Object.keys(fnMap).forEach(function (key) {
flw[key] = fnMap[key];
});
flw.make = make();
flw.wrap = wrap;
flw.eachSeries = eachSeries;
flw.each = each;
flw.n = n;
flw.times = times;
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = flw;
}
exports.flw = flw;
}
else {
root.flw = flw;
}
}).call(this);