1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/lib/utils/ipc/WindowMessenger.ts
Henry Heino 55cafb8891
Android: Add support for Markdown editor plugins (#10086)
Co-authored-by: Laurent Cozic <laurent22@users.noreply.github.com>
2024-03-11 15:02:15 +00:00

29 lines
835 B
TypeScript

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