Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise-parallel-throttle

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-parallel-throttle - npm Package Compare versions

Comparing version 3.0.0 to 3.1.0

2

build/throttle.d.ts

@@ -6,4 +6,6 @@ export interface Result<T> {

amountRejected: number;
amountNextCheckFalsey: number;
rejectedIndexes: number[];
resolvedIndexes: number[];
nextCheckFalseyIndexes: number[];
taskResults: T[];

@@ -10,0 +12,0 @@ }

65

build/throttle.js

@@ -25,3 +25,3 @@ "use strict";

failFast: false,
nextCheck: defaultNextTaskCheck,
nextCheck: defaultNextTaskCheck
};

@@ -42,4 +42,6 @@ /**

amountRejected: 0,
amountNextCheckFalsey: 0,
rejectedIndexes: [],
resolvedIndexes: [],
nextCheckFalseyIndexes: [],
taskResults: []

@@ -51,7 +53,7 @@ };

var failedFast = false;
var amountQueued = 0;
var currentTaskIndex = 0;
var executeTask = function (index) {
result.amountStarted++;
if (typeof tasks[index] === 'function') {
tasks[index]()
.then(function (taskResult) {
tasks[index]().then(function (taskResult) {
result.taskResults[index] = taskResult;

@@ -89,16 +91,16 @@ result.resolvedIndexes.push(index);

}
if (amountQueued < tasks.length) {
amountQueued++;
nextTask();
if (currentTaskIndex < tasks.length) {
nextTask(currentTaskIndex++);
}
};
var nextTask = function () {
var nextTask = function (index) {
//check if we can execute the next task
myOptions.nextCheck(result, tasks)
.then(function (canExecuteNextTask) {
myOptions.nextCheck(result, tasks).then(function (canExecuteNextTask) {
if (canExecuteNextTask === true) {
//execute it
executeTask(result.amountStarted++);
executeTask(index);
}
else {
result.amountNextCheckFalsey++;
result.nextCheckFalseyIndexes.push(index);
taskDone();

@@ -110,4 +112,3 @@ }

for (var i = 0; i < Math.min(myOptions.maxInProgress, tasks.length); i++) {
amountQueued++;
nextTask();
nextTask(currentTaskIndex++);
}

@@ -118,12 +119,10 @@ });

/**
* 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
* Executes the raw function, but only return the task array
* @param tasks
* @param options
* @returns {Promise}
*/
function sync(tasks, options) {
function executeRaw(tasks, options) {
return new Promise(function (resolve, reject) {
var myOptions = Object.assign({}, { maxInProgress: 1, failFast: true }, options);
raw(tasks, myOptions)
.then(function (result) {
raw(tasks, options).then(function (result) {
resolve(result.taskResults);

@@ -140,2 +139,12 @@ }, function (error) {

}
/**
* 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}
*/
function sync(tasks, options) {
var myOptions = Object.assign({}, { maxInProgress: 1, failFast: true }, options);
return executeRaw(tasks, myOptions);
}
exports.sync = sync;

@@ -149,18 +158,6 @@ /**

function all(tasks, options) {
return new Promise(function (resolve, reject) {
var myOptions = Object.assign({}, { failFast: true }, options);
raw(tasks, myOptions)
.then(function (result) {
resolve(result.taskResults);
}, function (error) {
if (error instanceof Error) {
reject(error);
}
else {
reject(error.taskResults[error.rejectedIndexes[0]]);
}
});
});
var myOptions = Object.assign({}, { failFast: true }, options);
return executeRaw(tasks, myOptions);
}
exports.all = all;
//# sourceMappingURL=throttle.js.map

@@ -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":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,IAAI,OAAO,CAAC,UAAC,OAAO,EAAE,MAAM;QAC/B,OAAO,CAAC,MAAM,CAAC,aAAa,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC;IACjD,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAEF,IAAM,eAAe,GAAG;IACpB,aAAa,EAAE,WAAW;IAC1B,QAAQ,EAAE,KAAK;IACf,SAAS,EAAE,oBAAoB;CAClC,CAAC;AAMF;;;;;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,eAAe,EAAE,OAAO,CAAC,CAAC;QAC9D,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,WAAW,GAAG,UAAC,KAAa;YAC9B,EAAE,C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;\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":{"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":110,"column":7}},"9":{"start":{"line":35,"column":24},"end":{"line":35,"column":67}},"10":{"start":{"line":36,"column":21},"end":{"line":46,"column":9}},"11":{"start":{"line":47,"column":8},"end":{"line":49,"column":9}},"12":{"start":{"line":48,"column":12},"end":{"line":48,"column":35}},"13":{"start":{"line":50,"column":25},"end":{"line":50,"column":30}},"14":{"start":{"line":51,"column":31},"end":{"line":51,"column":32}},"15":{"start":{"line":52,"column":26},"end":{"line":75,"column":9}},"16":{"start":{"line":53,"column":12},"end":{"line":53,"column":35}},"17":{"start":{"line":54,"column":12},"end":{"line":74,"column":13}},"18":{"start":{"line":55,"column":16},"end":{"line":69,"column":19}},"19":{"start":{"line":56,"column":20},"end":{"line":56,"column":59}},"20":{"start":{"line":57,"column":20},"end":{"line":57,"column":55}},"21":{"start":{"line":58,"column":20},"end":{"line":58,"column":44}},"22":{"start":{"line":59,"column":20},"end":{"line":59,"column":31}},"23":{"start":{"line":61,"column":20},"end":{"line":61,"column":54}},"24":{"start":{"line":62,"column":20},"end":{"line":62,"column":55}},"25":{"start":{"line":63,"column":20},"end":{"line":63,"column":44}},"26":{"start":{"line":64,"column":20},"end":{"line":67,"column":21}},"27":{"start":{"line":65,"column":24},"end":{"line":65,"column":42}},"28":{"start":{"line":66,"column":24},"end":{"line":66,"column":46}},"29":{"start":{"line":68,"column":20},"end":{"line":68,"column":31}},"30":{"start":{"line":72,"column":16},"end":{"line":72,"column":34}},"31":{"start":{"line":73,"column":16},"end":{"line":73,"column":123}},"32":{"start":{"line":76,"column":23},"end":{"line":91,"column":9}},"33":{"start":{"line":78,"column":12},"end":{"line":80,"column":13}},"34":{"start":{"line":79,"column":16},"end":{"line":79,"column":23}},"35":{"start":{"line":81,"column":12},"end":{"line":81,"column":32}},"36":{"start":{"line":82,"column":12},"end":{"line":84,"column":13}},"37":{"start":{"line":83,"column":16},"end":{"line":83,"column":51}},"38":{"start":{"line":85,"column":12},"end":{"line":87,"column":13}},"39":{"start":{"line":86,"column":16},"end":{"line":86,"column":39}},"40":{"start":{"line":88,"column":12},"end":{"line":90,"column":13}},"41":{"start":{"line":89,"column":16},"end":{"line":89,"column":45}},"42":{"start":{"line":92,"column":23},"end":{"line":105,"column":9}},"43":{"start":{"line":94,"column":12},"end":{"line":104,"column":23}},"44":{"start":{"line":95,"column":16},"end":{"line":103,"column":17}},"45":{"start":{"line":97,"column":20},"end":{"line":97,"column":39}},"46":{"start":{"line":100,"column":20},"end":{"line":100,"column":51}},"47":{"start":{"line":101,"column":20},"end":{"line":101,"column":62}},"48":{"start":{"line":102,"column":20},"end":{"line":102,"column":31}},"49":{"start":{"line":107,"column":8},"end":{"line":109,"column":9}},"50":{"start":{"line":108,"column":12},"end":{"line":108,"column":41}},"51":{"start":{"line":112,"column":0},"end":{"line":112,"column":18}},"52":{"start":{"line":120,"column":4},"end":{"line":131,"column":7}},"53":{"start":{"line":121,"column":8},"end":{"line":130,"column":11}},"54":{"start":{"line":122,"column":12},"end":{"line":122,"column":40}},"55":{"start":{"line":124,"column":12},"end":{"line":129,"column":13}},"56":{"start":{"line":125,"column":16},"end":{"line":125,"column":30}},"57":{"start":{"line":128,"column":16},"end":{"line":128,"column":68}},"58":{"start":{"line":140,"column":20},"end":{"line":140,"column":84}},"59":{"start":{"line":141,"column":4},"end":{"line":141,"column":40}},"60":{"start":{"line":143,"column":0},"end":{"line":143,"column":20}},"61":{"start":{"line":151,"column":20},"end":{"line":151,"column":66}},"62":{"start":{"line":152,"column":4},"end":{"line":152,"column":40}},"63":{"start":{"line":154,"column":0},"end":{"line":154,"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":111,"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":110,"column":5}},"line":34},"4":{"name":"(anonymous_4)","decl":{"start":{"line":52,"column":26},"end":{"line":52,"column":27}},"loc":{"start":{"line":52,"column":43},"end":{"line":75,"column":9}},"line":52},"5":{"name":"(anonymous_5)","decl":{"start":{"line":55,"column":36},"end":{"line":55,"column":37}},"loc":{"start":{"line":55,"column":58},"end":{"line":60,"column":17}},"line":55},"6":{"name":"(anonymous_6)","decl":{"start":{"line":60,"column":19},"end":{"line":60,"column":20}},"loc":{"start":{"line":60,"column":36},"end":{"line":69,"column":17}},"line":60},"7":{"name":"(anonymous_7)","decl":{"start":{"line":76,"column":23},"end":{"line":76,"column":24}},"loc":{"start":{"line":76,"column":35},"end":{"line":91,"column":9}},"line":76},"8":{"name":"(anonymous_8)","decl":{"start":{"line":92,"column":23},"end":{"line":92,"column":24}},"loc":{"start":{"line":92,"column":40},"end":{"line":105,"column":9}},"line":92},"9":{"name":"(anonymous_9)","decl":{"start":{"line":94,"column":52},"end":{"line":94,"column":53}},"loc":{"start":{"line":94,"column":82},"end":{"line":104,"column":13}},"line":94},"10":{"name":"executeRaw","decl":{"start":{"line":119,"column":9},"end":{"line":119,"column":19}},"loc":{"start":{"line":119,"column":36},"end":{"line":132,"column":1}},"line":119},"11":{"name":"(anonymous_11)","decl":{"start":{"line":120,"column":23},"end":{"line":120,"column":24}},"loc":{"start":{"line":120,"column":50},"end":{"line":131,"column":5}},"line":120},"12":{"name":"(anonymous_12)","decl":{"start":{"line":121,"column":33},"end":{"line":121,"column":34}},"loc":{"start":{"line":121,"column":51},"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":"sync","decl":{"start":{"line":139,"column":9},"end":{"line":139,"column":13}},"loc":{"start":{"line":139,"column":30},"end":{"line":142,"column":1}},"line":139},"15":{"name":"all","decl":{"start":{"line":150,"column":9},"end":{"line":150,"column":12}},"loc":{"start":{"line":150,"column":29},"end":{"line":153,"column":1}},"line":150}},"branchMap":{"0":{"loc":{"start":{"line":47,"column":8},"end":{"line":49,"column":9}},"type":"if","locations":[{"start":{"line":47,"column":8},"end":{"line":49,"column":9}},{"start":{"line":47,"column":8},"end":{"line":49,"column":9}}],"line":47},"1":{"loc":{"start":{"line":54,"column":12},"end":{"line":74,"column":13}},"type":"if","locations":[{"start":{"line":54,"column":12},"end":{"line":74,"column":13}},{"start":{"line":54,"column":12},"end":{"line":74,"column":13}}],"line":54},"2":{"loc":{"start":{"line":64,"column":20},"end":{"line":67,"column":21}},"type":"if","locations":[{"start":{"line":64,"column":20},"end":{"line":67,"column":21}},{"start":{"line":64,"column":20},"end":{"line":67,"column":21}}],"line":64},"3":{"loc":{"start":{"line":78,"column":12},"end":{"line":80,"column":13}},"type":"if","locations":[{"start":{"line":78,"column":12},"end":{"line":80,"column":13}},{"start":{"line":78,"column":12},"end":{"line":80,"column":13}}],"line":78},"4":{"loc":{"start":{"line":82,"column":12},"end":{"line":84,"column":13}},"type":"if","locations":[{"start":{"line":82,"column":12},"end":{"line":84,"column":13}},{"start":{"line":82,"column":12},"end":{"line":84,"column":13}}],"line":82},"5":{"loc":{"start":{"line":85,"column":12},"end":{"line":87,"column":13}},"type":"if","locations":[{"start":{"line":85,"column":12},"end":{"line":87,"column":13}},{"start":{"line":85,"column":12},"end":{"line":87,"column":13}}],"line":85},"6":{"loc":{"start":{"line":88,"column":12},"end":{"line":90,"column":13}},"type":"if","locations":[{"start":{"line":88,"column":12},"end":{"line":90,"column":13}},{"start":{"line":88,"column":12},"end":{"line":90,"column":13}}],"line":88},"7":{"loc":{"start":{"line":95,"column":16},"end":{"line":103,"column":17}},"type":"if","locations":[{"start":{"line":95,"column":16},"end":{"line":103,"column":17}},{"start":{"line":95,"column":16},"end":{"line":103,"column":17}}],"line":95},"8":{"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}},"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":81,"17":81,"18":78,"19":56,"20":56,"21":56,"22":56,"23":22,"24":22,"25":22,"26":22,"27":7,"28":7,"29":15,"30":3,"31":3,"32":18,"33":75,"34":8,"35":67,"36":67,"37":5,"38":67,"39":11,"40":56,"41":26,"42":18,"43":86,"44":85,"45":81,"46":4,"47":4,"48":4,"49":18,"50":60,"51":1,"52":8,"53":8,"54":4,"55":4,"56":2,"57":2,"58":4,"59":4,"60":1,"61":4,"62":4,"63":1},"f":{"0":71,"1":71,"2":19,"3":19,"4":81,"5":56,"6":22,"7":75,"8":86,"9":85,"10":8,"11":8,"12":4,"13":4,"14":4,"15":4},"b":{"0":[1,18],"1":[78,3],"2":[7,15],"3":[8,67],"4":[5,62],"5":[11,56],"6":[26,30],"7":[81,4],"8":[2,2]},"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;AAqBtB;;;;;;;;;;;;GAYG;AACH,IAAM,oBAAoB,GAAkB,UAAI,MAAiB,EAAE,KAAe;IAC9E,MAAM,CAAC,IAAI,OAAO,CAAC,UAAC,OAAO,EAAE,MAAM;QAC/B,OAAO,CAAC,MAAM,CAAC,aAAa,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC;IACjD,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAEF,IAAM,eAAe,GAAG;IACpB,aAAa,EAAE,WAAW;IAC1B,QAAQ,EAAE,KAAK;IACf,SAAS,EAAE,oBAAoB;CAClC,CAAC;AAMF;;;;;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,eAAe,EAAE,OAAO,CAAC,CAAC;QAC9D,IAAM,MAAM,GAAc;YACtB,UAAU,EAAE,CAAC;YACb,aAAa,EAAE,CAAC;YAChB,cAAc,EAAE,CAAC;YACjB,cAAc,EAAE,CAAC;YACjB,qBAAqB,EAAE,CAAC;YACxB,eAAe,EAAE,EAAE;YACnB,eAAe,EAAE,EAAE;YACnB,sBAAsB,EAAE,EAAE;YAC1B,WAAW,EAAE,EAAE;SAClB,CAAC;QAEF,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC;YACrB,MAAM,CAAC,O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: number;\r\n amountStarted: number;\r\n amountResolved: number;\r\n amountRejected: number;\r\n amountNextCheckFalsey: number;\r\n rejectedIndexes: number[];\r\n resolvedIndexes: number[];\r\n nextCheckFalseyIndexes: number[];\r\n taskResults: T[];\r\n}\r\n\r\nexport interface Options {\r\n maxInProgress?: number;\r\n failFast?: boolean;\r\n progressCallback?: <T>(result: Result<T>) => void;\r\n nextCheck?: nextTaskCheck;\r\n}\r\n\r\n/**\r\n * Default checker which validates if a next task should begin.\r\n * This can be overwritten to write own checks for example checking the amount\r\n * of used ram and waiting till the ram is low enough for a next task.\r\n *\r\n * It should always resolve with a boolean, either `true` to start a next task\r\n * or `false` to stop executing a new task.\r\n *\r\n * If this method rejects, the error will propagate to the caller\r\n * @param status\r\n * @param tasks\r\n * @returns {Promise}\r\n */\r\nconst defaultNextTaskCheck: nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>): Promise<boolean> => {\r\n return new Promise((resolve, reject) => {\r\n resolve(status.amountStarted < tasks.length);\r\n });\r\n};\r\n\r\nconst DEFAULT_OPTIONS = {\r\n maxInProgress: DEFAULT_MAX,\r\n failFast: false,\r\n nextCheck: defaultNextTaskCheck\r\n};\r\n\r\nexport type Task<T> = () => Promise<T>;\r\nexport type Tasks<T> = Array<Task<T>>;\r\nexport type nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>) => Promise<boolean>;\r\n\r\n/**\r\n * Raw throttle function, which can return extra meta data.\r\n * @param tasks required array of tasks to be executed\r\n * @param options Options object\r\n * @returns {Promise}\r\n */\r\nexport function raw<T>(tasks: Tasks<T>, options?: Options): Promise<Result<T>> {\r\n return new Promise((resolve, reject) => {\r\n const myOptions = Object.assign({}, DEFAULT_OPTIONS, options);\r\n const result: Result<T> = {\r\n amountDone: 0,\r\n amountStarted: 0,\r\n amountResolved: 0,\r\n amountRejected: 0,\r\n amountNextCheckFalsey: 0,\r\n rejectedIndexes: [],\r\n resolvedIndexes: [],\r\n nextCheckFalseyIndexes: [],\r\n taskResults: []\r\n };\r\n\r\n if (tasks.length === 0) {\r\n return resolve(result);\r\n }\r\n\r\n let failedFast = false;\r\n let currentTaskIndex = 0;\r\n const executeTask = (index: number) => {\r\n result.amountStarted++;\r\n\r\n if (typeof tasks[index] === 'function') {\r\n tasks[index]().then(\r\n taskResult => {\r\n result.taskResults[index] = taskResult;\r\n result.resolvedIndexes.push(index);\r\n result.amountResolved++;\r\n taskDone();\r\n },\r\n error => {\r\n result.taskResults[index] = error;\r\n result.rejectedIndexes.push(index);\r\n result.amountRejected++;\r\n if (myOptions.failFast === true) {\r\n failedFast = true;\r\n return reject(result);\r\n }\r\n taskDone();\r\n }\r\n );\r\n } else {\r\n failedFast = true;\r\n return reject(\r\n new Error('tasks[' + index + ']: ' + tasks[index] + ', is supposed to be of type function')\r\n );\r\n }\r\n };\r\n\r\n const taskDone = () => {\r\n //make sure no more tasks are spawned when we failedFast\r\n if (failedFast === true) {\r\n return;\r\n }\r\n\r\n result.amountDone++;\r\n if (typeof (myOptions as Options).progressCallback === 'function') {\r\n (myOptions as any).progressCallback(result);\r\n }\r\n if (result.amountDone === tasks.length) {\r\n return resolve(result);\r\n }\r\n if (currentTaskIndex < tasks.length) {\r\n nextTask(currentTaskIndex++);\r\n }\r\n };\r\n\r\n const nextTask = (index: number) => {\r\n //check if we can execute the next task\r\n myOptions.nextCheck(result, tasks).then(canExecuteNextTask => {\r\n if (canExecuteNextTask === true) {\r\n //execute it\r\n executeTask(index);\r\n } else {\r\n result.amountNextCheckFalsey++;\r\n result.nextCheckFalseyIndexes.push(index);\r\n taskDone();\r\n }\r\n }, reject);\r\n };\r\n\r\n //spawn the first X task\r\n for (let i = 0; i < Math.min(myOptions.maxInProgress, tasks.length); i++) {\r\n nextTask(currentTaskIndex++);\r\n }\r\n });\r\n}\r\n\r\n/**\r\n * Executes the raw function, but only return the task array\r\n * @param tasks \r\n * @param options \r\n * @returns {Promise}\r\n */\r\nfunction executeRaw<T>(tasks: Tasks<T>, options: Options): Promise<T[]> {\r\n return new Promise((resolve, reject) => {\r\n raw(tasks, options).then(\r\n (result: Result<T>) => {\r\n resolve(result.taskResults);\r\n },\r\n (error: Error | Result<T>) => {\r\n if (error instanceof Error) {\r\n reject(error);\r\n } else {\r\n reject(error.taskResults[error.rejectedIndexes[0]]);\r\n }\r\n }\r\n );\r\n });\r\n}\r\n\r\n/**\r\n * Simply run all the promises after each other, so in synchronous manner\r\n * @param tasks required array of tasks to be executed\r\n * @param options Options object\r\n * @returns {Promise}\r\n */\r\nexport function sync<T>(tasks: Tasks<T>, options?: Options): Promise<T[]> {\r\n const myOptions = Object.assign({}, { maxInProgress: 1, failFast: true }, options);\r\n return executeRaw(tasks, myOptions);\r\n}\r\n\r\n/**\r\n * Exposes the same behaviour as Promise.All(), but throttled!\r\n * @param tasks required array of tasks to be executed\r\n * @param options Options object\r\n * @returns {Promise}\r\n */\r\nexport function all<T>(tasks: Tasks<T>, options?: Options): Promise<T[]> {\r\n const myOptions = Object.assign({}, { failFast: true }, options);\r\n return executeRaw(tasks, myOptions);\r\n}\r\n"]},"_coverageSchema":"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c","hash":"579921541b26caec6f98bdd740b0875053cea541"}
}
{
"/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}}}
"/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":35,"column":44},"end":{"line":39,"column":1}},"3":{"start":{"line":36,"column":4},"end":{"line":38,"column":7}},"4":{"start":{"line":37,"column":8},"end":{"line":37,"column":53}},"5":{"start":{"line":41,"column":24},"end":{"line":45,"column":1}},"6":{"start":{"line":58,"column":4},"end":{"line":144,"column":7}},"7":{"start":{"line":59,"column":26},"end":{"line":59,"column":69}},"8":{"start":{"line":60,"column":34},"end":{"line":70,"column":9}},"9":{"start":{"line":72,"column":8},"end":{"line":74,"column":9}},"10":{"start":{"line":73,"column":12},"end":{"line":73,"column":35}},"11":{"start":{"line":76,"column":25},"end":{"line":76,"column":30}},"12":{"start":{"line":77,"column":31},"end":{"line":77,"column":32}},"13":{"start":{"line":78,"column":28},"end":{"line":106,"column":9}},"14":{"start":{"line":79,"column":12},"end":{"line":79,"column":35}},"15":{"start":{"line":81,"column":12},"end":{"line":105,"column":13}},"16":{"start":{"line":82,"column":16},"end":{"line":99,"column":18}},"17":{"start":{"line":84,"column":24},"end":{"line":84,"column":63}},"18":{"start":{"line":85,"column":24},"end":{"line":85,"column":59}},"19":{"start":{"line":86,"column":24},"end":{"line":86,"column":48}},"20":{"start":{"line":87,"column":24},"end":{"line":87,"column":35}},"21":{"start":{"line":90,"column":24},"end":{"line":90,"column":58}},"22":{"start":{"line":91,"column":24},"end":{"line":91,"column":59}},"23":{"start":{"line":92,"column":24},"end":{"line":92,"column":48}},"24":{"start":{"line":93,"column":24},"end":{"line":96,"column":25}},"25":{"start":{"line":94,"column":28},"end":{"line":94,"column":46}},"26":{"start":{"line":95,"column":28},"end":{"line":95,"column":50}},"27":{"start":{"line":97,"column":24},"end":{"line":97,"column":35}},"28":{"start":{"line":101,"column":16},"end":{"line":101,"column":34}},"29":{"start":{"line":102,"column":16},"end":{"line":104,"column":18}},"30":{"start":{"line":108,"column":25},"end":{"line":124,"column":9}},"31":{"start":{"line":110,"column":12},"end":{"line":112,"column":13}},"32":{"start":{"line":111,"column":16},"end":{"line":111,"column":23}},"33":{"start":{"line":114,"column":12},"end":{"line":114,"column":32}},"34":{"start":{"line":115,"column":12},"end":{"line":117,"column":13}},"35":{"start":{"line":116,"column":17},"end":{"line":116,"column":60}},"36":{"start":{"line":118,"column":12},"end":{"line":120,"column":13}},"37":{"start":{"line":119,"column":16},"end":{"line":119,"column":39}},"38":{"start":{"line":121,"column":12},"end":{"line":123,"column":13}},"39":{"start":{"line":122,"column":16},"end":{"line":122,"column":45}},"40":{"start":{"line":126,"column":25},"end":{"line":138,"column":9}},"41":{"start":{"line":128,"column":12},"end":{"line":137,"column":23}},"42":{"start":{"line":129,"column":16},"end":{"line":136,"column":17}},"43":{"start":{"line":131,"column":20},"end":{"line":131,"column":39}},"44":{"start":{"line":133,"column":20},"end":{"line":133,"column":51}},"45":{"start":{"line":134,"column":20},"end":{"line":134,"column":62}},"46":{"start":{"line":135,"column":20},"end":{"line":135,"column":31}},"47":{"start":{"line":141,"column":8},"end":{"line":143,"column":9}},"48":{"start":{"line":142,"column":12},"end":{"line":142,"column":41}},"49":{"start":{"line":57,"column":0},"end":{"line":145,"column":1}},"50":{"start":{"line":154,"column":4},"end":{"line":167,"column":7}},"51":{"start":{"line":155,"column":8},"end":{"line":166,"column":10}},"52":{"start":{"line":157,"column":16},"end":{"line":157,"column":44}},"53":{"start":{"line":160,"column":16},"end":{"line":164,"column":17}},"54":{"start":{"line":161,"column":20},"end":{"line":161,"column":34}},"55":{"start":{"line":163,"column":20},"end":{"line":163,"column":72}},"56":{"start":{"line":177,"column":22},"end":{"line":177,"column":86}},"57":{"start":{"line":178,"column":4},"end":{"line":178,"column":40}},"58":{"start":{"line":176,"column":0},"end":{"line":179,"column":1}},"59":{"start":{"line":188,"column":22},"end":{"line":188,"column":68}},"60":{"start":{"line":189,"column":4},"end":{"line":189,"column":40}},"61":{"start":{"line":187,"column":0},"end":{"line":190,"column":1}}},"fnMap":{"1":{"name":"(anonymous_0)","line":35,"loc":{"start":{"line":35,"column":82},"end":{"line":39,"column":1}}},"2":{"name":"(anonymous_1)","line":36,"loc":{"start":{"line":36,"column":39},"end":{"line":38,"column":5}}},"3":{"name":"raw","line":57,"loc":{"start":{"line":57,"column":57},"end":{"line":145,"column":1}}},"4":{"name":"(anonymous_3)","line":58,"loc":{"start":{"line":58,"column":39},"end":{"line":144,"column":5}}},"5":{"name":"(anonymous_4)","line":78,"loc":{"start":{"line":78,"column":42},"end":{"line":106,"column":9}}},"6":{"name":"(anonymous_5)","line":83,"loc":{"start":{"line":83,"column":30},"end":{"line":88,"column":21}}},"7":{"name":"(anonymous_6)","line":89,"loc":{"start":{"line":89,"column":25},"end":{"line":98,"column":21}}},"8":{"name":"(anonymous_7)","line":108,"loc":{"start":{"line":108,"column":25},"end":{"line":124,"column":9}}},"9":{"name":"(anonymous_8)","line":126,"loc":{"start":{"line":126,"column":39},"end":{"line":138,"column":9}}},"10":{"name":"(anonymous_9)","line":128,"loc":{"start":{"line":128,"column":70},"end":{"line":137,"column":13}}},"11":{"name":"executeRaw","line":153,"loc":{"start":{"line":153,"column":56},"end":{"line":168,"column":1}}},"12":{"name":"(anonymous_11)","line":154,"loc":{"start":{"line":154,"column":39},"end":{"line":167,"column":5}}},"13":{"name":"(anonymous_12)","line":156,"loc":{"start":{"line":156,"column":30},"end":{"line":158,"column":13}}},"14":{"name":"(anonymous_13)","line":159,"loc":{"start":{"line":159,"column":37},"end":{"line":165,"column":13}}},"15":{"name":"sync","line":176,"loc":{"start":{"line":176,"column":58},"end":{"line":179,"column":1}}},"16":{"name":"all","line":187,"loc":{"start":{"line":187,"column":57},"end":{"line":190,"column":1}}}},"branchMap":{"1":{"line":72,"type":"if","locations":[{"start":{"line":72,"column":8},"end":{"line":74,"column":9}},{"start":{"line":72,"column":8},"end":{"line":74,"column":9}}]},"2":{"line":81,"type":"if","locations":[{"start":{"line":81,"column":12},"end":{"line":105,"column":13}},{"start":{"line":81,"column":12},"end":{"line":105,"column":13}}]},"3":{"line":93,"type":"if","locations":[{"start":{"line":93,"column":24},"end":{"line":96,"column":25}},{"start":{"line":93,"column":24},"end":{"line":96,"column":25}}]},"4":{"line":110,"type":"if","locations":[{"start":{"line":110,"column":12},"end":{"line":112,"column":13}},{"start":{"line":110,"column":12},"end":{"line":112,"column":13}}]},"5":{"line":115,"type":"if","locations":[{"start":{"line":115,"column":12},"end":{"line":117,"column":13}},{"start":{"line":115,"column":12},"end":{"line":117,"column":13}}]},"6":{"line":118,"type":"if","locations":[{"start":{"line":118,"column":12},"end":{"line":120,"column":13}},{"start":{"line":118,"column":12},"end":{"line":120,"column":13}}]},"7":{"line":121,"type":"if","locations":[{"start":{"line":121,"column":12},"end":{"line":123,"column":13}},{"start":{"line":121,"column":12},"end":{"line":123,"column":13}}]},"8":{"line":129,"type":"if","locations":[{"start":{"line":129,"column":16},"end":{"line":136,"column":17}},{"start":{"line":129,"column":16},"end":{"line":136,"column":17}}]},"9":{"line":160,"type":"if","locations":[{"start":{"line":160,"column":16},"end":{"line":164,"column":17}},{"start":{"line":160,"column":16},"end":{"line":164,"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":81,"15":81,"16":78,"17":56,"18":56,"19":56,"20":56,"21":22,"22":22,"23":22,"24":22,"25":7,"26":7,"27":15,"28":3,"29":3,"30":18,"31":75,"32":8,"33":67,"34":67,"35":5,"36":67,"37":11,"38":56,"39":26,"40":18,"41":86,"42":85,"43":81,"44":4,"45":4,"46":4,"47":18,"48":60,"49":1,"50":8,"51":8,"52":4,"53":4,"54":2,"55":2,"56":4,"57":4,"58":1,"59":4,"60":4,"61":1},"b":{"1":[1,18],"2":[78,3],"3":[7,15],"4":[8,67],"5":[5,62],"6":[11,56],"7":[26,30],"8":[81,4],"9":[2,2]},"f":{"1":71,"2":71,"3":19,"4":19,"5":81,"6":56,"7":22,"8":75,"9":86,"10":85,"11":8,"12":8,"13":4,"14":4,"15":4,"16":4},"code":["const DEFAULT_MAX = 5;\r","\r","export interface Result<T> {\r"," amountDone: number;\r"," amountStarted: number;\r"," amountResolved: number;\r"," amountRejected: number;\r"," amountNextCheckFalsey: number;\r"," rejectedIndexes: number[];\r"," resolvedIndexes: number[];\r"," nextCheckFalseyIndexes: number[];\r"," taskResults: T[];\r","}\r","\r","export interface Options {\r"," maxInProgress?: number;\r"," failFast?: boolean;\r"," progressCallback?: <T>(result: Result<T>) => void;\r"," nextCheck?: nextTaskCheck;\r","}\r","\r","/**\r"," * Default checker which validates if a next task should begin.\r"," * This can be overwritten to write own checks for example checking the amount\r"," * of used ram and waiting till the ram is low enough for a next task.\r"," *\r"," * It should always resolve with a boolean, either `true` to start a next task\r"," * or `false` to stop executing a new task.\r"," *\r"," * If this method rejects, the error will propagate to the caller\r"," * @param status\r"," * @param tasks\r"," * @returns {Promise}\r"," */\r","const defaultNextTaskCheck: nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>): Promise<boolean> => {\r"," return new Promise((resolve, reject) => {\r"," resolve(status.amountStarted < tasks.length);\r"," });\r","};\r","\r","const DEFAULT_OPTIONS = {\r"," maxInProgress: DEFAULT_MAX,\r"," failFast: false,\r"," nextCheck: defaultNextTaskCheck\r","};\r","\r","export type Task<T> = () => Promise<T>;\r","export type Tasks<T> = Array<Task<T>>;\r","export type nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>) => Promise<boolean>;\r","\r","/**\r"," * Raw throttle function, which can return extra meta data.\r"," * @param tasks required array of tasks to be executed\r"," * @param options Options object\r"," * @returns {Promise}\r"," */\r","export function raw<T>(tasks: Tasks<T>, options?: Options): Promise<Result<T>> {\r"," return new Promise((resolve, reject) => {\r"," const myOptions = Object.assign({}, DEFAULT_OPTIONS, options);\r"," const result: Result<T> = {\r"," amountDone: 0,\r"," amountStarted: 0,\r"," amountResolved: 0,\r"," amountRejected: 0,\r"," amountNextCheckFalsey: 0,\r"," rejectedIndexes: [],\r"," resolvedIndexes: [],\r"," nextCheckFalseyIndexes: [],\r"," taskResults: []\r"," };\r","\r"," if (tasks.length === 0) {\r"," return resolve(result);\r"," }\r","\r"," let failedFast = false;\r"," let currentTaskIndex = 0;\r"," const executeTask = (index: number) => {\r"," result.amountStarted++;\r","\r"," if (typeof tasks[index] === 'function') {\r"," tasks[index]().then(\r"," taskResult => {\r"," result.taskResults[index] = taskResult;\r"," result.resolvedIndexes.push(index);\r"," result.amountResolved++;\r"," taskDone();\r"," },\r"," error => {\r"," result.taskResults[index] = error;\r"," result.rejectedIndexes.push(index);\r"," result.amountRejected++;\r"," if (myOptions.failFast === true) {\r"," failedFast = true;\r"," return reject(result);\r"," }\r"," taskDone();\r"," }\r"," );\r"," } else {\r"," failedFast = true;\r"," return reject(\r"," new Error('tasks[' + index + ']: ' + tasks[index] + ', is supposed to be of type function')\r"," );\r"," }\r"," };\r","\r"," const taskDone = () => {\r"," //make sure no more tasks are spawned when we failedFast\r"," if (failedFast === true) {\r"," return;\r"," }\r","\r"," result.amountDone++;\r"," if (typeof (myOptions as Options).progressCallback === 'function') {\r"," (myOptions as any).progressCallback(result);\r"," }\r"," if (result.amountDone === tasks.length) {\r"," return resolve(result);\r"," }\r"," if (currentTaskIndex < tasks.length) {\r"," nextTask(currentTaskIndex++);\r"," }\r"," };\r","\r"," const nextTask = (index: number) => {\r"," //check if we can execute the next task\r"," myOptions.nextCheck(result, tasks).then(canExecuteNextTask => {\r"," if (canExecuteNextTask === true) {\r"," //execute it\r"," executeTask(index);\r"," } else {\r"," result.amountNextCheckFalsey++;\r"," result.nextCheckFalseyIndexes.push(index);\r"," taskDone();\r"," }\r"," }, reject);\r"," };\r","\r"," //spawn the first X task\r"," for (let i = 0; i < Math.min(myOptions.maxInProgress, tasks.length); i++) {\r"," nextTask(currentTaskIndex++);\r"," }\r"," });\r","}\r","\r","/**\r"," * Executes the raw function, but only return the task array\r"," * @param tasks \r"," * @param options \r"," * @returns {Promise}\r"," */\r","function executeRaw<T>(tasks: Tasks<T>, options: Options): Promise<T[]> {\r"," return new Promise((resolve, reject) => {\r"," raw(tasks, options).then(\r"," (result: Result<T>) => {\r"," resolve(result.taskResults);\r"," },\r"," (error: Error | Result<T>) => {\r"," if (error instanceof Error) {\r"," reject(error);\r"," } else {\r"," reject(error.taskResults[error.rejectedIndexes[0]]);\r"," }\r"," }\r"," );\r"," });\r","}\r","\r","/**\r"," * Simply run all the promises after each other, so in synchronous manner\r"," * @param tasks required array of tasks to be executed\r"," * @param options Options object\r"," * @returns {Promise}\r"," */\r","export function sync<T>(tasks: Tasks<T>, options?: Options): Promise<T[]> {\r"," const myOptions = Object.assign({}, { maxInProgress: 1, failFast: true }, options);\r"," return executeRaw(tasks, myOptions);\r","}\r","\r","/**\r"," * Exposes the same behaviour as Promise.All(), but throttled!\r"," * @param tasks required array of tasks to be executed\r"," * @param options Options object\r"," * @returns {Promise}\r"," */\r","export function all<T>(tasks: Tasks<T>, options?: Options): Promise<T[]> {\r"," const myOptions = Object.assign({}, { failFast: true }, options);\r"," return executeRaw(tasks, myOptions);\r","}\r",""],"l":{"1":1,"35":1,"36":71,"37":71,"41":1,"57":1,"58":19,"59":19,"60":19,"72":19,"73":1,"76":18,"77":18,"78":18,"79":81,"81":81,"82":78,"84":56,"85":56,"86":56,"87":56,"90":22,"91":22,"92":22,"93":22,"94":7,"95":7,"97":15,"101":3,"102":3,"108":18,"110":75,"111":8,"114":67,"115":67,"116":5,"118":67,"119":11,"121":56,"122":26,"126":18,"128":86,"129":85,"131":81,"133":4,"134":4,"135":4,"141":18,"142":60,"154":8,"155":8,"157":4,"160":4,"161":2,"163":2,"176":1,"177":4,"178":4,"187":1,"188":4,"189":4}}}
{
"name": "promise-parallel-throttle",
"version": "3.0.0",
"description": "Run promises in parallel, but throttled",
"main": "build/throttle.js",
"scripts": {
"prepublish": "npm run build",
"build": "yarn lint && rm -rf ./build && tsc && npm run declarations",
"declarations": "tsc --declaration",
"test": "yarn lint && jest --coverage --no-cache",
"test:watch": "jest --watch",
"lint": "tslint -p ./tsconfig.json",
"lint:fix": "tslint -p ./tsconfig.json --fix"
},
"repository": {
"type": "git",
"url": "git+https://github.com/DJWassink/Promise-parallel-throttle.git"
},
"author": "\"Dirk-Jan Wassink <dirk.jan.wassink@gmail.com> (http://dirkjanwassink.nl)",
"license": "MIT",
"bugs": {
"url": "https://github.com/DJWassink/Promise-parallel-throttle/issues"
},
"types": "./build/throttle.d.ts",
"homepage": "https://github.com/DJWassink/Promise-parallel-throttle#readme",
"jest": {
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"name": "promise-parallel-throttle",
"version": "3.1.0",
"keywords": ["promise", "async", "parallel", "throttle", "promise.all()", "sync", "synchronously"],
"description": "Run promises in parallel, but throttled",
"main": "build/throttle.js",
"scripts": {
"precommit": "lint-staged",
"prepublish": "npm run build",
"build": "yarn lint && rm -rf ./build && tsc && npm run declarations",
"declarations": "tsc --declaration",
"test": "yarn lint && jest --coverage --no-cache",
"test:watch": "jest --watch",
"lint": "tslint -p ./tsconfig.json",
"lint:fix": "tslint -p ./tsconfig.json --fix",
"prettier": "prettier --write \"src/**/*.ts*\""
},
"testResultsProcessor": "<rootDir>/node_modules/ts-jest/coverageprocessor.js",
"testRegex": "(/tests/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
]
},
"devDependencies": {
"@types/jest": "^19.2.2",
"@types/node": "^6.0.46",
"jest": "^19.0.2",
"ts-jest": "^19.0.14",
"tslint": "^5.1.0",
"tslint-eslint-rules": "^4.0.0",
"typescript": "^2.3.2"
},
"dependencies": {}
"repository": {
"type": "git",
"url": "git+https://github.com/DJWassink/Promise-parallel-throttle.git"
},
"author": "Dirk-Jan Wassink <dirk.jan.wassink@gmail.com> (http://dirkjanwassink.nl)",
"license": "MIT",
"bugs": {
"url": "https://github.com/DJWassink/Promise-parallel-throttle/issues"
},
"types": "./build/throttle.d.ts",
"homepage": "https://github.com/DJWassink/Promise-parallel-throttle#readme",
"jest": {
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testResultsProcessor": "<rootDir>/node_modules/ts-jest/coverageprocessor.js",
"testRegex": "(/tests/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": ["ts", "tsx", "js"]
},
"lint-staged": {
"*.{ts,js}": ["prettier --write", "tslint -c ./tslint.json", "git add"]
},
"prettier": {
"printWidth": 120,
"tabWidth": 4,
"singleQuote": true,
"jsxBracketSameLine": true,
"bracketSpacing": true
},
"devDependencies": {
"@types/jest": "^19.2.2",
"@types/node": "^6.0.46",
"husky": "^0.14.3",
"jest": "^19.0.2",
"lint-staged": "^4.2.3",
"prettier": "^1.7.2",
"ts-jest": "^19.0.14",
"tslint": "^5.1.0",
"tslint-config-prettier": "^1.5.0",
"tslint-eslint-rules": "^4.1.1",
"typescript": "^2.6.1"
}
}

@@ -47,4 +47,4 @@ # Promise-parallel-throttle

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 tasks array parameter is required while the [options](#option-s-object) parameter is optional.
Throttle.all is made to behave exactly like Promise.all but instead of all the tasks running in parallel it runs a maxium amount of tasks in parallel.
Only the tasks parameter is required while the [options](#options-object) parameter is optional.

@@ -55,3 +55,4 @@ ### Throttle.sync

Throttle.sync runs all the tasks synchronously.
Once again the tasks array is required, the options are optional.
Once again the tasks array is required, the [options](#options-object) are optional.
Be aware that this method is simply a wrapper to pass `maxInProgress` with 1. So overwriting this option in the options object would run the tasks again in parallel.

@@ -61,4 +62,4 @@ ### Throttle.raw

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.
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. Once again the tasks are required while the [options](#options-object) are optional.

@@ -69,5 +70,5 @@ #### Option's Object

|maxInProgress |Integer|5| max amount of parallel threads|
|failFast |Boolean|false| reject after a single error, or keep running|
|failFast |Boolean|true (false for the [raw](#throttleraw) method)| 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 true the next task is spawn|
|nextCheck |Function|Optional| function which should return a promise, if the promise resolved true the next task is spawn, errors will propagate and should be handled in the calling code|

@@ -83,10 +84,12 @@ #### Result object / Progress callback

|amountRejected|Integer|0|amount of tasks which returned in an error and are aborted|
|amountNextCheckFalsey|Integer|0|amount of tasks which got a falsey value in the [nextCheck](#nextcheck)|
|rejectedIndexes|Array|[]|all the indexes in the tasks array where the promise rejected|
|resolvedIndexes|Array|[]|all the indexes in the tasks array where the promise resolved|
|nextCheckFalseyIndexes|Array|[]|all the indexes in the tasks array where the [nextCheck](#nextcheck) returned a falsey value|
|taskResults|Array|[]|array containing the result of every task|
#### nextCheck
All the methods have a `nextCheck` method which will be used to verify if a next task is allowed to start.
All the `Throttle` 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;
The default `nextCheck` is defined like this;
```js

@@ -100,10 +103,10 @@ const defaultNextTaskCheck = (status, tasks) => {

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.
This function will get a status object as parameter which adheres to the [Result object](#result-object--progress-callback) and it also 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.
This function can be useful to write your own scheduler based on, for example ram usage/cpu usage.
Lets say the tasks you defined use a lot of ram and you don't want to exceed a certain amount.
Then you could write logic inside a `nextCheck` function which resolves after there is enough ram available to start the next task.
This function can be useful to write your own scheduler based on, for example ram/cpu usage.
Lets say that your tasks use a lot of ram and you don't want to exceed a certain amount.
You could then write logic inside a `nextCheck` function which resolves after there is enough ram available to start the next task.
If a custom implementation decides to reject the error is propagated and should be handled in the user it's code.
If a custom implementation decides to reject, the error is propagated and should be handled in the user it's code. If a custom implementation returns a falsey value the task will simply not execute and the next task will be scheduled.

@@ -113,6 +116,6 @@ ## Example

To run the example, at least Node 7.x.x is required, since it supports native async/await.
To run the example, at least Node 8.x.x is required, since it supports native async/await.
Simply run the example with npm:
```bash
```
npm run-script names

@@ -122,4 +125,4 @@ ```

Or with Yarn:
```bash
```
yarn names
```
```
{
"extends": "tslint:recommended",
"rulesDirectory": "node_modules/tslint-eslint-rules/dist/rules",
"rules": {
"curly": true,
"object-literal-sort-keys": false,
"member-ordering": false,
"ordered-imports": false,
"trailing-comma": false,
"indent": [
true,
"spaces"
],
"ter-indent": [
true,
4,
{
"SwitchCase": 1
}
],
"variable-name": [
true,
"allow-leading-underscore",
"allow-pascal-case"
],
"interface-name": false,
"triple-equals": true,
"comment-format": false,
"eofline": false,
"semicolon": [
true,
"always",
"ignore-bound-class-methods"
],
"max-line-length": 120,
"quotemark": [
true,
"single",
"jsx-double"
],
"array-type": [
true,
"array-simple"
],
"arrow-parens": false,
"only-arrow-functions": false,
"object-literal-shorthand": false
}
}
"extends": ["tslint:recommended", "tslint-config-prettier"],
"rulesDirectory": "node_modules/tslint-eslint-rules/dist/rules",
"rules": {
"object-literal-sort-keys": false,
"ordered-imports": false,
"member-ordering": false,
"variable-name": [true, "allow-leading-underscore", "allow-pascal-case"],
"interface-name": false,
"triple-equals": true,
"comment-format": false,
"eofline": false,
"no-empty-interface": false,
"array-type": [true, "array-simple"],
"arrow-parens": false,
"only-arrow-functions": false,
"object-literal-shorthand": false,
"no-console": [true, "log"]
}
}

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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc