From 5b7d00b4d0f276c521db9f2145104a64f441d2fb Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 24 Sep 2023 17:51:05 +0200 Subject: [PATCH] fix: make the transition to EventEmitterAsyncResource transparent to TS MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit @types/node does not include EventEmitterAsyncResource type definition: https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/61365 Signed-off-by: Jérôme Benoit --- src/index.ts | 1 - src/pools/abstract-pool.ts | 10 ++++++---- src/pools/pool.ts | 9 ++------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index fab0d8ed..f3bbe503 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,6 @@ export { export { PoolEvents, PoolTypes } from './pools/pool' export type { IPool, - PoolEmitter, PoolEvent, PoolInfo, PoolOptions, diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index f7e6f8d2..c56beaa6 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1,6 +1,7 @@ import { randomUUID } from 'node:crypto' import { performance } from 'node:perf_hooks' import type { TransferListItem } from 'node:worker_threads' +import { type EventEmitter, EventEmitterAsyncResource } from 'node:events' import type { MessageValue, PromiseResponseWrapper, @@ -22,7 +23,6 @@ import { KillBehaviors } from '../worker/worker-options' import type { TaskFunction } from '../worker/task-functions' import { type IPool, - PoolEmitter, PoolEvents, type PoolInfo, type PoolOptions, @@ -70,7 +70,7 @@ export abstract class AbstractPool< public readonly workerNodes: Array> = [] /** @inheritDoc */ - public emitter?: PoolEmitter + public emitter?: EventEmitter | EventEmitterAsyncResource /** * The task execution response promise map: @@ -262,7 +262,7 @@ export abstract class AbstractPool< } private initializeEventEmitter (): void { - this.emitter = new PoolEmitter({ + this.emitter = new EventEmitterAsyncResource({ name: `poolifier:${this.type}-${this.worker}-pool` }) } @@ -944,7 +944,9 @@ export abstract class AbstractPool< }) ) this.emitter?.emit(PoolEvents.destroy, this.info) - this.emitter?.emitDestroy() + if (this.emitter instanceof EventEmitterAsyncResource) { + this.emitter?.emitDestroy() + } this.started = false } diff --git a/src/pools/pool.ts b/src/pools/pool.ts index a8d8849b..118b2832 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -1,5 +1,5 @@ -import { EventEmitterAsyncResource } from 'node:events' import type { TransferListItem } from 'node:worker_threads' +import type { EventEmitter, EventEmitterAsyncResource } from 'node:events' import type { TaskFunction } from '../worker/task-functions' import type { ErrorHandler, @@ -34,11 +34,6 @@ export const PoolTypes = Object.freeze({ */ export type PoolType = keyof typeof PoolTypes -/** - * Pool event emitter integrated with async resource. - */ -export class PoolEmitter extends EventEmitterAsyncResource {} - /** * Enumeration of pool events. */ @@ -240,7 +235,7 @@ export interface IPool< * - `'taskError'`: Emitted when an error occurs while executing a task. * - `'backPressure'`: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size \>= maximum queue size). */ - readonly emitter?: PoolEmitter + readonly emitter?: EventEmitter | EventEmitterAsyncResource /** * Executes the specified function in the worker constructor with the task data input parameter. * -- 2.34.1