X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=12fb67af32934383a9498e541fadc031e85cbafe;hb=0d80593b9a7596645612087f687fc6f5cab3101a;hp=2aca9e914b2f4f296624f842ccbee9e159790815;hpb=227be16668f5a3adf78a255c20e956d6f5b3f567;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 2aca9e91..12fb67af 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -1,14 +1,14 @@ import { AsyncResource } from 'node:async_hooks' import type { Worker } from 'node:cluster' import type { MessagePort } from 'node:worker_threads' -import { - type MessageValue, - type TaskFunctions, - type WorkerAsyncFunction, - type WorkerFunction, - type WorkerSyncFunction +import type { + MessageValue, + TaskFunctions, + WorkerAsyncFunction, + WorkerFunction, + WorkerSyncFunction } from '../utility-types' -import { EMPTY_FUNCTION } from '../utils' +import { EMPTY_FUNCTION, isPlainObject } from '../utils' import type { KillBehavior, WorkerOptions } from './worker-options' import { KillBehaviors } from './worker-options' @@ -45,7 +45,7 @@ export abstract class AbstractWorker< * * @param type - The type of async event. * @param isMain - Whether this is the main worker or not. - * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. + * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function. * @param mainWorker - Reference to main worker. * @param opts - Options for the worker. */ @@ -80,12 +80,7 @@ export abstract class AbstractWorker< this.checkAlive.bind(this)() } - this.mainWorker?.on( - 'message', - (message: MessageValue) => { - this.messageListener(message) - } - ) + this.mainWorker?.on('message', this.messageListener.bind(this)) } private checkWorkerOptions (opts: WorkerOptions): void { @@ -98,7 +93,7 @@ export abstract class AbstractWorker< /** * Checks if the `taskFunctions` parameter is passed to the constructor. * - * @param taskFunctions - The task function(s) that should be defined. + * @param taskFunctions - The task function(s) parameter that should be checked. */ private checkTaskFunctions ( taskFunctions: @@ -112,27 +107,32 @@ export abstract class AbstractWorker< typeof taskFunctions !== 'function' && typeof taskFunctions !== 'object' ) { - throw new Error('taskFunctions parameter is not a function or an object') - } - if ( - typeof taskFunctions === 'object' && - taskFunctions.constructor !== Object && - Object.prototype.toString.call(taskFunctions) !== '[object Object]' - ) { - throw new Error('taskFunctions parameter is not an object literal') + throw new TypeError( + 'taskFunctions parameter is not a function or an object' + ) } this.taskFunctions = new Map>() - if (typeof taskFunctions !== 'function') { + if (typeof taskFunctions === 'function') { + this.taskFunctions.set(DEFAULT_FUNCTION_NAME, taskFunctions.bind(this)) + } else if (isPlainObject(taskFunctions)) { + let firstEntry = true for (const [name, fn] of Object.entries(taskFunctions)) { if (typeof fn !== 'function') { - throw new Error( + throw new TypeError( 'A taskFunctions parameter object value is not a function' ) } this.taskFunctions.set(name, fn.bind(this)) + if (firstEntry) { + this.taskFunctions.set(DEFAULT_FUNCTION_NAME, fn.bind(this)) + firstEntry = false + } + } + if (firstEntry) { + throw new Error('taskFunctions parameter object is empty') } } else { - this.taskFunctions.set(DEFAULT_FUNCTION_NAME, taskFunctions.bind(this)) + throw new TypeError('taskFunctions parameter is not an object literal') } } @@ -148,7 +148,7 @@ export abstract class AbstractWorker< if (fn?.constructor.name === 'AsyncFunction') { this.runInAsyncScope(this.runAsync.bind(this), this, fn, message) } else { - this.runInAsyncScope(this.run.bind(this), this, fn, message) + this.runInAsyncScope(this.runSync.bind(this), this, fn, message) } } else if (message.parent != null) { // Main worker reference message received @@ -207,7 +207,7 @@ export abstract class AbstractWorker< * @param fn - Function that will be executed. * @param message - Input data for the given function. */ - protected run ( + protected runSync ( fn: WorkerSyncFunction, message: MessageValue ): void { @@ -259,6 +259,11 @@ export abstract class AbstractWorker< .catch(EMPTY_FUNCTION) } + /** + * Gets the task function in the given scope. + * + * @param name - Name of the function that will be returned. + */ private getTaskFunction (name?: string): WorkerFunction { name = name ?? DEFAULT_FUNCTION_NAME const fn = this.taskFunctions.get(name)