From: Jérôme Benoit Date: Sat, 25 Feb 2023 21:48:27 +0000 (+0100) Subject: refactor: freeze empty function type X-Git-Tag: v2.3.8~18 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4f3c3d894171421375559b43ce469bd5ccb475da;p=poolifier.git refactor: freeze empty function type Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 76f5bfdc..4d572d0a 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -2,7 +2,7 @@ import type { MessageValue, PromiseWorkerResponseWrapper } from '../utility-types' -import { EMPTY_FUNCTION, EMPTY_LITERAL } from '../utils' +import { EMPTY_FUNCTION, EMPTY_OBJECT_LITERAL } from '../utils' import { isKillBehavior, KillBehaviors } from '../worker/worker-options' import type { PoolOptions } from './pool' import { PoolEmitter } from './pool' @@ -208,7 +208,7 @@ export abstract class AbstractPool< const res = this.internalExecute(worker, this.nextMessageId) this.checkAndEmitBusy() this.sendToWorker(worker, { - data: data ?? (EMPTY_LITERAL as Data), + data: data ?? (EMPTY_OBJECT_LITERAL as Data), id: this.nextMessageId }) ++this.nextMessageId diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index 8b8c5185..513a73c5 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -1,3 +1,4 @@ +import { EMPTY_OBJECT_LITERAL } from '../../utils' import { PoolType } from '../pool-internal' import type { ClusterPoolOptions } from './fixed' import { FixedClusterPool } from './fixed' @@ -29,7 +30,7 @@ export class DynamicClusterPool< min: number, protected readonly max: number, filePath: string, - opts: ClusterPoolOptions = {} + opts: ClusterPoolOptions = EMPTY_OBJECT_LITERAL ) { super(min, filePath, opts) } diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 2e89b323..092e9778 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,6 +1,7 @@ import type { ClusterSettings, Worker } from 'cluster' import cluster from 'cluster' import type { MessageValue } from '../../utility-types' +import { EMPTY_OBJECT_LITERAL } from '../../utils' import { AbstractPool } from '../abstract-pool' import type { PoolOptions } from '../pool' import { PoolType } from '../pool-internal' @@ -50,7 +51,7 @@ export class FixedClusterPool< public constructor ( numberOfWorkers: number, filePath: string, - public readonly opts: ClusterPoolOptions = {} + public readonly opts: ClusterPoolOptions = EMPTY_OBJECT_LITERAL ) { super(numberOfWorkers, filePath, opts) } diff --git a/src/utils.ts b/src/utils.ts index 46ab5051..753219c5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,11 +1,11 @@ /** * An intentional empty function. */ -export const EMPTY_FUNCTION: () => void = () => { +export const EMPTY_FUNCTION: () => void = Object.freeze(() => { /* Intentionally empty */ -} +}) /** * An intentional empty object literal. */ -export const EMPTY_LITERAL = {} +export const EMPTY_OBJECT_LITERAL = {} diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index 4f5fbb6b..f551447e 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -1,6 +1,7 @@ import type { Worker } from 'cluster' import cluster from 'cluster' import type { MessageValue } from '../utility-types' +import { EMPTY_OBJECT_LITERAL } from '../utils' import { AbstractWorker } from './abstract-worker' import type { WorkerOptions } from './worker-options' @@ -28,7 +29,10 @@ export class ClusterWorker< * @param fn - Function processed by the worker when the pool's `execution` function is invoked. * @param opts - Options for the worker. */ - public constructor (fn: (data: Data) => Response, opts: WorkerOptions = {}) { + public constructor ( + fn: (data: Data) => Response, + opts: WorkerOptions = EMPTY_OBJECT_LITERAL + ) { super( 'worker-cluster-pool:poolifier', cluster.isPrimary, diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index a8424fbf..4ed85c29 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -1,6 +1,7 @@ import type { MessagePort } from 'worker_threads' import { isMainThread, parentPort } from 'worker_threads' import type { MessageValue } from '../utility-types' +import { EMPTY_OBJECT_LITERAL } from '../utils' import { AbstractWorker } from './abstract-worker' import type { WorkerOptions } from './worker-options' @@ -28,7 +29,10 @@ export class ThreadWorker< * @param fn - Function processed by the worker when the pool's `execution` function is invoked. * @param opts - Options for the worker. */ - public constructor (fn: (data: Data) => Response, opts: WorkerOptions = {}) { + public constructor ( + fn: (data: Data) => Response, + opts: WorkerOptions = EMPTY_OBJECT_LITERAL + ) { super('worker-thread-pool:poolifier', isMainThread, fn, parentPort, opts) }