type IWorkerNode,
type StrategyData,
type WorkerInfo,
- type WorkerNodeEventCallback,
+ type WorkerNodeEventDetail,
type WorkerType,
WorkerTypes,
type WorkerUsage
} from './worker'
+import { checkWorkerNodeArguments } from './utils'
/**
* Worker node.
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
*/
export class WorkerNode<Worker extends IWorker, Data = unknown>
-implements IWorkerNode<Worker, Data> {
+ extends EventTarget
+ implements IWorkerNode<Worker, Data> {
/** @inheritdoc */
public readonly worker: Worker
/** @inheritdoc */
public messageChannel?: MessageChannel
/** @inheritdoc */
public tasksQueueBackPressureSize: number
- /** @inheritdoc */
- public onBackPressure?: WorkerNodeEventCallback
- /** @inheritdoc */
- public onEmptyQueue?: WorkerNodeEventCallback
private readonly tasksQueue: Deque<Task<Data>>
private onBackPressureStarted: boolean
private onEmptyQueueCount: number
* @param tasksQueueBackPressureSize - The tasks queue back pressure size.
*/
constructor (worker: Worker, tasksQueueBackPressureSize: number) {
- this.checkWorkerNodeArguments(worker, tasksQueueBackPressureSize)
+ super()
+ checkWorkerNodeArguments<Worker>(worker, tasksQueueBackPressureSize)
this.worker = worker
this.info = this.initWorkerInfo(worker)
this.usage = this.initWorkerUsage()
/** @inheritdoc */
public enqueueTask (task: Task<Data>): 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<WorkerNodeEventDetail>('backpressure', {
+ detail: { workerId: this.info.id as number }
+ })
+ )
this.onBackPressureStarted = false
}
return tasksQueueSize
/** @inheritdoc */
public unshiftTask (task: Task<Data>): 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<WorkerNodeEventDetail>('backpressure', {
+ detail: { workerId: this.info.id as number }
+ })
+ )
this.onBackPressureStarted = false
}
return tasksQueueSize
/** @inheritdoc */
public dequeueTask (): Task<Data> | 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
/** @inheritdoc */
public popTask (): Task<Data> | 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
/** @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
}
}
return this.taskFunctionsUsage.get(name)
}
+ /** @inheritdoc */
+ public deleteTaskFunctionWorkerUsage (name: string): boolean {
+ return this.taskFunctionsUsage.delete(name)
+ }
+
private async startOnEmptyQueue (): Promise<void> {
if (
this.onEmptyQueueCount > 0 &&
return
}
++this.onEmptyQueueCount
- this.onEmptyQueue?.(this.info.id as number)
+ this.dispatchEvent(
+ new CustomEvent<WorkerNodeEventDetail>('emptyqueue', {
+ detail: { workerId: this.info.id as number }
+ })
+ )
await sleep(exponentialDelay(this.onEmptyQueueCount))
await this.startOnEmptyQueue()
}
failed: 0
},
runTime: {
- history: new CircularArray()
+ history: new CircularArray<number>()
},
waitTime: {
- history: new CircularArray()
+ history: new CircularArray<number>()
},
elu: {
idle: {
- history: new CircularArray()
+ history: new CircularArray<number>()
},
active: {
- history: new CircularArray()
+ history: new CircularArray<number>()
}
}
}
failed: 0
},
runTime: {
- history: new CircularArray()
+ history: new CircularArray<number>()
},
waitTime: {
- history: new CircularArray()
+ history: new CircularArray<number>()
},
elu: {
idle: {
- history: new CircularArray()
+ history: new CircularArray<number>()
},
active: {
- history: new CircularArray()
+ history: new CircularArray<number>()
}
}
}
}
-
- private checkWorkerNodeArguments (
- worker: Worker,
- tasksQueueBackPressureSize: number
- ): void {
- if (worker == null) {
- throw new TypeError('Cannot construct a worker node without a worker')
- }
- if (tasksQueueBackPressureSize == null) {
- throw new TypeError(
- 'Cannot construct a worker node without a tasks queue back pressure size'
- )
- }
- if (!Number.isSafeInteger(tasksQueueBackPressureSize)) {
- throw new TypeError(
- 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
- )
- }
- if (tasksQueueBackPressureSize <= 0) {
- throw new RangeError(
- 'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer'
- )
- }
- }
}