@webview-bridge/react-native 1.7.7 → 1.7.9
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 +58 -35
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +56 -33
- package/package.json +6 -9
- package/src/createWebView.tsx +3 -2
- package/src/error.ts +14 -0
- package/src/integrations/bridge.ts +58 -38
- package/src/integrations/console.ts +5 -10
- package/src/useBridge.ts +1 -1
package/dist/index.cjs
CHANGED
|
@@ -103,6 +103,13 @@ function _inherits(subClass, superClass) {
|
|
|
103
103
|
});
|
|
104
104
|
if (superClass) _set_prototype_of(subClass, superClass);
|
|
105
105
|
}
|
|
106
|
+
function _instanceof(left, right) {
|
|
107
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
108
|
+
return !!right[Symbol.hasInstance](left);
|
|
109
|
+
} else {
|
|
110
|
+
return left instanceof right;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
106
113
|
function _is_native_function(fn) {
|
|
107
114
|
return Function.toString.call(fn).indexOf("[native code]") !== -1;
|
|
108
115
|
}
|
|
@@ -435,11 +442,39 @@ __export(index_exports, {
|
|
|
435
442
|
});
|
|
436
443
|
module.exports = __toCommonJS(index_exports);
|
|
437
444
|
// src/createWebView.tsx
|
|
438
|
-
var
|
|
445
|
+
var import_utils3 = require("@webview-bridge/utils");
|
|
439
446
|
var import_react = require("react");
|
|
440
447
|
var import_react_native_webview = __toESM(require("react-native-webview"), 1);
|
|
441
448
|
// src/integrations/bridge.ts
|
|
442
449
|
var import_utils = require("@webview-bridge/utils");
|
|
450
|
+
// src/error.ts
|
|
451
|
+
var WebMethodError = /*#__PURE__*/ function(Error1) {
|
|
452
|
+
_inherits(WebMethodError, Error1);
|
|
453
|
+
function WebMethodError(methodName) {
|
|
454
|
+
_class_call_check(this, WebMethodError);
|
|
455
|
+
var _this;
|
|
456
|
+
_this = _call_super(this, WebMethodError, [
|
|
457
|
+
"An error occurred in the WebMethod.current.".concat(methodName)
|
|
458
|
+
]);
|
|
459
|
+
_this.name = "WebMethodError";
|
|
460
|
+
return _this;
|
|
461
|
+
}
|
|
462
|
+
return WebMethodError;
|
|
463
|
+
}(_wrap_native_super(Error));
|
|
464
|
+
function serializeError(error) {
|
|
465
|
+
return JSON.stringify(error, function(_, value) {
|
|
466
|
+
if (_instanceof(value, Error)) {
|
|
467
|
+
return {
|
|
468
|
+
name: value.name,
|
|
469
|
+
message: value.message,
|
|
470
|
+
stack: value.stack,
|
|
471
|
+
__isError: true
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
return value;
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
// src/integrations/bridge.ts
|
|
443
478
|
var bridge = function(procedures) {
|
|
444
479
|
var getState = function() {
|
|
445
480
|
return state;
|
|
@@ -501,8 +536,9 @@ var handleBridge = /*#__PURE__*/ function() {
|
|
|
501
536
|
bridge2 = param.bridge, method = param.method, args = param.args, webview = param.webview, eventId = param.eventId, bridgeId = param.bridgeId;
|
|
502
537
|
_bridge = bridge2.getState();
|
|
503
538
|
_method = _bridge[method];
|
|
504
|
-
handleThrow = function() {
|
|
505
|
-
|
|
539
|
+
handleThrow = function(error) {
|
|
540
|
+
var serializedError = error ? serializeError(error) : true;
|
|
541
|
+
webview.injectJavaScript(SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID(bridgeId, "".concat(method, "-").concat(eventId), serializedError));
|
|
506
542
|
};
|
|
507
543
|
if (!(method in _bridge)) {
|
|
508
544
|
handleThrow();
|
|
@@ -536,7 +572,7 @@ var handleBridge = /*#__PURE__*/ function() {
|
|
|
536
572
|
];
|
|
537
573
|
case 3:
|
|
538
574
|
error = _state.sent();
|
|
539
|
-
handleThrow();
|
|
575
|
+
handleThrow(_instanceof(error, Error) ? error : new Error(String(error)));
|
|
540
576
|
console.error(error);
|
|
541
577
|
return [
|
|
542
578
|
3,
|
|
@@ -554,26 +590,27 @@ var handleBridge = /*#__PURE__*/ function() {
|
|
|
554
590
|
};
|
|
555
591
|
}();
|
|
556
592
|
var INJECT_BRIDGE_METHODS = function(bridgeNames) {
|
|
557
|
-
return "\n window.__bridgeMethods__ = ".concat(JSON.stringify(bridgeNames), ";\n");
|
|
593
|
+
return "\n (function() {\n window.__bridgeMethods__ = ".concat(JSON.stringify(bridgeNames), ";\n })();\n");
|
|
558
594
|
};
|
|
559
595
|
var INJECT_BRIDGE_STATE = function(initialState) {
|
|
560
|
-
return "\n window.__bridgeInitialState__ = ".concat(JSON.stringify(initialState), ";\n");
|
|
596
|
+
return "\n (function() {\n window.__bridgeInitialState__ = ".concat(JSON.stringify(initialState), ";\n })();\n");
|
|
561
597
|
};
|
|
562
598
|
var SAFE_NATIVE_EMITTER_EMIT = function(eventName, data) {
|
|
563
599
|
var dataString = JSON.stringify(data);
|
|
564
|
-
return "\
|
|
600
|
+
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
601
|
};
|
|
566
602
|
var SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID = function(bridgeId, eventName, data) {
|
|
567
603
|
var dataString = JSON.stringify(data);
|
|
568
|
-
return "\
|
|
604
|
+
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
605
|
};
|
|
570
|
-
var SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID = function(bridgeId, eventName) {
|
|
571
|
-
|
|
606
|
+
var SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID = function(bridgeId, eventName, serializedError) {
|
|
607
|
+
var serializedErrorString = JSON.stringify(serializedError);
|
|
608
|
+
return "\n (function() {\n if (window.nativeEmitterMap && window.nativeEmitterMap['".concat(bridgeId, "']) {\n window.nativeEmitterMap['").concat(bridgeId, "'].emit('").concat(eventName, "', {}, ").concat(serializedErrorString, ");\n } else if (window.nativeEmitter) {\n // @deprecated This version is not used after 1.7.2\n window.nativeEmitter.emit('").concat(eventName, "', {}, ").concat(serializedErrorString, ");\n } else {\n window.nativeBatchedEvents = window.nativeBatchedEvents || [];\n window.nativeBatchedEvents.push(['").concat(eventName, "', {}, ").concat(serializedErrorString, "]);\n }\n return true;\n })();\n");
|
|
572
609
|
};
|
|
573
610
|
// src/integrations/console.ts
|
|
574
|
-
var
|
|
575
|
-
|
|
576
|
-
|
|
611
|
+
var INJECT_DEBUG = function(uniqueId) {
|
|
612
|
+
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');
|
|
613
|
+
};
|
|
577
614
|
var handleLog = function(type, message) {
|
|
578
615
|
var _JSON_parse = _to_array(JSON.parse(message)), formatMessage = _JSON_parse[0], parsedMessage = _JSON_parse.slice(1);
|
|
579
616
|
var webviewMark = "(WebView) ";
|
|
@@ -605,22 +642,7 @@ var handleLog = function(type, message) {
|
|
|
605
642
|
}
|
|
606
643
|
};
|
|
607
644
|
// src/integrations/handleRegisterWebMethod.ts
|
|
608
|
-
var
|
|
609
|
-
// src/error.ts
|
|
610
|
-
var WebMethodError = /*#__PURE__*/ function(Error1) {
|
|
611
|
-
_inherits(WebMethodError, Error1);
|
|
612
|
-
function WebMethodError(methodName) {
|
|
613
|
-
_class_call_check(this, WebMethodError);
|
|
614
|
-
var _this;
|
|
615
|
-
_this = _call_super(this, WebMethodError, [
|
|
616
|
-
"An error occurred in the WebMethod.current.".concat(methodName)
|
|
617
|
-
]);
|
|
618
|
-
_this.name = "WebMethodError";
|
|
619
|
-
return _this;
|
|
620
|
-
}
|
|
621
|
-
return WebMethodError;
|
|
622
|
-
}(_wrap_native_super(Error));
|
|
623
|
-
// src/integrations/handleRegisterWebMethod.ts
|
|
645
|
+
var import_utils2 = require("@webview-bridge/utils");
|
|
624
646
|
var handleRegisterWebMethod = function(emitter, webview, bridgeNames, responseTimeout) {
|
|
625
647
|
return bridgeNames.reduce(function(acc, methodName) {
|
|
626
648
|
acc[methodName] = /*#__PURE__*/ _async_to_generator(function() {
|
|
@@ -630,11 +652,11 @@ var handleRegisterWebMethod = function(emitter, webview, bridgeNames, responseTi
|
|
|
630
652
|
for(_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
631
653
|
args[_key] = _arguments[_key];
|
|
632
654
|
}
|
|
633
|
-
eventId = (0,
|
|
655
|
+
eventId = (0, import_utils2.createRandomId)();
|
|
634
656
|
return [
|
|
635
657
|
2,
|
|
636
658
|
Promise.race([
|
|
637
|
-
(0,
|
|
659
|
+
(0, import_utils2.createResolver)({
|
|
638
660
|
emitter: emitter,
|
|
639
661
|
methodName: methodName,
|
|
640
662
|
eventId: eventId,
|
|
@@ -643,7 +665,7 @@ var handleRegisterWebMethod = function(emitter, webview, bridgeNames, responseTi
|
|
|
643
665
|
},
|
|
644
666
|
failHandler: new WebMethodError(methodName)
|
|
645
667
|
}),
|
|
646
|
-
(0,
|
|
668
|
+
(0, import_utils2.timeout)(responseTimeout)
|
|
647
669
|
])
|
|
648
670
|
];
|
|
649
671
|
});
|
|
@@ -661,7 +683,8 @@ var createWebView = function(param) {
|
|
|
661
683
|
}
|
|
662
684
|
};
|
|
663
685
|
var webviewRefList = [];
|
|
664
|
-
var emitter = (0,
|
|
686
|
+
var emitter = (0, import_utils3.createEvents)();
|
|
687
|
+
var uniqueId = (0, import_utils3.createRandomId)();
|
|
665
688
|
bridge2.subscribe(function(state) {
|
|
666
689
|
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
667
690
|
try {
|
|
@@ -876,7 +899,7 @@ var createWebView = function(param) {
|
|
|
876
899
|
"true;"
|
|
877
900
|
].filter(Boolean).join("\n"),
|
|
878
901
|
injectedJavaScript: [
|
|
879
|
-
debug && INJECT_DEBUG,
|
|
902
|
+
debug && INJECT_DEBUG(uniqueId),
|
|
880
903
|
props.injectedJavaScript,
|
|
881
904
|
"true;"
|
|
882
905
|
].filter(Boolean).join("\n")
|
|
@@ -921,7 +944,7 @@ var postMessageSchema = function(schema) {
|
|
|
921
944
|
return schema;
|
|
922
945
|
};
|
|
923
946
|
// src/useBridge.ts
|
|
924
|
-
var import_with_selector = require("use-sync-external-store/with-selector
|
|
947
|
+
var import_with_selector = require("use-sync-external-store/with-selector");
|
|
925
948
|
function useBridge(store, selector) {
|
|
926
949
|
var $selector = selector !== null && selector !== void 0 ? selector : function(state) {
|
|
927
950
|
return state;
|
package/dist/index.d.cts
CHANGED
|
@@ -153,7 +153,7 @@ declare const INJECT_BRIDGE_METHODS: (bridgeNames: string[]) => string;
|
|
|
153
153
|
declare const INJECT_BRIDGE_STATE: (initialState: Record<string, Primitive>) => string;
|
|
154
154
|
declare const SAFE_NATIVE_EMITTER_EMIT: (eventName: string, data: unknown) => string;
|
|
155
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;
|
|
156
|
+
declare const SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID: (bridgeId: string, eventName: string, serializedError: string | true) => string;
|
|
157
157
|
|
|
158
158
|
declare const postMessageSchema: <T extends PostMessageSchemaObject>(schema: T) => T;
|
|
159
159
|
|
package/dist/index.d.ts
CHANGED
|
@@ -153,7 +153,7 @@ declare const INJECT_BRIDGE_METHODS: (bridgeNames: string[]) => string;
|
|
|
153
153
|
declare const INJECT_BRIDGE_STATE: (initialState: Record<string, Primitive>) => string;
|
|
154
154
|
declare const SAFE_NATIVE_EMITTER_EMIT: (eventName: string, data: unknown) => string;
|
|
155
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;
|
|
156
|
+
declare const SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID: (bridgeId: string, eventName: string, serializedError: string | true) => string;
|
|
157
157
|
|
|
158
158
|
declare const postMessageSchema: <T extends PostMessageSchemaObject>(schema: T) => T;
|
|
159
159
|
|
package/dist/index.js
CHANGED
|
@@ -103,6 +103,13 @@ function _inherits(subClass, superClass) {
|
|
|
103
103
|
});
|
|
104
104
|
if (superClass) _set_prototype_of(subClass, superClass);
|
|
105
105
|
}
|
|
106
|
+
function _instanceof(left, right) {
|
|
107
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
108
|
+
return !!right[Symbol.hasInstance](left);
|
|
109
|
+
} else {
|
|
110
|
+
return left instanceof right;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
106
113
|
function _is_native_function(fn) {
|
|
107
114
|
return Function.toString.call(fn).indexOf("[native code]") !== -1;
|
|
108
115
|
}
|
|
@@ -341,11 +348,40 @@ function _ts_generator(thisArg, body) {
|
|
|
341
348
|
};
|
|
342
349
|
}
|
|
343
350
|
}
|
|
344
|
-
import { createEvents } from "@webview-bridge/utils";
|
|
351
|
+
import { createEvents, createRandomId as createRandomId2 } from "@webview-bridge/utils";
|
|
345
352
|
import { forwardRef, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef } from "react";
|
|
346
353
|
import WebView from "react-native-webview";
|
|
347
354
|
// src/integrations/bridge.ts
|
|
348
355
|
import { equals, removeUndefinedKeys } from "@webview-bridge/utils";
|
|
356
|
+
// src/error.ts
|
|
357
|
+
var WebMethodError = /*#__PURE__*/ function(Error1) {
|
|
358
|
+
"use strict";
|
|
359
|
+
_inherits(WebMethodError, Error1);
|
|
360
|
+
function WebMethodError(methodName) {
|
|
361
|
+
_class_call_check(this, WebMethodError);
|
|
362
|
+
var _this;
|
|
363
|
+
_this = _call_super(this, WebMethodError, [
|
|
364
|
+
"An error occurred in the WebMethod.current.".concat(methodName)
|
|
365
|
+
]);
|
|
366
|
+
_this.name = "WebMethodError";
|
|
367
|
+
return _this;
|
|
368
|
+
}
|
|
369
|
+
return WebMethodError;
|
|
370
|
+
}(_wrap_native_super(Error));
|
|
371
|
+
function serializeError(error) {
|
|
372
|
+
return JSON.stringify(error, function(_, value) {
|
|
373
|
+
if (_instanceof(value, Error)) {
|
|
374
|
+
return {
|
|
375
|
+
name: value.name,
|
|
376
|
+
message: value.message,
|
|
377
|
+
stack: value.stack,
|
|
378
|
+
__isError: true
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
return value;
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
// src/integrations/bridge.ts
|
|
349
385
|
var bridge = function(procedures) {
|
|
350
386
|
var getState = function() {
|
|
351
387
|
return state;
|
|
@@ -407,8 +443,9 @@ var handleBridge = /*#__PURE__*/ function() {
|
|
|
407
443
|
bridge2 = param.bridge, method = param.method, args = param.args, webview = param.webview, eventId = param.eventId, bridgeId = param.bridgeId;
|
|
408
444
|
_bridge = bridge2.getState();
|
|
409
445
|
_method = _bridge[method];
|
|
410
|
-
handleThrow = function() {
|
|
411
|
-
|
|
446
|
+
handleThrow = function(error) {
|
|
447
|
+
var serializedError = error ? serializeError(error) : true;
|
|
448
|
+
webview.injectJavaScript(SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID(bridgeId, "".concat(method, "-").concat(eventId), serializedError));
|
|
412
449
|
};
|
|
413
450
|
if (!(method in _bridge)) {
|
|
414
451
|
handleThrow();
|
|
@@ -442,7 +479,7 @@ var handleBridge = /*#__PURE__*/ function() {
|
|
|
442
479
|
];
|
|
443
480
|
case 3:
|
|
444
481
|
error = _state.sent();
|
|
445
|
-
handleThrow();
|
|
482
|
+
handleThrow(_instanceof(error, Error) ? error : new Error(String(error)));
|
|
446
483
|
console.error(error);
|
|
447
484
|
return [
|
|
448
485
|
3,
|
|
@@ -460,26 +497,27 @@ var handleBridge = /*#__PURE__*/ function() {
|
|
|
460
497
|
};
|
|
461
498
|
}();
|
|
462
499
|
var INJECT_BRIDGE_METHODS = function(bridgeNames) {
|
|
463
|
-
return "\n window.__bridgeMethods__ = ".concat(JSON.stringify(bridgeNames), ";\n");
|
|
500
|
+
return "\n (function() {\n window.__bridgeMethods__ = ".concat(JSON.stringify(bridgeNames), ";\n })();\n");
|
|
464
501
|
};
|
|
465
502
|
var INJECT_BRIDGE_STATE = function(initialState) {
|
|
466
|
-
return "\n window.__bridgeInitialState__ = ".concat(JSON.stringify(initialState), ";\n");
|
|
503
|
+
return "\n (function() {\n window.__bridgeInitialState__ = ".concat(JSON.stringify(initialState), ";\n })();\n");
|
|
467
504
|
};
|
|
468
505
|
var SAFE_NATIVE_EMITTER_EMIT = function(eventName, data) {
|
|
469
506
|
var dataString = JSON.stringify(data);
|
|
470
|
-
return "\
|
|
507
|
+
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
508
|
};
|
|
472
509
|
var SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID = function(bridgeId, eventName, data) {
|
|
473
510
|
var dataString = JSON.stringify(data);
|
|
474
|
-
return "\
|
|
511
|
+
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
512
|
};
|
|
476
|
-
var SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID = function(bridgeId, eventName) {
|
|
477
|
-
|
|
513
|
+
var SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID = function(bridgeId, eventName, serializedError) {
|
|
514
|
+
var serializedErrorString = JSON.stringify(serializedError);
|
|
515
|
+
return "\n (function() {\n if (window.nativeEmitterMap && window.nativeEmitterMap['".concat(bridgeId, "']) {\n window.nativeEmitterMap['").concat(bridgeId, "'].emit('").concat(eventName, "', {}, ").concat(serializedErrorString, ");\n } else if (window.nativeEmitter) {\n // @deprecated This version is not used after 1.7.2\n window.nativeEmitter.emit('").concat(eventName, "', {}, ").concat(serializedErrorString, ");\n } else {\n window.nativeBatchedEvents = window.nativeBatchedEvents || [];\n window.nativeBatchedEvents.push(['").concat(eventName, "', {}, ").concat(serializedErrorString, "]);\n }\n return true;\n })();\n");
|
|
478
516
|
};
|
|
479
517
|
// src/integrations/console.ts
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
518
|
+
var INJECT_DEBUG = function(uniqueId) {
|
|
519
|
+
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');
|
|
520
|
+
};
|
|
483
521
|
var handleLog = function(type, message) {
|
|
484
522
|
var _JSON_parse = _to_array(JSON.parse(message)), formatMessage = _JSON_parse[0], parsedMessage = _JSON_parse.slice(1);
|
|
485
523
|
var webviewMark = "(WebView) ";
|
|
@@ -511,23 +549,7 @@ var handleLog = function(type, message) {
|
|
|
511
549
|
}
|
|
512
550
|
};
|
|
513
551
|
// src/integrations/handleRegisterWebMethod.ts
|
|
514
|
-
import { createRandomId
|
|
515
|
-
// src/error.ts
|
|
516
|
-
var WebMethodError = /*#__PURE__*/ function(Error1) {
|
|
517
|
-
"use strict";
|
|
518
|
-
_inherits(WebMethodError, Error1);
|
|
519
|
-
function WebMethodError(methodName) {
|
|
520
|
-
_class_call_check(this, WebMethodError);
|
|
521
|
-
var _this;
|
|
522
|
-
_this = _call_super(this, WebMethodError, [
|
|
523
|
-
"An error occurred in the WebMethod.current.".concat(methodName)
|
|
524
|
-
]);
|
|
525
|
-
_this.name = "WebMethodError";
|
|
526
|
-
return _this;
|
|
527
|
-
}
|
|
528
|
-
return WebMethodError;
|
|
529
|
-
}(_wrap_native_super(Error));
|
|
530
|
-
// src/integrations/handleRegisterWebMethod.ts
|
|
552
|
+
import { createRandomId, createResolver, timeout } from "@webview-bridge/utils";
|
|
531
553
|
var handleRegisterWebMethod = function(emitter, webview, bridgeNames, responseTimeout) {
|
|
532
554
|
return bridgeNames.reduce(function(acc, methodName) {
|
|
533
555
|
acc[methodName] = /*#__PURE__*/ _async_to_generator(function() {
|
|
@@ -537,7 +559,7 @@ var handleRegisterWebMethod = function(emitter, webview, bridgeNames, responseTi
|
|
|
537
559
|
for(_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
538
560
|
args[_key] = _arguments[_key];
|
|
539
561
|
}
|
|
540
|
-
eventId =
|
|
562
|
+
eventId = createRandomId();
|
|
541
563
|
return [
|
|
542
564
|
2,
|
|
543
565
|
Promise.race([
|
|
@@ -569,6 +591,7 @@ var createWebView = function(param) {
|
|
|
569
591
|
};
|
|
570
592
|
var webviewRefList = [];
|
|
571
593
|
var emitter = createEvents();
|
|
594
|
+
var uniqueId = createRandomId2();
|
|
572
595
|
bridge2.subscribe(function(state) {
|
|
573
596
|
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
574
597
|
try {
|
|
@@ -783,7 +806,7 @@ var createWebView = function(param) {
|
|
|
783
806
|
"true;"
|
|
784
807
|
].filter(Boolean).join("\n"),
|
|
785
808
|
injectedJavaScript: [
|
|
786
|
-
debug && INJECT_DEBUG,
|
|
809
|
+
debug && INJECT_DEBUG(uniqueId),
|
|
787
810
|
props.injectedJavaScript,
|
|
788
811
|
"true;"
|
|
789
812
|
].filter(Boolean).join("\n")
|
|
@@ -828,7 +851,7 @@ var postMessageSchema = function(schema) {
|
|
|
828
851
|
return schema;
|
|
829
852
|
};
|
|
830
853
|
// src/useBridge.ts
|
|
831
|
-
import { useSyncExternalStoreWithSelector } from "use-sync-external-store/with-selector
|
|
854
|
+
import { useSyncExternalStoreWithSelector } from "use-sync-external-store/with-selector";
|
|
832
855
|
function useBridge(store, selector) {
|
|
833
856
|
var $selector = selector !== null && selector !== void 0 ? selector : function(state) {
|
|
834
857
|
return state;
|
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.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Fully Type-Safe Integration for React Native WebView and Web",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -17,10 +17,7 @@
|
|
|
17
17
|
"publishConfig": {
|
|
18
18
|
"access": "public"
|
|
19
19
|
},
|
|
20
|
-
"repository":
|
|
21
|
-
"type": "git",
|
|
22
|
-
"url": "https://github.com/gronxb/webview-bridge.git"
|
|
23
|
-
},
|
|
20
|
+
"repository": "https://github.com/gronxb/webview-bridge",
|
|
24
21
|
"license": "MIT",
|
|
25
22
|
"files": [
|
|
26
23
|
"dist",
|
|
@@ -29,7 +26,7 @@
|
|
|
29
26
|
],
|
|
30
27
|
"devDependencies": {
|
|
31
28
|
"@types/react": "^18.2.25",
|
|
32
|
-
"@types/use-sync-external-store": "^
|
|
29
|
+
"@types/use-sync-external-store": "^1.5.0",
|
|
33
30
|
"react": "^18.3.1",
|
|
34
31
|
"react-native-webview": "^13.12.0",
|
|
35
32
|
"typescript": "^5.6.2"
|
|
@@ -39,9 +36,9 @@
|
|
|
39
36
|
"react-native-webview": "*"
|
|
40
37
|
},
|
|
41
38
|
"dependencies": {
|
|
42
|
-
"
|
|
43
|
-
"@webview-bridge/types": "1.7.
|
|
44
|
-
"
|
|
39
|
+
"use-sync-external-store": "1.6.0",
|
|
40
|
+
"@webview-bridge/types": "1.7.9",
|
|
41
|
+
"@webview-bridge/utils": "1.7.9"
|
|
45
42
|
},
|
|
46
43
|
"scripts": {
|
|
47
44
|
"build": "tsup --config ./tsup.config.ts",
|
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
|
]
|
package/src/error.ts
CHANGED
|
@@ -4,3 +4,17 @@ export class WebMethodError extends Error {
|
|
|
4
4
|
this.name = "WebMethodError";
|
|
5
5
|
}
|
|
6
6
|
}
|
|
7
|
+
|
|
8
|
+
export function serializeError(error: Error) {
|
|
9
|
+
return JSON.stringify(error, (_, value) => {
|
|
10
|
+
if (value instanceof Error) {
|
|
11
|
+
return {
|
|
12
|
+
name: value.name,
|
|
13
|
+
message: value.message,
|
|
14
|
+
stack: value.stack,
|
|
15
|
+
__isError: true,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
} from "@webview-bridge/types";
|
|
7
7
|
import { equals, removeUndefinedKeys } from "@webview-bridge/utils";
|
|
8
8
|
import type WebView from "react-native-webview";
|
|
9
|
+
import { serializeError } from "../error";
|
|
9
10
|
|
|
10
11
|
export type StoreCallback<T> = ({
|
|
11
12
|
get,
|
|
@@ -83,9 +84,14 @@ export const handleBridge = async ({
|
|
|
83
84
|
const _bridge = bridge.getState();
|
|
84
85
|
|
|
85
86
|
const _method = _bridge[method];
|
|
86
|
-
const handleThrow = () => {
|
|
87
|
+
const handleThrow = (error?: Error) => {
|
|
88
|
+
const serializedError = error ? serializeError(error) : true;
|
|
87
89
|
webview.injectJavaScript(
|
|
88
|
-
SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID(
|
|
90
|
+
SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID(
|
|
91
|
+
bridgeId,
|
|
92
|
+
`${method}-${eventId}`,
|
|
93
|
+
serializedError,
|
|
94
|
+
),
|
|
89
95
|
);
|
|
90
96
|
};
|
|
91
97
|
if (!(method in _bridge)) {
|
|
@@ -107,36 +113,42 @@ export const handleBridge = async ({
|
|
|
107
113
|
),
|
|
108
114
|
);
|
|
109
115
|
} catch (error) {
|
|
110
|
-
handleThrow();
|
|
116
|
+
handleThrow(error instanceof Error ? error : new Error(String(error)));
|
|
111
117
|
console.error(error);
|
|
112
118
|
}
|
|
113
119
|
};
|
|
114
120
|
|
|
115
121
|
export const INJECT_BRIDGE_METHODS = (bridgeNames: string[]) => `
|
|
116
|
-
|
|
122
|
+
(function() {
|
|
123
|
+
window.__bridgeMethods__ = ${JSON.stringify(bridgeNames)};
|
|
124
|
+
})();
|
|
117
125
|
`;
|
|
118
126
|
|
|
119
127
|
export const INJECT_BRIDGE_STATE = (
|
|
120
128
|
initialState: Record<string, Primitive>,
|
|
121
129
|
) => `
|
|
122
|
-
|
|
130
|
+
(function() {
|
|
131
|
+
window.__bridgeInitialState__ = ${JSON.stringify(initialState)};
|
|
132
|
+
})();
|
|
123
133
|
`;
|
|
124
134
|
|
|
125
135
|
export const SAFE_NATIVE_EMITTER_EMIT = (eventName: string, data: unknown) => {
|
|
126
136
|
const dataString = JSON.stringify(data);
|
|
127
137
|
return `
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
|
|
138
|
+
(function() {
|
|
139
|
+
if (window.nativeEmitterMap && Object.keys(window.nativeEmitterMap).length > 0) {
|
|
140
|
+
for (const [_, emitter] of Object.entries(window.nativeEmitterMap)) {
|
|
141
|
+
emitter.emit('${eventName}', ${dataString});
|
|
142
|
+
}
|
|
143
|
+
} else if (window.nativeEmitter) {
|
|
144
|
+
// @deprecated This version is not used after 1.7.2
|
|
145
|
+
window.nativeEmitter.emit('${eventName}', ${dataString});
|
|
146
|
+
} else {
|
|
147
|
+
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
|
|
148
|
+
window.nativeBatchedEvents.push(['${eventName}', ${dataString}]);
|
|
149
|
+
}
|
|
150
|
+
return true;
|
|
151
|
+
})();
|
|
140
152
|
`;
|
|
141
153
|
};
|
|
142
154
|
|
|
@@ -147,31 +159,39 @@ export const SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID = (
|
|
|
147
159
|
) => {
|
|
148
160
|
const dataString = JSON.stringify(data);
|
|
149
161
|
return `
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
162
|
+
(function() {
|
|
163
|
+
if (window.nativeEmitterMap && window.nativeEmitterMap['${bridgeId}']) {
|
|
164
|
+
window.nativeEmitterMap['${bridgeId}'].emit('${eventName}', ${dataString});
|
|
165
|
+
} else if (window.nativeEmitter) {
|
|
166
|
+
// @deprecated This version is not used after 1.7.2
|
|
167
|
+
window.nativeEmitter.emit('${eventName}', ${dataString});
|
|
168
|
+
} else {
|
|
169
|
+
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
|
|
170
|
+
window.nativeBatchedEvents.push(['${eventName}', ${dataString}]);
|
|
171
|
+
}
|
|
172
|
+
return true;
|
|
173
|
+
})();
|
|
160
174
|
`;
|
|
161
175
|
};
|
|
162
176
|
|
|
163
177
|
export const SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID = (
|
|
164
178
|
bridgeId: string,
|
|
165
179
|
eventName: string,
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
180
|
+
serializedError: string | true,
|
|
181
|
+
) => {
|
|
182
|
+
const serializedErrorString = JSON.stringify(serializedError);
|
|
183
|
+
return `
|
|
184
|
+
(function() {
|
|
185
|
+
if (window.nativeEmitterMap && window.nativeEmitterMap['${bridgeId}']) {
|
|
186
|
+
window.nativeEmitterMap['${bridgeId}'].emit('${eventName}', {}, ${serializedErrorString});
|
|
187
|
+
} else if (window.nativeEmitter) {
|
|
188
|
+
// @deprecated This version is not used after 1.7.2
|
|
189
|
+
window.nativeEmitter.emit('${eventName}', {}, ${serializedErrorString});
|
|
190
|
+
} else {
|
|
191
|
+
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
|
|
192
|
+
window.nativeBatchedEvents.push(['${eventName}', {}, ${serializedErrorString}]);
|
|
193
|
+
}
|
|
194
|
+
return true;
|
|
195
|
+
})();
|
|
177
196
|
`;
|
|
197
|
+
};
|
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const injectionDetectKey = `__bridge_debug_injected_${createRandomId()}`;
|
|
5
|
-
|
|
6
|
-
export const INJECT_DEBUG = `
|
|
7
|
-
{
|
|
8
|
-
if (window["${injectionDetectKey}"]) {
|
|
1
|
+
export const INJECT_DEBUG = (uniqueId: string) => `
|
|
2
|
+
(function() {
|
|
3
|
+
if (window["__bridge_debug_injected_${uniqueId}"]) {
|
|
9
4
|
return;
|
|
10
5
|
}
|
|
11
6
|
|
|
12
|
-
window["${
|
|
7
|
+
window["__bridge_debug_injected_${uniqueId}"] = true;
|
|
13
8
|
const originalConsoleLog = console.log;
|
|
14
9
|
const originalConsoleError = console.error;
|
|
15
10
|
const originalConsoleWarn = console.warn;
|
|
@@ -37,7 +32,7 @@ export const INJECT_DEBUG = `
|
|
|
37
32
|
);
|
|
38
33
|
originalConsoleWarn.apply(console, arguments);
|
|
39
34
|
};
|
|
40
|
-
};
|
|
35
|
+
})();
|
|
41
36
|
`;
|
|
42
37
|
|
|
43
38
|
export type LogType = "log" | "error" | "warn";
|
package/src/useBridge.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Bridge, BridgeStore, ExtractStore } from "@webview-bridge/types";
|
|
2
|
-
import { useSyncExternalStoreWithSelector } from "use-sync-external-store/with-selector
|
|
2
|
+
import { useSyncExternalStoreWithSelector } from "use-sync-external-store/with-selector";
|
|
3
3
|
|
|
4
4
|
export function useBridge<T extends Bridge>(
|
|
5
5
|
store: BridgeStore<T>,
|