Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ThreadWorker<Data, Response>

A thread worker used by a poolifier ThreadPool.

When this worker is inactive for more than the given maxInactiveTime, it will send a termination request to its main thread.

If you use a DynamicThreadPool the extra workers that were created will be terminated, but the minimum number of workers will be guaranteed.

author

Alessandro Pio Ardizio

since

0.0.1

Type parameters

  • Data = unknown

    Type of data this worker receives from pool's execution. This can only be serializable data.

  • Response = unknown

    Type of response the worker sends back to the main thread. This can only be serializable data.

Hierarchy

Index

Constructors

constructor

  • Constructs a new poolifier thread worker.

    Type parameters

    • Data = unknown

    • Response = unknown

    Parameters

    • fn: (data: Data) => Response

      Function processed by the worker when the pool's execution function is invoked.

        • (data: Data): Response
        • Parameters

          • data: Data

          Returns Response

    • opts: WorkerOptions = ...

      Options for the worker.

    Returns ThreadWorker<Data, Response>

Properties

Protected Readonly async

async: boolean

Whether the worker is working asynchronously or not.

Protected Optional Readonly interval

interval: undefined | Timeout

Handler ID of the interval alive check.

Protected Readonly killBehavior

killBehavior: "SOFT" | "HARD"

The kill behavior set as option on the Worker constructor or a default value.

Protected lastTask

lastTask: number

Timestamp of the last task processed by this worker.

Protected Optional mainWorker

mainWorker: undefined | null | MessagePort

Protected Readonly maxInactiveTime

maxInactiveTime: number

The maximum time to keep this worker alive while idle. The pool automatically checks and terminates this worker when the time expires.

Readonly opts

opts: WorkerOptions = ...

Methods

asyncId

  • asyncId(): number
  • Returns number

    the unique ID assigned to this AsyncResource instance.

bind

  • bind<Func>(fn: Func): Func & { asyncResource: AsyncResource }
  • Binds the given function to execute to this AsyncResource's scope.

    Type parameters

    • Func: (...args: any[]) => any

    Parameters

    • fn: Func

      The function to bind to the current AsyncResource.

    Returns Func & { asyncResource: AsyncResource }

Protected checkAlive

  • checkAlive(): void

emitDestroy

  • emitDestroy(): void
  • Call AsyncHooks destroy callbacks.

    Returns void

Protected getMainWorker

  • getMainWorker(): MessagePort

Protected handleError

  • handleError(e: string | Error): string

Protected run

  • run(fn: (data?: Data) => Response, value: MessageValue<Data, unknown>): void
  • Run the given function synchronously.

    Parameters

    • fn: (data?: Data) => Response

      Function that will be executed.

        • (data?: Data): Response
        • Parameters

          • Optional data: Data

          Returns Response

    • value: MessageValue<Data, unknown>

      Input data for the given function.

    Returns void

Protected runAsync

  • runAsync(fn: (data?: Data) => Promise<Response>, value: MessageValue<Data, unknown>): void
  • Run the given function asynchronously.

    Parameters

    • fn: (data?: Data) => Promise<Response>

      Function that will be executed.

        • (data?: Data): Promise<Response>
        • Parameters

          • Optional data: Data

          Returns Promise<Response>

    • value: MessageValue<Data, unknown>

      Input data for the given function.

    Returns void

runInAsyncScope

  • runInAsyncScope<This, Result>(fn: (...args: any[]) => Result, thisArg?: This, ...args: any[]): Result
  • Call the provided function with the provided arguments in the execution context of the async resource. This will establish the context, trigger the AsyncHooks before callbacks, call the function, trigger the AsyncHooks after callbacks, and then restore the original execution context.

    Type parameters

    • This

    • Result

    Parameters

    • fn: (...args: any[]) => Result

      The function to call in the execution context of this async resource.

        • (...args: any[]): Result
        • Parameters

          • Rest ...args: any[]

          Returns Result

    • Optional thisArg: This

      The receiver to be used for the function call.

    • Rest ...args: any[]

      Optional arguments to pass to the function.

    Returns Result

Protected sendToMainWorker

  • sendToMainWorker(message: MessageValue<Response, unknown>): void

triggerAsyncId

  • triggerAsyncId(): number
  • Returns number

    the trigger ID for this AsyncResource instance.

Static bind

  • bind<Func>(fn: Func, type?: string): Func & { asyncResource: AsyncResource }
  • Binds the given function to the current execution context.

    Type parameters

    • Func: (...args: any[]) => any

    Parameters

    • fn: Func

      The function to bind to the current execution context.

    • Optional type: string

      An optional name to associate with the underlying AsyncResource.

    Returns Func & { asyncResource: AsyncResource }

Generated using TypeDoc