@byterygon/workerr
Version:
Workerr is a lightweight library that simplifies working with Web Workers in JavaScript. It provides a more intuitive way to handle parallel tasks by using Promises, along with a built-in worker pool to efficiently manage worker instances. With strong Typ
50 lines (49 loc) • 1.44 kB
TypeScript
export interface WorkerMessageTypePayLoadMap {
"initialization:start": undefined;
"initialization:complete": undefined;
"initialization:error": Error;
"context:update": unknown;
"context:ack": {
messageId: string;
};
"excecute:accept": {
messageId: string;
};
"excecute:response": {
messageId: string;
result: unknown;
};
"excecute:error": {
messageId: string;
error: Error;
};
}
export interface MainThreadTypePayloadMap {
"initialization:ack": undefined;
"context:update": unknown;
"context:ack": {
messageId: string;
};
"excecute:start": {
cmd: string;
params: unknown;
abortSignalChannelPort?: MessagePort;
};
"stream:start": {
port: MessagePort;
context: unknown;
abortSignal: boolean;
};
}
export interface MessageData<M extends WorkerMessageTypePayLoadMap | MainThreadTypePayloadMap, Type extends keyof M = keyof M> {
messageId: string;
messageType: Type;
messagePayload: M[Type];
timestamp: number;
}
export type MainThreadMessages = {
[Type in keyof MainThreadTypePayloadMap]: MessageData<MainThreadTypePayloadMap, Type>;
}[keyof MainThreadTypePayloadMap];
export type WorkerMessages = {
[Type in keyof WorkerMessageTypePayLoadMap]: MessageData<WorkerMessageTypePayLoadMap, Type>;
}[keyof WorkerMessageTypePayLoadMap];