1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +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);
}
}