@webview-bridge/react-native 1.4.2 → 1.4.3

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.
@@ -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
- console && integrations_1.INJECT_DEBUG,
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;
@@ -43,11 +43,7 @@ const handleBridge = async ({ bridge, method, args, webview, eventId, }) => {
43
43
  const _bridge = bridge.getState();
44
44
  const _method = _bridge[method];
45
45
  const handleThrow = () => {
46
- webview.injectJavaScript(`
47
- window.nativeEmitter.emit('${method}-${eventId}', {}, true);
48
-
49
- true;
50
- `);
46
+ webview.injectJavaScript((0, exports.SAFE_NATIVE_EMITTER_THROW)(`${method}-${eventId}`));
51
47
  };
52
48
  if (!(method in _bridge)) {
53
49
  handleThrow();
@@ -58,11 +54,7 @@ const handleBridge = async ({ bridge, method, args, webview, eventId, }) => {
58
54
  }
59
55
  try {
60
56
  const response = await _method?.(...(args ?? []));
61
- webview.injectJavaScript(`
62
- window.nativeEmitter.emit('${method}-${eventId}',${JSON.stringify(response)});
63
-
64
- true;
65
- `);
57
+ webview.injectJavaScript((0, exports.SAFE_NATIVE_EMITTER_EMIT)(`${method}-${eventId}`, response));
66
58
  }
67
59
  catch (error) {
68
60
  handleThrow();
@@ -78,3 +70,26 @@ const INJECT_BRIDGE_STATE = (initialState) => `
78
70
  window.__bridgeInitialState__ = ${JSON.stringify(initialState)};
79
71
  `;
80
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webview-bridge/react-native",
3
- "version": "1.4.2",
3
+ "version": "1.4.3",
4
4
  "type": "module",
5
5
  "description": "Fully Type-Safe Integration for React Native WebView and Web",
6
6
  "main": "dist/packages/react-native/src/index.js",