web-worker-helper
Version:
Utilities for running tasks on worker threads
42 lines (41 loc) • 1.18 kB
TypeScript
export type WorkerThreadProps = {
name: string;
source?: string;
url?: string;
};
/**
* Represents one worker thread
*/
export default class WorkerThread {
readonly name: string;
readonly source: string | undefined;
readonly url: string | undefined;
terminated: boolean;
worker: Worker;
onMessage: (message: any) => void;
onError: (error: Error) => void;
private loadableURL;
constructor(props: WorkerThreadProps);
static isSupported(): boolean;
/**
* Terminate this worker thread
* @note Can free up significant memory
*/
destroy(): void;
get isRunning(): boolean;
/**
* Send a message to this worker thread
* @param data any data structure, ideally consisting mostly of transferrable objects
* @param transferList If not supplied, calculated automatically by traversing data
*/
postMessage(data: any, transferList?: any[]): void;
/**
* Generate a standard Error from an ErrorEvent
* @param {ErrorEvent} event
*/
private getErrorFromErrorEvent;
/**
* Creates a worker thread on the browser
*/
private createBrowserWorker;
}