1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-30 10:36:35 +02:00
joplin/packages/lib/utils/ipc/WorkerMessenger.ts
2024-08-02 14:51:49 +01:00

25 lines
763 B
TypeScript

import RemoteMessenger from './RemoteMessenger';
import { SerializableData } from './types';
export default class WorkerMessenger<LocalInterface, RemoteInterface> extends RemoteMessenger<LocalInterface, RemoteInterface> {
public constructor(channelId: string, private worker: Worker, localApi: LocalInterface|null) {
super(channelId, localApi);
worker.addEventListener('message', this.handleMessageEvent);
this.onReadyToReceive();
}
private handleMessageEvent = (event: MessageEvent) => {
void this.onMessage(event.data);
};
protected override postMessage(message: SerializableData): void {
this.worker.postMessage(message);
}
protected override onClose(): void {
this.worker.removeEventListener('message', this.handleMessageEvent);
}
}