@webview-bridge/react-native 1.7.6 → 1.7.8
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/index.cjs +10 -7
- package/dist/index.js +11 -8
- package/package.json +3 -3
- package/src/createWebView.tsx +3 -2
- package/src/integrations/bridge.ts +44 -34
- package/src/integrations/console.ts +8 -3
package/dist/index.cjs
CHANGED
|
@@ -554,24 +554,26 @@ var handleBridge = /*#__PURE__*/ function() {
|
|
|
554
554
|
};
|
|
555
555
|
}();
|
|
556
556
|
var INJECT_BRIDGE_METHODS = function(bridgeNames) {
|
|
557
|
-
return "\n window.__bridgeMethods__ = ".concat(JSON.stringify(bridgeNames), ";\n");
|
|
557
|
+
return "\n (function() {\n window.__bridgeMethods__ = ".concat(JSON.stringify(bridgeNames), ";\n })();\n");
|
|
558
558
|
};
|
|
559
559
|
var INJECT_BRIDGE_STATE = function(initialState) {
|
|
560
|
-
return "\n window.__bridgeInitialState__ = ".concat(JSON.stringify(initialState), ";\n");
|
|
560
|
+
return "\n (function() {\n window.__bridgeInitialState__ = ".concat(JSON.stringify(initialState), ";\n })();\n");
|
|
561
561
|
};
|
|
562
562
|
var SAFE_NATIVE_EMITTER_EMIT = function(eventName, data) {
|
|
563
563
|
var dataString = JSON.stringify(data);
|
|
564
|
-
return "\
|
|
564
|
+
return "\n (function() {\n if (window.nativeEmitterMap && Object.keys(window.nativeEmitterMap).length > 0) {\n for (const [_, emitter] of Object.entries(window.nativeEmitterMap)) {\n emitter.emit('".concat(eventName, "', ").concat(dataString, ");\n }\n } else if (window.nativeEmitter) {\n // @deprecated This version is not used after 1.7.2\n window.nativeEmitter.emit('").concat(eventName, "', ").concat(dataString, ");\n } else {\n window.nativeBatchedEvents = window.nativeBatchedEvents || [];\n window.nativeBatchedEvents.push(['").concat(eventName, "', ").concat(dataString, "]);\n }\n return true;\n })();\n");
|
|
565
565
|
};
|
|
566
566
|
var SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID = function(bridgeId, eventName, data) {
|
|
567
567
|
var dataString = JSON.stringify(data);
|
|
568
|
-
return "\
|
|
568
|
+
return "\n (function() {\n if (window.nativeEmitterMap && window.nativeEmitterMap['".concat(bridgeId, "']) {\n window.nativeEmitterMap['").concat(bridgeId, "'].emit('").concat(eventName, "', ").concat(dataString, ");\n } else if (window.nativeEmitter) {\n // @deprecated This version is not used after 1.7.2\n window.nativeEmitter.emit('").concat(eventName, "', ").concat(dataString, ");\n } else {\n window.nativeBatchedEvents = window.nativeBatchedEvents || [];\n window.nativeBatchedEvents.push(['").concat(eventName, "', ").concat(dataString, "]);\n }\n return true;\n })();\n");
|
|
569
569
|
};
|
|
570
570
|
var SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID = function(bridgeId, eventName) {
|
|
571
|
-
return "\
|
|
571
|
+
return "\n (function() {\n if (window.nativeEmitterMap && window.nativeEmitterMap['".concat(bridgeId, "']) {\n window.nativeEmitterMap['").concat(bridgeId, "'].emit('").concat(eventName, "', {}, true);\n } else if (window.nativeEmitter) {\n // @deprecated This version is not used after 1.7.2\n window.nativeEmitter.emit('").concat(eventName, "', {}, true);\n } else {\n window.nativeBatchedEvents = window.nativeBatchedEvents || [];\n window.nativeBatchedEvents.push(['").concat(eventName, "', {}, true]);\n }\n return true;\n })();\n");
|
|
572
572
|
};
|
|
573
573
|
// src/integrations/console.ts
|
|
574
|
-
var INJECT_DEBUG =
|
|
574
|
+
var INJECT_DEBUG = function(uniqueId) {
|
|
575
|
+
return '\n(function() {\n if (window["__bridge_debug_injected_'.concat(uniqueId, '"]) {\n return;\n }\n\n window["__bridge_debug_injected_').concat(uniqueId, '"] = true;\n const originalConsoleLog = console.log;\n const originalConsoleError = console.error;\n const originalConsoleWarn = console.warn;\n\n console.log = function() {\n var message = JSON.stringify(Array.from(arguments));\n window.ReactNativeWebView?.postMessage(\n JSON.stringify({ type: "log", body: { method: "log", args: message } }),\n );\n originalConsoleLog.apply(console, arguments);\n };\n\n console.error = function() {\n var message = JSON.stringify(Array.from(arguments));\n window.ReactNativeWebView?.postMessage(\n JSON.stringify({ type: "log", body: { method: "error", args: message } }),\n );\n originalConsoleError.apply(console, arguments);\n };\n\n console.warn = function() {\n var message = JSON.stringify(Array.from(arguments));\n window.ReactNativeWebView?.postMessage(\n JSON.stringify({ type: "log", body: { method: "warn", args: message } }),\n );\n originalConsoleWarn.apply(console, arguments);\n };\n})();\n');
|
|
576
|
+
};
|
|
575
577
|
var handleLog = function(type, message) {
|
|
576
578
|
var _JSON_parse = _to_array(JSON.parse(message)), formatMessage = _JSON_parse[0], parsedMessage = _JSON_parse.slice(1);
|
|
577
579
|
var webviewMark = "(WebView) ";
|
|
@@ -660,6 +662,7 @@ var createWebView = function(param) {
|
|
|
660
662
|
};
|
|
661
663
|
var webviewRefList = [];
|
|
662
664
|
var emitter = (0, import_utils3.createEvents)();
|
|
665
|
+
var uniqueId = (0, import_utils3.createRandomId)();
|
|
663
666
|
bridge2.subscribe(function(state) {
|
|
664
667
|
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
665
668
|
try {
|
|
@@ -874,7 +877,7 @@ var createWebView = function(param) {
|
|
|
874
877
|
"true;"
|
|
875
878
|
].filter(Boolean).join("\n"),
|
|
876
879
|
injectedJavaScript: [
|
|
877
|
-
debug && INJECT_DEBUG,
|
|
880
|
+
debug && INJECT_DEBUG(uniqueId),
|
|
878
881
|
props.injectedJavaScript,
|
|
879
882
|
"true;"
|
|
880
883
|
].filter(Boolean).join("\n")
|
package/dist/index.js
CHANGED
|
@@ -341,7 +341,7 @@ function _ts_generator(thisArg, body) {
|
|
|
341
341
|
};
|
|
342
342
|
}
|
|
343
343
|
}
|
|
344
|
-
import { createEvents } from "@webview-bridge/utils";
|
|
344
|
+
import { createEvents, createRandomId as createRandomId2 } from "@webview-bridge/utils";
|
|
345
345
|
import { forwardRef, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef } from "react";
|
|
346
346
|
import WebView from "react-native-webview";
|
|
347
347
|
// src/integrations/bridge.ts
|
|
@@ -460,24 +460,26 @@ var handleBridge = /*#__PURE__*/ function() {
|
|
|
460
460
|
};
|
|
461
461
|
}();
|
|
462
462
|
var INJECT_BRIDGE_METHODS = function(bridgeNames) {
|
|
463
|
-
return "\n window.__bridgeMethods__ = ".concat(JSON.stringify(bridgeNames), ";\n");
|
|
463
|
+
return "\n (function() {\n window.__bridgeMethods__ = ".concat(JSON.stringify(bridgeNames), ";\n })();\n");
|
|
464
464
|
};
|
|
465
465
|
var INJECT_BRIDGE_STATE = function(initialState) {
|
|
466
|
-
return "\n window.__bridgeInitialState__ = ".concat(JSON.stringify(initialState), ";\n");
|
|
466
|
+
return "\n (function() {\n window.__bridgeInitialState__ = ".concat(JSON.stringify(initialState), ";\n })();\n");
|
|
467
467
|
};
|
|
468
468
|
var SAFE_NATIVE_EMITTER_EMIT = function(eventName, data) {
|
|
469
469
|
var dataString = JSON.stringify(data);
|
|
470
|
-
return "\
|
|
470
|
+
return "\n (function() {\n if (window.nativeEmitterMap && Object.keys(window.nativeEmitterMap).length > 0) {\n for (const [_, emitter] of Object.entries(window.nativeEmitterMap)) {\n emitter.emit('".concat(eventName, "', ").concat(dataString, ");\n }\n } else if (window.nativeEmitter) {\n // @deprecated This version is not used after 1.7.2\n window.nativeEmitter.emit('").concat(eventName, "', ").concat(dataString, ");\n } else {\n window.nativeBatchedEvents = window.nativeBatchedEvents || [];\n window.nativeBatchedEvents.push(['").concat(eventName, "', ").concat(dataString, "]);\n }\n return true;\n })();\n");
|
|
471
471
|
};
|
|
472
472
|
var SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID = function(bridgeId, eventName, data) {
|
|
473
473
|
var dataString = JSON.stringify(data);
|
|
474
|
-
return "\
|
|
474
|
+
return "\n (function() {\n if (window.nativeEmitterMap && window.nativeEmitterMap['".concat(bridgeId, "']) {\n window.nativeEmitterMap['").concat(bridgeId, "'].emit('").concat(eventName, "', ").concat(dataString, ");\n } else if (window.nativeEmitter) {\n // @deprecated This version is not used after 1.7.2\n window.nativeEmitter.emit('").concat(eventName, "', ").concat(dataString, ");\n } else {\n window.nativeBatchedEvents = window.nativeBatchedEvents || [];\n window.nativeBatchedEvents.push(['").concat(eventName, "', ").concat(dataString, "]);\n }\n return true;\n })();\n");
|
|
475
475
|
};
|
|
476
476
|
var SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID = function(bridgeId, eventName) {
|
|
477
|
-
return "\
|
|
477
|
+
return "\n (function() {\n if (window.nativeEmitterMap && window.nativeEmitterMap['".concat(bridgeId, "']) {\n window.nativeEmitterMap['").concat(bridgeId, "'].emit('").concat(eventName, "', {}, true);\n } else if (window.nativeEmitter) {\n // @deprecated This version is not used after 1.7.2\n window.nativeEmitter.emit('").concat(eventName, "', {}, true);\n } else {\n window.nativeBatchedEvents = window.nativeBatchedEvents || [];\n window.nativeBatchedEvents.push(['").concat(eventName, "', {}, true]);\n }\n return true;\n })();\n");
|
|
478
478
|
};
|
|
479
479
|
// src/integrations/console.ts
|
|
480
|
-
var INJECT_DEBUG =
|
|
480
|
+
var INJECT_DEBUG = function(uniqueId) {
|
|
481
|
+
return '\n(function() {\n if (window["__bridge_debug_injected_'.concat(uniqueId, '"]) {\n return;\n }\n\n window["__bridge_debug_injected_').concat(uniqueId, '"] = true;\n const originalConsoleLog = console.log;\n const originalConsoleError = console.error;\n const originalConsoleWarn = console.warn;\n\n console.log = function() {\n var message = JSON.stringify(Array.from(arguments));\n window.ReactNativeWebView?.postMessage(\n JSON.stringify({ type: "log", body: { method: "log", args: message } }),\n );\n originalConsoleLog.apply(console, arguments);\n };\n\n console.error = function() {\n var message = JSON.stringify(Array.from(arguments));\n window.ReactNativeWebView?.postMessage(\n JSON.stringify({ type: "log", body: { method: "error", args: message } }),\n );\n originalConsoleError.apply(console, arguments);\n };\n\n console.warn = function() {\n var message = JSON.stringify(Array.from(arguments));\n window.ReactNativeWebView?.postMessage(\n JSON.stringify({ type: "log", body: { method: "warn", args: message } }),\n );\n originalConsoleWarn.apply(console, arguments);\n };\n})();\n');
|
|
482
|
+
};
|
|
481
483
|
var handleLog = function(type, message) {
|
|
482
484
|
var _JSON_parse = _to_array(JSON.parse(message)), formatMessage = _JSON_parse[0], parsedMessage = _JSON_parse.slice(1);
|
|
483
485
|
var webviewMark = "(WebView) ";
|
|
@@ -567,6 +569,7 @@ var createWebView = function(param) {
|
|
|
567
569
|
};
|
|
568
570
|
var webviewRefList = [];
|
|
569
571
|
var emitter = createEvents();
|
|
572
|
+
var uniqueId = createRandomId2();
|
|
570
573
|
bridge2.subscribe(function(state) {
|
|
571
574
|
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
572
575
|
try {
|
|
@@ -781,7 +784,7 @@ var createWebView = function(param) {
|
|
|
781
784
|
"true;"
|
|
782
785
|
].filter(Boolean).join("\n"),
|
|
783
786
|
injectedJavaScript: [
|
|
784
|
-
debug && INJECT_DEBUG,
|
|
787
|
+
debug && INJECT_DEBUG(uniqueId),
|
|
785
788
|
props.injectedJavaScript,
|
|
786
789
|
"true;"
|
|
787
790
|
].filter(Boolean).join("\n")
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webview-bridge/react-native",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Fully Type-Safe Integration for React Native WebView and Web",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
"react-native-webview": "*"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@webview-bridge/utils": "1.7.
|
|
43
|
-
"@webview-bridge/types": "1.7.
|
|
42
|
+
"@webview-bridge/utils": "1.7.8",
|
|
43
|
+
"@webview-bridge/types": "1.7.8",
|
|
44
44
|
"use-sync-external-store": "^1.2.0"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
package/src/createWebView.tsx
CHANGED
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
ParserSchema,
|
|
7
7
|
Primitive,
|
|
8
8
|
} from "@webview-bridge/types";
|
|
9
|
-
import { createEvents } from "@webview-bridge/utils";
|
|
9
|
+
import { createEvents, createRandomId } from "@webview-bridge/utils";
|
|
10
10
|
import type React from "react";
|
|
11
11
|
import {
|
|
12
12
|
forwardRef,
|
|
@@ -133,6 +133,7 @@ export const createWebView = <
|
|
|
133
133
|
|
|
134
134
|
const webviewRefList: React.RefObject<BridgeWebView>[] = [];
|
|
135
135
|
const emitter = createEvents();
|
|
136
|
+
const uniqueId = createRandomId();
|
|
136
137
|
|
|
137
138
|
bridge.subscribe((state) => {
|
|
138
139
|
for (const ref of webviewRefList) {
|
|
@@ -318,7 +319,7 @@ export const createWebView = <
|
|
|
318
319
|
.filter(Boolean)
|
|
319
320
|
.join("\n")}
|
|
320
321
|
injectedJavaScript={[
|
|
321
|
-
debug && INJECT_DEBUG,
|
|
322
|
+
debug && INJECT_DEBUG(uniqueId),
|
|
322
323
|
props.injectedJavaScript,
|
|
323
324
|
"true;",
|
|
324
325
|
]
|
|
@@ -113,30 +113,36 @@ export const handleBridge = async ({
|
|
|
113
113
|
};
|
|
114
114
|
|
|
115
115
|
export const INJECT_BRIDGE_METHODS = (bridgeNames: string[]) => `
|
|
116
|
-
|
|
116
|
+
(function() {
|
|
117
|
+
window.__bridgeMethods__ = ${JSON.stringify(bridgeNames)};
|
|
118
|
+
})();
|
|
117
119
|
`;
|
|
118
120
|
|
|
119
121
|
export const INJECT_BRIDGE_STATE = (
|
|
120
122
|
initialState: Record<string, Primitive>,
|
|
121
123
|
) => `
|
|
122
|
-
|
|
124
|
+
(function() {
|
|
125
|
+
window.__bridgeInitialState__ = ${JSON.stringify(initialState)};
|
|
126
|
+
})();
|
|
123
127
|
`;
|
|
124
128
|
|
|
125
129
|
export const SAFE_NATIVE_EMITTER_EMIT = (eventName: string, data: unknown) => {
|
|
126
130
|
const dataString = JSON.stringify(data);
|
|
127
131
|
return `
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
|
|
132
|
+
(function() {
|
|
133
|
+
if (window.nativeEmitterMap && Object.keys(window.nativeEmitterMap).length > 0) {
|
|
134
|
+
for (const [_, emitter] of Object.entries(window.nativeEmitterMap)) {
|
|
135
|
+
emitter.emit('${eventName}', ${dataString});
|
|
136
|
+
}
|
|
137
|
+
} else if (window.nativeEmitter) {
|
|
138
|
+
// @deprecated This version is not used after 1.7.2
|
|
139
|
+
window.nativeEmitter.emit('${eventName}', ${dataString});
|
|
140
|
+
} else {
|
|
141
|
+
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
|
|
142
|
+
window.nativeBatchedEvents.push(['${eventName}', ${dataString}]);
|
|
143
|
+
}
|
|
144
|
+
return true;
|
|
145
|
+
})();
|
|
140
146
|
`;
|
|
141
147
|
};
|
|
142
148
|
|
|
@@ -147,16 +153,18 @@ export const SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID = (
|
|
|
147
153
|
) => {
|
|
148
154
|
const dataString = JSON.stringify(data);
|
|
149
155
|
return `
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
156
|
+
(function() {
|
|
157
|
+
if (window.nativeEmitterMap && window.nativeEmitterMap['${bridgeId}']) {
|
|
158
|
+
window.nativeEmitterMap['${bridgeId}'].emit('${eventName}', ${dataString});
|
|
159
|
+
} else if (window.nativeEmitter) {
|
|
160
|
+
// @deprecated This version is not used after 1.7.2
|
|
161
|
+
window.nativeEmitter.emit('${eventName}', ${dataString});
|
|
162
|
+
} else {
|
|
163
|
+
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
|
|
164
|
+
window.nativeBatchedEvents.push(['${eventName}', ${dataString}]);
|
|
165
|
+
}
|
|
166
|
+
return true;
|
|
167
|
+
})();
|
|
160
168
|
`;
|
|
161
169
|
};
|
|
162
170
|
|
|
@@ -164,14 +172,16 @@ export const SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID = (
|
|
|
164
172
|
bridgeId: string,
|
|
165
173
|
eventName: string,
|
|
166
174
|
) => `
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
175
|
+
(function() {
|
|
176
|
+
if (window.nativeEmitterMap && window.nativeEmitterMap['${bridgeId}']) {
|
|
177
|
+
window.nativeEmitterMap['${bridgeId}'].emit('${eventName}', {}, true);
|
|
178
|
+
} else if (window.nativeEmitter) {
|
|
179
|
+
// @deprecated This version is not used after 1.7.2
|
|
180
|
+
window.nativeEmitter.emit('${eventName}', {}, true);
|
|
181
|
+
} else {
|
|
182
|
+
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
|
|
183
|
+
window.nativeBatchedEvents.push(['${eventName}', {}, true]);
|
|
184
|
+
}
|
|
185
|
+
return true;
|
|
186
|
+
})();
|
|
177
187
|
`;
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
export const INJECT_DEBUG = `
|
|
2
|
-
{
|
|
1
|
+
export const INJECT_DEBUG = (uniqueId: string) => `
|
|
2
|
+
(function() {
|
|
3
|
+
if (window["__bridge_debug_injected_${uniqueId}"]) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
window["__bridge_debug_injected_${uniqueId}"] = true;
|
|
3
8
|
const originalConsoleLog = console.log;
|
|
4
9
|
const originalConsoleError = console.error;
|
|
5
10
|
const originalConsoleWarn = console.warn;
|
|
@@ -27,7 +32,7 @@ export const INJECT_DEBUG = `
|
|
|
27
32
|
);
|
|
28
33
|
originalConsoleWarn.apply(console, arguments);
|
|
29
34
|
};
|
|
30
|
-
};
|
|
35
|
+
})();
|
|
31
36
|
`;
|
|
32
37
|
|
|
33
38
|
export type LogType = "log" | "error" | "warn";
|