@supercharge/promise-pool
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -0,1 +1,11 @@ | ||
export interface UsesConcurrency { | ||
/** | ||
* Assign the given `concurrency` as the number of tasks being processed concurrently the promise pool. | ||
*/ | ||
useConcurrency(concurrency: number): this; | ||
/** | ||
* Returns the number of concurrently processed tasks. | ||
*/ | ||
concurrency(): number; | ||
} | ||
export interface Stoppable { | ||
@@ -14,6 +24,12 @@ /** | ||
/** | ||
* Returns the number of currently processed tasks. | ||
* Returns the number of currently active tasks. | ||
* | ||
* @deprecated use the `activeTasksCount()` method (plural naming) instead | ||
*/ | ||
activeTaskCount(): number; | ||
/** | ||
* Returns the number of currently active tasks. | ||
*/ | ||
activeTasksCount(): number; | ||
/** | ||
* Returns the list of processed items. | ||
@@ -31,4 +47,4 @@ */ | ||
} | ||
export declare type ErrorHandler<T> = (error: Error, item: T, pool: Stoppable) => void | Promise<void>; | ||
export declare type ProcessHandler<T, R> = (item: T, index: number, pool: Stoppable) => R | Promise<R>; | ||
export declare type OnProgressCallback<T> = (item: T, pool: Stoppable & Statistics<T>) => void; | ||
export declare type ErrorHandler<T> = (error: Error, item: T, pool: Stoppable & UsesConcurrency) => void | Promise<void>; | ||
export declare type ProcessHandler<T, R> = (item: T, index: number, pool: Stoppable & UsesConcurrency) => R | Promise<R>; | ||
export declare type OnProgressCallback<T> = (item: T, pool: Stoppable & Statistics<T> & UsesConcurrency) => void; |
@@ -8,1 +8,2 @@ import { PromisePool } from './promise-pool'; | ||
export * from './stop-the-promise-pool-error'; | ||
export * from './validation-error'; |
@@ -24,1 +24,2 @@ 'use strict'; | ||
__exportStar(require("./stop-the-promise-pool-error"), exports); | ||
__exportStar(require("./validation-error"), exports); |
import { ReturnValue } from './return-value'; | ||
import { PromisePoolError } from './promise-pool-error'; | ||
import { ProcessHandler, OnProgressCallback, Statistics, Stoppable } from './contracts'; | ||
export declare class PromisePoolExecutor<T, R> implements Stoppable, Statistics<T> { | ||
import { ProcessHandler, OnProgressCallback, Statistics, Stoppable, UsesConcurrency } from './contracts'; | ||
export declare class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, Statistics<T> { | ||
/** | ||
@@ -36,4 +36,12 @@ * Stores the internal properties. | ||
*/ | ||
withConcurrency(concurrency: number): this; | ||
useConcurrency(concurrency: number): this; | ||
/** | ||
* Determine whether the given `concurrency` value is valid. | ||
* | ||
* @param {Number} concurrency | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
private isValidConcurrency; | ||
/** | ||
* Returns the number of concurrently processed tasks. | ||
@@ -71,8 +79,16 @@ * | ||
/** | ||
* Returns the number of currently processed tasks. | ||
* Returns the number of currently active tasks. | ||
* | ||
* @returns {Number} | ||
* | ||
* @deprecated use the `activeTasksCount()` method (plural naming) instead | ||
*/ | ||
activeTaskCount(): number; | ||
/** | ||
* Returns the number of currently active tasks. | ||
* | ||
* @returns {Number} | ||
*/ | ||
activeTasksCount(): number; | ||
/** | ||
* Returns the list of processed items. | ||
@@ -126,3 +142,3 @@ * | ||
*/ | ||
handleError(handler?: (error: Error, item: T, pool: Stoppable) => Promise<void> | void): this; | ||
handleError(handler?: (error: Error, item: T, pool: Stoppable & UsesConcurrency) => Promise<void> | void): this; | ||
/** | ||
@@ -151,8 +167,2 @@ * Set the handler function to execute when started a task. | ||
/** | ||
* Returns the number of active tasks. | ||
* | ||
* @returns {Number} | ||
*/ | ||
activeTasks(): number; | ||
/** | ||
* Stop a promise pool processing. | ||
@@ -199,3 +209,3 @@ */ | ||
*/ | ||
waitForTaskToFinish(): Promise<void>; | ||
waitForProcessingSlot(): Promise<void>; | ||
/** | ||
@@ -245,4 +255,12 @@ * Create a processing function for the given `item`. | ||
*/ | ||
isStoppingThePool(error: Error): boolean; | ||
isStoppingThePoolError(error: Error): boolean; | ||
/** | ||
* Determine whether the given `error` is a `ValidationError` instance. | ||
* | ||
* @param {Error} error | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
isValidationError(error: Error): boolean; | ||
/** | ||
* Run the user’s error handler, if available. | ||
@@ -249,0 +267,0 @@ * |
@@ -6,2 +6,3 @@ 'use strict'; | ||
const stop_the_promise_pool_error_1 = require("./stop-the-promise-pool-error"); | ||
const validation_error_1 = require("./validation-error"); | ||
class PromisePoolExecutor { | ||
@@ -33,3 +34,6 @@ /** | ||
*/ | ||
withConcurrency(concurrency) { | ||
useConcurrency(concurrency) { | ||
if (!this.isValidConcurrency(concurrency)) { | ||
throw validation_error_1.ValidationError.createFrom(`"concurrency" must be a number, 1 or up. Received "${concurrency}" (${typeof concurrency})`); | ||
} | ||
this.meta.concurrency = concurrency; | ||
@@ -39,2 +43,12 @@ return this; | ||
/** | ||
* Determine whether the given `concurrency` value is valid. | ||
* | ||
* @param {Number} concurrency | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
isValidConcurrency(concurrency) { | ||
return typeof concurrency === 'number' && concurrency >= 1; | ||
} | ||
/** | ||
* Returns the number of concurrently processed tasks. | ||
@@ -83,7 +97,17 @@ * | ||
/** | ||
* Returns the number of currently processed tasks. | ||
* Returns the number of currently active tasks. | ||
* | ||
* @returns {Number} | ||
* | ||
* @deprecated use the `activeTasksCount()` method (plural naming) instead | ||
*/ | ||
activeTaskCount() { | ||
return this.activeTasksCount(); | ||
} | ||
/** | ||
* Returns the number of currently active tasks. | ||
* | ||
* @returns {Number} | ||
*/ | ||
activeTasksCount() { | ||
return this.tasks().length; | ||
@@ -187,13 +211,5 @@ } | ||
hasReachedConcurrencyLimit() { | ||
return this.activeTasks() >= this.concurrency(); | ||
return this.activeTasksCount() >= this.concurrency(); | ||
} | ||
/** | ||
* Returns the number of active tasks. | ||
* | ||
* @returns {Number} | ||
*/ | ||
activeTasks() { | ||
return this.meta.tasks.length; | ||
} | ||
/** | ||
* Stop a promise pool processing. | ||
@@ -239,16 +255,13 @@ */ | ||
if (typeof this.handler !== 'function') { | ||
throw new Error('The first parameter for the .process(fn) method must be a function'); | ||
throw validation_error_1.ValidationError.createFrom('The first parameter for the .process(fn) method must be a function'); | ||
} | ||
if (!(typeof this.concurrency() === 'number' && this.concurrency() >= 1)) { | ||
throw new TypeError(`"concurrency" must be a number, 1 or up. Received "${this.concurrency()}" (${typeof this.concurrency()})`); | ||
} | ||
if (!Array.isArray(this.items())) { | ||
throw new TypeError(`"items" must be an array. Received ${typeof this.items()}`); | ||
throw validation_error_1.ValidationError.createFrom(`"items" must be an array. Received ${typeof this.items()}`); | ||
} | ||
if (this.errorHandler && typeof this.errorHandler !== 'function') { | ||
throw new Error(`The error handler must be a function. Received ${typeof this.errorHandler}`); | ||
throw validation_error_1.ValidationError.createFrom(`The error handler must be a function. Received ${typeof this.errorHandler}`); | ||
} | ||
this.onTaskStartedHandlers.forEach(handler => { | ||
if (handler && typeof handler !== 'function') { | ||
throw new Error(`The onTaskStarted handler must be a function. Received ${typeof handler}`); | ||
throw validation_error_1.ValidationError.createFrom(`The onTaskStarted handler must be a function. Received ${typeof handler}`); | ||
} | ||
@@ -258,3 +271,3 @@ }); | ||
if (handler && typeof handler !== 'function') { | ||
throw new Error(`The error handler must be a function. Received ${typeof handler}`); | ||
throw validation_error_1.ValidationError.createFrom(`The error handler must be a function. Received ${typeof handler}`); | ||
} | ||
@@ -277,5 +290,3 @@ }); | ||
} | ||
if (this.hasReachedConcurrencyLimit()) { | ||
await this.waitForTaskToFinish(); | ||
} | ||
await this.waitForProcessingSlot(); | ||
this.startProcessing(item, index); | ||
@@ -288,4 +299,6 @@ } | ||
*/ | ||
async waitForTaskToFinish() { | ||
await Promise.race(this.tasks()); | ||
async waitForProcessingSlot() { | ||
while (this.hasReachedConcurrencyLimit()) { | ||
await Promise.race(this.tasks()); | ||
} | ||
} | ||
@@ -354,5 +367,9 @@ /** | ||
async handleErrorFor(error, item) { | ||
if (this.isStoppingThePool(error)) { | ||
if (this.isStoppingThePoolError(error)) { | ||
return; | ||
} | ||
if (this.isValidationError(error)) { | ||
this.markAsStopped(); | ||
throw error; | ||
} | ||
return this.hasErrorHandler() | ||
@@ -369,6 +386,16 @@ ? await this.runErrorHandlerFor(error, item) | ||
*/ | ||
isStoppingThePool(error) { | ||
isStoppingThePoolError(error) { | ||
return error instanceof stop_the_promise_pool_error_1.StopThePromisePoolError; | ||
} | ||
/** | ||
* Determine whether the given `error` is a `ValidationError` instance. | ||
* | ||
* @param {Error} error | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
isValidationError(error) { | ||
return error instanceof validation_error_1.ValidationError; | ||
} | ||
/** | ||
* Run the user’s error handler, if available. | ||
@@ -410,3 +437,3 @@ * | ||
rethrowIfNotStoppingThePool(error) { | ||
if (this.isStoppingThePool(error)) { | ||
if (this.isStoppingThePoolError(error)) { | ||
return; | ||
@@ -413,0 +440,0 @@ } |
@@ -102,3 +102,3 @@ 'use strict'; | ||
return new promise_pool_executor_1.PromisePoolExecutor() | ||
.withConcurrency(this.concurrency) | ||
.useConcurrency(this.concurrency) | ||
.withHandler(callback) | ||
@@ -105,0 +105,0 @@ .handleError(this.errorHandler) |
{ | ||
"name": "@supercharge/promise-pool", | ||
"description": "Map-like, concurrent promise processing for Node.js", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"author": "Marcus Pöhls <marcus@superchargejs.com>", | ||
@@ -10,8 +10,8 @@ "bugs": { | ||
"devDependencies": { | ||
"@supercharge/eslint-config-typescript": "~2.0.0", | ||
"@supercharge/eslint-config-typescript": "~2.2.0", | ||
"@supercharge/tsconfig": "~1.0.0", | ||
"c8": "~7.11.3", | ||
"eslint": "~8.15.0", | ||
"expect": "~28.1.0", | ||
"typescript": "~4.6.4", | ||
"eslint": "~8.17.0", | ||
"expect": "~28.1.1", | ||
"typescript": "~4.7.3", | ||
"uvu": "~0.5.3" | ||
@@ -18,0 +18,0 @@ }, |
42866
25
1212