@webview-bridge/react-native 1.4.1 → 1.4.3-nightly.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/packages/react-native/src/createWebView.js +4 -10
- package/dist/packages/react-native/src/integrations/bridge.d.ts +2 -0
- package/dist/packages/react-native/src/integrations/bridge.js +33 -11
- package/dist/packages/react-native/src/integrations/handleRegisterWebMethod.js +9 -3
- package/dist/shared/util/src/createEvents.d.ts +9 -1
- package/dist/shared/util/src/createEvents.js +2 -1
- package/package.json +1 -1
|
@@ -41,9 +41,7 @@ const createWebView = ({ bridge, debug, responseTimeout = 2000, fallback, }) =>
|
|
|
41
41
|
const _webviewRef = (0, react_1.createRef)();
|
|
42
42
|
const emitter = (0, util_1.createEvents)();
|
|
43
43
|
bridge.subscribe((state) => {
|
|
44
|
-
_webviewRef.current?.injectJavaScript(
|
|
45
|
-
window.nativeEmitter.emit('bridgeStateChange', ${JSON.stringify(state)});
|
|
46
|
-
`);
|
|
44
|
+
_webviewRef.current?.injectJavaScript((0, integrations_1.SAFE_NATIVE_EMITTER_EMIT)("bridgeStateChange", state));
|
|
47
45
|
});
|
|
48
46
|
return {
|
|
49
47
|
WebView: (0, react_1.forwardRef)((props, ref) => {
|
|
@@ -53,9 +51,7 @@ const createWebView = ({ bridge, debug, responseTimeout = 2000, fallback, }) =>
|
|
|
53
51
|
.map(([name]) => {
|
|
54
52
|
return `'${name}'`;
|
|
55
53
|
}), []);
|
|
56
|
-
const initialState = (0, react_1.useMemo)(() => Object.fromEntries(Object.entries(bridge.getState() ?? {})
|
|
57
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
58
|
-
.filter(([_, value]) => typeof value !== "function")), []);
|
|
54
|
+
const initialState = (0, react_1.useMemo)(() => Object.fromEntries(Object.entries(bridge.getState() ?? {}).filter(([_, value]) => typeof value !== "function")), []);
|
|
59
55
|
(0, react_1.useImperativeHandle)(ref, () => webviewRef.current, []);
|
|
60
56
|
(0, react_1.useImperativeHandle)(_webviewRef, () => {
|
|
61
57
|
return webviewRef.current;
|
|
@@ -84,9 +80,7 @@ const createWebView = ({ bridge, debug, responseTimeout = 2000, fallback, }) =>
|
|
|
84
80
|
return;
|
|
85
81
|
}
|
|
86
82
|
case "getBridgeState": {
|
|
87
|
-
_webviewRef.current?.injectJavaScript(
|
|
88
|
-
window.nativeEmitter.emit('bridgeStateChange', ${JSON.stringify(bridge.getState())});
|
|
89
|
-
`);
|
|
83
|
+
_webviewRef.current?.injectJavaScript((0, integrations_1.SAFE_NATIVE_EMITTER_EMIT)("bridgeStateChange", bridge.getState()));
|
|
90
84
|
return;
|
|
91
85
|
}
|
|
92
86
|
case "registerWebMethod": {
|
|
@@ -120,7 +114,7 @@ const createWebView = ({ bridge, debug, responseTimeout = 2000, fallback, }) =>
|
|
|
120
114
|
]
|
|
121
115
|
.filter(Boolean)
|
|
122
116
|
.join("\n")} injectedJavaScript={[
|
|
123
|
-
|
|
117
|
+
debug && integrations_1.INJECT_DEBUG,
|
|
124
118
|
props.injectedJavaScript,
|
|
125
119
|
"true;",
|
|
126
120
|
]
|
|
@@ -15,4 +15,6 @@ type HandleBridgeArgs<ArgType = unknown> = {
|
|
|
15
15
|
export declare const handleBridge: ({ bridge, method, args, webview, eventId, }: HandleBridgeArgs) => Promise<void>;
|
|
16
16
|
export declare const INJECT_BRIDGE_METHODS: (bridgeNames: string[]) => string;
|
|
17
17
|
export declare const INJECT_BRIDGE_STATE: (initialState: Record<string, Primitive>) => string;
|
|
18
|
+
export declare const SAFE_NATIVE_EMITTER_EMIT: (eventName: string, data: unknown) => string;
|
|
19
|
+
export declare const SAFE_NATIVE_EMITTER_THROW: (eventName: string) => string;
|
|
18
20
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.INJECT_BRIDGE_STATE = exports.INJECT_BRIDGE_METHODS = exports.handleBridge = exports.bridge = void 0;
|
|
3
|
+
exports.SAFE_NATIVE_EMITTER_THROW = exports.SAFE_NATIVE_EMITTER_EMIT = exports.INJECT_BRIDGE_STATE = exports.INJECT_BRIDGE_METHODS = exports.handleBridge = exports.bridge = void 0;
|
|
4
4
|
const util_1 = require("../../../../shared/util/src");
|
|
5
5
|
const bridge = (procedures) => {
|
|
6
6
|
const getState = () => state;
|
|
@@ -42,24 +42,23 @@ exports.bridge = bridge;
|
|
|
42
42
|
const handleBridge = async ({ bridge, method, args, webview, eventId, }) => {
|
|
43
43
|
const _bridge = bridge.getState();
|
|
44
44
|
const _method = _bridge[method];
|
|
45
|
+
const handleThrow = () => {
|
|
46
|
+
webview.injectJavaScript((0, exports.SAFE_NATIVE_EMITTER_THROW)(`${method}-${eventId}`));
|
|
47
|
+
};
|
|
48
|
+
if (!(method in _bridge)) {
|
|
49
|
+
handleThrow();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
45
52
|
if (typeof _method !== "function") {
|
|
46
53
|
return;
|
|
47
54
|
}
|
|
48
55
|
try {
|
|
49
56
|
const response = await _method?.(...(args ?? []));
|
|
50
|
-
webview.injectJavaScript(
|
|
51
|
-
window.nativeEmitter.emit('${method}-${eventId}',${JSON.stringify(response)});
|
|
52
|
-
|
|
53
|
-
true;
|
|
54
|
-
`);
|
|
57
|
+
webview.injectJavaScript((0, exports.SAFE_NATIVE_EMITTER_EMIT)(`${method}-${eventId}`, response));
|
|
55
58
|
}
|
|
56
59
|
catch (error) {
|
|
60
|
+
handleThrow();
|
|
57
61
|
console.error(error);
|
|
58
|
-
webview.injectJavaScript(`
|
|
59
|
-
window.nativeEmitter.emit('${method}-${eventId}', {}, true);
|
|
60
|
-
|
|
61
|
-
true;
|
|
62
|
-
`);
|
|
63
62
|
}
|
|
64
63
|
};
|
|
65
64
|
exports.handleBridge = handleBridge;
|
|
@@ -71,3 +70,26 @@ const INJECT_BRIDGE_STATE = (initialState) => `
|
|
|
71
70
|
window.__bridgeInitialState__ = ${JSON.stringify(initialState)};
|
|
72
71
|
`;
|
|
73
72
|
exports.INJECT_BRIDGE_STATE = INJECT_BRIDGE_STATE;
|
|
73
|
+
const SAFE_NATIVE_EMITTER_EMIT = (eventName, data) => {
|
|
74
|
+
const dataString = JSON.stringify(data);
|
|
75
|
+
return `
|
|
76
|
+
if (window.nativeEmitter) {
|
|
77
|
+
window.nativeEmitter.emit('${eventName}', ${dataString});
|
|
78
|
+
} else {
|
|
79
|
+
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
|
|
80
|
+
window.nativeBatchedEvents.push(['${eventName}', ${dataString}]);
|
|
81
|
+
}
|
|
82
|
+
true;
|
|
83
|
+
`;
|
|
84
|
+
};
|
|
85
|
+
exports.SAFE_NATIVE_EMITTER_EMIT = SAFE_NATIVE_EMITTER_EMIT;
|
|
86
|
+
const SAFE_NATIVE_EMITTER_THROW = (eventName) => `
|
|
87
|
+
if (window.nativeEmitter) {
|
|
88
|
+
window.nativeEmitter.emit('${eventName}', {}, true);
|
|
89
|
+
} else {
|
|
90
|
+
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
|
|
91
|
+
window.nativeBatchedEvents.push(['${eventName}', {}, true]);
|
|
92
|
+
}
|
|
93
|
+
true;
|
|
94
|
+
`;
|
|
95
|
+
exports.SAFE_NATIVE_EMITTER_THROW = SAFE_NATIVE_EMITTER_THROW;
|
|
@@ -8,13 +8,19 @@ const handleRegisterWebMethod = (emitter, webview, bridgeNames, responseTimeout)
|
|
|
8
8
|
acc[methodName] = async (...args) => {
|
|
9
9
|
const eventId = (0, util_1.createRandomId)();
|
|
10
10
|
return Promise.race([
|
|
11
|
-
(0, util_1.createResolver)(
|
|
12
|
-
|
|
11
|
+
(0, util_1.createResolver)({
|
|
12
|
+
emitter,
|
|
13
|
+
methodName,
|
|
14
|
+
eventId,
|
|
15
|
+
evaluate: () => {
|
|
16
|
+
webview.injectJavaScript(`
|
|
13
17
|
window.webEmitter.emit('${methodName}', '${eventId}', ${JSON.stringify(args)});
|
|
14
18
|
|
|
15
19
|
true;
|
|
16
20
|
`);
|
|
17
|
-
|
|
21
|
+
},
|
|
22
|
+
failHandler: new error_1.WebMethodError(methodName),
|
|
23
|
+
}),
|
|
18
24
|
(0, util_1.timeout)(responseTimeout),
|
|
19
25
|
]);
|
|
20
26
|
};
|
|
@@ -12,5 +12,13 @@ export interface EventEmitter<Events extends EventsMap = DefaultEvents> {
|
|
|
12
12
|
on<K extends keyof Events>(this: this, event: K, cb: Events[K]): () => void;
|
|
13
13
|
}
|
|
14
14
|
export declare const createEvents: <Events extends EventsMap = DefaultEvents>() => EventEmitter<Events>;
|
|
15
|
-
export
|
|
15
|
+
export interface CreateResolverOptions {
|
|
16
|
+
emitter: EventEmitter<DefaultEvents>;
|
|
17
|
+
evaluate: () => void;
|
|
18
|
+
eventId: string;
|
|
19
|
+
failHandler?: Error | false;
|
|
20
|
+
methodName: string;
|
|
21
|
+
onFallback?: () => void;
|
|
22
|
+
}
|
|
23
|
+
export declare const createResolver: ({ emitter, evaluate, eventId, failHandler, methodName, onFallback, }: CreateResolverOptions) => Promise<unknown>;
|
|
16
24
|
export {};
|
|
@@ -17,12 +17,13 @@ const createEvents = () => ({
|
|
|
17
17
|
},
|
|
18
18
|
});
|
|
19
19
|
exports.createEvents = createEvents;
|
|
20
|
-
const createResolver = (emitter,
|
|
20
|
+
const createResolver = ({ emitter, evaluate, eventId, failHandler = false, methodName, onFallback, }) => {
|
|
21
21
|
return new Promise((resolve, reject) => {
|
|
22
22
|
const unbind = emitter.on(`${methodName}-${eventId}`, (data, throwOccurred) => {
|
|
23
23
|
unbind();
|
|
24
24
|
if (throwOccurred) {
|
|
25
25
|
if (failHandler instanceof Error) {
|
|
26
|
+
onFallback?.();
|
|
26
27
|
reject(failHandler);
|
|
27
28
|
}
|
|
28
29
|
else {
|
package/package.json
CHANGED