1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/packages/app-mobile/utils/ipc/WebViewToRNMessenger.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

34 lines
1.1 KiB
TypeScript

import RemoteMessenger from '@joplin/lib/utils/ipc/RemoteMessenger';
import { SerializableData } from '@joplin/lib/utils/ipc/types';
export default class WebViewToRNMessenger<LocalInterface, RemoteInterface> extends RemoteMessenger<LocalInterface, RemoteInterface> {
public constructor(channelId: string, localApi: LocalInterface) {
super(channelId, localApi);
window.addEventListener('message', this.handleMessage);
// Allow the event loop to run -- without this, calling
// injectJS('window.handleReactNativeMessage("message")')
// in ReactNative can (but doesn't always) fail if called immediately after
// sending ReadyToReceive.
setTimeout(() => {
this.onReadyToReceive();
}, 0);
}
private handleMessage = (message: MessageEvent) => {
if (typeof message.data === 'object' && message.origin === 'react-native') {
void this.onMessage(message.data);
}
};
protected override postMessage(message: SerializableData): void {
(window as any).ReactNativeWebView.postMessage(JSON.stringify(message));
}
protected override onClose(): void {
window.removeEventListener('message', this.handleMessage);
}
}