### `pool.execute(data, name)`
`data` (optional) An object that you want to pass to your worker implementation
-`name` (optional) A string with the task function name that you want to execute on the worker. Default: `'default'`
-This method is available on both pool implementations and returns a promise.
+`name` (optional) A string with the task function name that you want to execute on the worker. Default: `'default'`
+
+This method is available on both pool implementations and returns a promise with the task function execution response.
### `pool.destroy()`
-Destroy method is available on both pool implementations.
-This method will call the terminate method on each worker.
+This method is available on both pool implementations and will call the terminate method on each worker.
### `class YourWorker extends ThreadWorker/ClusterWorker`
-`taskFunctions` (mandatory) The task function or task functions object that you want to execute on the worker
+`taskFunctions` (mandatory) The task function or task functions object `{ name_1: fn_1, ..., name_n, fn_n }` that you want to execute on the worker
`opts` (optional) An object with these properties:
- `maxInactiveTime` (optional) - Max time to wait tasks to work on in milliseconds, after this period the new worker will die.
This option only apply to the newly created workers.
Default: `KillBehaviors.SOFT`
+#### `YourWorker.hasTaskFunction(name)`
+
+`name` (mandatory) The task function name
+
+This method is available on both worker implementations and returns a boolean.
+
+#### `YourWorker.addTaskFunction(name, fn)`
+
+`name` (mandatory) The task function name
+`fn` (mandatory) The task function
+
+This method is available on both worker implementations and returns a boolean.
+
+#### `YourWorker.removeTaskFunction(name)`
+
+`name` (mandatory) The task function name
+
+This method is available on both worker implementations and returns a boolean.
+
+#### `YourWorker.setDefaultTaskFunction(name)`
+
+`name` (mandatory) The task function name
+
+This method is available on both worker implementations and returns a boolean.
+
## General guidance
Performance is one of the main target of these worker pool implementations, we want to have a strong focus on this.
}
}
+ /**
+ * Checks if the worker has a task function with the given name.
+ *
+ * @param name - The name of the task function to check.
+ * @returns Whether the worker has a task function with the given name or not.
+ * @throws {@link TypeError} If the `name` parameter is not a string.
+ */
+ public hasTaskFunction (name: string): boolean {
+ if (typeof name !== 'string') {
+ throw new TypeError('name parameter is not a string')
+ }
+ return this.taskFunctions.has(name)
+ }
+
+ /**
+ * Adds a task function to the worker.
+ * If a task function with the same name already exists, it is replaced.
+ *
+ * @param name - The name of the task function to add.
+ * @param fn - The task function to add.
+ * @returns Whether the task function was added or not.
+ * @throws {@link TypeError} If the `name` parameter is not a string.
+ * @throws {@link Error} If the `name` parameter is the default task function reserved name.
+ * @throws {@link TypeError} If the `fn` parameter is not a function.
+ */
+ public addTaskFunction (
+ name: string,
+ fn: WorkerFunction<Data, Response>
+ ): boolean {
+ if (typeof name !== 'string') {
+ throw new TypeError('name parameter is not a string')
+ }
+ if (name === DEFAULT_TASK_NAME) {
+ throw new Error(
+ 'Cannot add a task function with the default reserved name'
+ )
+ }
+ if (typeof fn !== 'function') {
+ throw new TypeError('fn parameter is not a function')
+ }
+ try {
+ if (
+ this.taskFunctions.get(name) ===
+ this.taskFunctions.get(DEFAULT_TASK_NAME)
+ ) {
+ this.taskFunctions.set(DEFAULT_TASK_NAME, fn.bind(this))
+ }
+ this.taskFunctions.set(name, fn.bind(this))
+ return true
+ } catch {
+ return false
+ }
+ }
+
+ /**
+ * Removes a task function from the worker.
+ *
+ * @param name - The name of the task function to remove.
+ * @returns Whether the task function existed and was removed or not.
+ * @throws {@link TypeError} If the `name` parameter is not a string.
+ * @throws {@link Error} If the `name` parameter is the default task function reserved name.
+ * @throws {@link Error} If the `name` parameter is the task function used as default task function.
+ */
+ public removeTaskFunction (name: string): boolean {
+ if (typeof name !== 'string') {
+ throw new TypeError('name parameter is not a string')
+ }
+ if (name === DEFAULT_TASK_NAME) {
+ throw new Error(
+ 'Cannot remove the task function with the default reserved name'
+ )
+ }
+ if (
+ this.taskFunctions.get(name) === this.taskFunctions.get(DEFAULT_TASK_NAME)
+ ) {
+ throw new Error(
+ 'Cannot remove the task function used as the default task function'
+ )
+ }
+ return this.taskFunctions.delete(name)
+ }
+
+ /**
+ * Sets the default task function to use when no task function name is specified.
+ *
+ * @param name - The name of the task function to use as default task function.
+ * @returns Whether the default task function was set or not.
+ * @throws {@link TypeError} If the `name` parameter is not a string.
+ * @throws {@link Error} If the `name` parameter is the default task function reserved name.
+ * @throws {@link Error} If the `name` parameter is a non-existing task function.
+ */
+ public setDefaultTaskFunction (name: string): boolean {
+ if (typeof name !== 'string') {
+ throw new TypeError('name parameter is not a string')
+ }
+ if (name === DEFAULT_TASK_NAME) {
+ throw new Error(
+ 'Cannot set the default task function reserved name as the default task function'
+ )
+ }
+ if (!this.taskFunctions.has(name)) {
+ throw new Error(
+ 'Cannot set the default task function to a non-existing task function'
+ )
+ }
+ try {
+ this.taskFunctions.set(
+ DEFAULT_TASK_NAME,
+ this.taskFunctions.get(name)?.bind(this) as WorkerFunction<
+ Data,
+ Response
+ >
+ )
+ return true
+ } catch {
+ return false
+ }
+ }
+
/**
* Worker message listener.
*