Skip to content

Commit

Permalink
remove JS doc - we have TypeScript types
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuspoehls committed Mar 26, 2024
1 parent dab506c commit f39964e
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 160 deletions.
9 changes: 0 additions & 9 deletions src/promise-pool-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,13 @@ export class PromisePoolError<T, E = any> extends Error {

/**
* Returns a new promise pool error instance wrapping the `error` and `item`.
*
* @param {*} error
* @param {*} item
*
* @returns {PromisePoolError}
*/
static createFrom<T, E = any>(error: E, item: T): PromisePoolError<T> {
return new this(error, item)
}

/**
* Returns the error message from the given `error`.
*
* @param {*} error
*
* @returns {String}
*/
private messageFrom (error: any): string {
if (error instanceof Error) {
Expand Down
108 changes: 1 addition & 107 deletions src/promise-pool-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Set the number of tasks to process concurrently the promise pool.
*
* @param {Integer} concurrency
*
* @returns {PromisePoolExecutor}
*/
useConcurrency (concurrency: number): this {
if (!this.isValidConcurrency(concurrency)) {
Expand All @@ -120,21 +116,13 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Determine whether the given `concurrency` value is valid.
*
* @param {Number} concurrency
*
* @returns {Boolean}
*/
private isValidConcurrency (concurrency: number): boolean {
return typeof concurrency === 'number' && concurrency >= 1
}

/**
* Set the timeout in ms for the pool handler
*
* @param {Number} timeout
*
* @returns {PromisePool}
*/
withTaskTimeout (timeout: number | undefined): this {
this.meta.taskTimeout = timeout
Expand All @@ -144,8 +132,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Returns the number of concurrently processed tasks.
*
* @returns {Number}
*/
concurrency (): number {
return this.meta.concurrency
Expand Down Expand Up @@ -176,10 +162,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Set the items to be processed in the promise pool.
*
* @param {Array} items
*
* @returns {PromisePoolExecutor}
*/
for (items: SomeIterable<T>): this {
this.meta.items = items
Expand All @@ -189,17 +171,13 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Returns the list of items to process.
*
* @returns {T[] | Iterable<T> | AsyncIterable<T>}
*/
items (): SomeIterable<T> {
return this.meta.items
}

/**
* Returns the number of items to process, or `NaN` if items are not an array.
*
* @returns {Number}
*/
itemsCount (): number {
const items = this.items()
Expand All @@ -208,8 +186,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Returns the list of active tasks.
*
* @returns {Array}
*/
tasks (): any[] {
return this.meta.tasks
Expand All @@ -218,8 +194,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St
/**
* Returns the number of currently active tasks.
*
* @returns {Number}
*
* @deprecated use the `activeTasksCount()` method (plural naming) instead
*/
activeTaskCount (): number {
Expand All @@ -228,26 +202,20 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Returns the number of currently active tasks.
*
* @returns {Number}
*/
activeTasksCount (): number {
return this.tasks().length
}

/**
* Returns the list of processed items.
*
* @returns {T[]}
*/
processedItems (): T[] {
return this.meta.processedItems
}

/**
* Returns the number of processed items.
*
* @returns {Number}
*/
processedCount (): number {
return this.processedItems().length
Expand All @@ -262,28 +230,20 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Returns the list of results.
*
* @returns {R[]}
*/
results (): Array<R | symbol> {
return this.meta.results
}

/**
* Returns the list of errors.
*
* @returns {Array<PromisePoolError<T>>}
*/
errors (): Array<PromisePoolError<T>> {
return this.meta.errors
}

/**
* Set the handler that is applied to each item.
*
* @param {Function} action
*
* @returns {PromisePoolExecutor}
*/
withHandler (action: ProcessHandler<T, R>): this {
this.handler = action
Expand All @@ -293,19 +253,13 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Determine whether a custom error handle is available.
*
* @returns {Boolean}
*/
hasErrorHandler (): boolean {
return !!this.errorHandler
}

/**
* Set the error handler function to execute when an error occurs.
*
* @param {Function} errorHandler
*
* @returns {PromisePoolExecutor}
*/
handleError (handler?: (error: Error, item: T, pool: Stoppable & UsesConcurrency) => Promise<void> | void): this {
this.errorHandler = handler
Expand All @@ -315,10 +269,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Set the handler function to execute when started a task.
*
* @param {Function} handler
*
* @returns {this}
*/
onTaskStarted (handlers: Array<OnProgressCallback<T>>): this {
this.onTaskStartedHandlers = handlers
Expand All @@ -328,10 +278,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Assign the given callback `handler` function to run when a task finished.
*
* @param {OnProgressCallback<T>} handlers
*
* @returns {this}
*/

onTaskFinished (handlers: Array<OnProgressCallback<T>>): this {
Expand All @@ -342,8 +288,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Determines whether the number of active tasks is greater or equal to the concurrency limit.
*
* @returns {Boolean}
*/
hasReachedConcurrencyLimit (): boolean {
return this.activeTasksCount() >= this.concurrency()
Expand All @@ -360,8 +304,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Mark the promise pool as stopped.
*
* @returns {PromisePoolExecutor}
*/
markAsStopped (): this {
this.meta.stopped = true
Expand All @@ -371,17 +313,13 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Determine whether the pool is stopped.
*
* @returns {Boolean}
*/
isStopped (): boolean {
return this.meta.stopped
}

/**
* Start processing the promise pool.
*
* @returns {ReturnValue}
*/
async start (): Promise<any> {
return await this
Expand All @@ -391,11 +329,7 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St
}

/**
* Determine whether the pool should stop.
*
* @returns {PromisePoolExecutor}
*
* @throws
* Ensure that the given input values are valid or throw an error otherwise.
*/
validateInputs (): this {
if (typeof this.handler !== 'function') {
Expand Down Expand Up @@ -459,10 +393,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St
/**
* Starts processing the promise pool by iterating over the items
* and running each item through the async `callback` function.
*
* @param {Function} callback
*
* @returns {Promise}
*/
async process (): Promise<ReturnValue<T, R>> {
let index = 0
Expand Down Expand Up @@ -512,9 +442,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Create a processing function for the given `item`.
*
* @param {T} item
* @param {number} index
*/
startProcessing (item: T, index: number): void {
const task: Promise<void> = this.createTaskFor(item, index)
Expand All @@ -536,11 +463,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Ensures a returned promise for the processing of the given `item`.
*
* @param {T} item
* @param {number} index
*
* @returns {*}
*/
async createTaskFor (item: T, index: number): Promise<any> {
if (this.taskTimeout() === undefined) {
Expand Down Expand Up @@ -576,11 +498,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Save the given calculation `result`, possibly at the provided `position`.
*
* @param {*} result
* @param {number} position
*
* @returns {PromisePoolExecutor}
*/
save (result: any, position: number): this {
this.shouldUseCorrespondingResults()
Expand All @@ -592,8 +509,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Remove the given `task` from the list of active tasks.
*
* @param {Promise} task
*/
removeActive (task: Promise<void>): this {
this.tasks().splice(
Expand All @@ -605,10 +520,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Create and save an error for the the given `item`.
*
* @param {Error} error
* @param {T} item
* @param {number} index
*/
async handleErrorFor (error: Error, item: T, index: number): Promise<void> {
if (this.shouldUseCorrespondingResults()) {
Expand All @@ -631,31 +542,20 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Determine whether the given `error` is a `StopThePromisePoolError` instance.
*
* @param {Error} error
*
* @returns {Boolean}
*/
isStoppingThePoolError (error: Error): boolean {
return error instanceof StopThePromisePoolError
}

/**
* Determine whether the given `error` is a `ValidationError` instance.
*
* @param {Error} error
*
* @returns {Boolean}
*/
isValidationError (error: Error): boolean {
return error instanceof ValidationError
}

/**
* Run the user’s error handler, if available.
*
* @param {Error} processingError
* @param {T} item
*/
async runErrorHandlerFor (processingError: Error, item: T): Promise<void> {
try {
Expand Down Expand Up @@ -685,8 +585,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Rethrow the given `error` if it’s not an instance of `StopThePromisePoolError`.
*
* @param {Error} error
*/
rethrowIfNotStoppingThePool (error: Error): void {
if (this.isStoppingThePoolError(error)) {
Expand All @@ -698,8 +596,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St

/**
* Create and save an error for the the given `item`.
*
* @param {T} item
*/
saveErrorFor (error: Error, item: T): void {
this.errors().push(
Expand All @@ -710,8 +606,6 @@ export class PromisePoolExecutor<T, R> implements UsesConcurrency, Stoppable, St
/**
* Wait for all active tasks to finish. Once all the tasks finished
* processing, returns an object containing the results and errors.
*
* @returns {Object}
*/
async drained (): Promise<ReturnValue<T, any>> {
await this.drainActiveTasks()
Expand Down
Loading

0 comments on commit f39964e

Please sign in to comment.