+ /** @inheritDoc */
+ public execute (data: Data): Promise<Response> {
+ // Configure worker to handle message with the specified task
+ const worker = this.chooseWorker()
+ const messageId = ++this.nextMessageId
+ const res = this.internalExecute(worker, messageId)
+ this.checkAndEmitBusy()
+ data = data ?? ({} as Data)
+ this.sendToWorker(worker, { data, id: messageId })
+ return res
+ }
+
+ /** @inheritDoc */
+ public async destroy (): Promise<void> {
+ await Promise.all(this.workers.map(worker => this.destroyWorker(worker)))
+ }
+
+ /**
+ * Shutdowns given worker.
+ *
+ * @param worker A worker within `workers`.
+ */
+ protected abstract destroyWorker (worker: Worker): void | Promise<void>
+
+ /**
+ * Setup hook that can be overridden by a Poolifier pool implementation
+ * to run code before workers are created in the abstract constructor.
+ */
+ protected setupHook (): void {
+ // Can be overridden
+ }
+
+ /**
+ * Should return whether the worker is the main worker or not.
+ */
+ protected abstract isMain (): boolean
+
+ /**
+ * Hook executed before the worker task promise resolution.
+ * Can be overridden.
+ *
+ * @param worker The worker.
+ */
+ protected beforePromiseWorkerResponseHook (worker: Worker): void {
+ this.increaseWorkerRunningTasks(worker)
+ }
+
+ /**
+ * Hook executed after the worker task promise resolution.
+ * Can be overridden.
+ *
+ * @param message The received message.
+ * @param promise The Promise response.
+ */
+ protected afterPromiseWorkerResponseHook (
+ message: MessageValue<Response>,
+ promise: PromiseWorkerResponseWrapper<Worker, Response>
+ ): void {
+ this.decreaseWorkerRunningTasks(promise.worker)
+ this.stepWorkerRunTasks(promise.worker, 1)
+ this.updateWorkerTasksRunTime(promise.worker, message.taskRunTime)
+ }
+
+ /**
+ * Removes the given worker from the pool.
+ *
+ * @param worker The worker that will be removed.
+ */