Skip to main content
The Deno 2 Release Candidate is here
Learn more
Latest
class AbstractWorker
Abstract
import { AbstractWorker } from "https://deno.land/x/poolifier@v0.3.14/src/worker/abstract-worker.ts";

Base class that implements some shared logic for all poolifier workers.

Constructors

new
AbstractWorker(
isMain: boolean | undefined,
mainWorker: MainWorker | undefined,
)

Constructs a new poolifier worker.

Type Parameters

MainWorker extends WorkerGlobalScope & globalThis
optional
Data = unknown
optional
Response = unknown

Properties

protected
optional
activeInterval: number

Handler id of the activeInterval worker activity check.

protected
abstract
optional
id: string

Worker id.

protected
lastTaskTimestamp: number

Timestamp of the last task processed by this worker.

protected
readonly
run: (task: Task<Data>) => void

Runs the given task.

protected
readonly
runAsync: (fn: TaskAsyncFunction<Data, Response>, task: Task<Data>) => void

Runs the given task function asynchronously.

protected
readonly
runSync: (fn: TaskSyncFunction<Data, Response>, task: Task<Data>) => void

Runs the given task function synchronously.

protected
optional
statistics: WorkerStatistics

Performance statistics computation requirements.

protected
taskFunctions: Map<string, TaskFunction<Data, Response>>

Task function(s) processed by the worker when the pool's execution function is invoked.

Methods

private
beginTaskPerformance(name?: string): TaskPerformance
private
checkActive(): void

Checks if the worker should be terminated, because its living too long.

private
checkMessageWorkerId(message: MessageValue<Data>): void

Check if the message worker id is set and matches the worker id.

private
checkTaskFunctions(taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response> | undefined): void

Checks if the taskFunctions parameter is passed to the constructor and valid.

private
checkWorkerOptions(opts: WorkerOptions): void
private
startCheckActive(): void

Starts the worker check active interval.

private
stopCheckActive(): void

Stops the worker check active interval.

Returns the main worker.

protected
handleError(error: Error | string): string

Handles an error and convert it to a string so it can be sent back to the main worker.

protected
handleKillMessage(_message: MessageValue<Data>): void

Handles a kill message sent by the main worker.

protected
abstract
handleReadyMessageEvent(messageEvent: MessageEvent<MessageValue<Data>>): void

Handles the ready message event sent by the main worker.

protected
messageEventListener(messageEvent: MessageEvent<MessageValue<Data>>): void

Worker message event listener.

Sends task function names to the main worker.

protected
abstract
sendToMainWorker(message: MessageValue<Response, Data>): void

Sends a message to main worker.

Adds a task function to the worker. If a task function with the same name already exists, it is replaced.

Checks if the worker has a task function with the given name.

Lists the names of the worker's task functions.

Removes a task function from the worker.

Sets the default task function to use in the worker.