* Basic interface that describes the minimum required implementation of listener events for a pool-worker.
*/
export interface IWorker {
+ /**
+ * Register a listener to the error event.
+ *
+ * @param event `'error'`.
+ * @param handler The error handler.
+ */
on(event: 'error', handler: ErrorHandler<this>): void
+ /**
+ * Register a listener to the online event.
+ *
+ * @param event `'online'`.
+ * @param handler The online handler.
+ */
on(event: 'online', handler: OnlineHandler<this>): void
+ /**
+ * Register a listener to the exit event.
+ *
+ * @param event `'exit'`.
+ * @param handler The exit handler.
+ */
on(event: 'exit', handler: ExitHandler<this>): void
+ /**
+ * Register a listener to the exit event that will only performed once.
+ *
+ * @param event `'exit'`.
+ * @param handler The exit handler.
+ */
once(event: 'exit', handler: ExitHandler<this>): void
}
public nextWorkerIndex: number = 0
/**
+ * The tasks map.
+ *
* - `key`: The `Worker`
* - `value`: Number of tasks currently in progress on the worker.
*/
return this.nextWorkerIndex
}
+ /**
+ * Perform the task specified in the constructor with the data parameter.
+ *
+ * @param data The input for the specified task.
+ * @returns Promise that will be resolved when the task is successfully completed.
+ */
public execute (data: Data): Promise<Response> {
// Configure worker to handle message with the specified task
const worker = this.chooseWorker()
return res
}
+ /**
+ * Shut down every current worker in this pool.
+ */
public async destroy (): Promise<void> {
await Promise.all(this.workers.map(worker => this.destroyWorker(worker)))
}