From 4f3c3d894171421375559b43ce469bd5ccb475da Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 25 Feb 2023 22:48:27 +0100 Subject: [PATCH] refactor: freeze empty function type MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 4 ++-- src/pools/cluster/dynamic.ts | 3 ++- src/pools/cluster/fixed.ts | 3 ++- src/utils.ts | 6 +++--- src/worker/cluster-worker.ts | 6 +++++- src/worker/thread-worker.ts | 6 +++++- 6 files changed, 19 insertions(+), 9 deletions(-) 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) } -- 2.34.1