文件
mynode/packages/app-mobile/utils/ipc/WebViewToRNMessenger.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

42 行
1.4 KiB
TypeScript

import RemoteMessenger from '@joplin/lib/utils/ipc/RemoteMessenger';
import { SerializableData } from '@joplin/lib/utils/ipc/types';
interface ExtendedWindow extends Window {
ReactNativeWebView: {
postMessage(message: unknown): void;
supportsNonStringMessages?: boolean;
};
}
declare const window: ExtendedWindow;
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.ReactNativeWebView.postMessage(window.ReactNativeWebView.supportsNonStringMessages ? message : JSON.stringify(message));
}
protected override onClose(): void {
window.removeEventListener('message', this.handleMessage);
}
}