From: Jérôme Benoit Date: Sun, 24 Sep 2023 15:51:05 +0000 (+0200) Subject: fix: make the transition to EventEmitterAsyncResource transparent to TS X-Git-Tag: v2.7.3~1 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=5b7d00b4d0f276c521db9f2145104a64f441d2fb;p=poolifier.git fix: make the transition to EventEmitterAsyncResource transparent to TS @types/node does not include EventEmitterAsyncResource type definition: https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/61365 Signed-off-by: Jérôme Benoit --- 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. *