From: Jérôme Benoit Date: Fri, 19 Feb 2021 16:57:47 +0000 (+0100) Subject: JSONValue can not be used by custom defined interfaces (#201) X-Git-Tag: v2.0.0-beta.6~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=deb85c12b77faf6974551cefcd9676e62a392086;p=poolifier.git JSONValue can not be used by custom defined interfaces (#201) Co-authored-by: Shinigami92 --- diff --git a/README.md b/README.md index 0b8b0307..95b3f0c1 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ You can implement a worker-threads worker in a simple way by extending the class '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 } @@ -137,6 +137,8 @@ You can do the same with the classes ClusterWorker, FixedClusterPool and Dynamic **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 diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index c951c6f2..0435a32c 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -94,8 +94,8 @@ class PoolEmitter extends EventEmitter {} * 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, @@ -168,7 +168,7 @@ export abstract class AbstractPool< /** * 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 { diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index d71abd01..2a25f7b3 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -1,5 +1,4 @@ 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' @@ -10,15 +9,15 @@ 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 { /** * Constructs a new poolifier dynamic cluster pool. diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 7cc3bf53..616523cb 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,5 +1,5 @@ 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' @@ -23,15 +23,15 @@ export interface ClusterPoolOptions extends PoolOptions { * * 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 { /** * Constructs a new poolifier fixed cluster pool. diff --git a/src/pools/pool.ts b/src/pools/pool.ts index f96e5c0c..f48d8843 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -1,14 +1,14 @@ /** * 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 { /** * 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 diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index 39867e6c..3e74a4ea 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -1,4 +1,3 @@ -import type { JSONValue } from '../../utility-types' import { isKillBehavior, KillBehaviors } from '../../worker/worker-options' import type { PoolOptions } from '../abstract-pool' import type { ThreadWorkerWithMessageChannel } from './fixed' @@ -10,15 +9,15 @@ import { FixedThreadPool } 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 { /** * Constructs a new poolifier dynamic thread pool. diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index 8ecf116c..a61c10b2 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -1,5 +1,5 @@ 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' @@ -15,15 +15,15 @@ export type ThreadWorkerWithMessageChannel = Worker & Draft * * 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 { /** * Constructs a new poolifier fixed thread pool. diff --git a/src/utility-types.ts b/src/utility-types.ts index 7ea86042..2403b8a2 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -7,24 +7,6 @@ import type { KillBehavior } from './worker/worker-options' */ export type Draft = { -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 - /** * Message object that is passed between worker and main worker. */ diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 03d954fe..d41115bc 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -12,8 +12,8 @@ const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT * 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, diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index 4b1efaca..6dea4c5e 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -1,6 +1,6 @@ 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' @@ -13,15 +13,15 @@ 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 { /** * Constructs a new poolifier cluster worker. diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index 615e1d8b..1070ed31 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -1,6 +1,6 @@ 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' @@ -13,15 +13,15 @@ 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 { /** * Constructs a new poolifier thread worker.