@webview-bridge/react-native 1.7.2 → 1.7.4
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 +23 -14
- package/dist/index.d.cts +5 -3
- package/dist/index.d.ts +5 -3
- package/dist/index.js +16 -11
- package/package.json +5 -4
- package/src/createWebView.tsx +8 -4
- package/src/integrations/bridge.ts +45 -6
- package/src/integrations/handleRegisterWebMethod.ts +2 -2
- package/src/types/webview.ts +3 -2
package/dist/index.cjs
CHANGED
|
@@ -411,8 +411,11 @@ __export(src_exports, {
|
|
|
411
411
|
SAFE_NATIVE_EMITTER_EMIT: function() {
|
|
412
412
|
return SAFE_NATIVE_EMITTER_EMIT;
|
|
413
413
|
},
|
|
414
|
-
|
|
415
|
-
return
|
|
414
|
+
SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID: function() {
|
|
415
|
+
return SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID;
|
|
416
|
+
},
|
|
417
|
+
SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID: function() {
|
|
418
|
+
return SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID;
|
|
416
419
|
},
|
|
417
420
|
bridge: function() {
|
|
418
421
|
return bridge;
|
|
@@ -433,7 +436,7 @@ __export(src_exports, {
|
|
|
433
436
|
module.exports = __toCommonJS(src_exports);
|
|
434
437
|
// src/createWebView.tsx
|
|
435
438
|
var import_utils3 = require("@webview-bridge/utils");
|
|
436
|
-
var import_react =
|
|
439
|
+
var import_react = require("react");
|
|
437
440
|
var import_react_native_webview = __toESM(require("react-native-webview"), 1);
|
|
438
441
|
// src/integrations/bridge.ts
|
|
439
442
|
var import_utils = require("@webview-bridge/utils");
|
|
@@ -491,15 +494,15 @@ var bridge = function(procedures) {
|
|
|
491
494
|
};
|
|
492
495
|
var handleBridge = /*#__PURE__*/ function() {
|
|
493
496
|
var _ref = _async_to_generator(function(param) {
|
|
494
|
-
var bridge2, method, args, webview, eventId, _bridge, _method, handleThrow, response, error;
|
|
497
|
+
var bridge2, method, args, webview, eventId, bridgeId, _bridge, _method, handleThrow, response, error;
|
|
495
498
|
return _ts_generator(this, function(_state) {
|
|
496
499
|
switch(_state.label){
|
|
497
500
|
case 0:
|
|
498
|
-
bridge2 = param.bridge, method = param.method, args = param.args, webview = param.webview, eventId = param.eventId;
|
|
501
|
+
bridge2 = param.bridge, method = param.method, args = param.args, webview = param.webview, eventId = param.eventId, bridgeId = param.bridgeId;
|
|
499
502
|
_bridge = bridge2.getState();
|
|
500
503
|
_method = _bridge[method];
|
|
501
504
|
handleThrow = function() {
|
|
502
|
-
webview.injectJavaScript(
|
|
505
|
+
webview.injectJavaScript(SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID(bridgeId, "".concat(method, "-").concat(eventId)));
|
|
503
506
|
};
|
|
504
507
|
if (!(method in _bridge)) {
|
|
505
508
|
handleThrow();
|
|
@@ -526,7 +529,7 @@ var handleBridge = /*#__PURE__*/ function() {
|
|
|
526
529
|
];
|
|
527
530
|
case 2:
|
|
528
531
|
response = _state.sent();
|
|
529
|
-
webview.injectJavaScript(
|
|
532
|
+
webview.injectJavaScript(SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID(bridgeId, "".concat(method, "-").concat(eventId), response));
|
|
530
533
|
return [
|
|
531
534
|
3,
|
|
532
535
|
4
|
|
@@ -558,10 +561,14 @@ var INJECT_BRIDGE_STATE = function(initialState) {
|
|
|
558
561
|
};
|
|
559
562
|
var SAFE_NATIVE_EMITTER_EMIT = function(eventName, data) {
|
|
560
563
|
var dataString = JSON.stringify(data);
|
|
561
|
-
return "\nif (window.nativeEmitter) {\n window.nativeEmitter.emit('".concat(eventName, "', ").concat(dataString, ");\n} else {\n window.nativeBatchedEvents = window.nativeBatchedEvents || [];\n window.nativeBatchedEvents.push(['").concat(eventName, "', ").concat(dataString, "]);\n}\ntrue;\n");
|
|
564
|
+
return "\nif (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}\ntrue;\n");
|
|
565
|
+
};
|
|
566
|
+
var SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID = function(bridgeId, eventName, data) {
|
|
567
|
+
var dataString = JSON.stringify(data);
|
|
568
|
+
return "\nif (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}\ntrue;\n");
|
|
562
569
|
};
|
|
563
|
-
var
|
|
564
|
-
return "\nif (window.nativeEmitter) {\n window.nativeEmitter.emit('".concat(eventName, "', {}, true);\n} else {\n window.nativeBatchedEvents = window.nativeBatchedEvents || [];\n window.nativeBatchedEvents.push(['").concat(eventName, "', {}, true]);\n}\ntrue;\n");
|
|
570
|
+
var SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID = function(bridgeId, eventName) {
|
|
571
|
+
return "\nif (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}\ntrue;\n");
|
|
565
572
|
};
|
|
566
573
|
// src/integrations/console.ts
|
|
567
574
|
var INJECT_DEBUG = '\n{\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';
|
|
@@ -753,7 +760,7 @@ var createWebView = function(param) {
|
|
|
753
760
|
}, []);
|
|
754
761
|
var handleMessage = /*#__PURE__*/ function() {
|
|
755
762
|
var _ref = _async_to_generator(function(event) {
|
|
756
|
-
var _props_onMessage, _JSON_parse, type, body, method, args, method1, args1, eventId, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, ref2, _ref2_current, bridgeNames, eventId1, funcName, value, eventId2, funcName1, method2;
|
|
763
|
+
var _props_onMessage, _JSON_parse, type, body, bridgeId, method, args, method1, args1, eventId, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, ref2, _ref2_current, bridgeNames, eventId1, funcName, value, eventId2, funcName1, method2;
|
|
757
764
|
return _ts_generator(this, function(_state) {
|
|
758
765
|
(_props_onMessage = props.onMessage) === null || _props_onMessage === void 0 ? void 0 : _props_onMessage.call(props, event);
|
|
759
766
|
if (!webviewRef.current) {
|
|
@@ -761,7 +768,7 @@ var createWebView = function(param) {
|
|
|
761
768
|
2
|
|
762
769
|
];
|
|
763
770
|
}
|
|
764
|
-
_JSON_parse = JSON.parse(event.nativeEvent.data), type = _JSON_parse.type, body = _JSON_parse.body;
|
|
771
|
+
_JSON_parse = JSON.parse(event.nativeEvent.data), type = _JSON_parse.type, body = _JSON_parse.body, bridgeId = _JSON_parse.bridgeId;
|
|
765
772
|
switch(type){
|
|
766
773
|
case "log":
|
|
767
774
|
{
|
|
@@ -775,6 +782,7 @@ var createWebView = function(param) {
|
|
|
775
782
|
{
|
|
776
783
|
method1 = body.method, args1 = body.args, eventId = body.eventId;
|
|
777
784
|
handleBridge({
|
|
785
|
+
bridgeId: bridgeId,
|
|
778
786
|
bridge: bridge2,
|
|
779
787
|
method: method1,
|
|
780
788
|
args: args1,
|
|
@@ -855,7 +863,7 @@ var createWebView = function(param) {
|
|
|
855
863
|
return _ref.apply(this, arguments);
|
|
856
864
|
};
|
|
857
865
|
}();
|
|
858
|
-
return /* @__PURE__ */
|
|
866
|
+
return /* @__PURE__ */ React.createElement(import_react_native_webview.default, _object_spread_props(_object_spread({}, props), {
|
|
859
867
|
ref: webviewRef,
|
|
860
868
|
onMessage: handleMessage,
|
|
861
869
|
injectedJavaScriptBeforeContentLoaded: [
|
|
@@ -922,7 +930,8 @@ function useBridge(store, selector) {
|
|
|
922
930
|
INJECT_BRIDGE_METHODS: INJECT_BRIDGE_METHODS,
|
|
923
931
|
INJECT_BRIDGE_STATE: INJECT_BRIDGE_STATE,
|
|
924
932
|
SAFE_NATIVE_EMITTER_EMIT: SAFE_NATIVE_EMITTER_EMIT,
|
|
925
|
-
|
|
933
|
+
SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID: SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID,
|
|
934
|
+
SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID: SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID,
|
|
926
935
|
bridge: bridge,
|
|
927
936
|
createWebView: createWebView,
|
|
928
937
|
handleBridge: handleBridge,
|
package/dist/index.d.cts
CHANGED
|
@@ -146,16 +146,18 @@ type HandleBridgeArgs<ArgType = unknown> = {
|
|
|
146
146
|
args?: ArgType[];
|
|
147
147
|
webview: WebView;
|
|
148
148
|
eventId: string;
|
|
149
|
+
bridgeId: string;
|
|
149
150
|
};
|
|
150
|
-
declare const handleBridge: ({ bridge, method, args, webview, eventId, }: HandleBridgeArgs) => Promise<void>;
|
|
151
|
+
declare const handleBridge: ({ bridge, method, args, webview, eventId, bridgeId, }: HandleBridgeArgs) => Promise<void>;
|
|
151
152
|
declare const INJECT_BRIDGE_METHODS: (bridgeNames: string[]) => string;
|
|
152
153
|
declare const INJECT_BRIDGE_STATE: (initialState: Record<string, Primitive>) => string;
|
|
153
154
|
declare const SAFE_NATIVE_EMITTER_EMIT: (eventName: string, data: unknown) => string;
|
|
154
|
-
declare const
|
|
155
|
+
declare const SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID: (bridgeId: string, eventName: string, data: unknown) => string;
|
|
156
|
+
declare const SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID: (bridgeId: string, eventName: string) => string;
|
|
155
157
|
|
|
156
158
|
declare const postMessageSchema: <T extends PostMessageSchemaObject>(schema: T) => T;
|
|
157
159
|
|
|
158
160
|
declare function useBridge<T extends Bridge>(store: BridgeStore<T>): ExtractStore<BridgeStore<T>>;
|
|
159
161
|
declare function useBridge<T extends Bridge, U extends ExtractStore<BridgeStore<T>>, V>(store: BridgeStore<T>, selector?: (state: U) => V): V;
|
|
160
162
|
|
|
161
|
-
export { type BridgeWebView, type CreateWebViewArgs, INJECT_BRIDGE_METHODS, INJECT_BRIDGE_STATE, SAFE_NATIVE_EMITTER_EMIT,
|
|
163
|
+
export { type BridgeWebView, type CreateWebViewArgs, INJECT_BRIDGE_METHODS, INJECT_BRIDGE_STATE, SAFE_NATIVE_EMITTER_EMIT, SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID, SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID, type StoreCallback, type WebMethod, bridge, createWebView, handleBridge, postMessageSchema, useBridge };
|
package/dist/index.d.ts
CHANGED
|
@@ -146,16 +146,18 @@ type HandleBridgeArgs<ArgType = unknown> = {
|
|
|
146
146
|
args?: ArgType[];
|
|
147
147
|
webview: WebView;
|
|
148
148
|
eventId: string;
|
|
149
|
+
bridgeId: string;
|
|
149
150
|
};
|
|
150
|
-
declare const handleBridge: ({ bridge, method, args, webview, eventId, }: HandleBridgeArgs) => Promise<void>;
|
|
151
|
+
declare const handleBridge: ({ bridge, method, args, webview, eventId, bridgeId, }: HandleBridgeArgs) => Promise<void>;
|
|
151
152
|
declare const INJECT_BRIDGE_METHODS: (bridgeNames: string[]) => string;
|
|
152
153
|
declare const INJECT_BRIDGE_STATE: (initialState: Record<string, Primitive>) => string;
|
|
153
154
|
declare const SAFE_NATIVE_EMITTER_EMIT: (eventName: string, data: unknown) => string;
|
|
154
|
-
declare const
|
|
155
|
+
declare const SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID: (bridgeId: string, eventName: string, data: unknown) => string;
|
|
156
|
+
declare const SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID: (bridgeId: string, eventName: string) => string;
|
|
155
157
|
|
|
156
158
|
declare const postMessageSchema: <T extends PostMessageSchemaObject>(schema: T) => T;
|
|
157
159
|
|
|
158
160
|
declare function useBridge<T extends Bridge>(store: BridgeStore<T>): ExtractStore<BridgeStore<T>>;
|
|
159
161
|
declare function useBridge<T extends Bridge, U extends ExtractStore<BridgeStore<T>>, V>(store: BridgeStore<T>, selector?: (state: U) => V): V;
|
|
160
162
|
|
|
161
|
-
export { type BridgeWebView, type CreateWebViewArgs, INJECT_BRIDGE_METHODS, INJECT_BRIDGE_STATE, SAFE_NATIVE_EMITTER_EMIT,
|
|
163
|
+
export { type BridgeWebView, type CreateWebViewArgs, INJECT_BRIDGE_METHODS, INJECT_BRIDGE_STATE, SAFE_NATIVE_EMITTER_EMIT, SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID, SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID, type StoreCallback, type WebMethod, bridge, createWebView, handleBridge, postMessageSchema, useBridge };
|
package/dist/index.js
CHANGED
|
@@ -342,7 +342,7 @@ function _ts_generator(thisArg, body) {
|
|
|
342
342
|
}
|
|
343
343
|
}
|
|
344
344
|
import { createEvents } from "@webview-bridge/utils";
|
|
345
|
-
import
|
|
345
|
+
import { forwardRef, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef } from "react";
|
|
346
346
|
import WebView from "react-native-webview";
|
|
347
347
|
// src/integrations/bridge.ts
|
|
348
348
|
import { equals, removeUndefinedKeys } from "@webview-bridge/utils";
|
|
@@ -400,15 +400,15 @@ var bridge = function(procedures) {
|
|
|
400
400
|
};
|
|
401
401
|
var handleBridge = /*#__PURE__*/ function() {
|
|
402
402
|
var _ref = _async_to_generator(function(param) {
|
|
403
|
-
var bridge2, method, args, webview, eventId, _bridge, _method, handleThrow, response, error;
|
|
403
|
+
var bridge2, method, args, webview, eventId, bridgeId, _bridge, _method, handleThrow, response, error;
|
|
404
404
|
return _ts_generator(this, function(_state) {
|
|
405
405
|
switch(_state.label){
|
|
406
406
|
case 0:
|
|
407
|
-
bridge2 = param.bridge, method = param.method, args = param.args, webview = param.webview, eventId = param.eventId;
|
|
407
|
+
bridge2 = param.bridge, method = param.method, args = param.args, webview = param.webview, eventId = param.eventId, bridgeId = param.bridgeId;
|
|
408
408
|
_bridge = bridge2.getState();
|
|
409
409
|
_method = _bridge[method];
|
|
410
410
|
handleThrow = function() {
|
|
411
|
-
webview.injectJavaScript(
|
|
411
|
+
webview.injectJavaScript(SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID(bridgeId, "".concat(method, "-").concat(eventId)));
|
|
412
412
|
};
|
|
413
413
|
if (!(method in _bridge)) {
|
|
414
414
|
handleThrow();
|
|
@@ -435,7 +435,7 @@ var handleBridge = /*#__PURE__*/ function() {
|
|
|
435
435
|
];
|
|
436
436
|
case 2:
|
|
437
437
|
response = _state.sent();
|
|
438
|
-
webview.injectJavaScript(
|
|
438
|
+
webview.injectJavaScript(SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID(bridgeId, "".concat(method, "-").concat(eventId), response));
|
|
439
439
|
return [
|
|
440
440
|
3,
|
|
441
441
|
4
|
|
@@ -467,10 +467,14 @@ var INJECT_BRIDGE_STATE = function(initialState) {
|
|
|
467
467
|
};
|
|
468
468
|
var SAFE_NATIVE_EMITTER_EMIT = function(eventName, data) {
|
|
469
469
|
var dataString = JSON.stringify(data);
|
|
470
|
-
return "\nif (window.nativeEmitter) {\n window.nativeEmitter.emit('".concat(eventName, "', ").concat(dataString, ");\n} else {\n window.nativeBatchedEvents = window.nativeBatchedEvents || [];\n window.nativeBatchedEvents.push(['").concat(eventName, "', ").concat(dataString, "]);\n}\ntrue;\n");
|
|
470
|
+
return "\nif (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}\ntrue;\n");
|
|
471
471
|
};
|
|
472
|
-
var
|
|
473
|
-
|
|
472
|
+
var SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID = function(bridgeId, eventName, data) {
|
|
473
|
+
var dataString = JSON.stringify(data);
|
|
474
|
+
return "\nif (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}\ntrue;\n");
|
|
475
|
+
};
|
|
476
|
+
var SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID = function(bridgeId, eventName) {
|
|
477
|
+
return "\nif (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}\ntrue;\n");
|
|
474
478
|
};
|
|
475
479
|
// src/integrations/console.ts
|
|
476
480
|
var INJECT_DEBUG = '\n{\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';
|
|
@@ -663,7 +667,7 @@ var createWebView = function(param) {
|
|
|
663
667
|
}, []);
|
|
664
668
|
var handleMessage = /*#__PURE__*/ function() {
|
|
665
669
|
var _ref = _async_to_generator(function(event) {
|
|
666
|
-
var _props_onMessage, _JSON_parse, type, body, method, args, method1, args1, eventId, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, ref2, _ref2_current, bridgeNames, eventId1, funcName, value, eventId2, funcName1, method2;
|
|
670
|
+
var _props_onMessage, _JSON_parse, type, body, bridgeId, method, args, method1, args1, eventId, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, ref2, _ref2_current, bridgeNames, eventId1, funcName, value, eventId2, funcName1, method2;
|
|
667
671
|
return _ts_generator(this, function(_state) {
|
|
668
672
|
(_props_onMessage = props.onMessage) === null || _props_onMessage === void 0 ? void 0 : _props_onMessage.call(props, event);
|
|
669
673
|
if (!webviewRef.current) {
|
|
@@ -671,7 +675,7 @@ var createWebView = function(param) {
|
|
|
671
675
|
2
|
|
672
676
|
];
|
|
673
677
|
}
|
|
674
|
-
_JSON_parse = JSON.parse(event.nativeEvent.data), type = _JSON_parse.type, body = _JSON_parse.body;
|
|
678
|
+
_JSON_parse = JSON.parse(event.nativeEvent.data), type = _JSON_parse.type, body = _JSON_parse.body, bridgeId = _JSON_parse.bridgeId;
|
|
675
679
|
switch(type){
|
|
676
680
|
case "log":
|
|
677
681
|
{
|
|
@@ -685,6 +689,7 @@ var createWebView = function(param) {
|
|
|
685
689
|
{
|
|
686
690
|
method1 = body.method, args1 = body.args, eventId = body.eventId;
|
|
687
691
|
handleBridge({
|
|
692
|
+
bridgeId: bridgeId,
|
|
688
693
|
bridge: bridge2,
|
|
689
694
|
method: method1,
|
|
690
695
|
args: args1,
|
|
@@ -827,4 +832,4 @@ function useBridge(store, selector) {
|
|
|
827
832
|
};
|
|
828
833
|
return useSyncExternalStoreWithSelector(store.subscribe, store.getState, store.getState, $selector);
|
|
829
834
|
}
|
|
830
|
-
export { INJECT_BRIDGE_METHODS, INJECT_BRIDGE_STATE, SAFE_NATIVE_EMITTER_EMIT,
|
|
835
|
+
export { INJECT_BRIDGE_METHODS, INJECT_BRIDGE_STATE, SAFE_NATIVE_EMITTER_EMIT, SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID, SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID, bridge, createWebView, handleBridge, postMessageSchema, useBridge };
|
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.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Fully Type-Safe Integration for React Native WebView and Web",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -39,12 +39,13 @@
|
|
|
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.4",
|
|
43
|
+
"@webview-bridge/types": "1.7.4",
|
|
44
44
|
"use-sync-external-store": "^1.2.0"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "tsup src/index.ts --format cjs,esm --dts --target es5",
|
|
48
|
-
"test:type": "tsc --noEmit"
|
|
48
|
+
"test:type": "tsc --noEmit",
|
|
49
|
+
"lint": "biome check"
|
|
49
50
|
}
|
|
50
51
|
}
|
package/src/createWebView.tsx
CHANGED
|
@@ -7,7 +7,8 @@ import type {
|
|
|
7
7
|
Primitive,
|
|
8
8
|
} from "@webview-bridge/types";
|
|
9
9
|
import { createEvents } from "@webview-bridge/utils";
|
|
10
|
-
import React
|
|
10
|
+
import type React from "react";
|
|
11
|
+
import {
|
|
11
12
|
forwardRef,
|
|
12
13
|
useEffect,
|
|
13
14
|
useImperativeHandle,
|
|
@@ -19,12 +20,12 @@ import type { WebViewMessageEvent, WebViewProps } from "react-native-webview";
|
|
|
19
20
|
import WebView from "react-native-webview";
|
|
20
21
|
|
|
21
22
|
import {
|
|
22
|
-
handleBridge,
|
|
23
23
|
INJECT_BRIDGE_METHODS,
|
|
24
24
|
INJECT_BRIDGE_STATE,
|
|
25
25
|
SAFE_NATIVE_EMITTER_EMIT,
|
|
26
|
+
handleBridge,
|
|
26
27
|
} from "./integrations/bridge";
|
|
27
|
-
import {
|
|
28
|
+
import { INJECT_DEBUG, type LogType, handleLog } from "./integrations/console";
|
|
28
29
|
import { handleRegisterWebMethod } from "./integrations/handleRegisterWebMethod";
|
|
29
30
|
import type { BridgeWebView } from "./types/webview";
|
|
30
31
|
|
|
@@ -190,6 +191,7 @@ export const createWebView = <
|
|
|
190
191
|
};
|
|
191
192
|
}, []);
|
|
192
193
|
|
|
194
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
|
|
193
195
|
const initData = useMemo(() => {
|
|
194
196
|
const bridgeMethods = Object.entries(bridge.getState() ?? {})
|
|
195
197
|
.filter(([_, bridge]) => typeof bridge === "function")
|
|
@@ -203,6 +205,7 @@ export const createWebView = <
|
|
|
203
205
|
}, []);
|
|
204
206
|
|
|
205
207
|
useEffect(() => {
|
|
208
|
+
// lazy initialize the bridgeId
|
|
206
209
|
webviewRef.current?.injectJavaScript(
|
|
207
210
|
SAFE_NATIVE_EMITTER_EMIT("hydrate", initData),
|
|
208
211
|
);
|
|
@@ -216,7 +219,7 @@ export const createWebView = <
|
|
|
216
219
|
if (!webviewRef.current) {
|
|
217
220
|
return;
|
|
218
221
|
}
|
|
219
|
-
const { type, body } = JSON.parse(event.nativeEvent.data);
|
|
222
|
+
const { type, body, bridgeId } = JSON.parse(event.nativeEvent.data);
|
|
220
223
|
|
|
221
224
|
switch (type) {
|
|
222
225
|
case "log": {
|
|
@@ -235,6 +238,7 @@ export const createWebView = <
|
|
|
235
238
|
};
|
|
236
239
|
|
|
237
240
|
handleBridge({
|
|
241
|
+
bridgeId,
|
|
238
242
|
bridge,
|
|
239
243
|
method,
|
|
240
244
|
args,
|
|
@@ -5,7 +5,7 @@ import type {
|
|
|
5
5
|
Primitive,
|
|
6
6
|
} from "@webview-bridge/types";
|
|
7
7
|
import { equals, removeUndefinedKeys } from "@webview-bridge/utils";
|
|
8
|
-
import WebView from "react-native-webview";
|
|
8
|
+
import type WebView from "react-native-webview";
|
|
9
9
|
|
|
10
10
|
export type StoreCallback<T> = ({
|
|
11
11
|
get,
|
|
@@ -69,6 +69,7 @@ type HandleBridgeArgs<ArgType = unknown> = {
|
|
|
69
69
|
args?: ArgType[];
|
|
70
70
|
webview: WebView;
|
|
71
71
|
eventId: string;
|
|
72
|
+
bridgeId: string;
|
|
72
73
|
};
|
|
73
74
|
|
|
74
75
|
export const handleBridge = async ({
|
|
@@ -77,12 +78,15 @@ export const handleBridge = async ({
|
|
|
77
78
|
args,
|
|
78
79
|
webview,
|
|
79
80
|
eventId,
|
|
81
|
+
bridgeId,
|
|
80
82
|
}: HandleBridgeArgs) => {
|
|
81
83
|
const _bridge = bridge.getState();
|
|
82
84
|
|
|
83
85
|
const _method = _bridge[method];
|
|
84
86
|
const handleThrow = () => {
|
|
85
|
-
webview.injectJavaScript(
|
|
87
|
+
webview.injectJavaScript(
|
|
88
|
+
SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID(bridgeId, `${method}-${eventId}`),
|
|
89
|
+
);
|
|
86
90
|
};
|
|
87
91
|
if (!(method in _bridge)) {
|
|
88
92
|
handleThrow();
|
|
@@ -96,7 +100,11 @@ export const handleBridge = async ({
|
|
|
96
100
|
const response = await _method?.(...(args ?? []));
|
|
97
101
|
|
|
98
102
|
webview.injectJavaScript(
|
|
99
|
-
|
|
103
|
+
SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID(
|
|
104
|
+
bridgeId,
|
|
105
|
+
`${method}-${eventId}`,
|
|
106
|
+
response,
|
|
107
|
+
),
|
|
100
108
|
);
|
|
101
109
|
} catch (error) {
|
|
102
110
|
handleThrow();
|
|
@@ -117,7 +125,12 @@ export const INJECT_BRIDGE_STATE = (
|
|
|
117
125
|
export const SAFE_NATIVE_EMITTER_EMIT = (eventName: string, data: unknown) => {
|
|
118
126
|
const dataString = JSON.stringify(data);
|
|
119
127
|
return `
|
|
120
|
-
if (window.
|
|
128
|
+
if (window.nativeEmitterMap && Object.keys(window.nativeEmitterMap).length > 0) {
|
|
129
|
+
for (const [_, emitter] of Object.entries(window.nativeEmitterMap)) {
|
|
130
|
+
emitter.emit('${eventName}', ${dataString});
|
|
131
|
+
}
|
|
132
|
+
} else if (window.nativeEmitter) {
|
|
133
|
+
// @deprecated This version is not used after 1.7.2
|
|
121
134
|
window.nativeEmitter.emit('${eventName}', ${dataString});
|
|
122
135
|
} else {
|
|
123
136
|
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
|
|
@@ -127,8 +140,34 @@ true;
|
|
|
127
140
|
`;
|
|
128
141
|
};
|
|
129
142
|
|
|
130
|
-
export const
|
|
131
|
-
|
|
143
|
+
export const SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID = (
|
|
144
|
+
bridgeId: string,
|
|
145
|
+
eventName: string,
|
|
146
|
+
data: unknown,
|
|
147
|
+
) => {
|
|
148
|
+
const dataString = JSON.stringify(data);
|
|
149
|
+
return `
|
|
150
|
+
if (window.nativeEmitterMap && window.nativeEmitterMap['${bridgeId}']) {
|
|
151
|
+
window.nativeEmitterMap['${bridgeId}'].emit('${eventName}', ${dataString});
|
|
152
|
+
} else if (window.nativeEmitter) {
|
|
153
|
+
// @deprecated This version is not used after 1.7.2
|
|
154
|
+
window.nativeEmitter.emit('${eventName}', ${dataString});
|
|
155
|
+
} else {
|
|
156
|
+
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
|
|
157
|
+
window.nativeBatchedEvents.push(['${eventName}', ${dataString}]);
|
|
158
|
+
}
|
|
159
|
+
true;
|
|
160
|
+
`;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
export const SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID = (
|
|
164
|
+
bridgeId: string,
|
|
165
|
+
eventName: string,
|
|
166
|
+
) => `
|
|
167
|
+
if (window.nativeEmitterMap && window.nativeEmitterMap['${bridgeId}']) {
|
|
168
|
+
window.nativeEmitterMap['${bridgeId}'].emit('${eventName}', {}, true);
|
|
169
|
+
} else if (window.nativeEmitter) {
|
|
170
|
+
// @deprecated This version is not used after 1.7.2
|
|
132
171
|
window.nativeEmitter.emit('${eventName}', {}, true);
|
|
133
172
|
} else {
|
|
134
173
|
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { Bridge } from "@webview-bridge/types";
|
|
2
2
|
import {
|
|
3
|
+
type EventEmitter,
|
|
3
4
|
createRandomId,
|
|
4
5
|
createResolver,
|
|
5
|
-
type EventEmitter,
|
|
6
6
|
timeout,
|
|
7
7
|
} from "@webview-bridge/utils";
|
|
8
|
-
import WebView from "react-native-webview";
|
|
8
|
+
import type WebView from "react-native-webview";
|
|
9
9
|
|
|
10
10
|
import { WebMethodError } from "../error";
|
|
11
11
|
|
package/src/types/webview.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Component } from "react";
|
|
2
|
-
import WebView
|
|
1
|
+
import type { Component } from "react";
|
|
2
|
+
import type WebView from "react-native-webview";
|
|
3
|
+
import type { WebViewProps } from "react-native-webview";
|
|
3
4
|
|
|
4
5
|
export type BridgeWebView = Pick<
|
|
5
6
|
WebView,
|