promise-parallel-throttle
Advanced tools
Comparing version 2.5.0 to 3.0.0
@@ -10,2 +10,8 @@ export interface Result<T> { | ||
} | ||
export interface Options { | ||
maxInProgress?: number; | ||
failFast?: boolean; | ||
progressCallback?: <T>(result: Result<T>) => void; | ||
nextCheck?: nextTaskCheck; | ||
} | ||
export declare type Task<T> = () => Promise<T>; | ||
@@ -16,26 +22,20 @@ export declare type Tasks<T> = Array<Task<T>>; | ||
* Raw throttle function, which can return extra meta data. | ||
* @param tasks array[] of functions | ||
* @param maxInProgress integer with the max amount of tasks to be run in parallel | ||
* @param failFast boolean if true, do fail-fast behaviour (see Promise.all() documentation) | ||
* @param progressCallback function which can be used to see the progress | ||
* @param nextCheck | ||
* @param tasks required array of tasks to be executed | ||
* @param options Options object | ||
* @returns {Promise} | ||
*/ | ||
export declare function raw<T>(tasks: Tasks<T>, maxInProgress?: number, failFast?: boolean, progressCallback?: (result: Result<T>) => void, nextCheck?: nextTaskCheck): Promise<Result<T>>; | ||
export declare function raw<T>(tasks: Tasks<T>, options?: Options): Promise<Result<T>>; | ||
/** | ||
* Simply run all the promises after each other, so in synchronous manner | ||
* @param tasks required array of tasks to be executed | ||
* @param failFast optional boolean if we directly reject on a single error defaults to true | ||
* @param progressCallback optional function to be run to get status updates | ||
* @param nextCheck function which should return a promise, when resolved the next task will spawn | ||
* @param options Options object | ||
* @returns {Promise} | ||
*/ | ||
export declare function sync<T>(tasks: Tasks<T>, failFast?: boolean, progressCallback?: () => void, nextCheck?: nextTaskCheck): Promise<T[]>; | ||
export declare function sync<T>(tasks: Tasks<T>, options?: Options): Promise<T[]>; | ||
/** | ||
* Exposes the same behaviour as Promise.All(), but throttled! | ||
* @param tasks required array of tasks to be executed | ||
* @param maxInProgress optional integer max amount of parallel tasks to be run defaults to 5 | ||
* @param failFast optional boolean if we directly reject on a single error defaults to true | ||
* @param progressCallback optional function to be run to get status updates | ||
* @param nextCheck function which should return a promise, when resolved the next task will spawn | ||
* @param options Options object | ||
* @returns {Promise} | ||
*/ | ||
export declare function all<T>(tasks: Tasks<T>, maxInProgress?: number, failFast?: boolean, progressCallback?: () => void, nextCheck?: nextTaskCheck): Promise<T[]>; | ||
export declare function all<T>(tasks: Tasks<T>, options?: Options): Promise<T[]>; |
@@ -5,15 +5,33 @@ "use strict"; | ||
/** | ||
* Default checker which validates if a next task should begin. | ||
* This can be overwritten to write own checks for example checking the amount | ||
* of used ram and waiting till the ram is low enough for a next task. | ||
* | ||
* It should always resolve with a boolean, either `true` to start a next task | ||
* or `false` to stop executing a new task. | ||
* | ||
* If this method rejects, the error will propagate to the caller | ||
* @param status | ||
* @param tasks | ||
* @returns {Promise} | ||
*/ | ||
var defaultNextTaskCheck = function (status, tasks) { | ||
return new Promise(function (resolve, reject) { | ||
resolve(status.amountStarted < tasks.length); | ||
}); | ||
}; | ||
var DEFAULT_OPTIONS = { | ||
maxInProgress: DEFAULT_MAX, | ||
failFast: false, | ||
nextCheck: defaultNextTaskCheck, | ||
}; | ||
/** | ||
* Raw throttle function, which can return extra meta data. | ||
* @param tasks array[] of functions | ||
* @param maxInProgress integer with the max amount of tasks to be run in parallel | ||
* @param failFast boolean if true, do fail-fast behaviour (see Promise.all() documentation) | ||
* @param progressCallback function which can be used to see the progress | ||
* @param nextCheck | ||
* @param tasks required array of tasks to be executed | ||
* @param options Options object | ||
* @returns {Promise} | ||
*/ | ||
function raw(tasks, maxInProgress, failFast, progressCallback, nextCheck) { | ||
if (maxInProgress === void 0) { maxInProgress = DEFAULT_MAX; } | ||
if (failFast === void 0) { failFast = false; } | ||
if (nextCheck === void 0) { nextCheck = defaultNextTaskCheck; } | ||
function raw(tasks, options) { | ||
return new Promise(function (resolve, reject) { | ||
var myOptions = Object.assign({}, DEFAULT_OPTIONS, options); | ||
var result = { | ||
@@ -45,3 +63,3 @@ amountDone: 0, | ||
result.amountRejected++; | ||
if (failFast) { | ||
if (myOptions.failFast === true) { | ||
failedFast = true; | ||
@@ -60,8 +78,8 @@ return reject(result); | ||
//make sure no more tasks are spawned when we failedFast | ||
if (failedFast) { | ||
if (failedFast === true) { | ||
return; | ||
} | ||
result.amountDone++; | ||
if (progressCallback) { | ||
progressCallback(result); | ||
if (typeof myOptions.progressCallback === 'function') { | ||
myOptions.progressCallback(result); | ||
} | ||
@@ -78,3 +96,3 @@ if (result.amountDone === tasks.length) { | ||
//check if we can execute the next task | ||
nextCheck(result, tasks) | ||
myOptions.nextCheck(result, tasks) | ||
.then(function (canExecuteNextTask) { | ||
@@ -85,6 +103,9 @@ if (canExecuteNextTask === true) { | ||
} | ||
else { | ||
taskDone(); | ||
} | ||
}, reject); | ||
}; | ||
//spawn the first X task | ||
for (var i = 0; i < Math.min(maxInProgress, tasks.length); i++) { | ||
for (var i = 0; i < Math.min(myOptions.maxInProgress, tasks.length); i++) { | ||
amountQueued++; | ||
@@ -97,31 +118,11 @@ nextTask(); | ||
/** | ||
* Default checker which validates if a next task should begin. | ||
* This can be overwritten to write own checks for example checking the amount | ||
* of used ram and waiting till the ram is low enough for a next task. | ||
* | ||
* It should always resolve with a boolean, either `true` to start a next task | ||
* or `false` to stop executing a new task. | ||
* | ||
* If this method rejects, the error will propagate to the caller | ||
* @param status | ||
* @param tasks | ||
* @returns {Promise} | ||
*/ | ||
var defaultNextTaskCheck = function (status, tasks) { | ||
return new Promise(function (resolve, reject) { | ||
resolve(status.amountStarted < tasks.length); | ||
}); | ||
}; | ||
/** | ||
* Simply run all the promises after each other, so in synchronous manner | ||
* @param tasks required array of tasks to be executed | ||
* @param failFast optional boolean if we directly reject on a single error defaults to true | ||
* @param progressCallback optional function to be run to get status updates | ||
* @param nextCheck function which should return a promise, when resolved the next task will spawn | ||
* @param options Options object | ||
* @returns {Promise} | ||
*/ | ||
function sync(tasks, failFast, progressCallback, nextCheck) { | ||
if (failFast === void 0) { failFast = true; } | ||
if (nextCheck === void 0) { nextCheck = defaultNextTaskCheck; } | ||
function sync(tasks, options) { | ||
return new Promise(function (resolve, reject) { | ||
raw(tasks, 1, failFast, progressCallback, nextCheck) | ||
var myOptions = Object.assign({}, { maxInProgress: 1, failFast: true }, options); | ||
raw(tasks, myOptions) | ||
.then(function (result) { | ||
@@ -143,13 +144,9 @@ resolve(result.taskResults); | ||
* @param tasks required array of tasks to be executed | ||
* @param maxInProgress optional integer max amount of parallel tasks to be run defaults to 5 | ||
* @param failFast optional boolean if we directly reject on a single error defaults to true | ||
* @param progressCallback optional function to be run to get status updates | ||
* @param nextCheck function which should return a promise, when resolved the next task will spawn | ||
* @param options Options object | ||
* @returns {Promise} | ||
*/ | ||
function all(tasks, maxInProgress, failFast, progressCallback, nextCheck) { | ||
if (maxInProgress === void 0) { maxInProgress = DEFAULT_MAX; } | ||
if (failFast === void 0) { failFast = true; } | ||
if (nextCheck === void 0) { nextCheck = defaultNextTaskCheck; } | ||
function all(tasks, options) { | ||
return new Promise(function (resolve, reject) { | ||
raw(tasks, maxInProgress, failFast, progressCallback, nextCheck) | ||
var myOptions = Object.assign({}, { failFast: true }, options); | ||
raw(tasks, myOptions) | ||
.then(function (result) { | ||
@@ -156,0 +153,0 @@ resolve(result.taskResults); |
@@ -1,2 +0,2 @@ | ||
{"/mnt/d/Projecten/Promise-parallel-throttle/src/throttle.ts": {"path":"/mnt/d/Projecten/Promise-parallel-throttle/src/throttle.ts","statementMap":{"0":{"start":{"line":1,"column":13},"end":{"line":1,"column":42}},"1":{"start":{"line":1,"column":42},"end":{"line":1,"column":55}},"2":{"start":{"line":2,"column":0},"end":{"line":2,"column":62}},"3":{"start":{"line":3,"column":18},"end":{"line":3,"column":19}},"4":{"start":{"line":14,"column":4},"end":{"line":14,"column":66}},"5":{"start":{"line":14,"column":36},"end":{"line":14,"column":64}},"6":{"start":{"line":15,"column":4},"end":{"line":15,"column":50}},"7":{"start":{"line":15,"column":31},"end":{"line":15,"column":48}},"8":{"start":{"line":16,"column":4},"end":{"line":16,"column":67}},"9":{"start":{"line":16,"column":32},"end":{"line":16,"column":65}},"10":{"start":{"line":17,"column":4},"end":{"line":88,"column":7}},"11":{"start":{"line":18,"column":21},"end":{"line":26,"column":9}},"12":{"start":{"line":27,"column":8},"end":{"line":29,"column":9}},"13":{"start":{"line":28,"column":12},"end":{"line":28,"column":35}},"14":{"start":{"line":30,"column":25},"end":{"line":30,"column":30}},"15":{"start":{"line":31,"column":27},"end":{"line":31,"column":28}},"16":{"start":{"line":32,"column":26},"end":{"line":55,"column":9}},"17":{"start":{"line":33,"column":12},"end":{"line":54,"column":13}},"18":{"start":{"line":34,"column":16},"end":{"line":49,"column":19}},"19":{"start":{"line":36,"column":20},"end":{"line":36,"column":59}},"20":{"start":{"line":37,"column":20},"end":{"line":37,"column":55}},"21":{"start":{"line":38,"column":20},"end":{"line":38,"column":44}},"22":{"start":{"line":39,"column":20},"end":{"line":39,"column":31}},"23":{"start":{"line":41,"column":20},"end":{"line":41,"column":54}},"24":{"start":{"line":42,"column":20},"end":{"line":42,"column":55}},"25":{"start":{"line":43,"column":20},"end":{"line":43,"column":44}},"26":{"start":{"line":44,"column":20},"end":{"line":47,"column":21}},"27":{"start":{"line":45,"column":24},"end":{"line":45,"column":42}},"28":{"start":{"line":46,"column":24},"end":{"line":46,"column":46}},"29":{"start":{"line":48,"column":20},"end":{"line":48,"column":31}},"30":{"start":{"line":52,"column":16},"end":{"line":52,"column":34}},"31":{"start":{"line":53,"column":16},"end":{"line":53,"column":123}},"32":{"start":{"line":56,"column":23},"end":{"line":72,"column":9}},"33":{"start":{"line":58,"column":12},"end":{"line":60,"column":13}},"34":{"start":{"line":59,"column":16},"end":{"line":59,"column":23}},"35":{"start":{"line":61,"column":12},"end":{"line":61,"column":32}},"36":{"start":{"line":62,"column":12},"end":{"line":64,"column":13}},"37":{"start":{"line":63,"column":16},"end":{"line":63,"column":41}},"38":{"start":{"line":65,"column":12},"end":{"line":67,"column":13}},"39":{"start":{"line":66,"column":16},"end":{"line":66,"column":39}},"40":{"start":{"line":68,"column":12},"end":{"line":71,"column":13}},"41":{"start":{"line":69,"column":16},"end":{"line":69,"column":31}},"42":{"start":{"line":70,"column":16},"end":{"line":70,"column":27}},"43":{"start":{"line":73,"column":23},"end":{"line":82,"column":9}},"44":{"start":{"line":75,"column":12},"end":{"line":81,"column":23}},"45":{"start":{"line":77,"column":16},"end":{"line":80,"column":17}},"46":{"start":{"line":79,"column":20},"end":{"line":79,"column":56}},"47":{"start":{"line":84,"column":8},"end":{"line":87,"column":9}},"48":{"start":{"line":85,"column":12},"end":{"line":85,"column":27}},"49":{"start":{"line":86,"column":12},"end":{"line":86,"column":23}},"50":{"start":{"line":90,"column":0},"end":{"line":90,"column":18}},"51":{"start":{"line":104,"column":27},"end":{"line":108,"column":1}},"52":{"start":{"line":105,"column":4},"end":{"line":107,"column":7}},"53":{"start":{"line":106,"column":8},"end":{"line":106,"column":53}},"54":{"start":{"line":117,"column":4},"end":{"line":117,"column":49}},"55":{"start":{"line":117,"column":31},"end":{"line":117,"column":47}},"56":{"start":{"line":118,"column":4},"end":{"line":118,"column":67}},"57":{"start":{"line":118,"column":32},"end":{"line":118,"column":65}},"58":{"start":{"line":119,"column":4},"end":{"line":131,"column":7}},"59":{"start":{"line":120,"column":8},"end":{"line":130,"column":11}},"60":{"start":{"line":122,"column":12},"end":{"line":122,"column":40}},"61":{"start":{"line":124,"column":12},"end":{"line":129,"column":13}},"62":{"start":{"line":125,"column":16},"end":{"line":125,"column":30}},"63":{"start":{"line":128,"column":16},"end":{"line":128,"column":68}},"64":{"start":{"line":133,"column":0},"end":{"line":133,"column":20}},"65":{"start":{"line":143,"column":4},"end":{"line":143,"column":66}},"66":{"start":{"line":143,"column":36},"end":{"line":143,"column":64}},"67":{"start":{"line":144,"column":4},"end":{"line":144,"column":49}},"68":{"start":{"line":144,"column":31},"end":{"line":144,"column":47}},"69":{"start":{"line":145,"column":4},"end":{"line":145,"column":67}},"70":{"start":{"line":145,"column":32},"end":{"line":145,"column":65}},"71":{"start":{"line":146,"column":4},"end":{"line":158,"column":7}},"72":{"start":{"line":147,"column":8},"end":{"line":157,"column":11}},"73":{"start":{"line":149,"column":12},"end":{"line":149,"column":40}},"74":{"start":{"line":151,"column":12},"end":{"line":156,"column":13}},"75":{"start":{"line":152,"column":16},"end":{"line":152,"column":30}},"76":{"start":{"line":155,"column":16},"end":{"line":155,"column":68}},"77":{"start":{"line":160,"column":0},"end":{"line":160,"column":18}}},"fnMap":{"0":{"name":"raw","decl":{"start":{"line":13,"column":9},"end":{"line":13,"column":12}},"loc":{"start":{"line":13,"column":74},"end":{"line":89,"column":1}},"line":13},"1":{"name":"(anonymous_1)","decl":{"start":{"line":17,"column":23},"end":{"line":17,"column":24}},"loc":{"start":{"line":17,"column":50},"end":{"line":88,"column":5}},"line":17},"2":{"name":"(anonymous_2)","decl":{"start":{"line":32,"column":26},"end":{"line":32,"column":27}},"loc":{"start":{"line":32,"column":43},"end":{"line":55,"column":9}},"line":32},"3":{"name":"(anonymous_3)","decl":{"start":{"line":35,"column":26},"end":{"line":35,"column":27}},"loc":{"start":{"line":35,"column":48},"end":{"line":40,"column":17}},"line":35},"4":{"name":"(anonymous_4)","decl":{"start":{"line":40,"column":19},"end":{"line":40,"column":20}},"loc":{"start":{"line":40,"column":36},"end":{"line":49,"column":17}},"line":40},"5":{"name":"(anonymous_5)","decl":{"start":{"line":56,"column":23},"end":{"line":56,"column":24}},"loc":{"start":{"line":56,"column":35},"end":{"line":72,"column":9}},"line":56},"6":{"name":"(anonymous_6)","decl":{"start":{"line":73,"column":23},"end":{"line":73,"column":24}},"loc":{"start":{"line":73,"column":35},"end":{"line":82,"column":9}},"line":73},"7":{"name":"(anonymous_7)","decl":{"start":{"line":76,"column":22},"end":{"line":76,"column":23}},"loc":{"start":{"line":76,"column":52},"end":{"line":81,"column":13}},"line":76},"8":{"name":"(anonymous_8)","decl":{"start":{"line":104,"column":27},"end":{"line":104,"column":28}},"loc":{"start":{"line":104,"column":52},"end":{"line":108,"column":1}},"line":104},"9":{"name":"(anonymous_9)","decl":{"start":{"line":105,"column":23},"end":{"line":105,"column":24}},"loc":{"start":{"line":105,"column":50},"end":{"line":107,"column":5}},"line":105},"10":{"name":"sync","decl":{"start":{"line":116,"column":9},"end":{"line":116,"column":13}},"loc":{"start":{"line":116,"column":60},"end":{"line":132,"column":1}},"line":116},"11":{"name":"(anonymous_11)","decl":{"start":{"line":119,"column":23},"end":{"line":119,"column":24}},"loc":{"start":{"line":119,"column":50},"end":{"line":131,"column":5}},"line":119},"12":{"name":"(anonymous_12)","decl":{"start":{"line":121,"column":18},"end":{"line":121,"column":19}},"loc":{"start":{"line":121,"column":36},"end":{"line":123,"column":9}},"line":121},"13":{"name":"(anonymous_13)","decl":{"start":{"line":123,"column":11},"end":{"line":123,"column":12}},"loc":{"start":{"line":123,"column":28},"end":{"line":130,"column":9}},"line":123},"14":{"name":"all","decl":{"start":{"line":142,"column":9},"end":{"line":142,"column":12}},"loc":{"start":{"line":142,"column":74},"end":{"line":159,"column":1}},"line":142},"15":{"name":"(anonymous_15)","decl":{"start":{"line":146,"column":23},"end":{"line":146,"column":24}},"loc":{"start":{"line":146,"column":50},"end":{"line":158,"column":5}},"line":146},"16":{"name":"(anonymous_16)","decl":{"start":{"line":148,"column":18},"end":{"line":148,"column":19}},"loc":{"start":{"line":148,"column":36},"end":{"line":150,"column":9}},"line":148},"17":{"name":"(anonymous_17)","decl":{"start":{"line":150,"column":11},"end":{"line":150,"column":12}},"loc":{"start":{"line":150,"column":28},"end":{"line":157,"column":9}},"line":150}},"branchMap":{"0":{"loc":{"start":{"line":14,"column":4},"end":{"line":14,"column":66}},"type":"if","locations":[{"start":{"line":14,"column":4},"end":{"line":14,"column":66}},{"start":{"line":14,"column":4},"end":{"line":14,"column":66}}],"line":14},"1":{"loc":{"start":{"line":15,"column":4},"end":{"line":15,"column":50}},"type":"if","locations":[{"start":{"line":15,"column":4},"end":{"line":15,"column":50}},{"start":{"line":15,"column":4},"end":{"line":15,"column":50}}],"line":15},"2":{"loc":{"start":{"line":16,"column":4},"end":{"line":16,"column":67}},"type":"if","locations":[{"start":{"line":16,"column":4},"end":{"line":16,"column":67}},{"start":{"line":16,"column":4},"end":{"line":16,"column":67}}],"line":16},"3":{"loc":{"start":{"line":27,"column":8},"end":{"line":29,"column":9}},"type":"if","locations":[{"start":{"line":27,"column":8},"end":{"line":29,"column":9}},{"start":{"line":27,"column":8},"end":{"line":29,"column":9}}],"line":27},"4":{"loc":{"start":{"line":33,"column":12},"end":{"line":54,"column":13}},"type":"if","locations":[{"start":{"line":33,"column":12},"end":{"line":54,"column":13}},{"start":{"line":33,"column":12},"end":{"line":54,"column":13}}],"line":33},"5":{"loc":{"start":{"line":44,"column":20},"end":{"line":47,"column":21}},"type":"if","locations":[{"start":{"line":44,"column":20},"end":{"line":47,"column":21}},{"start":{"line":44,"column":20},"end":{"line":47,"column":21}}],"line":44},"6":{"loc":{"start":{"line":58,"column":12},"end":{"line":60,"column":13}},"type":"if","locations":[{"start":{"line":58,"column":12},"end":{"line":60,"column":13}},{"start":{"line":58,"column":12},"end":{"line":60,"column":13}}],"line":58},"7":{"loc":{"start":{"line":62,"column":12},"end":{"line":64,"column":13}},"type":"if","locations":[{"start":{"line":62,"column":12},"end":{"line":64,"column":13}},{"start":{"line":62,"column":12},"end":{"line":64,"column":13}}],"line":62},"8":{"loc":{"start":{"line":65,"column":12},"end":{"line":67,"column":13}},"type":"if","locations":[{"start":{"line":65,"column":12},"end":{"line":67,"column":13}},{"start":{"line":65,"column":12},"end":{"line":67,"column":13}}],"line":65},"9":{"loc":{"start":{"line":68,"column":12},"end":{"line":71,"column":13}},"type":"if","locations":[{"start":{"line":68,"column":12},"end":{"line":71,"column":13}},{"start":{"line":68,"column":12},"end":{"line":71,"column":13}}],"line":68},"10":{"loc":{"start":{"line":77,"column":16},"end":{"line":80,"column":17}},"type":"if","locations":[{"start":{"line":77,"column":16},"end":{"line":80,"column":17}},{"start":{"line":77,"column":16},"end":{"line":80,"column":17}}],"line":77},"11":{"loc":{"start":{"line":117,"column":4},"end":{"line":117,"column":49}},"type":"if","locations":[{"start":{"line":117,"column":4},"end":{"line":117,"column":49}},{"start":{"line":117,"column":4},"end":{"line":117,"column":49}}],"line":117},"12":{"loc":{"start":{"line":118,"column":4},"end":{"line":118,"column":67}},"type":"if","locations":[{"start":{"line":118,"column":4},"end":{"line":118,"column":67}},{"start":{"line":118,"column":4},"end":{"line":118,"column":67}}],"line":118},"13":{"loc":{"start":{"line":124,"column":12},"end":{"line":129,"column":13}},"type":"if","locations":[{"start":{"line":124,"column":12},"end":{"line":129,"column":13}},{"start":{"line":124,"column":12},"end":{"line":129,"column":13}}],"line":124},"14":{"loc":{"start":{"line":143,"column":4},"end":{"line":143,"column":66}},"type":"if","locations":[{"start":{"line":143,"column":4},"end":{"line":143,"column":66}},{"start":{"line":143,"column":4},"end":{"line":143,"column":66}}],"line":143},"15":{"loc":{"start":{"line":144,"column":4},"end":{"line":144,"column":49}},"type":"if","locations":[{"start":{"line":144,"column":4},"end":{"line":144,"column":49}},{"start":{"line":144,"column":4},"end":{"line":144,"column":49}}],"line":144},"16":{"loc":{"start":{"line":145,"column":4},"end":{"line":145,"column":67}},"type":"if","locations":[{"start":{"line":145,"column":4},"end":{"line":145,"column":67}},{"start":{"line":145,"column":4},"end":{"line":145,"column":67}}],"line":145},"17":{"loc":{"start":{"line":151,"column":12},"end":{"line":156,"column":13}},"type":"if","locations":[{"start":{"line":151,"column":12},"end":{"line":156,"column":13}},{"start":{"line":151,"column":12},"end":{"line":156,"column":13}}],"line":151}},"s":{"0":1,"1":1,"2":1,"3":1,"4":15,"5":7,"6":15,"7":7,"8":15,"9":8,"10":15,"11":15,"12":15,"13":1,"14":14,"15":14,"16":14,"17":60,"18":57,"19":45,"20":45,"21":45,"22":45,"23":12,"24":12,"25":12,"26":12,"27":7,"28":7,"29":5,"30":3,"31":3,"32":14,"33":50,"34":8,"35":42,"36":42,"37":4,"38":42,"39":7,"40":35,"41":14,"42":14,"43":14,"44":61,"45":60,"46":60,"47":14,"48":47,"49":47,"50":1,"51":1,"52":56,"53":56,"54":3,"55":3,"56":3,"57":3,"58":3,"59":3,"60":1,"61":2,"62":1,"63":1,"64":1,"65":3,"66":3,"67":3,"68":3,"69":3,"70":3,"71":3,"72":3,"73":1,"74":2,"75":1,"76":1,"77":1},"f":{"0":15,"1":15,"2":60,"3":45,"4":12,"5":50,"6":61,"7":60,"8":56,"9":56,"10":3,"11":3,"12":1,"13":2,"14":3,"15":3,"16":1,"17":2},"b":{"0":[7,8],"1":[7,8],"2":[8,7],"3":[1,14],"4":[57,3],"5":[7,5],"6":[8,42],"7":[4,38],"8":[7,35],"9":[14,21],"10":[60,0],"11":[3,0],"12":[3,0],"13":[1,1],"14":[3,0],"15":[3,0],"16":[3,0],"17":[1,1]},"inputSourceMap":{"version":3,"file":"throttle.js","sourceRoot":"","sources":["file:////mnt/d/Projecten/Promise-parallel-throttle/src/throttle.ts"],"names":[],"mappings":";;AAAA,IAAM,WAAW,GAAG,CAAC,CAAC;AAgBtB;;;;;;;;GAQG;AACH,aAAuB,KAAe,EACf,aAA2B,EAC3B,QAAgB,EAChB,gBAA8C,EAC9C,SAAgC;IAHhC,8BAAA,EAAA,2BAA2B;IAC3B,yBAAA,EAAA,gBAAgB;IAEhB,0BAAA,EAAA,gCAAgC;IACnD,MAAM,CAAC,IAAI,OAAO,CAAC,UAAC,OAAO,EAAE,MAAM;QAC/B,IAAM,MAAM,GAAc;YACtB,UAAU,EAAE,CAAC;YACb,aAAa,EAAE,CAAC;YAChB,cAAc,EAAE,CAAC;YACjB,cAAc,EAAE,CAAC;YACjB,eAAe,EAAE,EAAE;YACnB,eAAe,EAAE,EAAE;YACnB,WAAW,EAAE,EAAE;SAClB,CAAC;QAEF,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC;YACrB,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAC3B,CAAC;QAED,IAAI,UAAU,GAAG,KAAK,CAAC;QACvB,IAAI,YAAY,GAAG,CAAC,CAAC;QACrB,IAAM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C,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YACxD,CAAC;QACL,CAAC,CAAC,CAAC;IACX,CAAC,CAAC,CAAC;AACP,CAAC;AAjBD,kBAiBC","sourcesContent":["const DEFAULT_MAX = 5;\n\nexport interface Result<T> {\n amountDone: number;\n amountStarted: number;\n amountResolved: number;\n amountRejected: number;\n rejectedIndexes: number[];\n resolvedIndexes: number[];\n taskResults: T[];\n}\n\nexport type Task<T> = () => Promise<T>;\nexport type Tasks<T> = Array<Task<T>>;\nexport type nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>) => Promise<boolean>;\n\n/**\n * Raw throttle function, which can return extra meta data.\n * @param tasks array[] of functions\n * @param maxInProgress integer with the max amount of tasks to be run in parallel\n * @param failFast boolean if true, do fail-fast behaviour (see Promise.all() documentation)\n * @param progressCallback function which can be used to see the progress\n * @param nextCheck\n * @returns {Promise}\n */\nexport function raw<T>(tasks: Tasks<T>,\n maxInProgress = DEFAULT_MAX,\n failFast = false,\n progressCallback?: (result: Result<T>) => void,\n nextCheck = defaultNextTaskCheck): Promise<Result<T>> {\n return new Promise((resolve, reject) => {\n const result: Result<T> = {\n amountDone: 0,\n amountStarted: 0,\n amountResolved: 0,\n amountRejected: 0,\n rejectedIndexes: [],\n resolvedIndexes: [],\n taskResults: []\n };\n\n if (tasks.length === 0) {\n return resolve(result);\n }\n\n let failedFast = false;\n let amountQueued = 0;\n const executeTask = (index: number) => {\n if (typeof tasks[index] === 'function') {\n tasks[index]()\n .then((taskResult: T) => {\n result.taskResults[index] = taskResult;\n result.resolvedIndexes.push(index);\n result.amountResolved++;\n taskDone();\n }, error => {\n result.taskResults[index] = error;\n result.rejectedIndexes.push(index);\n result.amountRejected++;\n if (failFast) {\n failedFast = true;\n return reject(result);\n }\n taskDone();\n });\n } else {\n failedFast = true;\n return reject(new Error('tasks[' + index + ']: ' + tasks[index] + ', is supposed to be of type function'));\n }\n };\n\n const taskDone = () => {\n //make sure no more tasks are spawned when we failedFast\n if (failedFast) {\n return;\n }\n\n result.amountDone++;\n if (progressCallback) {\n progressCallback(result);\n }\n if (result.amountDone === tasks.length) {\n return resolve(result);\n }\n if (amountQueued < tasks.length) {\n amountQueued++;\n nextTask();\n }\n };\n\n const nextTask = () => {\n //check if we can execute the next task\n nextCheck(result, tasks)\n .then(canExecuteNextTask => {\n if (canExecuteNextTask === true) {\n //execute it\n executeTask(result.amountStarted++);\n }\n }, reject);\n };\n\n //spawn the first X task\n for (let i = 0; i < Math.min(maxInProgress, tasks.length); i++) {\n amountQueued++;\n nextTask();\n }\n });\n}\n\n/**\n * Default checker which validates if a next task should begin.\n * This can be overwritten to write own checks for example checking the amount\n * of used ram and waiting till the ram is low enough for a next task.\n *\n * It should always resolve with a boolean, either `true` to start a next task\n * or `false` to stop executing a new task.\n *\n * If this method rejects, the error will propagate to the caller\n * @param status\n * @param tasks\n * @returns {Promise}\n */\nconst defaultNextTaskCheck: nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>): Promise<boolean> => {\n return new Promise((resolve, reject) => {\n resolve(status.amountStarted < tasks.length);\n });\n};\n\n/**\n * Simply run all the promises after each other, so in synchronous manner\n * @param tasks required array of tasks to be executed\n * @param failFast optional boolean if we directly reject on a single error defaults to true\n * @param progressCallback optional function to be run to get status updates\n * @param nextCheck function which should return a promise, when resolved the next task will spawn\n */\nexport function sync<T>(tasks: Tasks<T>,\n failFast = true,\n progressCallback?: () => void,\n nextCheck = defaultNextTaskCheck): Promise<T[]> {\n return new Promise((resolve, reject) => {\n raw(tasks, 1, failFast, progressCallback, nextCheck)\n .then((result: Result<T>) => {\n resolve(result.taskResults);\n }, (error: Error|Result<T>) => {\n if (error instanceof Error) {\n reject(error);\n } else {\n reject(error.taskResults[error.rejectedIndexes[0]]);\n }\n });\n });\n}\n\n/**\n * Exposes the same behaviour as Promise.All(), but throttled!\n * @param tasks required array of tasks to be executed\n * @param maxInProgress optional integer max amount of parallel tasks to be run defaults to 5\n * @param failFast optional boolean if we directly reject on a single error defaults to true\n * @param progressCallback optional function to be run to get status updates\n * @param nextCheck function which should return a promise, when resolved the next task will spawn\n */\nexport function all<T>(tasks: Tasks<T>,\n maxInProgress = DEFAULT_MAX,\n failFast = true,\n progressCallback?: () => void,\n nextCheck = defaultNextTaskCheck): Promise<T[]> {\n return new Promise((resolve, reject) => {\n raw(tasks, maxInProgress, failFast, progressCallback, nextCheck)\n .then((result: Result<T>) => {\n resolve(result.taskResults);\n }, (error: Error|Result<T>) => {\n if (error instanceof Error) {\n reject(error);\n } else {\n reject(error.taskResults[error.rejectedIndexes[0]]);\n }\n });\n });\n}\n"]},"_coverageSchema":"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c","hash":"2bd824f876f3a4cdd536f3600f65f942615d500c"} | ||
{"/mnt/d/Projecten/Promise-parallel-throttle/src/throttle.ts": {"path":"/mnt/d/Projecten/Promise-parallel-throttle/src/throttle.ts","statementMap":{"0":{"start":{"line":1,"column":13},"end":{"line":1,"column":42}},"1":{"start":{"line":1,"column":42},"end":{"line":1,"column":55}},"2":{"start":{"line":2,"column":0},"end":{"line":2,"column":62}},"3":{"start":{"line":3,"column":18},"end":{"line":3,"column":19}},"4":{"start":{"line":17,"column":27},"end":{"line":21,"column":1}},"5":{"start":{"line":18,"column":4},"end":{"line":20,"column":7}},"6":{"start":{"line":19,"column":8},"end":{"line":19,"column":53}},"7":{"start":{"line":22,"column":22},"end":{"line":26,"column":1}},"8":{"start":{"line":34,"column":4},"end":{"line":109,"column":7}},"9":{"start":{"line":35,"column":24},"end":{"line":35,"column":67}},"10":{"start":{"line":36,"column":21},"end":{"line":44,"column":9}},"11":{"start":{"line":45,"column":8},"end":{"line":47,"column":9}},"12":{"start":{"line":46,"column":12},"end":{"line":46,"column":35}},"13":{"start":{"line":48,"column":25},"end":{"line":48,"column":30}},"14":{"start":{"line":49,"column":27},"end":{"line":49,"column":28}},"15":{"start":{"line":50,"column":26},"end":{"line":73,"column":9}},"16":{"start":{"line":51,"column":12},"end":{"line":72,"column":13}},"17":{"start":{"line":52,"column":16},"end":{"line":67,"column":19}},"18":{"start":{"line":54,"column":20},"end":{"line":54,"column":59}},"19":{"start":{"line":55,"column":20},"end":{"line":55,"column":55}},"20":{"start":{"line":56,"column":20},"end":{"line":56,"column":44}},"21":{"start":{"line":57,"column":20},"end":{"line":57,"column":31}},"22":{"start":{"line":59,"column":20},"end":{"line":59,"column":54}},"23":{"start":{"line":60,"column":20},"end":{"line":60,"column":55}},"24":{"start":{"line":61,"column":20},"end":{"line":61,"column":44}},"25":{"start":{"line":62,"column":20},"end":{"line":65,"column":21}},"26":{"start":{"line":63,"column":24},"end":{"line":63,"column":42}},"27":{"start":{"line":64,"column":24},"end":{"line":64,"column":46}},"28":{"start":{"line":66,"column":20},"end":{"line":66,"column":31}},"29":{"start":{"line":70,"column":16},"end":{"line":70,"column":34}},"30":{"start":{"line":71,"column":16},"end":{"line":71,"column":123}},"31":{"start":{"line":74,"column":23},"end":{"line":90,"column":9}},"32":{"start":{"line":76,"column":12},"end":{"line":78,"column":13}},"33":{"start":{"line":77,"column":16},"end":{"line":77,"column":23}},"34":{"start":{"line":79,"column":12},"end":{"line":79,"column":32}},"35":{"start":{"line":80,"column":12},"end":{"line":82,"column":13}},"36":{"start":{"line":81,"column":16},"end":{"line":81,"column":51}},"37":{"start":{"line":83,"column":12},"end":{"line":85,"column":13}},"38":{"start":{"line":84,"column":16},"end":{"line":84,"column":39}},"39":{"start":{"line":86,"column":12},"end":{"line":89,"column":13}},"40":{"start":{"line":87,"column":16},"end":{"line":87,"column":31}},"41":{"start":{"line":88,"column":16},"end":{"line":88,"column":27}},"42":{"start":{"line":91,"column":23},"end":{"line":103,"column":9}},"43":{"start":{"line":93,"column":12},"end":{"line":102,"column":23}},"44":{"start":{"line":95,"column":16},"end":{"line":101,"column":17}},"45":{"start":{"line":97,"column":20},"end":{"line":97,"column":56}},"46":{"start":{"line":100,"column":20},"end":{"line":100,"column":31}},"47":{"start":{"line":105,"column":8},"end":{"line":108,"column":9}},"48":{"start":{"line":106,"column":12},"end":{"line":106,"column":27}},"49":{"start":{"line":107,"column":12},"end":{"line":107,"column":23}},"50":{"start":{"line":111,"column":0},"end":{"line":111,"column":18}},"51":{"start":{"line":119,"column":4},"end":{"line":132,"column":7}},"52":{"start":{"line":120,"column":24},"end":{"line":120,"column":88}},"53":{"start":{"line":121,"column":8},"end":{"line":131,"column":11}},"54":{"start":{"line":123,"column":12},"end":{"line":123,"column":40}},"55":{"start":{"line":125,"column":12},"end":{"line":130,"column":13}},"56":{"start":{"line":126,"column":16},"end":{"line":126,"column":30}},"57":{"start":{"line":129,"column":16},"end":{"line":129,"column":68}},"58":{"start":{"line":134,"column":0},"end":{"line":134,"column":20}},"59":{"start":{"line":142,"column":4},"end":{"line":155,"column":7}},"60":{"start":{"line":143,"column":24},"end":{"line":143,"column":70}},"61":{"start":{"line":144,"column":8},"end":{"line":154,"column":11}},"62":{"start":{"line":146,"column":12},"end":{"line":146,"column":40}},"63":{"start":{"line":148,"column":12},"end":{"line":153,"column":13}},"64":{"start":{"line":149,"column":16},"end":{"line":149,"column":30}},"65":{"start":{"line":152,"column":16},"end":{"line":152,"column":68}},"66":{"start":{"line":157,"column":0},"end":{"line":157,"column":18}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":17,"column":27},"end":{"line":17,"column":28}},"loc":{"start":{"line":17,"column":52},"end":{"line":21,"column":1}},"line":17},"1":{"name":"(anonymous_1)","decl":{"start":{"line":18,"column":23},"end":{"line":18,"column":24}},"loc":{"start":{"line":18,"column":50},"end":{"line":20,"column":5}},"line":18},"2":{"name":"raw","decl":{"start":{"line":33,"column":9},"end":{"line":33,"column":12}},"loc":{"start":{"line":33,"column":29},"end":{"line":110,"column":1}},"line":33},"3":{"name":"(anonymous_3)","decl":{"start":{"line":34,"column":23},"end":{"line":34,"column":24}},"loc":{"start":{"line":34,"column":50},"end":{"line":109,"column":5}},"line":34},"4":{"name":"(anonymous_4)","decl":{"start":{"line":50,"column":26},"end":{"line":50,"column":27}},"loc":{"start":{"line":50,"column":43},"end":{"line":73,"column":9}},"line":50},"5":{"name":"(anonymous_5)","decl":{"start":{"line":53,"column":26},"end":{"line":53,"column":27}},"loc":{"start":{"line":53,"column":48},"end":{"line":58,"column":17}},"line":53},"6":{"name":"(anonymous_6)","decl":{"start":{"line":58,"column":19},"end":{"line":58,"column":20}},"loc":{"start":{"line":58,"column":36},"end":{"line":67,"column":17}},"line":58},"7":{"name":"(anonymous_7)","decl":{"start":{"line":74,"column":23},"end":{"line":74,"column":24}},"loc":{"start":{"line":74,"column":35},"end":{"line":90,"column":9}},"line":74},"8":{"name":"(anonymous_8)","decl":{"start":{"line":91,"column":23},"end":{"line":91,"column":24}},"loc":{"start":{"line":91,"column":35},"end":{"line":103,"column":9}},"line":91},"9":{"name":"(anonymous_9)","decl":{"start":{"line":94,"column":22},"end":{"line":94,"column":23}},"loc":{"start":{"line":94,"column":52},"end":{"line":102,"column":13}},"line":94},"10":{"name":"sync","decl":{"start":{"line":118,"column":9},"end":{"line":118,"column":13}},"loc":{"start":{"line":118,"column":30},"end":{"line":133,"column":1}},"line":118},"11":{"name":"(anonymous_11)","decl":{"start":{"line":119,"column":23},"end":{"line":119,"column":24}},"loc":{"start":{"line":119,"column":50},"end":{"line":132,"column":5}},"line":119},"12":{"name":"(anonymous_12)","decl":{"start":{"line":122,"column":18},"end":{"line":122,"column":19}},"loc":{"start":{"line":122,"column":36},"end":{"line":124,"column":9}},"line":122},"13":{"name":"(anonymous_13)","decl":{"start":{"line":124,"column":11},"end":{"line":124,"column":12}},"loc":{"start":{"line":124,"column":28},"end":{"line":131,"column":9}},"line":124},"14":{"name":"all","decl":{"start":{"line":141,"column":9},"end":{"line":141,"column":12}},"loc":{"start":{"line":141,"column":29},"end":{"line":156,"column":1}},"line":141},"15":{"name":"(anonymous_15)","decl":{"start":{"line":142,"column":23},"end":{"line":142,"column":24}},"loc":{"start":{"line":142,"column":50},"end":{"line":155,"column":5}},"line":142},"16":{"name":"(anonymous_16)","decl":{"start":{"line":145,"column":18},"end":{"line":145,"column":19}},"loc":{"start":{"line":145,"column":36},"end":{"line":147,"column":9}},"line":145},"17":{"name":"(anonymous_17)","decl":{"start":{"line":147,"column":11},"end":{"line":147,"column":12}},"loc":{"start":{"line":147,"column":28},"end":{"line":154,"column":9}},"line":147}},"branchMap":{"0":{"loc":{"start":{"line":45,"column":8},"end":{"line":47,"column":9}},"type":"if","locations":[{"start":{"line":45,"column":8},"end":{"line":47,"column":9}},{"start":{"line":45,"column":8},"end":{"line":47,"column":9}}],"line":45},"1":{"loc":{"start":{"line":51,"column":12},"end":{"line":72,"column":13}},"type":"if","locations":[{"start":{"line":51,"column":12},"end":{"line":72,"column":13}},{"start":{"line":51,"column":12},"end":{"line":72,"column":13}}],"line":51},"2":{"loc":{"start":{"line":62,"column":20},"end":{"line":65,"column":21}},"type":"if","locations":[{"start":{"line":62,"column":20},"end":{"line":65,"column":21}},{"start":{"line":62,"column":20},"end":{"line":65,"column":21}}],"line":62},"3":{"loc":{"start":{"line":76,"column":12},"end":{"line":78,"column":13}},"type":"if","locations":[{"start":{"line":76,"column":12},"end":{"line":78,"column":13}},{"start":{"line":76,"column":12},"end":{"line":78,"column":13}}],"line":76},"4":{"loc":{"start":{"line":80,"column":12},"end":{"line":82,"column":13}},"type":"if","locations":[{"start":{"line":80,"column":12},"end":{"line":82,"column":13}},{"start":{"line":80,"column":12},"end":{"line":82,"column":13}}],"line":80},"5":{"loc":{"start":{"line":83,"column":12},"end":{"line":85,"column":13}},"type":"if","locations":[{"start":{"line":83,"column":12},"end":{"line":85,"column":13}},{"start":{"line":83,"column":12},"end":{"line":85,"column":13}}],"line":83},"6":{"loc":{"start":{"line":86,"column":12},"end":{"line":89,"column":13}},"type":"if","locations":[{"start":{"line":86,"column":12},"end":{"line":89,"column":13}},{"start":{"line":86,"column":12},"end":{"line":89,"column":13}}],"line":86},"7":{"loc":{"start":{"line":95,"column":16},"end":{"line":101,"column":17}},"type":"if","locations":[{"start":{"line":95,"column":16},"end":{"line":101,"column":17}},{"start":{"line":95,"column":16},"end":{"line":101,"column":17}}],"line":95},"8":{"loc":{"start":{"line":125,"column":12},"end":{"line":130,"column":13}},"type":"if","locations":[{"start":{"line":125,"column":12},"end":{"line":130,"column":13}},{"start":{"line":125,"column":12},"end":{"line":130,"column":13}}],"line":125},"9":{"loc":{"start":{"line":148,"column":12},"end":{"line":153,"column":13}},"type":"if","locations":[{"start":{"line":148,"column":12},"end":{"line":153,"column":13}},{"start":{"line":148,"column":12},"end":{"line":153,"column":13}}],"line":148}},"s":{"0":1,"1":1,"2":1,"3":1,"4":1,"5":71,"6":71,"7":1,"8":19,"9":19,"10":19,"11":19,"12":1,"13":18,"14":18,"15":18,"16":80,"17":77,"18":55,"19":55,"20":55,"21":55,"22":22,"23":22,"24":22,"25":22,"26":7,"27":7,"28":15,"29":3,"30":3,"31":18,"32":75,"33":8,"34":67,"35":67,"36":5,"37":67,"38":11,"39":56,"40":26,"41":26,"42":18,"43":86,"44":85,"45":80,"46":5,"47":18,"48":60,"49":60,"50":1,"51":4,"52":4,"53":4,"54":2,"55":2,"56":1,"57":1,"58":1,"59":4,"60":4,"61":4,"62":2,"63":2,"64":1,"65":1,"66":1},"f":{"0":71,"1":71,"2":19,"3":19,"4":80,"5":55,"6":22,"7":75,"8":86,"9":85,"10":4,"11":4,"12":2,"13":2,"14":4,"15":4,"16":2,"17":2},"b":{"0":[1,18],"1":[77,3],"2":[7,15],"3":[8,67],"4":[5,62],"5":[11,56],"6":[26,30],"7":[80,5],"8":[1,1],"9":[1,1]},"inputSourceMap":{"version":3,"file":"throttle.js","sourceRoot":"","sources":["file:////mnt/d/Projecten/Promise-parallel-throttle/src/throttle.ts"],"names":[],"mappings":";;AAAA,IAAM,WAAW,GAAG,CAAC,CAAC;AAmBtB;;;;;;;;;;;;GAYG;AACH,IAAM,oBAAoB,GAAkB,UAAI,MAAiB,EAAE,KAAe;IAC9E,MAAM,CAAC,IAA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AC;YAAC,IAAI,CAAC,CAAC;gBACJ,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YACxD,CAAC;QACL,CAAC,CAAC,CAAC;IACX,CAAC,CAAC,CAAC;AACP,CAAC;AAdD,oBAcC;AAED;;;;;GAKG;AACH,aAAuB,KAAe,EAAE,OAAiB;IACrD,MAAM,CAAC,IAAI,OAAO,CAAC,UAAC,OAAO,EAAE,MAAM;QAC/B,IAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,EAAC,QAAQ,EAAE,IAAI,EAAC,EAAE,OAAO,CAAC,CAAC;QAC/D,GAAG,CAAC,KAAK,EAAE,SAAS,CAAC;aAChB,IAAI,CAAC,UAAC,MAAiB;YACpB,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;QAChC,CAAC,EAAE,UAAC,KAAsB;YACtB,EAAE,CAAC,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC;gBACzB,MAAM,CAAC,KAAK,CAAC,CAAC;YAClB,CAAC;YAAC,IAAI,CAAC,CAAC;gBACJ,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YACxD,CAAC;QACL,CAAC,CAAC,CAAC;IACX,CAAC,CAAC,CAAC;AACP,CAAC;AAdD,kBAcC","sourcesContent":["const DEFAULT_MAX = 5;\n\nexport interface Result<T> {\n amountDone: number;\n amountStarted: number;\n amountResolved: number;\n amountRejected: number;\n rejectedIndexes: number[];\n resolvedIndexes: number[];\n taskResults: T[];\n}\n\nexport interface Options {\n maxInProgress?: number;\n failFast?: boolean;\n progressCallback?: <T>(result: Result<T>) => void;\n nextCheck?: nextTaskCheck;\n}\n\n/**\n * Default checker which validates if a next task should begin.\n * This can be overwritten to write own checks for example checking the amount\n * of used ram and waiting till the ram is low enough for a next task.\n *\n * It should always resolve with a boolean, either `true` to start a next task\n * or `false` to stop executing a new task.\n *\n * If this method rejects, the error will propagate to the caller\n * @param status\n * @param tasks\n * @returns {Promise}\n */\nconst defaultNextTaskCheck: nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>): Promise<boolean> => {\n return new Promise((resolve, reject) => {\n resolve(status.amountStarted < tasks.length);\n });\n};\n\nconst DEFAULT_OPTIONS = {\n maxInProgress: DEFAULT_MAX,\n failFast: false,\n nextCheck: defaultNextTaskCheck,\n};\n\nexport type Task<T> = () => Promise<T>;\nexport type Tasks<T> = Array<Task<T>>;\nexport type nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>) => Promise<boolean>;\n\n/**\n * Raw throttle function, which can return extra meta data.\n * @param tasks required array of tasks to be executed\n * @param options Options object\n * @returns {Promise}\n */\nexport function raw<T>(tasks: Tasks<T>, options?: Options): Promise<Result<T>> {\n return new Promise((resolve, reject) => {\n const myOptions = Object.assign({}, DEFAULT_OPTIONS, options);\n const result: Result<T> = {\n amountDone: 0,\n amountStarted: 0,\n amountResolved: 0,\n amountRejected: 0,\n rejectedIndexes: [],\n resolvedIndexes: [],\n taskResults: []\n };\n\n if (tasks.length === 0) {\n return resolve(result);\n }\n\n let failedFast = false;\n let amountQueued = 0;\n const executeTask = (index: number) => {\n if (typeof tasks[index] === 'function') {\n tasks[index]()\n .then((taskResult: T) => {\n result.taskResults[index] = taskResult;\n result.resolvedIndexes.push(index);\n result.amountResolved++;\n taskDone();\n }, error => {\n result.taskResults[index] = error;\n result.rejectedIndexes.push(index);\n result.amountRejected++;\n if (myOptions.failFast === true) {\n failedFast = true;\n return reject(result);\n }\n taskDone();\n });\n } else {\n failedFast = true;\n return reject(new Error('tasks[' + index + ']: ' + tasks[index] + ', is supposed to be of type function'));\n }\n };\n\n const taskDone = () => {\n //make sure no more tasks are spawned when we failedFast\n if (failedFast === true) {\n return;\n }\n\n result.amountDone++;\n if (typeof (myOptions as Options).progressCallback === 'function') {\n (myOptions as any).progressCallback(result);\n }\n if (result.amountDone === tasks.length) {\n return resolve(result);\n }\n if (amountQueued < tasks.length) {\n amountQueued++;\n nextTask();\n }\n };\n\n const nextTask = () => {\n //check if we can execute the next task\n myOptions.nextCheck(result, tasks)\n .then(canExecuteNextTask => {\n if (canExecuteNextTask === true) {\n //execute it\n executeTask(result.amountStarted++);\n } else {\n taskDone();\n }\n }, reject);\n };\n\n //spawn the first X task\n for (let i = 0; i < Math.min(myOptions.maxInProgress, tasks.length); i++) {\n amountQueued++;\n nextTask();\n }\n });\n}\n\n/**\n * Simply run all the promises after each other, so in synchronous manner\n * @param tasks required array of tasks to be executed\n * @param options Options object\n * @returns {Promise}\n */\nexport function sync<T>(tasks: Tasks<T>, options?: Options): Promise<T[]> {\n return new Promise((resolve, reject) => {\n const myOptions = Object.assign({}, {maxInProgress: 1, failFast: true}, options);\n raw(tasks, myOptions)\n .then((result: Result<T>) => {\n resolve(result.taskResults);\n }, (error: Error|Result<T>) => {\n if (error instanceof Error) {\n reject(error);\n } else {\n reject(error.taskResults[error.rejectedIndexes[0]]);\n }\n });\n });\n}\n\n/**\n * Exposes the same behaviour as Promise.All(), but throttled!\n * @param tasks required array of tasks to be executed\n * @param options Options object\n * @returns {Promise}\n */\nexport function all<T>(tasks: Tasks<T>, options?: Options): Promise<T[]> {\n return new Promise((resolve, reject) => {\n const myOptions = Object.assign({}, {failFast: true}, options);\n raw(tasks, myOptions)\n .then((result: Result<T>) => {\n resolve(result.taskResults);\n }, (error: Error|Result<T>) => {\n if (error instanceof Error) {\n reject(error);\n } else {\n reject(error.taskResults[error.rejectedIndexes[0]]);\n }\n });\n });\n}\n"]},"_coverageSchema":"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c","hash":"664b51d6e5503353b5b399eaed67ef85616d826a"} | ||
} |
{ | ||
"/mnt/d/Projecten/Promise-parallel-throttle/src/throttle.ts":{"path":"/mnt/d/Projecten/Promise-parallel-throttle/src/throttle.ts","statementMap":{"1":{"start":{"line":1,"column":20},"end":{"line":1,"column":21}},"2":{"start":{"line":27,"column":23},"end":{"line":27,"column":50}},"3":{"start":{"line":28,"column":23},"end":{"line":28,"column":39}},"4":{"start":{"line":30,"column":23},"end":{"line":30,"column":55}},"5":{"start":{"line":31,"column":4},"end":{"line":107,"column":7}},"6":{"start":{"line":32,"column":34},"end":{"line":40,"column":9}},"7":{"start":{"line":42,"column":8},"end":{"line":44,"column":9}},"8":{"start":{"line":43,"column":12},"end":{"line":43,"column":35}},"9":{"start":{"line":46,"column":25},"end":{"line":46,"column":30}},"10":{"start":{"line":47,"column":27},"end":{"line":47,"column":28}},"11":{"start":{"line":48,"column":28},"end":{"line":70,"column":9}},"12":{"start":{"line":49,"column":12},"end":{"line":69,"column":13}},"13":{"start":{"line":50,"column":16},"end":{"line":65,"column":23}},"14":{"start":{"line":52,"column":24},"end":{"line":52,"column":63}},"15":{"start":{"line":53,"column":24},"end":{"line":53,"column":59}},"16":{"start":{"line":54,"column":24},"end":{"line":54,"column":48}},"17":{"start":{"line":55,"column":24},"end":{"line":55,"column":35}},"18":{"start":{"line":57,"column":24},"end":{"line":57,"column":58}},"19":{"start":{"line":58,"column":24},"end":{"line":58,"column":59}},"20":{"start":{"line":59,"column":24},"end":{"line":59,"column":48}},"21":{"start":{"line":60,"column":24},"end":{"line":63,"column":25}},"22":{"start":{"line":61,"column":28},"end":{"line":61,"column":46}},"23":{"start":{"line":62,"column":28},"end":{"line":62,"column":50}},"24":{"start":{"line":64,"column":24},"end":{"line":64,"column":35}},"25":{"start":{"line":67,"column":16},"end":{"line":67,"column":34}},"26":{"start":{"line":68,"column":16},"end":{"line":68,"column":123}},"27":{"start":{"line":72,"column":25},"end":{"line":89,"column":9}},"28":{"start":{"line":74,"column":12},"end":{"line":76,"column":13}},"29":{"start":{"line":75,"column":16},"end":{"line":75,"column":23}},"30":{"start":{"line":78,"column":12},"end":{"line":78,"column":32}},"31":{"start":{"line":79,"column":12},"end":{"line":81,"column":13}},"32":{"start":{"line":80,"column":16},"end":{"line":80,"column":41}},"33":{"start":{"line":82,"column":12},"end":{"line":84,"column":13}},"34":{"start":{"line":83,"column":16},"end":{"line":83,"column":39}},"35":{"start":{"line":85,"column":12},"end":{"line":88,"column":13}},"36":{"start":{"line":86,"column":16},"end":{"line":86,"column":31}},"37":{"start":{"line":87,"column":16},"end":{"line":87,"column":27}},"38":{"start":{"line":91,"column":25},"end":{"line":100,"column":9}},"39":{"start":{"line":93,"column":12},"end":{"line":99,"column":27}},"40":{"start":{"line":95,"column":20},"end":{"line":98,"column":21}},"41":{"start":{"line":97,"column":24},"end":{"line":97,"column":60}},"42":{"start":{"line":103,"column":8},"end":{"line":106,"column":9}},"43":{"start":{"line":104,"column":12},"end":{"line":104,"column":27}},"44":{"start":{"line":105,"column":12},"end":{"line":105,"column":23}},"45":{"start":{"line":26,"column":0},"end":{"line":108,"column":1}},"46":{"start":{"line":123,"column":44},"end":{"line":127,"column":1}},"47":{"start":{"line":124,"column":4},"end":{"line":126,"column":7}},"48":{"start":{"line":125,"column":8},"end":{"line":125,"column":53}},"49":{"start":{"line":137,"column":24},"end":{"line":137,"column":39}},"50":{"start":{"line":139,"column":24},"end":{"line":139,"column":56}},"51":{"start":{"line":140,"column":4},"end":{"line":151,"column":7}},"52":{"start":{"line":141,"column":8},"end":{"line":150,"column":15}},"53":{"start":{"line":143,"column":16},"end":{"line":143,"column":44}},"54":{"start":{"line":145,"column":16},"end":{"line":149,"column":17}},"55":{"start":{"line":146,"column":20},"end":{"line":146,"column":34}},"56":{"start":{"line":148,"column":20},"end":{"line":148,"column":72}},"57":{"start":{"line":136,"column":0},"end":{"line":152,"column":1}},"58":{"start":{"line":163,"column":23},"end":{"line":163,"column":50}},"59":{"start":{"line":164,"column":23},"end":{"line":164,"column":38}},"60":{"start":{"line":166,"column":23},"end":{"line":166,"column":55}},"61":{"start":{"line":167,"column":4},"end":{"line":178,"column":7}},"62":{"start":{"line":168,"column":8},"end":{"line":177,"column":15}},"63":{"start":{"line":170,"column":16},"end":{"line":170,"column":44}},"64":{"start":{"line":172,"column":16},"end":{"line":176,"column":17}},"65":{"start":{"line":173,"column":20},"end":{"line":173,"column":34}},"66":{"start":{"line":175,"column":20},"end":{"line":175,"column":72}},"67":{"start":{"line":162,"column":0},"end":{"line":179,"column":1}}},"fnMap":{"1":{"name":"raw","line":30,"loc":{"start":{"line":30,"column":55},"end":{"line":108,"column":1}}},"2":{"name":"(anonymous_1)","line":31,"loc":{"start":{"line":31,"column":39},"end":{"line":107,"column":5}}},"3":{"name":"(anonymous_2)","line":48,"loc":{"start":{"line":48,"column":42},"end":{"line":70,"column":9}}},"4":{"name":"(anonymous_3)","line":51,"loc":{"start":{"line":51,"column":40},"end":{"line":56,"column":21}}},"5":{"name":"(anonymous_4)","line":56,"loc":{"start":{"line":56,"column":28},"end":{"line":65,"column":21}}},"6":{"name":"(anonymous_5)","line":72,"loc":{"start":{"line":72,"column":25},"end":{"line":89,"column":9}}},"7":{"name":"(anonymous_6)","line":91,"loc":{"start":{"line":91,"column":25},"end":{"line":100,"column":9}}},"8":{"name":"(anonymous_7)","line":94,"loc":{"start":{"line":94,"column":40},"end":{"line":99,"column":17}}},"9":{"name":"(anonymous_8)","line":123,"loc":{"start":{"line":123,"column":82},"end":{"line":127,"column":1}}},"10":{"name":"(anonymous_9)","line":124,"loc":{"start":{"line":124,"column":39},"end":{"line":126,"column":5}}},"11":{"name":"sync","line":139,"loc":{"start":{"line":139,"column":56},"end":{"line":152,"column":1}}},"12":{"name":"(anonymous_11)","line":140,"loc":{"start":{"line":140,"column":39},"end":{"line":151,"column":5}}},"13":{"name":"(anonymous_12)","line":142,"loc":{"start":{"line":142,"column":36},"end":{"line":144,"column":13}}},"14":{"name":"(anonymous_13)","line":144,"loc":{"start":{"line":144,"column":38},"end":{"line":150,"column":13}}},"15":{"name":"all","line":166,"loc":{"start":{"line":166,"column":55},"end":{"line":179,"column":1}}},"16":{"name":"(anonymous_15)","line":167,"loc":{"start":{"line":167,"column":39},"end":{"line":178,"column":5}}},"17":{"name":"(anonymous_16)","line":169,"loc":{"start":{"line":169,"column":36},"end":{"line":171,"column":13}}},"18":{"name":"(anonymous_17)","line":171,"loc":{"start":{"line":171,"column":38},"end":{"line":177,"column":13}}}},"branchMap":{"1":{"line":27,"type":"if","locations":[{"start":{"line":27,"column":23},"end":{"line":27,"column":50}},{"start":{"line":27,"column":23},"end":{"line":27,"column":50}}]},"2":{"line":28,"type":"if","locations":[{"start":{"line":28,"column":23},"end":{"line":28,"column":39}},{"start":{"line":28,"column":23},"end":{"line":28,"column":39}}]},"3":{"line":30,"type":"if","locations":[{"start":{"line":30,"column":23},"end":{"line":30,"column":55}},{"start":{"line":30,"column":23},"end":{"line":30,"column":55}}]},"4":{"line":42,"type":"if","locations":[{"start":{"line":42,"column":8},"end":{"line":44,"column":9}},{"start":{"line":42,"column":8},"end":{"line":44,"column":9}}]},"5":{"line":49,"type":"if","locations":[{"start":{"line":49,"column":12},"end":{"line":69,"column":13}},{"start":{"line":49,"column":12},"end":{"line":69,"column":13}}]},"6":{"line":60,"type":"if","locations":[{"start":{"line":60,"column":24},"end":{"line":63,"column":25}},{"start":{"line":60,"column":24},"end":{"line":63,"column":25}}]},"7":{"line":74,"type":"if","locations":[{"start":{"line":74,"column":12},"end":{"line":76,"column":13}},{"start":{"line":74,"column":12},"end":{"line":76,"column":13}}]},"8":{"line":79,"type":"if","locations":[{"start":{"line":79,"column":12},"end":{"line":81,"column":13}},{"start":{"line":79,"column":12},"end":{"line":81,"column":13}}]},"9":{"line":82,"type":"if","locations":[{"start":{"line":82,"column":12},"end":{"line":84,"column":13}},{"start":{"line":82,"column":12},"end":{"line":84,"column":13}}]},"10":{"line":85,"type":"if","locations":[{"start":{"line":85,"column":12},"end":{"line":88,"column":13}},{"start":{"line":85,"column":12},"end":{"line":88,"column":13}}]},"11":{"line":95,"type":"if","locations":[{"start":{"line":95,"column":20},"end":{"line":98,"column":21}},{"start":{"line":95,"column":20},"end":{"line":98,"column":21}}]},"12":{"line":137,"type":"if","locations":[{"start":{"line":137,"column":24},"end":{"line":137,"column":39}},{"start":{"line":137,"column":24},"end":{"line":137,"column":39}}]},"13":{"line":139,"type":"if","locations":[{"start":{"line":139,"column":24},"end":{"line":139,"column":56}},{"start":{"line":139,"column":24},"end":{"line":139,"column":56}}]},"14":{"line":145,"type":"if","locations":[{"start":{"line":145,"column":16},"end":{"line":149,"column":17}},{"start":{"line":145,"column":16},"end":{"line":149,"column":17}}]},"15":{"line":163,"type":"if","locations":[{"start":{"line":163,"column":23},"end":{"line":163,"column":50}},{"start":{"line":163,"column":23},"end":{"line":163,"column":50}}]},"16":{"line":164,"type":"if","locations":[{"start":{"line":164,"column":23},"end":{"line":164,"column":38}},{"start":{"line":164,"column":23},"end":{"line":164,"column":38}}]},"17":{"line":166,"type":"if","locations":[{"start":{"line":166,"column":23},"end":{"line":166,"column":55}},{"start":{"line":166,"column":23},"end":{"line":166,"column":55}}]},"18":{"line":172,"type":"if","locations":[{"start":{"line":172,"column":16},"end":{"line":176,"column":17}},{"start":{"line":172,"column":16},"end":{"line":176,"column":17}}]}},"s":{"1":1,"2":22,"3":22,"4":23,"5":15,"6":15,"7":15,"8":1,"9":14,"10":14,"11":14,"12":60,"13":57,"14":45,"15":45,"16":45,"17":45,"18":12,"19":12,"20":12,"21":12,"22":7,"23":7,"24":5,"25":3,"26":3,"27":14,"28":50,"29":8,"30":42,"31":42,"32":4,"33":42,"34":7,"35":35,"36":14,"37":14,"38":14,"39":61,"40":60,"41":60,"42":14,"43":47,"44":47,"45":1,"46":1,"47":56,"48":56,"49":6,"50":6,"51":3,"52":3,"53":1,"54":2,"55":1,"56":1,"57":1,"58":6,"59":6,"60":6,"61":3,"62":3,"63":1,"64":2,"65":1,"66":1,"67":1},"b":{"1":[7,8],"2":[7,8],"3":[8,7],"4":[1,14],"5":[57,3],"6":[7,5],"7":[8,42],"8":[4,38],"9":[7,35],"10":[14,21],"11":[60,0],"12":[3,0],"13":[3,0],"14":[1,1],"15":[3,0],"16":[3,0],"17":[3,0],"18":[1,1]},"f":{"1":15,"2":15,"3":60,"4":45,"5":12,"6":50,"7":61,"8":60,"9":56,"10":56,"11":3,"12":3,"13":1,"14":2,"15":3,"16":3,"17":1,"18":2},"code":["const DEFAULT_MAX = 5;","","export interface Result<T> {"," amountDone: number;"," amountStarted: number;"," amountResolved: number;"," amountRejected: number;"," rejectedIndexes: number[];"," resolvedIndexes: number[];"," taskResults: T[];","}","","export type Task<T> = () => Promise<T>;","export type Tasks<T> = Array<Task<T>>;","export type nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>) => Promise<boolean>;","","/**"," * Raw throttle function, which can return extra meta data."," * @param tasks array[] of functions"," * @param maxInProgress integer with the max amount of tasks to be run in parallel"," * @param failFast boolean if true, do fail-fast behaviour (see Promise.all() documentation)"," * @param progressCallback function which can be used to see the progress"," * @param nextCheck"," * @returns {Promise}"," */","export function raw<T>(tasks: Tasks<T>,"," maxInProgress = DEFAULT_MAX,"," failFast = false,"," progressCallback?: (result: Result<T>) => void,"," nextCheck = defaultNextTaskCheck): Promise<Result<T>> {"," return new Promise((resolve, reject) => {"," const result: Result<T> = {"," amountDone: 0,"," amountStarted: 0,"," amountResolved: 0,"," amountRejected: 0,"," rejectedIndexes: [],"," resolvedIndexes: [],"," taskResults: []"," };",""," if (tasks.length === 0) {"," return resolve(result);"," }",""," let failedFast = false;"," let amountQueued = 0;"," const executeTask = (index: number) => {"," if (typeof tasks[index] === 'function') {"," tasks[index]()"," .then((taskResult: T) => {"," result.taskResults[index] = taskResult;"," result.resolvedIndexes.push(index);"," result.amountResolved++;"," taskDone();"," }, error => {"," result.taskResults[index] = error;"," result.rejectedIndexes.push(index);"," result.amountRejected++;"," if (failFast) {"," failedFast = true;"," return reject(result);"," }"," taskDone();"," });"," } else {"," failedFast = true;"," return reject(new Error('tasks[' + index + ']: ' + tasks[index] + ', is supposed to be of type function'));"," }"," };",""," const taskDone = () => {"," //make sure no more tasks are spawned when we failedFast"," if (failedFast) {"," return;"," }",""," result.amountDone++;"," if (progressCallback) {"," progressCallback(result);"," }"," if (result.amountDone === tasks.length) {"," return resolve(result);"," }"," if (amountQueued < tasks.length) {"," amountQueued++;"," nextTask();"," }"," };",""," const nextTask = () => {"," //check if we can execute the next task"," nextCheck(result, tasks)"," .then(canExecuteNextTask => {"," if (canExecuteNextTask === true) {"," //execute it"," executeTask(result.amountStarted++);"," }"," }, reject);"," };",""," //spawn the first X task"," for (let i = 0; i < Math.min(maxInProgress, tasks.length); i++) {"," amountQueued++;"," nextTask();"," }"," });","}","","/**"," * Default checker which validates if a next task should begin."," * This can be overwritten to write own checks for example checking the amount"," * of used ram and waiting till the ram is low enough for a next task."," *"," * It should always resolve with a boolean, either `true` to start a next task"," * or `false` to stop executing a new task."," *"," * If this method rejects, the error will propagate to the caller"," * @param status"," * @param tasks"," * @returns {Promise}"," */","const defaultNextTaskCheck: nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>): Promise<boolean> => {"," return new Promise((resolve, reject) => {"," resolve(status.amountStarted < tasks.length);"," });","};","","/**"," * Simply run all the promises after each other, so in synchronous manner"," * @param tasks required array of tasks to be executed"," * @param failFast optional boolean if we directly reject on a single error defaults to true"," * @param progressCallback optional function to be run to get status updates"," * @param nextCheck function which should return a promise, when resolved the next task will spawn"," */","export function sync<T>(tasks: Tasks<T>,"," failFast = true,"," progressCallback?: () => void,"," nextCheck = defaultNextTaskCheck): Promise<T[]> {"," return new Promise((resolve, reject) => {"," raw(tasks, 1, failFast, progressCallback, nextCheck)"," .then((result: Result<T>) => {"," resolve(result.taskResults);"," }, (error: Error|Result<T>) => {"," if (error instanceof Error) {"," reject(error);"," } else {"," reject(error.taskResults[error.rejectedIndexes[0]]);"," }"," });"," });","}","","/**"," * Exposes the same behaviour as Promise.All(), but throttled!"," * @param tasks required array of tasks to be executed"," * @param maxInProgress optional integer max amount of parallel tasks to be run defaults to 5"," * @param failFast optional boolean if we directly reject on a single error defaults to true"," * @param progressCallback optional function to be run to get status updates"," * @param nextCheck function which should return a promise, when resolved the next task will spawn"," */","export function all<T>(tasks: Tasks<T>,"," maxInProgress = DEFAULT_MAX,"," failFast = true,"," progressCallback?: () => void,"," nextCheck = defaultNextTaskCheck): Promise<T[]> {"," return new Promise((resolve, reject) => {"," raw(tasks, maxInProgress, failFast, progressCallback, nextCheck)"," .then((result: Result<T>) => {"," resolve(result.taskResults);"," }, (error: Error|Result<T>) => {"," if (error instanceof Error) {"," reject(error);"," } else {"," reject(error.taskResults[error.rejectedIndexes[0]]);"," }"," });"," });","}",""],"l":{"1":1,"26":1,"27":22,"28":22,"30":23,"31":15,"32":15,"42":15,"43":1,"46":14,"47":14,"48":14,"49":60,"50":57,"52":45,"53":45,"54":45,"55":45,"57":12,"58":12,"59":12,"60":12,"61":7,"62":7,"64":5,"67":3,"68":3,"72":14,"74":50,"75":8,"78":42,"79":42,"80":4,"82":42,"83":7,"85":35,"86":14,"87":14,"91":14,"93":61,"95":60,"97":60,"103":14,"104":47,"105":47,"123":1,"124":56,"125":56,"136":1,"137":6,"139":6,"140":3,"141":3,"143":1,"145":2,"146":1,"148":1,"162":1,"163":6,"164":6,"166":6,"167":3,"168":3,"170":1,"172":2,"173":1,"175":1}}} | ||
"/mnt/d/Projecten/Promise-parallel-throttle/src/throttle.ts":{"path":"/mnt/d/Projecten/Promise-parallel-throttle/src/throttle.ts","statementMap":{"1":{"start":{"line":1,"column":20},"end":{"line":1,"column":21}},"2":{"start":{"line":33,"column":44},"end":{"line":37,"column":1}},"3":{"start":{"line":34,"column":4},"end":{"line":36,"column":7}},"4":{"start":{"line":35,"column":8},"end":{"line":35,"column":53}},"5":{"start":{"line":39,"column":24},"end":{"line":43,"column":1}},"6":{"start":{"line":56,"column":4},"end":{"line":135,"column":7}},"7":{"start":{"line":57,"column":26},"end":{"line":57,"column":69}},"8":{"start":{"line":58,"column":34},"end":{"line":66,"column":9}},"9":{"start":{"line":68,"column":8},"end":{"line":70,"column":9}},"10":{"start":{"line":69,"column":12},"end":{"line":69,"column":35}},"11":{"start":{"line":72,"column":25},"end":{"line":72,"column":30}},"12":{"start":{"line":73,"column":27},"end":{"line":73,"column":28}},"13":{"start":{"line":74,"column":28},"end":{"line":96,"column":9}},"14":{"start":{"line":75,"column":12},"end":{"line":95,"column":13}},"15":{"start":{"line":76,"column":16},"end":{"line":91,"column":23}},"16":{"start":{"line":78,"column":24},"end":{"line":78,"column":63}},"17":{"start":{"line":79,"column":24},"end":{"line":79,"column":59}},"18":{"start":{"line":80,"column":24},"end":{"line":80,"column":48}},"19":{"start":{"line":81,"column":24},"end":{"line":81,"column":35}},"20":{"start":{"line":83,"column":24},"end":{"line":83,"column":58}},"21":{"start":{"line":84,"column":24},"end":{"line":84,"column":59}},"22":{"start":{"line":85,"column":24},"end":{"line":85,"column":48}},"23":{"start":{"line":86,"column":24},"end":{"line":89,"column":25}},"24":{"start":{"line":87,"column":28},"end":{"line":87,"column":46}},"25":{"start":{"line":88,"column":28},"end":{"line":88,"column":50}},"26":{"start":{"line":90,"column":24},"end":{"line":90,"column":35}},"27":{"start":{"line":93,"column":16},"end":{"line":93,"column":34}},"28":{"start":{"line":94,"column":16},"end":{"line":94,"column":123}},"29":{"start":{"line":98,"column":25},"end":{"line":115,"column":9}},"30":{"start":{"line":100,"column":12},"end":{"line":102,"column":13}},"31":{"start":{"line":101,"column":16},"end":{"line":101,"column":23}},"32":{"start":{"line":104,"column":12},"end":{"line":104,"column":32}},"33":{"start":{"line":105,"column":12},"end":{"line":107,"column":13}},"34":{"start":{"line":106,"column":17},"end":{"line":106,"column":60}},"35":{"start":{"line":108,"column":12},"end":{"line":110,"column":13}},"36":{"start":{"line":109,"column":16},"end":{"line":109,"column":39}},"37":{"start":{"line":111,"column":12},"end":{"line":114,"column":13}},"38":{"start":{"line":112,"column":16},"end":{"line":112,"column":31}},"39":{"start":{"line":113,"column":16},"end":{"line":113,"column":27}},"40":{"start":{"line":117,"column":25},"end":{"line":128,"column":9}},"41":{"start":{"line":119,"column":12},"end":{"line":127,"column":27}},"42":{"start":{"line":121,"column":20},"end":{"line":126,"column":21}},"43":{"start":{"line":123,"column":24},"end":{"line":123,"column":60}},"44":{"start":{"line":125,"column":24},"end":{"line":125,"column":35}},"45":{"start":{"line":131,"column":8},"end":{"line":134,"column":9}},"46":{"start":{"line":132,"column":12},"end":{"line":132,"column":27}},"47":{"start":{"line":133,"column":12},"end":{"line":133,"column":23}},"48":{"start":{"line":55,"column":0},"end":{"line":136,"column":1}},"49":{"start":{"line":145,"column":4},"end":{"line":157,"column":7}},"50":{"start":{"line":146,"column":26},"end":{"line":146,"column":88}},"51":{"start":{"line":147,"column":8},"end":{"line":156,"column":15}},"52":{"start":{"line":149,"column":16},"end":{"line":149,"column":44}},"53":{"start":{"line":151,"column":16},"end":{"line":155,"column":17}},"54":{"start":{"line":152,"column":20},"end":{"line":152,"column":34}},"55":{"start":{"line":154,"column":20},"end":{"line":154,"column":72}},"56":{"start":{"line":144,"column":0},"end":{"line":158,"column":1}},"57":{"start":{"line":167,"column":4},"end":{"line":179,"column":7}},"58":{"start":{"line":168,"column":26},"end":{"line":168,"column":70}},"59":{"start":{"line":169,"column":8},"end":{"line":178,"column":15}},"60":{"start":{"line":171,"column":16},"end":{"line":171,"column":44}},"61":{"start":{"line":173,"column":16},"end":{"line":177,"column":17}},"62":{"start":{"line":174,"column":20},"end":{"line":174,"column":34}},"63":{"start":{"line":176,"column":20},"end":{"line":176,"column":72}},"64":{"start":{"line":166,"column":0},"end":{"line":180,"column":1}}},"fnMap":{"1":{"name":"(anonymous_0)","line":33,"loc":{"start":{"line":33,"column":82},"end":{"line":37,"column":1}}},"2":{"name":"(anonymous_1)","line":34,"loc":{"start":{"line":34,"column":39},"end":{"line":36,"column":5}}},"3":{"name":"raw","line":55,"loc":{"start":{"line":55,"column":57},"end":{"line":136,"column":1}}},"4":{"name":"(anonymous_3)","line":56,"loc":{"start":{"line":56,"column":39},"end":{"line":135,"column":5}}},"5":{"name":"(anonymous_4)","line":74,"loc":{"start":{"line":74,"column":42},"end":{"line":96,"column":9}}},"6":{"name":"(anonymous_5)","line":77,"loc":{"start":{"line":77,"column":40},"end":{"line":82,"column":21}}},"7":{"name":"(anonymous_6)","line":82,"loc":{"start":{"line":82,"column":28},"end":{"line":91,"column":21}}},"8":{"name":"(anonymous_7)","line":98,"loc":{"start":{"line":98,"column":25},"end":{"line":115,"column":9}}},"9":{"name":"(anonymous_8)","line":117,"loc":{"start":{"line":117,"column":25},"end":{"line":128,"column":9}}},"10":{"name":"(anonymous_9)","line":120,"loc":{"start":{"line":120,"column":40},"end":{"line":127,"column":17}}},"11":{"name":"sync","line":144,"loc":{"start":{"line":144,"column":58},"end":{"line":158,"column":1}}},"12":{"name":"(anonymous_11)","line":145,"loc":{"start":{"line":145,"column":39},"end":{"line":157,"column":5}}},"13":{"name":"(anonymous_12)","line":148,"loc":{"start":{"line":148,"column":36},"end":{"line":150,"column":13}}},"14":{"name":"(anonymous_13)","line":150,"loc":{"start":{"line":150,"column":38},"end":{"line":156,"column":13}}},"15":{"name":"all","line":166,"loc":{"start":{"line":166,"column":57},"end":{"line":180,"column":1}}},"16":{"name":"(anonymous_15)","line":167,"loc":{"start":{"line":167,"column":39},"end":{"line":179,"column":5}}},"17":{"name":"(anonymous_16)","line":170,"loc":{"start":{"line":170,"column":36},"end":{"line":172,"column":13}}},"18":{"name":"(anonymous_17)","line":172,"loc":{"start":{"line":172,"column":38},"end":{"line":178,"column":13}}}},"branchMap":{"1":{"line":68,"type":"if","locations":[{"start":{"line":68,"column":8},"end":{"line":70,"column":9}},{"start":{"line":68,"column":8},"end":{"line":70,"column":9}}]},"2":{"line":75,"type":"if","locations":[{"start":{"line":75,"column":12},"end":{"line":95,"column":13}},{"start":{"line":75,"column":12},"end":{"line":95,"column":13}}]},"3":{"line":86,"type":"if","locations":[{"start":{"line":86,"column":24},"end":{"line":89,"column":25}},{"start":{"line":86,"column":24},"end":{"line":89,"column":25}}]},"4":{"line":100,"type":"if","locations":[{"start":{"line":100,"column":12},"end":{"line":102,"column":13}},{"start":{"line":100,"column":12},"end":{"line":102,"column":13}}]},"5":{"line":105,"type":"if","locations":[{"start":{"line":105,"column":12},"end":{"line":107,"column":13}},{"start":{"line":105,"column":12},"end":{"line":107,"column":13}}]},"6":{"line":108,"type":"if","locations":[{"start":{"line":108,"column":12},"end":{"line":110,"column":13}},{"start":{"line":108,"column":12},"end":{"line":110,"column":13}}]},"7":{"line":111,"type":"if","locations":[{"start":{"line":111,"column":12},"end":{"line":114,"column":13}},{"start":{"line":111,"column":12},"end":{"line":114,"column":13}}]},"8":{"line":121,"type":"if","locations":[{"start":{"line":121,"column":20},"end":{"line":126,"column":21}},{"start":{"line":121,"column":20},"end":{"line":126,"column":21}}]},"9":{"line":151,"type":"if","locations":[{"start":{"line":151,"column":16},"end":{"line":155,"column":17}},{"start":{"line":151,"column":16},"end":{"line":155,"column":17}}]},"10":{"line":173,"type":"if","locations":[{"start":{"line":173,"column":16},"end":{"line":177,"column":17}},{"start":{"line":173,"column":16},"end":{"line":177,"column":17}}]}},"s":{"1":1,"2":1,"3":71,"4":71,"5":1,"6":19,"7":19,"8":19,"9":19,"10":1,"11":18,"12":18,"13":18,"14":80,"15":77,"16":55,"17":55,"18":55,"19":55,"20":22,"21":22,"22":22,"23":22,"24":7,"25":7,"26":15,"27":3,"28":3,"29":18,"30":75,"31":8,"32":67,"33":67,"34":5,"35":67,"36":11,"37":56,"38":26,"39":26,"40":18,"41":86,"42":85,"43":80,"44":5,"45":18,"46":60,"47":60,"48":1,"49":4,"50":4,"51":4,"52":2,"53":2,"54":1,"55":1,"56":1,"57":4,"58":4,"59":4,"60":2,"61":2,"62":1,"63":1,"64":1},"b":{"1":[1,18],"2":[77,3],"3":[7,15],"4":[8,67],"5":[5,62],"6":[11,56],"7":[26,30],"8":[80,5],"9":[1,1],"10":[1,1]},"f":{"1":71,"2":71,"3":19,"4":19,"5":80,"6":55,"7":22,"8":75,"9":86,"10":85,"11":4,"12":4,"13":2,"14":2,"15":4,"16":4,"17":2,"18":2},"code":["const DEFAULT_MAX = 5;","","export interface Result<T> {"," amountDone: number;"," amountStarted: number;"," amountResolved: number;"," amountRejected: number;"," rejectedIndexes: number[];"," resolvedIndexes: number[];"," taskResults: T[];","}","","export interface Options {"," maxInProgress?: number;"," failFast?: boolean;"," progressCallback?: <T>(result: Result<T>) => void;"," nextCheck?: nextTaskCheck;","}","","/**"," * Default checker which validates if a next task should begin."," * This can be overwritten to write own checks for example checking the amount"," * of used ram and waiting till the ram is low enough for a next task."," *"," * It should always resolve with a boolean, either `true` to start a next task"," * or `false` to stop executing a new task."," *"," * If this method rejects, the error will propagate to the caller"," * @param status"," * @param tasks"," * @returns {Promise}"," */","const defaultNextTaskCheck: nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>): Promise<boolean> => {"," return new Promise((resolve, reject) => {"," resolve(status.amountStarted < tasks.length);"," });","};","","const DEFAULT_OPTIONS = {"," maxInProgress: DEFAULT_MAX,"," failFast: false,"," nextCheck: defaultNextTaskCheck,","};","","export type Task<T> = () => Promise<T>;","export type Tasks<T> = Array<Task<T>>;","export type nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>) => Promise<boolean>;","","/**"," * Raw throttle function, which can return extra meta data."," * @param tasks required array of tasks to be executed"," * @param options Options object"," * @returns {Promise}"," */","export function raw<T>(tasks: Tasks<T>, options?: Options): Promise<Result<T>> {"," return new Promise((resolve, reject) => {"," const myOptions = Object.assign({}, DEFAULT_OPTIONS, options);"," const result: Result<T> = {"," amountDone: 0,"," amountStarted: 0,"," amountResolved: 0,"," amountRejected: 0,"," rejectedIndexes: [],"," resolvedIndexes: [],"," taskResults: []"," };",""," if (tasks.length === 0) {"," return resolve(result);"," }",""," let failedFast = false;"," let amountQueued = 0;"," const executeTask = (index: number) => {"," if (typeof tasks[index] === 'function') {"," tasks[index]()"," .then((taskResult: T) => {"," result.taskResults[index] = taskResult;"," result.resolvedIndexes.push(index);"," result.amountResolved++;"," taskDone();"," }, error => {"," result.taskResults[index] = error;"," result.rejectedIndexes.push(index);"," result.amountRejected++;"," if (myOptions.failFast === true) {"," failedFast = true;"," return reject(result);"," }"," taskDone();"," });"," } else {"," failedFast = true;"," return reject(new Error('tasks[' + index + ']: ' + tasks[index] + ', is supposed to be of type function'));"," }"," };",""," const taskDone = () => {"," //make sure no more tasks are spawned when we failedFast"," if (failedFast === true) {"," return;"," }",""," result.amountDone++;"," if (typeof (myOptions as Options).progressCallback === 'function') {"," (myOptions as any).progressCallback(result);"," }"," if (result.amountDone === tasks.length) {"," return resolve(result);"," }"," if (amountQueued < tasks.length) {"," amountQueued++;"," nextTask();"," }"," };",""," const nextTask = () => {"," //check if we can execute the next task"," myOptions.nextCheck(result, tasks)"," .then(canExecuteNextTask => {"," if (canExecuteNextTask === true) {"," //execute it"," executeTask(result.amountStarted++);"," } else {"," taskDone();"," }"," }, reject);"," };",""," //spawn the first X task"," for (let i = 0; i < Math.min(myOptions.maxInProgress, tasks.length); i++) {"," amountQueued++;"," nextTask();"," }"," });","}","","/**"," * Simply run all the promises after each other, so in synchronous manner"," * @param tasks required array of tasks to be executed"," * @param options Options object"," * @returns {Promise}"," */","export function sync<T>(tasks: Tasks<T>, options?: Options): Promise<T[]> {"," return new Promise((resolve, reject) => {"," const myOptions = Object.assign({}, {maxInProgress: 1, failFast: true}, options);"," raw(tasks, myOptions)"," .then((result: Result<T>) => {"," resolve(result.taskResults);"," }, (error: Error|Result<T>) => {"," if (error instanceof Error) {"," reject(error);"," } else {"," reject(error.taskResults[error.rejectedIndexes[0]]);"," }"," });"," });","}","","/**"," * Exposes the same behaviour as Promise.All(), but throttled!"," * @param tasks required array of tasks to be executed"," * @param options Options object"," * @returns {Promise}"," */","export function all<T>(tasks: Tasks<T>, options?: Options): Promise<T[]> {"," return new Promise((resolve, reject) => {"," const myOptions = Object.assign({}, {failFast: true}, options);"," raw(tasks, myOptions)"," .then((result: Result<T>) => {"," resolve(result.taskResults);"," }, (error: Error|Result<T>) => {"," if (error instanceof Error) {"," reject(error);"," } else {"," reject(error.taskResults[error.rejectedIndexes[0]]);"," }"," });"," });","}",""],"l":{"1":1,"33":1,"34":71,"35":71,"39":1,"55":1,"56":19,"57":19,"58":19,"68":19,"69":1,"72":18,"73":18,"74":18,"75":80,"76":77,"78":55,"79":55,"80":55,"81":55,"83":22,"84":22,"85":22,"86":22,"87":7,"88":7,"90":15,"93":3,"94":3,"98":18,"100":75,"101":8,"104":67,"105":67,"106":5,"108":67,"109":11,"111":56,"112":26,"113":26,"117":18,"119":86,"121":85,"123":80,"125":5,"131":18,"132":60,"133":60,"144":1,"145":4,"146":4,"147":4,"149":2,"151":2,"152":1,"154":1,"166":1,"167":4,"168":4,"169":4,"171":2,"173":2,"174":1,"176":1}}} |
--------------|----------|----------|----------|----------|----------------| | ||
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | | ||
--------------|----------|----------|----------|----------|----------------| | ||
[32;1msrc/ [0m |[32;1m 100[0m |[32;1m 83.33[0m |[32;1m 100[0m |[32;1m 100[0m |[31;1m [0m | | ||
[32;1mthrottle.ts[0m |[32;1m 100[0m |[32;1m 83.33[0m |[32;1m 100[0m |[32;1m 100[0m |[31;1m [0m | | ||
[32;1msrc/ [0m |[32;1m 100[0m |[32;1m 100[0m |[32;1m 100[0m |[32;1m 100[0m |[31;1m [0m | | ||
[32;1mthrottle.ts[0m |[32;1m 100[0m |[32;1m 100[0m |[32;1m 100[0m |[32;1m 100[0m |[31;1m [0m | | ||
--------------|----------|----------|----------|----------|----------------| | ||
[32;1mAll files [0m |[32;1m 100[0m |[32;1m 83.33[0m |[32;1m 100[0m |[32;1m 100[0m |[31;1m [0m | | ||
[32;1mAll files [0m |[32;1m 100[0m |[32;1m 100[0m |[32;1m 100[0m |[32;1m 100[0m |[31;1m [0m | | ||
--------------|----------|----------|----------|----------|----------------| |
{ | ||
"name": "promise-parallel-throttle", | ||
"version": "2.5.0", | ||
"version": "3.0.0", | ||
"description": "Run promises in parallel, but throttled", | ||
@@ -10,3 +10,3 @@ "main": "build/throttle.js", | ||
"declarations": "tsc --declaration", | ||
"test": "yarn lint && jest --coverage", | ||
"test": "yarn lint && jest --coverage --no-cache", | ||
"test:watch": "jest --watch", | ||
@@ -13,0 +13,0 @@ "lint": "tslint -p ./tsconfig.json", |
@@ -45,30 +45,29 @@ # Promise-parallel-throttle | ||
### Throttle.all | ||
`Throttle.all(tasks, maxInProgress = DEFAULT_MAX, failFast = true, progressCallback, nextCheck = defaultNextTaskCheck)` | ||
`Throttle.all(tasks, options)` | ||
Throttle.all is made to behave exactly like Promise.all but instead of all the tasks run parallel it runs a max amount of tasks parallel. | ||
All the parameters are optional instead of tasks array which is required. | ||
For the defenition of the other parameters see [Throttle.raw](#Throttle.raw) | ||
All the tasks array parameter is required while the [options](#option-s-object) parameter is optional. | ||
### Throttle.sync | ||
`Throttle.sync(tasks, failFast = true, progressCallback, nextCheck = defaultNextTaskCheck)` | ||
`Throttle.sync(tasks, options)` | ||
Throttle.sync runs all the tasks synchronously. All the parameters are optional except for the tasks array. | ||
For the defenition of the other parameters see [Throttle.raw](#Throttle.raw) | ||
Throttle.sync runs all the tasks synchronously. | ||
Once again the tasks array is required, the options are optional. | ||
### Throttle.raw | ||
#### Options (parameters) | ||
`Throttle.raw` requires only a array of functions to work properly. However there are more parameters. | ||
`Throttle.raw(tasks, options)` | ||
The raw method instead of returning the tasks their results will return a [Result](#result-object--progress-callback) object. | ||
Useful if you wan't more statistics about the execution of your tasks. | ||
`Throttle.raw(tasks, maxInProgress = DEFAULT_MAX, failFast = false, progressCallback, nextCheck = defaultNextTaskCheck)` | ||
#### Option's Object | ||
|Parameter|Type|Default|Definition| | ||
|:---|:---|:---|:---| | ||
|tasks|Array|Required|queue of tasks to be run| | ||
|maxInProgress |Integer|5| max amount of parallel threads| | ||
|failFast |Boolean|false| reject after a single error, or keep running| | ||
|progressCallback |Function|Optional| callback with progress reports| | ||
|nextCheck |Function|Optional| function which should return a promise, if the promise resolved next task is spawn| | ||
|nextCheck |Function|Optional| function which should return a promise, if the promise resolved true the next task is spawn| | ||
#### Result / Progress callback | ||
The progressCallback and the Throttle itself will return a object with the following properties: | ||
#### Result object / Progress callback | ||
The `progressCallback` and the `Raw` will return a `Result` object with the following properties: | ||
@@ -80,3 +79,3 @@ |Property|Type|Start value|Definition| | ||
|amountResolved|Integer|0|amount of tasks which successfully resolved| | ||
|amountRejected|Integer|0|amount of tasks which errored and are aborted| | ||
|amountRejected|Integer|0|amount of tasks which returned in an error and are aborted| | ||
|rejectedIndexes|Array|[]|all the indexes in the tasks array where the promise rejected| | ||
@@ -87,19 +86,16 @@ |resolvedIndexes|Array|[]|all the indexes in the tasks array where the promise resolved| | ||
#### nextCheck | ||
All the functions got a parameter which will use the passed function as a check before spawning the next task. | ||
All the methods have a `nextCheck` method which will be used to verify if a next task is allowed to start. | ||
The default `nextCheck` look like this; | ||
```js | ||
const defaultNextTaskCheck = (status, tasks) => | ||
const defaultNextTaskCheck = (status, tasks) => { | ||
return new Promise((resolve, reject) => { | ||
if (status.amountStarted < tasks.length) { | ||
return resolve(true); | ||
} | ||
resolve(false); | ||
resolve(status.amountStarted < tasks.length); | ||
}); | ||
} | ||
``` | ||
This function will get a status object as parameter which adheres to the object in [Result / Progress callback](#result--progress-callback) and the list of tasks. | ||
In the default we simply check if the amount of started exceeds the amount to be done, if not we are free to start a other task. | ||
This function will get a status object as parameter which adheres to the object in [Result object / Progress callback](#result-object--progress-callback) and also it receives the list of tasks. | ||
In the default `nextCheck` we simply check if the amount of started exceeds the amount to be done, if not we are free to start an other task. | ||
If a custom implementation decides to reject the error is propagated and should be handled in the user it's code. | ||
This function can be useful to write your own scheduler based on, for example ram usage/cpu usage. | ||
@@ -109,2 +105,3 @@ Lets say the tasks you defined use a lot of ram and you don't want to exceed a certain amount. | ||
If a custom implementation decides to reject the error is propagated and should be handled in the user it's code. | ||
@@ -111,0 +108,0 @@ ## Example |
@@ -8,3 +8,3 @@ { | ||
"target": "es5", | ||
"lib": ["es5", "ES2015.Promise"], | ||
"lib": ["es6"], | ||
"strict": true | ||
@@ -11,0 +11,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
256296
1225
117