/** * This file contains the utility function to send and receive messages from the worker. * TRequestMessage is used to send a request to/from the worker. * TResponseMessage is used to send a response to/from the worker. * TDefaultMessage is used to send a message to/from worker. Does not expect a response. */ export enum MessageType { REQUEST = "REQUEST", RESPONSE = "RESPONSE", DEFAULT = "DEFAULT", } interface TRequestMessage { body: TBody; messageId: string; messageType: MessageType.REQUEST; } interface TResponseMessage { body: TBody; messageId: string; messageType: MessageType.RESPONSE; } export interface TDefaultMessage { messageId?: string; body: TBody; messageType: MessageType.DEFAULT; } export type TMessage = | TRequestMessage | TResponseMessage | TDefaultMessage; /** Avoid from using postMessage directly. * This function should be used to send messages to the worker and back. * Purpose: To have some standardization in the messages that are transferred. * TODO: Add support for window postMessage options * TODO: Add support for transferable objects. */ export function sendMessage( this: Worker | typeof globalThis, message: TMessage, ) { this.postMessage(message); }