Class EsThreadPool<ApiType>

A pool of EsThreads.

Example

When used with SharedWorker, make sure to spawn each EsThread thread with a unique name in WorkerOptions.

const pool = await EsThreadPool.Spawn((threadId) => EsThread.Spawn<HelloWorldApiType>(
new SharedWorker(new URL("threads/valid/hello-world.worker.ts", import.meta.url),
{type: "module", name: `HelloWorldWorker #${threadId}`})), {size: 8});

Type Parameters

Hierarchy

  • EventTarget
    • EsThreadPool

Implements

  • Terminable

Constructors

Properties

handleErrorEvent: ((evt) => void) = ...

Type declaration

    • (evt): void
    • Parameters

      • evt: Event

      Returns void

options: Readonly<EsPoolOptions>
terminateThread: undefined | ThreadLifecycleFn<ApiType>
threads: EsThread<ApiType>[] = []

Methods

  • Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.

    The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.

    When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.

    When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.

    When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.

    If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.

    The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.

    MDN Reference

    Parameters

    • type: string
    • callback: null | EventListenerOrEventListenerObject
    • Optional options: boolean | AddEventListenerOptions

    Returns void

  • Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.

    The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.

    When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.

    When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.

    When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.

    If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.

    The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.

    MDN Reference

    Parameters

    • type: string
    • callback: null | EventListenerOrEventListenerObject
    • Optional options: boolean | AddEventListenerOptions

    Returns void

  • Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.

    MDN Reference

    Parameters

    • event: Event

    Returns boolean

  • Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.

    MDN Reference

    Parameters

    • event: Event

    Returns boolean

  • Queue a new task on the pool.

    Finds a thread with few queued tasks and queues another. Tasks are immediately sent to the thread.

    Type Parameters

    • Return

    Parameters

    • taskFunction: ((thread) => Promise<Return>)

      A callback to execute on a thread.

        • (thread): Promise<Return>
        • Parameters

          Returns Promise<Return>

    Returns Promise<Return>

    The task result promise.

  • Removes the event listener in target's event listener list with the same type, callback, and options.

    MDN Reference

    Parameters

    • type: string
    • callback: null | EventListenerOrEventListenerObject
    • Optional options: boolean | EventListenerOptions

    Returns void

  • Removes the event listener in target's event listener list with the same type, callback, and options.

    MDN Reference

    Parameters

    • type: string
    • callback: null | EventListenerOrEventListenerObject
    • Optional options: boolean | EventListenerOptions

    Returns void

  • Returns a promise that resolves when all tasks in all threads are resolved and rejects when any task rejects.

    Returns Promise<void>

  • Returns a promise that resolves when all tasks in all threads are settled.

    Returns Promise<void>

  • Terminate all threads in the pool.

    Waits for all tasks in all threads to settle. If tasks resolving is required, call EsThreadPool#resolved before calling EsThreadPool#terminate.

    Parameters

    • Optional forceTerminateShared: boolean

      If you want to make sure SharedWorkers abort. Probably not a great idea, but one might want to do it.

    Returns Promise<void>

  • Spawn a new thread pool.

    If any thread fails to spawn or any threads initialiseThread fails, the entire pool will terminate. Threads are (attempted to be) terminated in that case.

    Type Parameters

    Parameters

    • spawnThread: ((threadId) => Promise<EsThread<ApiType>>)

      Callback that spawns a new thread. If you need to run custom init per thread, use the initialiseThread lifecycle function.

        • (threadId): Promise<EsThread<ApiType>>
        • Parameters

          • threadId: number

          Returns Promise<EsThread<ApiType>>

    • poolOptions: Partial<EsPoolOptions> = {}

      The options for this thread pool.

    • Optional initialiseThread: ThreadLifecycleFn<ApiType>

      Allows running custom init per thread.

    • Optional terminateThread: ThreadLifecycleFn<ApiType>

      Allows running custom cleanup per thread.

    Returns Promise<EsThreadPool<ApiType>>

    A new thread pool.

    Example

    Spawning a thread pool with thread lifecyle callbacks.

    const pool = await EsThreadPool.Spawn(threadId => EsThread.Spawn<CustomTerminateApiType>(
    new Worker(new URL("threads/valid/custom-terminate.worker.ts", import.meta.url),
    {type: "module", name: `LongRunningWorker #${threadId}`})),
    {size: 2},
    (threadId, thread) => {
    // returns Promise<void>
    return thread.methods.initialise();
    },
    (threadId, thread) => {
    // returns Promise<void>
    return thread.methods.terminate();
    });

Generated using TypeDoc