@supercharge/promise-pool
Advanced tools
Comparing version 1.7.0 to 1.8.0
import { ReturnValue } from './return-value'; | ||
export declare type ProcessHandler<T, R> = (item: T, index: number) => R | Promise<R>; | ||
export declare class PromisePoolExecutor<T, R> { | ||
@@ -58,3 +59,3 @@ /** | ||
*/ | ||
withHandler(action: (item: T) => R | Promise<R>): this; | ||
withHandler(action: ProcessHandler<T, R>): this; | ||
/** | ||
@@ -89,5 +90,7 @@ * Set the error handler function to execute when an error occurs. | ||
* | ||
* @returns {PromisePoolExecutor} | ||
* | ||
* @throws | ||
*/ | ||
validateInputs(): void; | ||
validateInputs(): this; | ||
/** | ||
@@ -116,13 +119,15 @@ * Starts processing the promise pool by iterating over the items | ||
* | ||
* @param {*} item | ||
* @param {T} item | ||
* @param {number} index | ||
*/ | ||
startProcessing(item: T): void; | ||
startProcessing(item: T, index: number): void; | ||
/** | ||
* Ensures a returned promise for the processing of the given `item`. | ||
* | ||
* @param item | ||
* @param {T} item | ||
* @param {number} index | ||
* | ||
* @returns {*} | ||
*/ | ||
createTaskFor(item: T): Promise<any>; | ||
createTaskFor(item: T, index: number): Promise<any>; | ||
/** | ||
@@ -129,0 +134,0 @@ * Wait for all active tasks to finish. Once all the tasks finished |
@@ -84,4 +84,3 @@ 'use strict'; | ||
async start() { | ||
this.validateInputs(); | ||
return await this.process(); | ||
return await this.validateInputs().process(); | ||
} | ||
@@ -91,2 +90,4 @@ /** | ||
* | ||
* @returns {PromisePoolExecutor} | ||
* | ||
* @throws | ||
@@ -104,7 +105,6 @@ */ | ||
} | ||
if (this.errorHandler) { | ||
if (typeof this.errorHandler !== 'function') { | ||
throw new Error(`The error handler must be a function. Received ${typeof this.errorHandler}`); | ||
} | ||
if (this.errorHandler && typeof this.errorHandler !== 'function') { | ||
throw new Error(`The error handler must be a function. Received ${typeof this.errorHandler}`); | ||
} | ||
return this; | ||
} | ||
@@ -120,9 +120,9 @@ /** | ||
async process() { | ||
for (const item of this.items) { | ||
for (const [index, item] of this.items.entries()) { | ||
if (this.hasReachedConcurrencyLimit()) { | ||
await this.processingSlot(); | ||
} | ||
this.startProcessing(item); | ||
this.startProcessing(item, index); | ||
} | ||
return this.drained(); | ||
return await this.drained(); | ||
} | ||
@@ -136,3 +136,3 @@ /** | ||
async processingSlot() { | ||
return this.waitForTaskToFinish(); | ||
return await this.waitForTaskToFinish(); | ||
} | ||
@@ -148,6 +148,7 @@ /** | ||
* | ||
* @param {*} item | ||
* @param {T} item | ||
* @param {number} index | ||
*/ | ||
startProcessing(item) { | ||
const task = this.createTaskFor(item) | ||
startProcessing(item, index) { | ||
const task = this.createTaskFor(item, index) | ||
.then(result => { | ||
@@ -169,8 +170,9 @@ this.results.push(result); | ||
* | ||
* @param item | ||
* @param {T} item | ||
* @param {number} index | ||
* | ||
* @returns {*} | ||
*/ | ||
async createTaskFor(item) { | ||
return this.handler(item); | ||
async createTaskFor(item, index) { | ||
return this.handler(item, index); | ||
} | ||
@@ -177,0 +179,0 @@ /** |
import { ReturnValue } from './return-value'; | ||
import { ProcessHandler } from './promise-pool-executor'; | ||
export declare class PromisePool<T> { | ||
@@ -69,3 +70,3 @@ /** | ||
*/ | ||
process<R>(callback: (item: T) => R | Promise<R>): Promise<ReturnValue<T, R>>; | ||
process<R>(callback: ProcessHandler<T, R>): Promise<ReturnValue<T, R>>; | ||
} |
{ | ||
"name": "@supercharge/promise-pool", | ||
"description": "Map-like, concurrent promise processing for Node.js", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"author": "Marcus Pöhls <marcus@superchargejs.com>", | ||
@@ -12,12 +12,12 @@ "bugs": { | ||
"@supercharge/tsconfig": "~1.0.0", | ||
"@types/jest": "~26.0.22", | ||
"@typescript-eslint/eslint-plugin": "~4.21.0", | ||
"eslint": "~7.24.0", | ||
"eslint-config-standard-with-typescript": "~20.0.0", | ||
"eslint-plugin-import": "~2.22.1", | ||
"@types/jest": "~27.0.2", | ||
"@typescript-eslint/eslint-plugin": "~4.31.2", | ||
"eslint": "~7.32.0", | ||
"eslint-config-standard-with-typescript": "~21.0.1", | ||
"eslint-plugin-import": "~2.24.2", | ||
"eslint-plugin-node": "~11.1.0", | ||
"eslint-plugin-promise": "~4.3.1", | ||
"jest": "~26.6.3", | ||
"eslint-plugin-promise": "~5.1.0", | ||
"jest": "~27.2.1", | ||
"jest-extended": "~0.11.5", | ||
"typescript": "~4.2.4" | ||
"typescript": "~4.4.3" | ||
}, | ||
@@ -24,0 +24,0 @@ "engines": { |
@@ -60,4 +60,4 @@ <div align="center"> | ||
.for(users) | ||
.process(async data => { | ||
const user = await User.createIfNotExisting(data) | ||
.process(async (userData, index) => { | ||
const user = await User.createIfNotExisting(userData) | ||
@@ -134,3 +134,3 @@ return user | ||
> [superchargejs.com](https://superchargejs.com) · | ||
> GitHub [@superchargejs](https://github.com/superchargejs/) · | ||
> GitHub [@supercharge](https://github.com/supercharge) · | ||
> Twitter [@superchargejs](https://twitter.com/superchargejs) |
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
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
23583
17
605