'use strict'
const { ThreadWorker } = require('poolifier')
-function yourFunction(data) {
+function yourFunction (data) {
// this will be executed in the worker thread,
// the data will be received by using the execute method
return { ok: 1 }
**See examples folder for more details (in particular if you want to use a pool for [multiple functions](./examples/multiFunctionExample.js)).**
**Now TypeScript is also supported, find how to use it into the example folder**.
+Remember that workers can only send and receive serializable (JSON) data.
+
## Node versions
You can use node versions 12.x, 13.x, 14.x
* Base class containing some shared logic for all poolifier pools.
*
* @template Worker Type of worker which manages this pool.
- * @template Data Type of data sent to the worker.
- * @template Response Type of response of execution.
+ * @template Data Type of data sent to the worker. This can only be serializable data.
+ * @template Response Type of response of execution. This can only be serializable data.
*/
export abstract class AbstractPool<
Worker extends IWorker,
/**
* Perform the task specified in the constructor with the data parameter.
*
- * @param data The input for the specified task.
+ * @param data The input for the specified task. This can only be serializable data.
* @returns Promise that will be resolved when the task is successfully completed.
*/
public execute (data: Data): Promise<Response> {
import type { Worker } from 'cluster'
-import type { JSONValue } from '../../utility-types'
import { isKillBehavior, KillBehaviors } from '../../worker/worker-options'
import type { ClusterPoolOptions } from './fixed'
import { FixedClusterPool } from './fixed'
* This cluster pool creates new workers when the others are busy, up to the maximum number of workers.
* When the maximum number of workers is reached, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
*
- * @template Data Type of data sent to the worker.
- * @template Response Type of response of execution.
+ * @template Data Type of data sent to the worker. This can only be serializable data.
+ * @template Response Type of response of execution. This can only be serializable data.
*
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
* @since 2.0.0
*/
export class DynamicClusterPool<
- Data extends JSONValue = JSONValue,
- Response extends JSONValue = JSONValue
+ Data = unknown,
+ Response = unknown
> extends FixedClusterPool<Data, Response> {
/**
* Constructs a new poolifier dynamic cluster pool.
import { fork, isMaster, setupMaster, Worker } from 'cluster'
-import type { JSONValue, MessageValue } from '../../utility-types'
+import type { MessageValue } from '../../utility-types'
import type { PoolOptions } from '../abstract-pool'
import { AbstractPool } from '../abstract-pool'
*
* This pool selects the workers in a round robin fashion.
*
- * @template Data Type of data sent to the worker.
- * @template Response Type of response of execution.
+ * @template Data Type of data sent to the worker. This can only be serializable data.
+ * @template Response Type of response of execution. This can only be serializable data.
*
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
* @since 2.0.0
*/
export class FixedClusterPool<
- Data extends JSONValue = JSONValue,
- Response extends JSONValue = JSONValue
+ Data = unknown,
+ Response = unknown
> extends AbstractPool<Worker, Data, Response> {
/**
* Constructs a new poolifier fixed cluster pool.
/**
* Contract definition for a poolifier pool.
*
- * @template Data Type of data sent to the worker.
- * @template Response Type of response of execution.
+ * @template Data Type of data sent to the worker. This can only be serializable data.
+ * @template Response Type of response of execution. This can only be serializable data.
*/
export interface IPool<Data = unknown, Response = unknown> {
/**
* Perform the task specified in the constructor with the data parameter.
*
- * @param data The input for the specified task.
+ * @param data The input for the specified task. This can only be serializable data.
* @returns Promise that will be resolved when the task is successfully completed.
*/
execute(data: Data): Promise<Response>
-import type { JSONValue } from '../../utility-types'
import { isKillBehavior, KillBehaviors } from '../../worker/worker-options'
import type { PoolOptions } from '../abstract-pool'
import type { ThreadWorkerWithMessageChannel } from './fixed'
* This thread pool creates new threads when the others are busy, up to the maximum number of threads.
* When the maximum number of threads is reached, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
*
- * @template Data Type of data sent to the worker.
- * @template Response Type of response of execution.
+ * @template Data Type of data sent to the worker. This can only be serializable data.
+ * @template Response Type of response of execution. This can only be serializable data.
*
* @author [Alessandro Pio Ardizio](https://github.com/pioardi)
* @since 0.0.1
*/
export class DynamicThreadPool<
- Data extends JSONValue = JSONValue,
- Response extends JSONValue = JSONValue
+ Data = unknown,
+ Response = unknown
> extends FixedThreadPool<Data, Response> {
/**
* Constructs a new poolifier dynamic thread pool.
import { isMainThread, MessageChannel, SHARE_ENV, Worker } from 'worker_threads'
-import type { Draft, JSONValue, MessageValue } from '../../utility-types'
+import type { Draft, MessageValue } from '../../utility-types'
import type { PoolOptions } from '../abstract-pool'
import { AbstractPool } from '../abstract-pool'
*
* This pool selects the threads in a round robin fashion.
*
- * @template Data Type of data sent to the worker.
- * @template Response Type of response of execution.
+ * @template Data Type of data sent to the worker. This can only be serializable data.
+ * @template Response Type of response of execution. This can only be serializable data.
*
* @author [Alessandro Pio Ardizio](https://github.com/pioardi)
* @since 0.0.1
*/
export class FixedThreadPool<
- Data extends JSONValue = JSONValue,
- Response extends JSONValue = JSONValue
+ Data = unknown,
+ Response = unknown
> extends AbstractPool<ThreadWorkerWithMessageChannel, Data, Response> {
/**
* Constructs a new poolifier fixed thread pool.
*/
export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
-/**
- * Serializable primitive JSON value.
- */
-export type JSONPrimitive = number | boolean | string | null
-/**
- * Serializable JSON value.
- */
-// eslint-disable-next-line no-use-before-define
-export type JSONValue = JSONPrimitive | JSONArray | JSONObject
-/**
- * Serializable JSON object.
- */
-export type JSONObject = { [k: string]: JSONValue }
-/**
- * Serializable JSON array.
- */
-export type JSONArray = Array<JSONValue>
-
/**
* Message object that is passed between worker and main worker.
*/
* Base class containing some shared logic for all poolifier workers.
*
* @template MainWorker Type of main worker.
- * @template Data Type of data this worker receives from pool's execution.
- * @template Response Type of response the worker sends back to the main worker.
+ * @template Data Type of data this worker receives from pool's execution. This can only be serializable data.
+ * @template Response Type of response the worker sends back to the main worker. This can only be serializable data.
*/
export abstract class AbstractWorker<
MainWorker extends Worker | MessagePort,
import type { Worker } from 'cluster'
import { isMaster, worker } from 'cluster'
-import type { JSONValue, MessageValue } from '../utility-types'
+import type { MessageValue } from '../utility-types'
import { AbstractWorker } from './abstract-worker'
import type { WorkerOptions } from './worker-options'
* If you use a `DynamicClusterPool` the extra workers that were created will be terminated,
* but the minimum number of workers will be guaranteed.
*
- * @template Data Type of data this worker receives from pool's execution.
- * @template Response Type of response the worker sends back to the main worker.
+ * @template Data Type of data this worker receives from pool's execution. This can only be serializable data.
+ * @template Response Type of response the worker sends back to the main worker. This can only be serializable data.
*
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
* @since 2.0.0
*/
export class ClusterWorker<
- Data extends JSONValue = JSONValue,
- Response extends JSONValue = JSONValue
+ Data = unknown,
+ Response = unknown
> extends AbstractWorker<Worker, Data, Response> {
/**
* Constructs a new poolifier cluster worker.
import type { MessagePort } from 'worker_threads'
import { isMainThread, parentPort } from 'worker_threads'
-import type { JSONValue, MessageValue } from '../utility-types'
+import type { MessageValue } from '../utility-types'
import { AbstractWorker } from './abstract-worker'
import type { WorkerOptions } from './worker-options'
* If you use a `DynamicThreadPool` the extra workers that were created will be terminated,
* but the minimum number of workers will be guaranteed.
*
- * @template Data Type of data this worker receives from pool's execution.
- * @template Response Type of response the worker sends back to the main thread.
+ * @template Data Type of data this worker receives from pool's execution. This can only be serializable data.
+ * @template Response Type of response the worker sends back to the main thread. This can only be serializable data.
*
* @author [Alessandro Pio Ardizio](https://github.com/pioardi)
* @since 0.0.1
*/
export class ThreadWorker<
- Data extends JSONValue = JSONValue,
- Response extends JSONValue = JSONValue
+ Data = unknown,
+ Response = unknown
> extends AbstractWorker<MessagePort, Data, Response> {
/**
* Constructs a new poolifier thread worker.