1049 lines
35 KiB
JavaScript
1049 lines
35 KiB
JavaScript
// node_modules/.pnpm/@webview-bridge+utils@1.7.8/node_modules/@webview-bridge/utils/dist/index.js
|
|
function _array_like_to_array(arr, len) {
|
|
if (len == null || len > arr.length) len = arr.length;
|
|
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
return arr2;
|
|
}
|
|
function _array_without_holes(arr) {
|
|
if (Array.isArray(arr)) return _array_like_to_array(arr);
|
|
}
|
|
function _instanceof(left, right) {
|
|
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
return !!right[Symbol.hasInstance](left);
|
|
} else {
|
|
return left instanceof right;
|
|
}
|
|
}
|
|
function _iterable_to_array(iter) {
|
|
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
}
|
|
function _non_iterable_spread() {
|
|
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
}
|
|
function _to_consumable_array(arr) {
|
|
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
|
|
}
|
|
function _type_of(obj) {
|
|
"@swc/helpers - typeof";
|
|
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
}
|
|
function _unsupported_iterable_to_array(o, minLen) {
|
|
if (!o) return;
|
|
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
if (n === "Map" || n === "Set") return Array.from(n);
|
|
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
}
|
|
var createEvents = function() {
|
|
return {
|
|
events: {},
|
|
emit: function emit(event) {
|
|
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
args[_key - 1] = arguments[_key];
|
|
}
|
|
var callbacks = this.events[event] || [];
|
|
for (var i = 0, length = callbacks.length; i < length; i++) {
|
|
var _callbacks;
|
|
(_callbacks = callbacks)[i].apply(_callbacks, _to_consumable_array(args));
|
|
}
|
|
},
|
|
on: function on(event, cb) {
|
|
var _this = this;
|
|
if (!this.events[event]) {
|
|
this.events[event] = [
|
|
cb
|
|
];
|
|
} else {
|
|
var _this_events_event;
|
|
(_this_events_event = this.events[event]) === null || _this_events_event === void 0 ? void 0 : _this_events_event.push(cb);
|
|
}
|
|
return function() {
|
|
var _this_events_event2;
|
|
_this.events[event] = (_this_events_event2 = _this.events[event]) === null || _this_events_event2 === void 0 ? void 0 : _this_events_event2.filter(function(i) {
|
|
return cb !== i;
|
|
});
|
|
};
|
|
}
|
|
};
|
|
};
|
|
var createResolver = function(param) {
|
|
var emitter = param.emitter, evaluate = param.evaluate, eventId = param.eventId, _param_failHandler = param.failHandler, failHandler = _param_failHandler === void 0 ? false : _param_failHandler, methodName = param.methodName, onFallback = param.onFallback;
|
|
return new Promise(function(resolve, reject) {
|
|
var unbind = emitter.on("".concat(methodName, "-").concat(eventId), function(data, throwOccurred) {
|
|
unbind();
|
|
if (throwOccurred) {
|
|
if (_instanceof(failHandler, Error)) {
|
|
onFallback === null || onFallback === void 0 ? void 0 : onFallback();
|
|
reject(failHandler);
|
|
} else {
|
|
resolve(void 0);
|
|
}
|
|
} else {
|
|
resolve(data);
|
|
}
|
|
});
|
|
evaluate();
|
|
});
|
|
};
|
|
var TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
var ID_LENGTH = 21;
|
|
var createRandomId = function() {
|
|
var size = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : ID_LENGTH;
|
|
var randomValues = Array.from({
|
|
length: size
|
|
}, function() {
|
|
return TABLE[Math.floor(Math.random() * TABLE.length)];
|
|
});
|
|
return randomValues.join("");
|
|
};
|
|
var equals = function(a, b) {
|
|
if (a === b) {
|
|
return true;
|
|
}
|
|
if (a && b && (typeof a === "undefined" ? "undefined" : _type_of(a)) === "object" && (typeof b === "undefined" ? "undefined" : _type_of(b)) === "object") {
|
|
var arrA = Array.isArray(a);
|
|
var arrB = Array.isArray(b);
|
|
var i;
|
|
var length;
|
|
var key;
|
|
if (arrA && arrB) {
|
|
length = a.length;
|
|
if (length !== b.length) {
|
|
return false;
|
|
}
|
|
for (i = length; i-- !== 0; ) {
|
|
if (!equals(a[i], b[i])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
if (arrA !== arrB) {
|
|
return false;
|
|
}
|
|
var keys = Object.keys(a);
|
|
length = keys.length;
|
|
if (length !== Object.keys(b).length) {
|
|
return false;
|
|
}
|
|
for (i = length; i-- !== 0; ) {
|
|
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) {
|
|
return false;
|
|
}
|
|
}
|
|
for (i = length; i-- !== 0; ) {
|
|
key = keys[i];
|
|
if (!equals(a[key], b[key])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
return a !== a && b !== b;
|
|
};
|
|
var removeUndefinedKeys = function(obj) {
|
|
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
try {
|
|
for (var _iterator = Object.keys(obj)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
var key = _step.value;
|
|
if (obj[key] === void 0) {
|
|
delete obj[key];
|
|
}
|
|
}
|
|
} catch (err) {
|
|
_didIteratorError = true;
|
|
_iteratorError = err;
|
|
} finally {
|
|
try {
|
|
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
_iterator.return();
|
|
}
|
|
} finally {
|
|
if (_didIteratorError) {
|
|
throw _iteratorError;
|
|
}
|
|
}
|
|
}
|
|
return obj;
|
|
};
|
|
var timeout = function(ms) {
|
|
var throwOnError = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : true;
|
|
return new Promise(function(resolve, reject) {
|
|
setTimeout(function() {
|
|
if (throwOnError) {
|
|
reject(new Error("Timeout"));
|
|
} else {
|
|
resolve(void 0);
|
|
}
|
|
}, ms);
|
|
});
|
|
};
|
|
|
|
// node_modules/.pnpm/@webview-bridge+web@1.7.8/node_modules/@webview-bridge/web/dist/index.js
|
|
function _array_like_to_array2(arr, len) {
|
|
if (len == null || len > arr.length) len = arr.length;
|
|
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
return arr2;
|
|
}
|
|
function _array_with_holes(arr) {
|
|
if (Array.isArray(arr)) return arr;
|
|
}
|
|
function _array_without_holes2(arr) {
|
|
if (Array.isArray(arr)) return _array_like_to_array2(arr);
|
|
}
|
|
function _assert_this_initialized(self) {
|
|
if (self === void 0) {
|
|
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
}
|
|
return self;
|
|
}
|
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
try {
|
|
var info = gen[key](arg);
|
|
var value = info.value;
|
|
} catch (error) {
|
|
reject(error);
|
|
return;
|
|
}
|
|
if (info.done) {
|
|
resolve(value);
|
|
} else {
|
|
Promise.resolve(value).then(_next, _throw);
|
|
}
|
|
}
|
|
function _async_to_generator(fn) {
|
|
return function() {
|
|
var self = this, args = arguments;
|
|
return new Promise(function(resolve, reject) {
|
|
var gen = fn.apply(self, args);
|
|
function _next(value) {
|
|
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
}
|
|
function _throw(err) {
|
|
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
}
|
|
_next(void 0);
|
|
});
|
|
};
|
|
}
|
|
function _call_super(_this, derived, args) {
|
|
derived = _get_prototype_of(derived);
|
|
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
|
|
}
|
|
function _class_call_check(instance, Constructor) {
|
|
if (!(instance instanceof Constructor)) {
|
|
throw new TypeError("Cannot call a class as a function");
|
|
}
|
|
}
|
|
function _construct(Parent, args, Class) {
|
|
if (_is_native_reflect_construct()) {
|
|
_construct = Reflect.construct;
|
|
} else {
|
|
_construct = function construct(Parent2, args2, Class2) {
|
|
var a = [
|
|
null
|
|
];
|
|
a.push.apply(a, args2);
|
|
var Constructor = Function.bind.apply(Parent2, a);
|
|
var instance = new Constructor();
|
|
if (Class2) _set_prototype_of(instance, Class2.prototype);
|
|
return instance;
|
|
};
|
|
}
|
|
return _construct.apply(null, arguments);
|
|
}
|
|
function _defineProperties(target, props) {
|
|
for (var i = 0; i < props.length; i++) {
|
|
var descriptor = props[i];
|
|
descriptor.enumerable = descriptor.enumerable || false;
|
|
descriptor.configurable = true;
|
|
if ("value" in descriptor) descriptor.writable = true;
|
|
Object.defineProperty(target, descriptor.key, descriptor);
|
|
}
|
|
}
|
|
function _create_class(Constructor, protoProps, staticProps) {
|
|
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
return Constructor;
|
|
}
|
|
function _define_property(obj, key, value) {
|
|
if (key in obj) {
|
|
Object.defineProperty(obj, key, {
|
|
value,
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true
|
|
});
|
|
} else {
|
|
obj[key] = value;
|
|
}
|
|
return obj;
|
|
}
|
|
function _get_prototype_of(o) {
|
|
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o2) {
|
|
return o2.__proto__ || Object.getPrototypeOf(o2);
|
|
};
|
|
return _get_prototype_of(o);
|
|
}
|
|
function _inherits(subClass, superClass) {
|
|
if (typeof superClass !== "function" && superClass !== null) {
|
|
throw new TypeError("Super expression must either be null or a function");
|
|
}
|
|
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
|
constructor: {
|
|
value: subClass,
|
|
writable: true,
|
|
configurable: true
|
|
}
|
|
});
|
|
if (superClass) _set_prototype_of(subClass, superClass);
|
|
}
|
|
function _is_native_function(fn) {
|
|
return Function.toString.call(fn).indexOf("[native code]") !== -1;
|
|
}
|
|
function _iterable_to_array2(iter) {
|
|
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
}
|
|
function _iterable_to_array_limit(arr, i) {
|
|
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
if (_i == null) return;
|
|
var _arr = [];
|
|
var _n = true;
|
|
var _d = false;
|
|
var _s, _e;
|
|
try {
|
|
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
|
|
_arr.push(_s.value);
|
|
if (i && _arr.length === i) break;
|
|
}
|
|
} catch (err) {
|
|
_d = true;
|
|
_e = err;
|
|
} finally {
|
|
try {
|
|
if (!_n && _i["return"] != null) _i["return"]();
|
|
} finally {
|
|
if (_d) throw _e;
|
|
}
|
|
}
|
|
return _arr;
|
|
}
|
|
function _non_iterable_rest() {
|
|
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
}
|
|
function _non_iterable_spread2() {
|
|
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
}
|
|
function _object_spread(target) {
|
|
for (var i = 1; i < arguments.length; i++) {
|
|
var source = arguments[i] != null ? arguments[i] : {};
|
|
var ownKeys2 = Object.keys(source);
|
|
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
ownKeys2 = ownKeys2.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
}));
|
|
}
|
|
ownKeys2.forEach(function(key) {
|
|
_define_property(target, key, source[key]);
|
|
});
|
|
}
|
|
return target;
|
|
}
|
|
function ownKeys(object, enumerableOnly) {
|
|
var keys = Object.keys(object);
|
|
if (Object.getOwnPropertySymbols) {
|
|
var symbols = Object.getOwnPropertySymbols(object);
|
|
if (enumerableOnly) {
|
|
symbols = symbols.filter(function(sym) {
|
|
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
});
|
|
}
|
|
keys.push.apply(keys, symbols);
|
|
}
|
|
return keys;
|
|
}
|
|
function _object_spread_props(target, source) {
|
|
source = source != null ? source : {};
|
|
if (Object.getOwnPropertyDescriptors) {
|
|
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
} else {
|
|
ownKeys(Object(source)).forEach(function(key) {
|
|
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
});
|
|
}
|
|
return target;
|
|
}
|
|
function _possible_constructor_return(self, call) {
|
|
if (call && (_type_of2(call) === "object" || typeof call === "function")) {
|
|
return call;
|
|
}
|
|
return _assert_this_initialized(self);
|
|
}
|
|
function _set_prototype_of(o, p) {
|
|
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o2, p2) {
|
|
o2.__proto__ = p2;
|
|
return o2;
|
|
};
|
|
return _set_prototype_of(o, p);
|
|
}
|
|
function _sliced_to_array(arr, i) {
|
|
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array2(arr, i) || _non_iterable_rest();
|
|
}
|
|
function _to_array(arr) {
|
|
return _array_with_holes(arr) || _iterable_to_array2(arr) || _unsupported_iterable_to_array2(arr) || _non_iterable_rest();
|
|
}
|
|
function _to_consumable_array2(arr) {
|
|
return _array_without_holes2(arr) || _iterable_to_array2(arr) || _unsupported_iterable_to_array2(arr) || _non_iterable_spread2();
|
|
}
|
|
function _type_of2(obj) {
|
|
"@swc/helpers - typeof";
|
|
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
}
|
|
function _unsupported_iterable_to_array2(o, minLen) {
|
|
if (!o) return;
|
|
if (typeof o === "string") return _array_like_to_array2(o, minLen);
|
|
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
if (n === "Map" || n === "Set") return Array.from(n);
|
|
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array2(o, minLen);
|
|
}
|
|
function _wrap_native_super(Class) {
|
|
var _cache = typeof Map === "function" ? /* @__PURE__ */ new Map() : void 0;
|
|
_wrap_native_super = function wrapNativeSuper(Class2) {
|
|
if (Class2 === null || !_is_native_function(Class2)) return Class2;
|
|
if (typeof Class2 !== "function") {
|
|
throw new TypeError("Super expression must either be null or a function");
|
|
}
|
|
if (typeof _cache !== "undefined") {
|
|
if (_cache.has(Class2)) return _cache.get(Class2);
|
|
_cache.set(Class2, Wrapper);
|
|
}
|
|
function Wrapper() {
|
|
return _construct(Class2, arguments, _get_prototype_of(this).constructor);
|
|
}
|
|
Wrapper.prototype = Object.create(Class2.prototype, {
|
|
constructor: {
|
|
value: Wrapper,
|
|
enumerable: false,
|
|
writable: true,
|
|
configurable: true
|
|
}
|
|
});
|
|
return _set_prototype_of(Wrapper, Class2);
|
|
};
|
|
return _wrap_native_super(Class);
|
|
}
|
|
function _is_native_reflect_construct() {
|
|
try {
|
|
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {
|
|
}));
|
|
} catch (_) {
|
|
}
|
|
return (_is_native_reflect_construct = function() {
|
|
return !!result;
|
|
})();
|
|
}
|
|
function _ts_generator(thisArg, body) {
|
|
var f, y, t, g, _ = {
|
|
label: 0,
|
|
sent: function() {
|
|
if (t[0] & 1) throw t[1];
|
|
return t[1];
|
|
},
|
|
trys: [],
|
|
ops: []
|
|
};
|
|
return g = {
|
|
next: verb(0),
|
|
"throw": verb(1),
|
|
"return": verb(2)
|
|
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
return this;
|
|
}), g;
|
|
function verb(n) {
|
|
return function(v) {
|
|
return step([
|
|
n,
|
|
v
|
|
]);
|
|
};
|
|
}
|
|
function step(op) {
|
|
if (f) throw new TypeError("Generator is already executing.");
|
|
while (_) try {
|
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
if (y = 0, t) op = [
|
|
op[0] & 2,
|
|
t.value
|
|
];
|
|
switch (op[0]) {
|
|
case 0:
|
|
case 1:
|
|
t = op;
|
|
break;
|
|
case 4:
|
|
_.label++;
|
|
return {
|
|
value: op[1],
|
|
done: false
|
|
};
|
|
case 5:
|
|
_.label++;
|
|
y = op[1];
|
|
op = [
|
|
0
|
|
];
|
|
continue;
|
|
case 7:
|
|
op = _.ops.pop();
|
|
_.trys.pop();
|
|
continue;
|
|
default:
|
|
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
_ = 0;
|
|
continue;
|
|
}
|
|
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
_.label = op[1];
|
|
break;
|
|
}
|
|
if (op[0] === 6 && _.label < t[1]) {
|
|
_.label = t[1];
|
|
t = op;
|
|
break;
|
|
}
|
|
if (t && _.label < t[2]) {
|
|
_.label = t[2];
|
|
_.ops.push(op);
|
|
break;
|
|
}
|
|
if (t[2]) _.ops.pop();
|
|
_.trys.pop();
|
|
continue;
|
|
}
|
|
op = body.call(thisArg, _);
|
|
} catch (e) {
|
|
op = [
|
|
6,
|
|
e
|
|
];
|
|
y = 0;
|
|
} finally {
|
|
f = t = 0;
|
|
}
|
|
if (op[0] & 5) throw op[1];
|
|
return {
|
|
value: op[0] ? op[1] : void 0,
|
|
done: true
|
|
};
|
|
}
|
|
}
|
|
var __defProp = Object.defineProperty;
|
|
var __defNormalProp = function(obj, key, value) {
|
|
return key in obj ? __defProp(obj, key, {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value
|
|
}) : obj[key] = value;
|
|
};
|
|
var __publicField = function(obj, key, value) {
|
|
return __defNormalProp(obj, (typeof key === "undefined" ? "undefined" : _type_of2(key)) !== "symbol" ? key + "" : key, value);
|
|
};
|
|
var MethodNotFoundError = function(Error1) {
|
|
"use strict";
|
|
_inherits(MethodNotFoundError2, Error1);
|
|
function MethodNotFoundError2(methodName) {
|
|
_class_call_check(this, MethodNotFoundError2);
|
|
var _this;
|
|
_this = _call_super(this, MethodNotFoundError2, [
|
|
"Method ".concat(methodName, " is not defined")
|
|
]);
|
|
_this.name = "MethodNotFoundError";
|
|
return _this;
|
|
}
|
|
return MethodNotFoundError2;
|
|
}(_wrap_native_super(Error));
|
|
var NativeMethodError = function(Error1) {
|
|
"use strict";
|
|
_inherits(NativeMethodError2, Error1);
|
|
function NativeMethodError2(methodName) {
|
|
_class_call_check(this, NativeMethodError2);
|
|
var _this;
|
|
_this = _call_super(this, NativeMethodError2, [
|
|
"An error occurred in the native bridge: ".concat(methodName)
|
|
]);
|
|
_this.name = "NativeMethodError";
|
|
return _this;
|
|
}
|
|
return NativeMethodError2;
|
|
}(_wrap_native_super(Error));
|
|
var createPromiseProxy = function() {
|
|
return new Proxy({}, {
|
|
get: function() {
|
|
return function() {
|
|
return Promise.resolve();
|
|
};
|
|
}
|
|
});
|
|
};
|
|
var linkBridgeStore = function(emitter) {
|
|
var initialState = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
var getState = function() {
|
|
return state;
|
|
};
|
|
var setState = function(newState) {
|
|
var _newState = _object_spread({}, state, removeUndefinedKeys(newState));
|
|
if (equals(state, _newState)) {
|
|
return;
|
|
}
|
|
var prevState = state;
|
|
state = _newState;
|
|
emitChange(state, prevState);
|
|
};
|
|
emitter.on("bridgeStateChange", function(data) {
|
|
setState(data);
|
|
});
|
|
var state = _object_spread({}, initialState);
|
|
var listeners = /* @__PURE__ */ new Set();
|
|
var emitChange = function(newState, prevState) {
|
|
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
try {
|
|
for (var _iterator = listeners[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
var listener = _step.value;
|
|
listener(newState, prevState);
|
|
}
|
|
} catch (err) {
|
|
_didIteratorError = true;
|
|
_iteratorError = err;
|
|
} finally {
|
|
try {
|
|
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
_iterator.return();
|
|
}
|
|
} finally {
|
|
if (_didIteratorError) {
|
|
throw _iteratorError;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
var subscribe = function(listener) {
|
|
listeners.add(listener);
|
|
return function() {
|
|
return listeners.delete(listener);
|
|
};
|
|
};
|
|
return {
|
|
getState,
|
|
subscribe
|
|
};
|
|
};
|
|
var mockStore = function() {
|
|
var initialState = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
var state = initialState;
|
|
var getState = function() {
|
|
return state;
|
|
};
|
|
var subscribe = function() {
|
|
return function() {
|
|
};
|
|
};
|
|
return {
|
|
getState,
|
|
subscribe
|
|
};
|
|
};
|
|
var BridgeInstance = function() {
|
|
"use strict";
|
|
function BridgeInstance2(_bridgeId, _options, _emitter, _bridgeMethods, _nativeInitialState) {
|
|
_class_call_check(this, BridgeInstance2);
|
|
this._bridgeId = _bridgeId;
|
|
this._options = _options;
|
|
this._emitter = _emitter;
|
|
this._bridgeMethods = _bridgeMethods;
|
|
this._nativeInitialState = _nativeInitialState;
|
|
__publicField(this, "_defaultTimeoutMs", 2e3);
|
|
__publicField(this, "_isListenerRegistered", false);
|
|
__publicField(this, "store", mockStore());
|
|
__publicField(this, "loose", createPromiseProxy());
|
|
this._hydrate(_bridgeMethods, _nativeInitialState);
|
|
}
|
|
_create_class(BridgeInstance2, [
|
|
{
|
|
key: "isReactNativeWebView",
|
|
get: function get() {
|
|
return Boolean(window.ReactNativeWebView);
|
|
}
|
|
},
|
|
{
|
|
key: "isWebViewBridgeAvailable",
|
|
get: function get() {
|
|
return this._bridgeMethods.length > 0;
|
|
}
|
|
},
|
|
{
|
|
key: "isNativeMethodAvailable",
|
|
value: function isNativeMethodAvailable(methodName) {
|
|
return typeof methodName === "string" && this._bridgeMethods.includes(methodName);
|
|
}
|
|
},
|
|
{
|
|
key: "addEventListener",
|
|
value: function addEventListener(eventName, listener) {
|
|
return this._emitter.on("postMessage/".concat(String(eventName)), listener);
|
|
}
|
|
},
|
|
{
|
|
key: "_postMessage",
|
|
value: function _postMessage(type, body) {
|
|
var _window_ReactNativeWebView;
|
|
(_window_ReactNativeWebView = window.ReactNativeWebView) === null || _window_ReactNativeWebView === void 0 ? void 0 : _window_ReactNativeWebView.postMessage(JSON.stringify(body ? {
|
|
type,
|
|
body,
|
|
bridgeId: this._bridgeId
|
|
} : {
|
|
type,
|
|
bridgeId: this._bridgeId
|
|
}));
|
|
}
|
|
},
|
|
{
|
|
key: "_createNativeMethod",
|
|
value: function _createNativeMethod(methodName, throwOnError, timeoutMs, onFallback) {
|
|
var _this = this;
|
|
return function() {
|
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
args[_key] = arguments[_key];
|
|
}
|
|
var eventId = createRandomId();
|
|
return Promise.race([
|
|
createResolver({
|
|
emitter: _this._emitter,
|
|
methodName,
|
|
eventId,
|
|
evaluate: function() {
|
|
_this._postMessage("bridge", {
|
|
method: methodName,
|
|
eventId,
|
|
args
|
|
});
|
|
},
|
|
onFallback: function() {
|
|
onFallback === null || onFallback === void 0 ? void 0 : onFallback(methodName, args);
|
|
},
|
|
failHandler: throwOnError && new NativeMethodError(methodName)
|
|
}),
|
|
timeoutMs > 0 && timeout(timeoutMs, throwOnError)
|
|
].filter(Boolean));
|
|
};
|
|
}
|
|
},
|
|
{
|
|
key: "_willMethodThrowOnError",
|
|
value: function _willMethodThrowOnError(methodName) {
|
|
var throwOnError = this._options.throwOnError;
|
|
return throwOnError === true || Array.isArray(throwOnError) && throwOnError.includes(methodName);
|
|
}
|
|
},
|
|
{
|
|
key: "_createLoose",
|
|
value: function _createLoose(initialState) {
|
|
var _this = this;
|
|
var _this__options = this._options, tmp = _this__options.timeout, timeoutMs = tmp === void 0 ? this._defaultTimeoutMs : tmp, onFallback = _this__options.onFallback;
|
|
return new Proxy(initialState, {
|
|
get: function(target, methodName) {
|
|
if (methodName in target && ![
|
|
"isWebViewBridgeAvailable",
|
|
"isNativeMethodAvailable"
|
|
].includes(methodName)) {
|
|
return target[methodName];
|
|
}
|
|
return _this._createNativeMethod(methodName, _this._willMethodThrowOnError(methodName), timeoutMs, onFallback);
|
|
}
|
|
});
|
|
}
|
|
},
|
|
{
|
|
key: "_hydrate",
|
|
value: function _hydrate(bridgeMethods) {
|
|
var _this = this;
|
|
var nativeInitialState = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
var _this__options = this._options, tmp = _this__options.timeout, timeoutMs = tmp === void 0 ? this._defaultTimeoutMs : tmp, onFallback = _this__options.onFallback, _this__options_initialBridge = _this__options.initialBridge, initialBridge = _this__options_initialBridge === void 0 ? {} : _this__options_initialBridge;
|
|
var initialMethods = Object.entries(initialBridge).filter(function(param) {
|
|
var _param = _sliced_to_array(param, 2), _ = _param[0], bridge = _param[1];
|
|
return typeof bridge === "function";
|
|
});
|
|
var initialBridgeMethodNames = initialMethods.map(function(param) {
|
|
var _param = _sliced_to_array(param, 1), methodName = _param[0];
|
|
return methodName;
|
|
});
|
|
Object.defineProperties(this, Object.fromEntries(initialMethods.map(function(param) {
|
|
var _param = _sliced_to_array(param, 2), methodName = _param[0], value = _param[1];
|
|
return [
|
|
methodName,
|
|
{
|
|
value,
|
|
writable: true
|
|
}
|
|
];
|
|
})));
|
|
this._bridgeMethods = _to_consumable_array2(bridgeMethods).concat(_to_consumable_array2(initialBridgeMethodNames));
|
|
this._nativeInitialState = nativeInitialState;
|
|
var nativeMethods = bridgeMethods.reduce(function(acc, methodName) {
|
|
if (!_this.isReactNativeWebView) {
|
|
return acc;
|
|
}
|
|
var nativeMethod = _this._createNativeMethod(methodName, _this._willMethodThrowOnError(methodName), timeoutMs, onFallback);
|
|
Object.defineProperty(_this, methodName, {
|
|
value: nativeMethod,
|
|
writable: false
|
|
});
|
|
return Object.assign(acc, _define_property({}, methodName, nativeMethod));
|
|
}, initialBridge);
|
|
this.loose = this._createLoose(nativeMethods);
|
|
this.store = linkBridgeStore(this._emitter, _object_spread({}, nativeMethods, nativeInitialState));
|
|
if (!this._isListenerRegistered) {
|
|
var handleVisibilityChange = function() {
|
|
if (document.visibilityState === "visible") {
|
|
_this._postMessage("getBridgeState");
|
|
}
|
|
};
|
|
document.addEventListener("visibilitychange", handleVisibilityChange);
|
|
this._isListenerRegistered = true;
|
|
}
|
|
this._postMessage("getBridgeState");
|
|
var _window_nativeBatchedEvents;
|
|
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
try {
|
|
for (var _iterator = ((_window_nativeBatchedEvents = window.nativeBatchedEvents) !== null && _window_nativeBatchedEvents !== void 0 ? _window_nativeBatchedEvents : [])[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
var _step_value = _to_array(_step.value), eventName = _step_value[0], args = _step_value.slice(1);
|
|
var _this__emitter;
|
|
(_this__emitter = this._emitter).emit.apply(_this__emitter, [
|
|
eventName
|
|
].concat(_to_consumable_array2(args)));
|
|
}
|
|
} catch (err) {
|
|
_didIteratorError = true;
|
|
_iteratorError = err;
|
|
} finally {
|
|
try {
|
|
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
_iterator.return();
|
|
}
|
|
} finally {
|
|
if (_didIteratorError) {
|
|
throw _iteratorError;
|
|
}
|
|
}
|
|
}
|
|
window.nativeBatchedEvents = [];
|
|
return true;
|
|
}
|
|
}
|
|
]);
|
|
return BridgeInstance2;
|
|
}();
|
|
var linkBridge = function() {
|
|
var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {
|
|
timeout: 2e3,
|
|
throwOnError: false,
|
|
debug: false
|
|
};
|
|
if (typeof window === "undefined") {
|
|
var _options_initialBridge;
|
|
var initialBridge = (_options_initialBridge = options === null || options === void 0 ? void 0 : options.initialBridge) !== null && _options_initialBridge !== void 0 ? _options_initialBridge : {};
|
|
var initialMethods = Object.entries(initialBridge).filter(function(param) {
|
|
var _param = _sliced_to_array(param, 2), _ = _param[0], bridge = _param[1];
|
|
return typeof bridge === "function";
|
|
});
|
|
var initialBridgeMethodNames = initialMethods.map(function(param) {
|
|
var _param = _sliced_to_array(param, 1), methodName = _param[0];
|
|
return methodName;
|
|
});
|
|
return {
|
|
addEventListener: function(_eventName, _listener) {
|
|
return function() {
|
|
};
|
|
},
|
|
loose: {},
|
|
isWebViewBridgeAvailable: initialBridgeMethodNames.length > 0,
|
|
isNativeMethodAvailable: function(method) {
|
|
return initialBridgeMethodNames.includes(method);
|
|
},
|
|
store: mockStore(options === null || options === void 0 ? void 0 : options.initialBridge)
|
|
};
|
|
}
|
|
if (options.debug && !window.ReactNativeWebView) {
|
|
console.warn("[WebViewBridge] Not in a WebView environment");
|
|
}
|
|
var bridgeId = createRandomId();
|
|
var emitter = createEvents();
|
|
window.nativeEmitterMap = _object_spread_props(_object_spread({}, window.nativeEmitterMap || {}), _define_property({}, bridgeId, emitter));
|
|
if (!window.nativeEmitter) {
|
|
window.nativeEmitter = emitter;
|
|
}
|
|
var _window___bridgeMethods__;
|
|
var bridgeMethods = (_window___bridgeMethods__ = window.__bridgeMethods__) !== null && _window___bridgeMethods__ !== void 0 ? _window___bridgeMethods__ : [];
|
|
var _window___bridgeInitialState__;
|
|
var nativeInitialState = (_window___bridgeInitialState__ = window.__bridgeInitialState__) !== null && _window___bridgeInitialState__ !== void 0 ? _window___bridgeInitialState__ : {};
|
|
var instance = new BridgeInstance(bridgeId, options, emitter, bridgeMethods, nativeInitialState);
|
|
if (bridgeMethods.length === 0) {
|
|
var unsubscribe = emitter.on("hydrate", function(param) {
|
|
var bridgeMethods2 = param.bridgeMethods, nativeInitialState2 = param.nativeInitialState;
|
|
instance._hydrate(bridgeMethods2, nativeInitialState2);
|
|
unsubscribe();
|
|
});
|
|
}
|
|
var onFallback = options.onFallback, onReady = options.onReady;
|
|
var proxy = new Proxy(instance, {
|
|
get: function(target, methodName, proxy2) {
|
|
if (methodName in target) {
|
|
return target[methodName];
|
|
}
|
|
proxy2._postMessage("fallback", {
|
|
method: methodName
|
|
});
|
|
if (proxy2._willMethodThrowOnError(methodName)) {
|
|
return function() {
|
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
args[_key] = arguments[_key];
|
|
}
|
|
onFallback === null || onFallback === void 0 ? void 0 : onFallback(methodName, args);
|
|
return Promise.reject(new MethodNotFoundError(methodName));
|
|
};
|
|
}
|
|
if (options.debug) {
|
|
console.warn("[WebViewBridge] ".concat(methodName, " is not defined, using fallback."));
|
|
}
|
|
return function() {
|
|
return Promise.resolve();
|
|
};
|
|
}
|
|
});
|
|
onReady === null || onReady === void 0 ? void 0 : onReady(proxy);
|
|
return proxy;
|
|
};
|
|
var registerWebMethod = function(bridge) {
|
|
if (typeof window !== "undefined" && !window.ReactNativeWebView) {
|
|
console.warn("[WebViewBridge] Not in a WebView environment");
|
|
return bridge;
|
|
}
|
|
var bridgeEntries = Object.entries(bridge);
|
|
var bridgeNames = Object.keys(bridge);
|
|
var emitter = createEvents();
|
|
window.webEmitter = emitter;
|
|
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
try {
|
|
var _loop = function() {
|
|
var _step_value = _sliced_to_array(_step.value, 2), funcName = _step_value[0], func = _step_value[1];
|
|
var $func = function() {
|
|
var _ref = _async_to_generator(function(eventId, args) {
|
|
var _window_ReactNativeWebView, value, e, _window_ReactNativeWebView1;
|
|
return _ts_generator(this, function(_state) {
|
|
switch (_state.label) {
|
|
case 0:
|
|
_state.trys.push([
|
|
0,
|
|
2,
|
|
,
|
|
3
|
|
]);
|
|
return [
|
|
4,
|
|
func.apply(void 0, _to_consumable_array2(args))
|
|
];
|
|
case 1:
|
|
value = _state.sent();
|
|
(_window_ReactNativeWebView = window.ReactNativeWebView) === null || _window_ReactNativeWebView === void 0 ? void 0 : _window_ReactNativeWebView.postMessage(JSON.stringify({
|
|
type: "webMethodResponse",
|
|
body: {
|
|
funcName,
|
|
eventId,
|
|
value
|
|
}
|
|
}));
|
|
return [
|
|
3,
|
|
3
|
|
];
|
|
case 2:
|
|
e = _state.sent();
|
|
(_window_ReactNativeWebView1 = window.ReactNativeWebView) === null || _window_ReactNativeWebView1 === void 0 ? void 0 : _window_ReactNativeWebView1.postMessage(JSON.stringify({
|
|
type: "webMethodError",
|
|
body: {
|
|
funcName,
|
|
eventId,
|
|
error: JSON.stringify(e)
|
|
}
|
|
}));
|
|
return [
|
|
3,
|
|
3
|
|
];
|
|
case 3:
|
|
return [
|
|
2
|
|
];
|
|
}
|
|
});
|
|
});
|
|
return function $func2(eventId, args) {
|
|
return _ref.apply(this, arguments);
|
|
};
|
|
}();
|
|
emitter.on(funcName, $func);
|
|
};
|
|
for (var _iterator = bridgeEntries[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) _loop();
|
|
} catch (err) {
|
|
_didIteratorError = true;
|
|
_iteratorError = err;
|
|
} finally {
|
|
try {
|
|
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
_iterator.return();
|
|
}
|
|
} finally {
|
|
if (_didIteratorError) {
|
|
throw _iteratorError;
|
|
}
|
|
}
|
|
}
|
|
var register = function() {
|
|
var _window_ReactNativeWebView;
|
|
(_window_ReactNativeWebView = window.ReactNativeWebView) === null || _window_ReactNativeWebView === void 0 ? void 0 : _window_ReactNativeWebView.postMessage(JSON.stringify({
|
|
type: "registerWebMethod",
|
|
body: {
|
|
bridgeNames
|
|
}
|
|
}));
|
|
window.removeEventListener("DOMContentLoaded", register);
|
|
};
|
|
if (!window.ReactNativeWebView) {
|
|
window.addEventListener("DOMContentLoaded", register);
|
|
return bridge;
|
|
}
|
|
document.addEventListener("visibilitychange", function() {
|
|
if (document.visibilityState === "visible") {
|
|
var _window_ReactNativeWebView;
|
|
(_window_ReactNativeWebView = window.ReactNativeWebView) === null || _window_ReactNativeWebView === void 0 ? void 0 : _window_ReactNativeWebView.postMessage(JSON.stringify({
|
|
type: "registerWebMethod",
|
|
body: {
|
|
bridgeNames
|
|
}
|
|
}));
|
|
}
|
|
});
|
|
register();
|
|
return bridge;
|
|
};
|
|
|
|
export {
|
|
MethodNotFoundError,
|
|
NativeMethodError,
|
|
linkBridge,
|
|
registerWebMethod
|
|
};
|
|
//# sourceMappingURL=chunk-4UG2A3YC.js.map
|