Private
constructorOptional
terminateThread: ThreadLifecycleFn<ApiType>Private
Readonly
handleReadonly
optionsPrivate
Readonly
terminatePrivate
Readonly
threadsAppends 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.
Optional
options: boolean | AddEventListenerOptionsAppends 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.
Optional
options: boolean | AddEventListenerOptionsDispatches 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.
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.
Private
doOptional
forceTerminateShared: booleanPrivate
findQueue a new task on the pool.
Finds a thread with few queued tasks and queues another. Tasks are immediately sent to the thread.
A callback to execute on a thread.
The task result promise.
Removes the event listener in target's event listener list with the same type, callback, and options.
Optional
options: boolean | EventListenerOptionsRemoves the event listener in target's event listener list with the same type, callback, and options.
Optional
options: boolean | EventListenerOptionsTerminate 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.
Optional
forceTerminateShared: booleanIf you want to make sure SharedWorkers abort. Probably not a great idea, but one might want to do it.
Static
SpawnSpawn 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.
Callback that spawns a new thread. If you need to
run custom init per thread, use the initialiseThread
lifecycle function.
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.
A new thread pool.
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
A pool of EsThreads.
Example
When used with SharedWorker, make sure to spawn each EsThread thread with a unique name in WorkerOptions.