promise-parallel-throttle
Advanced tools
Comparing version 2.3.2 to 2.4.0
@@ -10,2 +10,5 @@ export interface Result<T> { | ||
} | ||
export declare type Task<T> = () => Promise<T>; | ||
export declare type Tasks<T> = Array<Task<T>>; | ||
export declare type nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>) => Promise<boolean>; | ||
/** | ||
@@ -20,3 +23,3 @@ * Raw throttle function, which can return extra meta data. | ||
*/ | ||
export declare const raw: <T>(tasks: (() => Promise<T>)[], maxInProgress?: number, failFast?: boolean, progressCallback?: Function | undefined, nextCheck?: (status: Result<T>, tasks: (() => Promise<T>)[]) => Promise<any>) => Promise<Result<T>>; | ||
export declare function raw<T>(tasks: Tasks<T>, maxInProgress?: number, failFast?: boolean, progressCallback?: (result: Result<T>) => void, nextCheck?: nextTaskCheck): Promise<Result<T>>; | ||
/** | ||
@@ -29,3 +32,3 @@ * Simply run all the promises after each other, so in synchronous manner | ||
*/ | ||
export declare const sync: <T>(tasks: (() => Promise<T>)[], failFast?: boolean, progressCallback?: Function | undefined, nextCheck?: (status: Result<T>, tasks: (() => Promise<T>)[]) => Promise<any>) => Promise<T[]>; | ||
export declare function sync<T>(tasks: Tasks<T>, failFast?: boolean, progressCallback?: () => void, nextCheck?: nextTaskCheck): Promise<T[]>; | ||
/** | ||
@@ -39,2 +42,2 @@ * Exposes the same behaviour as Promise.All(), but throttled! | ||
*/ | ||
export declare const all: <T>(tasks: (() => Promise<T>)[], maxInProgress?: number, failFast?: boolean, progressCallback?: Function | undefined, nextCheck?: (status: Result<T>, tasks: (() => Promise<T>)[]) => Promise<any>) => Promise<T[]>; | ||
export declare function all<T>(tasks: Tasks<T>, maxInProgress?: number, failFast?: boolean, progressCallback?: () => void, nextCheck?: nextTaskCheck): Promise<T[]>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var DEFAULT_MAX = 5; | ||
@@ -12,3 +13,3 @@ /** | ||
*/ | ||
exports.raw = function (tasks, maxInProgress, failFast, progressCallback, nextCheck) { | ||
function raw(tasks, maxInProgress, failFast, progressCallback, nextCheck) { | ||
if (maxInProgress === void 0) { maxInProgress = DEFAULT_MAX; } | ||
@@ -18,3 +19,2 @@ if (failFast === void 0) { failFast = false; } | ||
return new Promise(function (resolve, reject) { | ||
var failedFast = false; | ||
var result = { | ||
@@ -32,2 +32,4 @@ amountDone: 0, | ||
} | ||
var failedFast = false; | ||
var amountQueued = 0; | ||
var executeTask = function (index) { | ||
@@ -53,2 +55,3 @@ if (typeof tasks[index] === 'function') { | ||
else { | ||
failedFast = true; | ||
return reject(new Error('tasks[' + index + ']: ' + tasks[index] + ', is supposed to be of type function')); | ||
@@ -59,4 +62,5 @@ } | ||
//make sure no more tasks are spawned when we failedFast | ||
if (failedFast) | ||
if (failedFast) { | ||
return; | ||
} | ||
result.amountDone++; | ||
@@ -69,3 +73,4 @@ if (progressCallback) { | ||
} | ||
if (result.amountStarted !== tasks.length) { | ||
if (amountQueued < tasks.length) { | ||
amountQueued++; | ||
nextTask(); | ||
@@ -77,4 +82,4 @@ } | ||
nextCheck(result, tasks) | ||
.then(function (resolve) { | ||
if (resolve === true) { | ||
.then(function (canExecuteNextTask) { | ||
if (canExecuteNextTask === true) { | ||
//execute it | ||
@@ -87,6 +92,8 @@ executeTask(result.amountStarted++); | ||
for (var i = 0; i < Math.min(maxInProgress, tasks.length); i++) { | ||
amountQueued++; | ||
nextTask(); | ||
} | ||
}); | ||
}; | ||
} | ||
exports.raw = raw; | ||
/** | ||
@@ -100,3 +107,3 @@ * Default checker which validates if a next task should begin. | ||
* | ||
* If this method rejects, the | ||
* If this method rejects, the error will propagate to the caller | ||
* @param status | ||
@@ -121,7 +128,7 @@ * @param tasks | ||
*/ | ||
exports.sync = function (tasks, failFast, progressCallback, nextCheck) { | ||
function sync(tasks, failFast, progressCallback, nextCheck) { | ||
if (failFast === void 0) { failFast = true; } | ||
if (nextCheck === void 0) { nextCheck = defaultNextTaskCheck; } | ||
return new Promise(function (resolve, reject) { | ||
return exports.raw(tasks, 1, failFast, progressCallback, nextCheck) | ||
raw(tasks, 1, failFast, progressCallback, nextCheck) | ||
.then(function (result) { | ||
@@ -131,3 +138,4 @@ resolve(result.taskResults); | ||
}); | ||
}; | ||
} | ||
exports.sync = sync; | ||
/** | ||
@@ -141,3 +149,3 @@ * Exposes the same behaviour as Promise.All(), but throttled! | ||
*/ | ||
exports.all = function (tasks, maxInProgress, failFast, progressCallback, nextCheck) { | ||
function all(tasks, maxInProgress, failFast, progressCallback, nextCheck) { | ||
if (maxInProgress === void 0) { maxInProgress = DEFAULT_MAX; } | ||
@@ -147,3 +155,3 @@ if (failFast === void 0) { failFast = true; } | ||
return new Promise(function (resolve, reject) { | ||
return exports.raw(tasks, maxInProgress, failFast, progressCallback, nextCheck) | ||
raw(tasks, maxInProgress, failFast, progressCallback, nextCheck) | ||
.then(function (result) { | ||
@@ -153,3 +161,4 @@ resolve(result.taskResults); | ||
}); | ||
}; | ||
} | ||
exports.all = all; | ||
//# sourceMappingURL=throttle.js.map |
{ | ||
"name": "promise-parallel-throttle", | ||
"version": "2.3.2", | ||
"version": "2.4.0", | ||
"description": "Run promises in parallel, but throttled", | ||
@@ -8,5 +8,8 @@ "main": "build/throttle.js", | ||
"prepublish": "npm run build", | ||
"build": "rm -rf ./build && tsc && npm run declarations", | ||
"test": "npm run build && mocha tests --recursive --harmony-async-await", | ||
"declarations": "tsc --declaration" | ||
"build": "yarn lint && rm -rf ./build && tsc && npm run declarations", | ||
"declarations": "tsc --declaration", | ||
"test": "yarn lint && jest", | ||
"test:watch": "jest --watch", | ||
"lint": "tslint -p ./tsconfig.json", | ||
"lint:fix": "tslint -p ./tsconfig.json --fix" | ||
}, | ||
@@ -24,11 +27,24 @@ "repository": { | ||
"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" | ||
] | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^3.4.34", | ||
"@types/mocha": "^2.2.32", | ||
"@types/jest": "^19.2.2", | ||
"@types/node": "^6.0.46", | ||
"chai": "^3.5.0", | ||
"mocha": "^3.1.2", | ||
"typescript": "^2.0.7" | ||
"jest": "^19.0.2", | ||
"ts-jest": "^19.0.14", | ||
"tslint": "^5.1.0", | ||
"tslint-eslint-rules": "^4.0.0", | ||
"typescript": "^2.3.2" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -9,3 +9,3 @@ { | ||
"lib": ["es5", "ES2015.Promise"], | ||
"strictNullChecks": true | ||
"strict": true | ||
}, | ||
@@ -12,0 +12,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
264565
31
1226
7
1