-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (51 loc) · 2.02 KB
/
index.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
let Q= require("q");
module.exports = {
/**
* Batches call to the delegate in batchSize
* @batchSize The batch size
* @parameters An array of parameters, this array should be the size of total calls that are batched.
* Each delegate call will contain an item from the parameters at given index
* @delegate The delegate to call *
*/
batchExecute: (nodeFunc, parameters, batchSize, partial) => {
if(!parameters || !nodeFunc) {
return Q.reject("Invalid parameters or delegate value.");
}
//We allow 0 or more of batch size to allow for some integration scenarios
if(batchSize < 0) {
return Q.reject("Invalid batch size.");
}
if(batchSize > parameters.length) {
return Q.reject("Not sufficient parameters");
}
let currentBatch = 1;
return call(nodeFunc, parameters, batchSize, partial, currentBatch, []);
}
}
function call(nodeFunc, parameters, batchSize, partial, currentBatch, outputs) {
let totalBatches = Math.ceil(parameters.length/batchSize);
let promises = [];
let start = (currentBatch -1) * batchSize;
let end = start+ batchSize;
for(let i=start; i <start + batchSize && i <parameters.length;i++) {
promises.push(Q.fcall(nodeFunc, ...parameters[i]));
}
let errors=[];
return Q.allSettled(promises).then((results) => {
let failed = false;
for(let j=0;j<results.length;j++) {
failed = failed || results[j].state !== "fulfilled";
outputs.push({
state: results[j].state === "fulfilled",
value: results[j].value
});
}
if(failed && partial){
return Q.reject(outputs);
}
if(currentBatch === totalBatches) {
return Q(outputs);
}
return call(nodeFunc, parameters, batchSize, partial, currentBatch+1, outputs);
});
}