X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=ef0133ac716b2a01087799154f3ff414518309cc;hb=b5e75be8f136c28e738b90acb094b031b0517b07;hp=5a25f8d9bf233c4afdd205bfd504a6e43be23aab;hpb=9a38f99e676160c0bc7d28fe88f27b01fa31b5a1;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 5a25f8d9..ef0133ac 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -15,7 +15,7 @@ import { type IWorkerNode, type StrategyData, type WorkerInfo, - type WorkerNodeEventCallback, + type WorkerNodeEventDetail, type WorkerType, WorkerTypes, type WorkerUsage @@ -29,7 +29,8 @@ import { checkWorkerNodeArguments } from './utils' * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. */ export class WorkerNode -implements IWorkerNode { + extends EventTarget + implements IWorkerNode { /** @inheritdoc */ public readonly worker: Worker /** @inheritdoc */ @@ -42,10 +43,6 @@ implements IWorkerNode { public messageChannel?: MessageChannel /** @inheritdoc */ public tasksQueueBackPressureSize: number - /** @inheritdoc */ - public onBackPressure?: WorkerNodeEventCallback - /** @inheritdoc */ - public onEmptyQueue?: WorkerNodeEventCallback private readonly tasksQueue: Deque> private onBackPressureStarted: boolean private onEmptyQueueCount: number @@ -58,6 +55,7 @@ implements IWorkerNode { * @param tasksQueueBackPressureSize - The tasks queue back pressure size. */ constructor (worker: Worker, tasksQueueBackPressureSize: number) { + super() checkWorkerNodeArguments(worker, tasksQueueBackPressureSize) this.worker = worker this.info = this.initWorkerInfo(worker) @@ -80,13 +78,13 @@ implements IWorkerNode { /** @inheritdoc */ public enqueueTask (task: Task): number { const tasksQueueSize = this.tasksQueue.push(task) - if ( - this.onBackPressure != null && - this.hasBackPressure() && - !this.onBackPressureStarted - ) { + if (this.hasBackPressure() && !this.onBackPressureStarted) { this.onBackPressureStarted = true - this.onBackPressure(this.info.id as number) + this.dispatchEvent( + new CustomEvent('backPressure', { + detail: { workerId: this.info.id as number } + }) + ) this.onBackPressureStarted = false } return tasksQueueSize @@ -95,13 +93,13 @@ implements IWorkerNode { /** @inheritdoc */ public unshiftTask (task: Task): number { const tasksQueueSize = this.tasksQueue.unshift(task) - if ( - this.onBackPressure != null && - this.hasBackPressure() && - !this.onBackPressureStarted - ) { + if (this.hasBackPressure() && !this.onBackPressureStarted) { this.onBackPressureStarted = true - this.onBackPressure(this.info.id as number) + this.dispatchEvent( + new CustomEvent('backPressure', { + detail: { workerId: this.info.id as number } + }) + ) this.onBackPressureStarted = false } return tasksQueueSize @@ -110,11 +108,7 @@ implements IWorkerNode { /** @inheritdoc */ public dequeueTask (): Task | undefined { const task = this.tasksQueue.shift() - if ( - this.onEmptyQueue != null && - this.tasksQueue.size === 0 && - this.onEmptyQueueCount === 0 - ) { + if (this.tasksQueue.size === 0 && this.onEmptyQueueCount === 0) { this.startOnEmptyQueue().catch(EMPTY_FUNCTION) } return task @@ -123,11 +117,7 @@ implements IWorkerNode { /** @inheritdoc */ public popTask (): Task | undefined { const task = this.tasksQueue.pop() - if ( - this.onEmptyQueue != null && - this.tasksQueue.size === 0 && - this.onEmptyQueueCount === 0 - ) { + if (this.tasksQueue.size === 0 && this.onEmptyQueueCount === 0) { this.startOnEmptyQueue().catch(EMPTY_FUNCTION) } return task @@ -152,10 +142,10 @@ implements IWorkerNode { /** @inheritdoc */ public closeChannel (): void { if (this.messageChannel != null) { - this.messageChannel?.port1.unref() - this.messageChannel?.port2.unref() - this.messageChannel?.port1.close() - this.messageChannel?.port2.close() + this.messageChannel.port1.unref() + this.messageChannel.port2.unref() + this.messageChannel.port1.close() + this.messageChannel.port2.close() delete this.messageChannel } } @@ -198,7 +188,11 @@ implements IWorkerNode { return } ++this.onEmptyQueueCount - this.onEmptyQueue?.(this.info.id as number) + this.dispatchEvent( + new CustomEvent('emptyQueue', { + detail: { workerId: this.info.id as number } + }) + ) await sleep(exponentialDelay(this.onEmptyQueueCount)) await this.startOnEmptyQueue() } @@ -233,17 +227,17 @@ implements IWorkerNode { failed: 0 }, runTime: { - history: new CircularArray() + history: new CircularArray() }, waitTime: { - history: new CircularArray() + history: new CircularArray() }, elu: { idle: { - history: new CircularArray() + history: new CircularArray() }, active: { - history: new CircularArray() + history: new CircularArray() } } } @@ -274,17 +268,17 @@ implements IWorkerNode { failed: 0 }, runTime: { - history: new CircularArray() + history: new CircularArray() }, waitTime: { - history: new CircularArray() + history: new CircularArray() }, elu: { idle: { - history: new CircularArray() + history: new CircularArray() }, active: { - history: new CircularArray() + history: new CircularArray() } } }