mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-21 09:38:01 +02:00
25 lines
763 B
TypeScript
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);
|
|
}
|
|
}
|