Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/linonetwo/react-native-postmessage-cat
Simplify the postMessage between RN webview content and RN react code.
https://github.com/linonetwo/react-native-postmessage-cat
bridge invoke post-message postmessage postmessage-library proxy react-native react-native-webview react-native-webview-bridge webview
Last synced: 11 days ago
JSON representation
Simplify the postMessage between RN webview content and RN react code.
- Host: GitHub
- URL: https://github.com/linonetwo/react-native-postmessage-cat
- Owner: linonetwo
- License: mit
- Created: 2023-08-27T08:11:59.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-12-15T14:26:48.000Z (about 1 year ago)
- Last Synced: 2024-11-30T09:46:30.652Z (about 1 month ago)
- Topics: bridge, invoke, post-message, postmessage, postmessage-library, proxy, react-native, react-native-webview, react-native-webview-bridge, webview
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/react-native-postmessage-cat
- Size: 341 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# react-native-postmessage-cat
> Based on [linonetwo/electron-ipc-cat](https://github.com/linonetwo/electron-ipc-cat), and used in [TidGi-Mobile](https://github.com/tiddly-gittly/TidGi-Mobile]).
react-native-postmessage-cat
Passing object and type between React Native main process and WebView process simply via proxy.
## Overview
In latest react-native-webview, the `injectedJavaScriptBeforeContentLoaded` accepts stringified code, and you are required to passing data using postMessage. It requires tons of boilerplate code to build up this message bridge, just like the vanilla Redux.
Luckily we have frankwallis/electron-ipc-proxy which provide a good example about how to automatically build IPC channel for a class in the main process. But it doesn't work in react native, so here we have `react-native-postmessage-cat`!
We wrap our react native side class, and can use them from the `window.xxx` in the webview. All types are preserved, so you can get typescript intellisense just like using a local function.
## Install
```sh
pnpm i react-native-postmessage-cat
```## Example
Real use case in [TidGi-Mobile's sync-adaptor](https://github.com/tiddly-gittly/TidGi-Mobile)
### 1. The class
```ts
/** workspaces.ts */
import { ProxyPropertyType } from 'react-native-postmessage-cat';
import type { ProxyDescriptor } from 'react-native-postmessage-cat/common';export class Workspace implements IWorkspaceService {
/**
* Record from workspace id to workspace settings
*/
private workspaces: Record = {};
public workspaces$: BehaviorSubject>;public async getWorkspacesAsList(): Promise {
return Object.values(this.workspaces).sort((a, b) => a.order - b.order);
}public async get(id: string): Promise {
return this.workspaces[id];
}public get$(id: string): Observable {
return this.workspaces$.pipe(map((workspaces) => workspaces[id]));
}
}export interface IWorkspaceService {
workspaces$: BehaviorSubject>;
getWorkspacesAsList(): Promise;
get(id: string): Promise;
get$(id: string): Observable;
}export const WorkspaceServiceIPCDescriptor: ProxyDescriptor = {
channel: WorkspaceChannel.name,
properties: {
workspaces$: ProxyPropertyType.Value$,
getWorkspacesAsList: ProxyPropertyType.Function,
get: ProxyPropertyType.Function,
get$: ProxyPropertyType.Function$,
},
};
```### 2. bindServiceAndProxy in React Native side & bridge proxy in preload script
```tsx
/**
* Provide API from main services to WebView
* This file should be required by BrowserView's preload script to work
*/
import { useMemo } from 'react';
import { ProxyPropertyType, useRegisterProxy, webviewPreloadedJS } from 'react-native-postmessage-cat';
import type { ProxyDescriptor } from 'react-native-postmessage-cat/common';import { WorkspaceServiceIPCDescriptor } from '@services/workspaces/interface';
const workspaceService = new WorkspaceService();
export const WikiViewer = () => {
const [webViewReference, onMessageReference] = useRegisterProxy(workspaceService, WorkspaceServiceIPCDescriptor)
const preloadScript = useMemo(() =>`
${webviewPreloadedJS}
true; // note: this is required, or you'll sometimes get silent failures
`, []);
return (
);
};
```### 3. receive in JS that runs inside WebView
```ts
/** renderer.tsx */
import 'react-native-postmessage-cat/fixContextIsolation';import { IWorkspace } from '@services/workspaces';
import { WorkspaceServiceIPCDescriptor } from '@services/workspaces/interface';// `window.PostMessageCat`'s type is same as `createProxy` in `'react-native-postmessage-cat/webview'`
const workspaceService = window.PostMessageCat(WorkspaceServiceIPCDescriptor)const workspacesList$ = workspaceService.workspaces$.pipe(map, IWorkspace[]>((workspaces) => Object.values(workspaces)));
const workspace$ = workspaceService.get$(id)
```### Another example
```tsx
import { useMemo } from 'react';
import { ProxyPropertyType, useRegisterProxy, webviewPreloadedJS } from 'react-native-postmessage-cat';
import type { ProxyDescriptor } from 'react-native-postmessage-cat/common';
import { WebView } from 'react-native-webview';
import { styled } from 'styled-components/native';
import { useTiddlyWiki } from './useTiddlyWiki';const WebViewContainer = styled.View`
flex: 2;
height: 100%;
`;class WikiStorage {
save(data: string) {
console.log('Saved', data);
return true;
}
}
enum WikiStorageChannel {
name = 'wiki-storage',
}
export const WikiStorageIPCDescriptor: ProxyDescriptor = {
channel: WikiStorageChannel.name,
properties: {
save: ProxyPropertyType.Function,
},
};
const wikiStorage = new WikiStorage();
const tryWikiStorage = `
const wikiStorage = window.PostMessageCat(${JSON.stringify(WikiStorageIPCDescriptor)});
wikiStorage.save('Hello World').then(console.log);
// play with it: window.wikiStorage.save('BBB').then(console.log)
window.wikiStorage = wikiStorage;
`;export const WikiViewer = () => {
const wikiHTMLString = useTiddlyWiki();
const [webViewReference, onMessageReference] = useRegisterProxy(wikiStorage, WikiStorageIPCDescriptor);
const preloadScript = useMemo(() => `
window.onerror = function(message, sourcefile, lineno, colno, error) {
if (error === null) return false;
alert("Message: " + message + " - Source: " + sourcefile + " Line: " + lineno + ":" + colno);
console.error(error);
return true;
};${webviewPreloadedJS}
${tryWikiStorage}
true; // note: this is required, or you'll sometimes get silent failures
`, []);
return (
);
};
```### Multiple references
```ts
import { useMergedReference } from 'react-native-postmessage-cat';
import { IWikiWorkspace } from '../store/wiki';
import { AppDataServiceIPCDescriptor } from './AppDataService/descriptor';
import { useAppDataService } from './AppDataService/hooks';
import { BackgroundSyncServiceIPCDescriptor } from './BackgroundSyncService/descriptor';
import { useBackgroundSyncService } from './BackgroundSyncService/hooks';
import { NativeServiceIPCDescriptor } from './NativeService/descriptor';
import { useNativeService } from './NativeService/hooks';
import { WikiStorageServiceIPCDescriptor } from './WikiStorageService/descriptor';
import { useWikiStorageService } from './WikiStorageService/hooks';const registerServiceOnWebView = `
window.service = window.service || {};
var wikiStorageService = window.PostMessageCat(${JSON.stringify(WikiStorageServiceIPCDescriptor)});
window.service.wikiStorageService = wikiStorageService;
var backgroundSyncService = window.PostMessageCat(${JSON.stringify(BackgroundSyncServiceIPCDescriptor)});
window.service.backgroundSyncService = backgroundSyncService;
var nativeService = window.PostMessageCat(${JSON.stringify(NativeServiceIPCDescriptor)});
window.service.nativeService = nativeService;
var appDataService = window.PostMessageCat(${JSON.stringify(AppDataServiceIPCDescriptor)});
window.service.appDataService = appDataService;
`;export function useRegisterService(workspace: IWikiWorkspace) {
const [wikiStorageServiceWebViewReference, wikiStorageServiceOnMessageReference] = useWikiStorageService(workspace);
const [backgroundSyncServiceWebViewReference, backgroundSyncServiceOnMessageReference] = useBackgroundSyncService();
const [nativeServiceWebViewReference, nativeServiceOnMessageReference] = useNativeService();
const [appDataServiceWebViewReference, appDataServiceOnMessageReference] = useAppDataService();const mergedWebViewReference = useMergedReference(
wikiStorageServiceWebViewReference,
backgroundSyncServiceWebViewReference,
nativeServiceWebViewReference,
appDataServiceWebViewReference,
);const mergedOnMessageReference = useMergedReference(
wikiStorageServiceOnMessageReference,
backgroundSyncServiceOnMessageReference,
nativeServiceOnMessageReference,
appDataServiceOnMessageReference,
);return [mergedWebViewReference, mergedOnMessageReference, registerServiceOnWebView] as const;
}
```Where `./WikiStorageService/hooks` is
```ts
import { useMemo } from 'react';
import { useRegisterProxy } from 'react-native-postmessage-cat';
import { IWikiWorkspace } from '../../store/wiki';
import { WikiStorageService } from '.';
import { WikiStorageServiceIPCDescriptor } from './descriptor';export function useWikiStorageService(workspace: IWikiWorkspace) {
const wikiStorageService = useMemo(() => new WikiStorageService(workspace), [workspace]);
const [webViewReference, onMessageReference] = useRegisterProxy(wikiStorageService, WikiStorageServiceIPCDescriptor, { debugLog: true });
return [webViewReference, onMessageReference] as const;
}
```Use it in React Component
```tsx
const [webViewReference, onMessageReference, registerWikiStorageServiceOnWebView] = useRegisterService(wikiWorkspace);
const preloadScript = useMemo(() => `
${webviewSideReceiver}
true; // note: this is required, or you'll sometimes get silent failures
`, [webviewSideReceiver]);return (
` }}
onMessage={onMessageReference.current}
ref={webViewReference}
injectedJavaScriptBeforeContentLoaded={preloadScript}
webviewDebuggingEnabled={true /* Open chrome://inspect/#devices to debug the WebView */}
/>
);
```## Notes
All `Values` and `Functions` will return promises on the renderer side, no matter how they have been defined on the source object. This is because communication happens asynchronously. For this reason it is recommended that you make them promises on the source object as well, so the interface is the same on both sides.
Use `Value$` and `Function$` when you want to expose or return an Observable stream across IPC.
Only plain objects can be passed between the 2 sides of the proxy, as the data is serialized to JSON, so no functions or prototypes will make it across to the other side.
Notice the second parameter of `createProxy` - `Observable` this is done so that the library itself does not need to take on a dependency to rxjs. You need to pass in the Observable constructor yourself if you want to consume Observable streams.
The channel specified must be unique and match on both sides of the proxy.
The packages exposes 2 entry points in the "main" and "browser" fields of package.json. "main" is for the main thread and "browser" is for the renderer thread.
## See it working
[Example in TiddlyGit](https://github.com/tiddly-gittly/TiddlyGit-Desktop/blob/0c6b26c0c1113e0c66d6f49f022c5733d4fa85e8/src/preload/common/services.ts#L27-L42)
## FAQ
### WebView onMessage (before onMessageReference.current ready)
[See issue 1829](https://github.com/react-native-webview/react-native-webview/issues/1829#issuecomment-1699235643), [You must set onMessage or the window.ReactNativeWebView.postMessage method will not be injected into the web page.](https://github.com/react-native-webview/react-native-webview/blob/master/docs/Guide.md#the-windowreactnativewebviewpostmessage-method-and-onmessage-prop).
So a default callback is provided, and will log this, this can be safely ignored.
### Unable to resolve "react-native-postmessage-cat/react-native" from xxx
Use `import { useRegisterProxy } from 'react-native-postmessage-cat';` instead of `"react-native-postmessage-cat/react-native"`.
### reject string
You should reject an Error, other wise `serialize-error` can't handle it well.
```diff
- reject(errorMessage);
+ reject(new Error(errorMessage));
```## Change Log