@webview-bridge/web 1.6.2 → 1.7.0-rc.0

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 ADDED
@@ -0,0 +1,927 @@
1
+ "use strict";
2
+ function _array_like_to_array(arr, len) {
3
+ if (len == null || len > arr.length) len = arr.length;
4
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
5
+ return arr2;
6
+ }
7
+ function _array_with_holes(arr) {
8
+ if (Array.isArray(arr)) return arr;
9
+ }
10
+ function _array_without_holes(arr) {
11
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
12
+ }
13
+ function _assert_this_initialized(self) {
14
+ if (self === void 0) {
15
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
16
+ }
17
+ return self;
18
+ }
19
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
20
+ try {
21
+ var info = gen[key](arg);
22
+ var value = info.value;
23
+ } catch (error) {
24
+ reject(error);
25
+ return;
26
+ }
27
+ if (info.done) {
28
+ resolve(value);
29
+ } else {
30
+ Promise.resolve(value).then(_next, _throw);
31
+ }
32
+ }
33
+ function _async_to_generator(fn) {
34
+ return function() {
35
+ var self = this, args = arguments;
36
+ return new Promise(function(resolve, reject) {
37
+ var gen = fn.apply(self, args);
38
+ function _next(value) {
39
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
40
+ }
41
+ function _throw(err) {
42
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
43
+ }
44
+ _next(undefined);
45
+ });
46
+ };
47
+ }
48
+ function _call_super(_this, derived, args) {
49
+ derived = _get_prototype_of(derived);
50
+ return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
51
+ }
52
+ function _class_call_check(instance, Constructor) {
53
+ if (!(instance instanceof Constructor)) {
54
+ throw new TypeError("Cannot call a class as a function");
55
+ }
56
+ }
57
+ function _construct(Parent, args, Class) {
58
+ if (_is_native_reflect_construct()) {
59
+ _construct = Reflect.construct;
60
+ } else {
61
+ _construct = function construct(Parent, args, Class) {
62
+ var a = [
63
+ null
64
+ ];
65
+ a.push.apply(a, args);
66
+ var Constructor = Function.bind.apply(Parent, a);
67
+ var instance = new Constructor();
68
+ if (Class) _set_prototype_of(instance, Class.prototype);
69
+ return instance;
70
+ };
71
+ }
72
+ return _construct.apply(null, arguments);
73
+ }
74
+ function _defineProperties(target, props) {
75
+ for(var i = 0; i < props.length; i++){
76
+ var descriptor = props[i];
77
+ descriptor.enumerable = descriptor.enumerable || false;
78
+ descriptor.configurable = true;
79
+ if ("value" in descriptor) descriptor.writable = true;
80
+ Object.defineProperty(target, descriptor.key, descriptor);
81
+ }
82
+ }
83
+ function _create_class(Constructor, protoProps, staticProps) {
84
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
85
+ if (staticProps) _defineProperties(Constructor, staticProps);
86
+ return Constructor;
87
+ }
88
+ function _define_property(obj, key, value) {
89
+ if (key in obj) {
90
+ Object.defineProperty(obj, key, {
91
+ value: value,
92
+ enumerable: true,
93
+ configurable: true,
94
+ writable: true
95
+ });
96
+ } else {
97
+ obj[key] = value;
98
+ }
99
+ return obj;
100
+ }
101
+ function _get_prototype_of(o) {
102
+ _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
103
+ return o.__proto__ || Object.getPrototypeOf(o);
104
+ };
105
+ return _get_prototype_of(o);
106
+ }
107
+ function _inherits(subClass, superClass) {
108
+ if (typeof superClass !== "function" && superClass !== null) {
109
+ throw new TypeError("Super expression must either be null or a function");
110
+ }
111
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
112
+ constructor: {
113
+ value: subClass,
114
+ writable: true,
115
+ configurable: true
116
+ }
117
+ });
118
+ if (superClass) _set_prototype_of(subClass, superClass);
119
+ }
120
+ function _is_native_function(fn) {
121
+ return Function.toString.call(fn).indexOf("[native code]") !== -1;
122
+ }
123
+ function _iterable_to_array(iter) {
124
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
125
+ }
126
+ function _iterable_to_array_limit(arr, i) {
127
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
128
+ if (_i == null) return;
129
+ var _arr = [];
130
+ var _n = true;
131
+ var _d = false;
132
+ var _s, _e;
133
+ try {
134
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
135
+ _arr.push(_s.value);
136
+ if (i && _arr.length === i) break;
137
+ }
138
+ } catch (err) {
139
+ _d = true;
140
+ _e = err;
141
+ } finally{
142
+ try {
143
+ if (!_n && _i["return"] != null) _i["return"]();
144
+ } finally{
145
+ if (_d) throw _e;
146
+ }
147
+ }
148
+ return _arr;
149
+ }
150
+ function _non_iterable_rest() {
151
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
152
+ }
153
+ function _non_iterable_spread() {
154
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
155
+ }
156
+ function _object_spread(target) {
157
+ for(var i = 1; i < arguments.length; i++){
158
+ var source = arguments[i] != null ? arguments[i] : {};
159
+ var ownKeys = Object.keys(source);
160
+ if (typeof Object.getOwnPropertySymbols === "function") {
161
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
162
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
163
+ }));
164
+ }
165
+ ownKeys.forEach(function(key) {
166
+ _define_property(target, key, source[key]);
167
+ });
168
+ }
169
+ return target;
170
+ }
171
+ function ownKeys(object, enumerableOnly) {
172
+ var keys = Object.keys(object);
173
+ if (Object.getOwnPropertySymbols) {
174
+ var symbols = Object.getOwnPropertySymbols(object);
175
+ if (enumerableOnly) {
176
+ symbols = symbols.filter(function(sym) {
177
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
178
+ });
179
+ }
180
+ keys.push.apply(keys, symbols);
181
+ }
182
+ return keys;
183
+ }
184
+ function _object_spread_props(target, source) {
185
+ source = source != null ? source : {};
186
+ if (Object.getOwnPropertyDescriptors) {
187
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
188
+ } else {
189
+ ownKeys(Object(source)).forEach(function(key) {
190
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
191
+ });
192
+ }
193
+ return target;
194
+ }
195
+ function _possible_constructor_return(self, call) {
196
+ if (call && (_type_of(call) === "object" || typeof call === "function")) {
197
+ return call;
198
+ }
199
+ return _assert_this_initialized(self);
200
+ }
201
+ function _set_prototype_of(o, p) {
202
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
203
+ o.__proto__ = p;
204
+ return o;
205
+ };
206
+ return _set_prototype_of(o, p);
207
+ }
208
+ function _sliced_to_array(arr, i) {
209
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
210
+ }
211
+ function _to_array(arr) {
212
+ return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_rest();
213
+ }
214
+ function _to_consumable_array(arr) {
215
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
216
+ }
217
+ function _type_of(obj) {
218
+ "@swc/helpers - typeof";
219
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
220
+ }
221
+ function _unsupported_iterable_to_array(o, minLen) {
222
+ if (!o) return;
223
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
224
+ var n = Object.prototype.toString.call(o).slice(8, -1);
225
+ if (n === "Object" && o.constructor) n = o.constructor.name;
226
+ if (n === "Map" || n === "Set") return Array.from(n);
227
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
228
+ }
229
+ function _wrap_native_super(Class) {
230
+ var _cache = typeof Map === "function" ? new Map() : undefined;
231
+ _wrap_native_super = function wrapNativeSuper(Class) {
232
+ if (Class === null || !_is_native_function(Class)) return Class;
233
+ if (typeof Class !== "function") {
234
+ throw new TypeError("Super expression must either be null or a function");
235
+ }
236
+ if (typeof _cache !== "undefined") {
237
+ if (_cache.has(Class)) return _cache.get(Class);
238
+ _cache.set(Class, Wrapper);
239
+ }
240
+ function Wrapper() {
241
+ return _construct(Class, arguments, _get_prototype_of(this).constructor);
242
+ }
243
+ Wrapper.prototype = Object.create(Class.prototype, {
244
+ constructor: {
245
+ value: Wrapper,
246
+ enumerable: false,
247
+ writable: true,
248
+ configurable: true
249
+ }
250
+ });
251
+ return _set_prototype_of(Wrapper, Class);
252
+ };
253
+ return _wrap_native_super(Class);
254
+ }
255
+ function _is_native_reflect_construct() {
256
+ try {
257
+ var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
258
+ } catch (_) {}
259
+ return (_is_native_reflect_construct = function() {
260
+ return !!result;
261
+ })();
262
+ }
263
+ function _ts_generator(thisArg, body) {
264
+ var f, y, t, g, _ = {
265
+ label: 0,
266
+ sent: function() {
267
+ if (t[0] & 1) throw t[1];
268
+ return t[1];
269
+ },
270
+ trys: [],
271
+ ops: []
272
+ };
273
+ return g = {
274
+ next: verb(0),
275
+ "throw": verb(1),
276
+ "return": verb(2)
277
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
278
+ return this;
279
+ }), g;
280
+ function verb(n) {
281
+ return function(v) {
282
+ return step([
283
+ n,
284
+ v
285
+ ]);
286
+ };
287
+ }
288
+ function step(op) {
289
+ if (f) throw new TypeError("Generator is already executing.");
290
+ while(_)try {
291
+ 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;
292
+ if (y = 0, t) op = [
293
+ op[0] & 2,
294
+ t.value
295
+ ];
296
+ switch(op[0]){
297
+ case 0:
298
+ case 1:
299
+ t = op;
300
+ break;
301
+ case 4:
302
+ _.label++;
303
+ return {
304
+ value: op[1],
305
+ done: false
306
+ };
307
+ case 5:
308
+ _.label++;
309
+ y = op[1];
310
+ op = [
311
+ 0
312
+ ];
313
+ continue;
314
+ case 7:
315
+ op = _.ops.pop();
316
+ _.trys.pop();
317
+ continue;
318
+ default:
319
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
320
+ _ = 0;
321
+ continue;
322
+ }
323
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
324
+ _.label = op[1];
325
+ break;
326
+ }
327
+ if (op[0] === 6 && _.label < t[1]) {
328
+ _.label = t[1];
329
+ t = op;
330
+ break;
331
+ }
332
+ if (t && _.label < t[2]) {
333
+ _.label = t[2];
334
+ _.ops.push(op);
335
+ break;
336
+ }
337
+ if (t[2]) _.ops.pop();
338
+ _.trys.pop();
339
+ continue;
340
+ }
341
+ op = body.call(thisArg, _);
342
+ } catch (e) {
343
+ op = [
344
+ 6,
345
+ e
346
+ ];
347
+ y = 0;
348
+ } finally{
349
+ f = t = 0;
350
+ }
351
+ if (op[0] & 5) throw op[1];
352
+ return {
353
+ value: op[0] ? op[1] : void 0,
354
+ done: true
355
+ };
356
+ }
357
+ }
358
+ var __defProp = Object.defineProperty;
359
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
360
+ var __getOwnPropNames = Object.getOwnPropertyNames;
361
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
362
+ var __defNormalProp = function(obj, key, value) {
363
+ return key in obj ? __defProp(obj, key, {
364
+ enumerable: true,
365
+ configurable: true,
366
+ writable: true,
367
+ value: value
368
+ }) : obj[key] = value;
369
+ };
370
+ var __export = function(target, all) {
371
+ for(var name in all)__defProp(target, name, {
372
+ get: all[name],
373
+ enumerable: true
374
+ });
375
+ };
376
+ var __copyProps = function(to, from, except, desc) {
377
+ if (from && (typeof from === "undefined" ? "undefined" : _type_of(from)) === "object" || typeof from === "function") {
378
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
379
+ try {
380
+ var _loop = function() {
381
+ var key = _step.value;
382
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
383
+ get: function() {
384
+ return from[key];
385
+ },
386
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
387
+ });
388
+ };
389
+ for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
390
+ } catch (err) {
391
+ _didIteratorError = true;
392
+ _iteratorError = err;
393
+ } finally{
394
+ try {
395
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
396
+ _iterator.return();
397
+ }
398
+ } finally{
399
+ if (_didIteratorError) {
400
+ throw _iteratorError;
401
+ }
402
+ }
403
+ }
404
+ }
405
+ return to;
406
+ };
407
+ var __toCommonJS = function(mod) {
408
+ return __copyProps(__defProp({}, "__esModule", {
409
+ value: true
410
+ }), mod);
411
+ };
412
+ var __publicField = function(obj, key, value) {
413
+ return __defNormalProp(obj, (typeof key === "undefined" ? "undefined" : _type_of(key)) !== "symbol" ? key + "" : key, value);
414
+ };
415
+ // src/index.ts
416
+ var src_exports = {};
417
+ __export(src_exports, {
418
+ MethodNotFoundError: function() {
419
+ return MethodNotFoundError;
420
+ },
421
+ NativeMethodError: function() {
422
+ return NativeMethodError;
423
+ },
424
+ linkBridge: function() {
425
+ return linkBridge;
426
+ },
427
+ registerWebMethod: function() {
428
+ return registerWebMethod;
429
+ }
430
+ });
431
+ module.exports = __toCommonJS(src_exports);
432
+ // src/error.ts
433
+ var MethodNotFoundError = /*#__PURE__*/ function(Error1) {
434
+ _inherits(MethodNotFoundError, Error1);
435
+ function MethodNotFoundError(methodName) {
436
+ _class_call_check(this, MethodNotFoundError);
437
+ var _this;
438
+ _this = _call_super(this, MethodNotFoundError, [
439
+ "Method ".concat(methodName, " is not defined")
440
+ ]);
441
+ _this.name = "MethodNotFoundError";
442
+ return _this;
443
+ }
444
+ return MethodNotFoundError;
445
+ }(_wrap_native_super(Error));
446
+ var NativeMethodError = /*#__PURE__*/ function(Error1) {
447
+ _inherits(NativeMethodError, Error1);
448
+ function NativeMethodError(methodName) {
449
+ _class_call_check(this, NativeMethodError);
450
+ var _this;
451
+ _this = _call_super(this, NativeMethodError, [
452
+ "An error occurred in the native bridge: ".concat(methodName)
453
+ ]);
454
+ _this.name = "NativeMethodError";
455
+ return _this;
456
+ }
457
+ return NativeMethodError;
458
+ }(_wrap_native_super(Error));
459
+ // src/linkBridge.ts
460
+ var import_utils3 = require("@webview-bridge/utils");
461
+ // src/internal/bridgeInstance.ts
462
+ var import_utils2 = require("@webview-bridge/utils");
463
+ // src/internal/createPromiseProxy.ts
464
+ var createPromiseProxy = function() {
465
+ return new Proxy({}, {
466
+ get: function() {
467
+ return function() {
468
+ return Promise.resolve();
469
+ };
470
+ }
471
+ });
472
+ };
473
+ // src/internal/linkBridgeStore.ts
474
+ var import_utils = require("@webview-bridge/utils");
475
+ var linkBridgeStore = function(emitter) {
476
+ var initialState = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
477
+ var getState = function() {
478
+ return state;
479
+ };
480
+ var setState = function(newState) {
481
+ var _newState = _object_spread({}, state, (0, import_utils.removeUndefinedKeys)(newState));
482
+ if ((0, import_utils.equals)(state, _newState)) {
483
+ return;
484
+ }
485
+ var prevState = state;
486
+ state = _newState;
487
+ emitChange(state, prevState);
488
+ };
489
+ emitter.on("bridgeStateChange", function(data) {
490
+ setState(data);
491
+ });
492
+ var state = _object_spread({}, initialState);
493
+ var listeners = /* @__PURE__ */ new Set();
494
+ var emitChange = function(newState, prevState) {
495
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
496
+ try {
497
+ for(var _iterator = listeners[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
498
+ var listener = _step.value;
499
+ listener(newState, prevState);
500
+ }
501
+ } catch (err) {
502
+ _didIteratorError = true;
503
+ _iteratorError = err;
504
+ } finally{
505
+ try {
506
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
507
+ _iterator.return();
508
+ }
509
+ } finally{
510
+ if (_didIteratorError) {
511
+ throw _iteratorError;
512
+ }
513
+ }
514
+ }
515
+ };
516
+ var subscribe = function(listener) {
517
+ listeners.add(listener);
518
+ return function() {
519
+ return listeners.delete(listener);
520
+ };
521
+ };
522
+ return {
523
+ getState: getState,
524
+ subscribe: subscribe
525
+ };
526
+ };
527
+ // src/internal/mockStore.ts
528
+ var mockStore = function() {
529
+ var initialState = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
530
+ var state = initialState;
531
+ var getState = function() {
532
+ return state;
533
+ };
534
+ var subscribe = function() {
535
+ return function() {};
536
+ };
537
+ return {
538
+ getState: getState,
539
+ subscribe: subscribe
540
+ };
541
+ };
542
+ // src/internal/bridgeInstance.ts
543
+ var BridgeInstance = /*#__PURE__*/ function() {
544
+ function BridgeInstance(_options, _emitter) {
545
+ var _bridgeMethods = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : [], _nativeInitialState = arguments.length > 3 ? arguments[3] : void 0;
546
+ _class_call_check(this, BridgeInstance);
547
+ this._options = _options;
548
+ this._emitter = _emitter;
549
+ this._bridgeMethods = _bridgeMethods;
550
+ this._nativeInitialState = _nativeInitialState;
551
+ __publicField(this, "_defaultTimeoutMs", 2e3);
552
+ __publicField(this, "_isListenerRegistered", false);
553
+ __publicField(this, "store", mockStore());
554
+ __publicField(this, "loose", createPromiseProxy());
555
+ this._hydrate(_bridgeMethods, _nativeInitialState);
556
+ }
557
+ _create_class(BridgeInstance, [
558
+ {
559
+ key: "isReactNativeWebView",
560
+ get: function get() {
561
+ return Boolean(window.ReactNativeWebView);
562
+ }
563
+ },
564
+ {
565
+ key: "isWebViewBridgeAvailable",
566
+ get: function get() {
567
+ return this._bridgeMethods.length > 0;
568
+ }
569
+ },
570
+ {
571
+ key: "isNativeMethodAvailable",
572
+ value: function isNativeMethodAvailable(methodName) {
573
+ return typeof methodName === "string" && this._bridgeMethods.includes(methodName);
574
+ }
575
+ },
576
+ {
577
+ key: "addEventListener",
578
+ value: function addEventListener(eventName, listener) {
579
+ return this._emitter.on("postMessage/".concat(String(eventName)), listener);
580
+ }
581
+ },
582
+ {
583
+ key: "_postMessage",
584
+ value: function _postMessage(type, body) {
585
+ var _window_ReactNativeWebView;
586
+ (_window_ReactNativeWebView = window.ReactNativeWebView) === null || _window_ReactNativeWebView === void 0 ? void 0 : _window_ReactNativeWebView.postMessage(JSON.stringify(body ? {
587
+ type: type,
588
+ body: body
589
+ } : {
590
+ type: type
591
+ }));
592
+ }
593
+ },
594
+ {
595
+ key: "_createNativeMethod",
596
+ value: function _createNativeMethod(methodName, throwOnError, timeoutMs, onFallback) {
597
+ var _this = this;
598
+ return function() {
599
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
600
+ args[_key] = arguments[_key];
601
+ }
602
+ var eventId = (0, import_utils2.createRandomId)();
603
+ return Promise.race([
604
+ (0, import_utils2.createResolver)({
605
+ emitter: _this._emitter,
606
+ methodName: methodName,
607
+ eventId: eventId,
608
+ evaluate: function() {
609
+ _this._postMessage("bridge", {
610
+ method: methodName,
611
+ eventId: eventId,
612
+ args: args
613
+ });
614
+ },
615
+ onFallback: function() {
616
+ onFallback === null || onFallback === void 0 ? void 0 : onFallback(methodName, args);
617
+ },
618
+ failHandler: throwOnError && new NativeMethodError(methodName)
619
+ }),
620
+ timeoutMs > 0 && (0, import_utils2.timeout)(timeoutMs, throwOnError)
621
+ ].filter(Boolean));
622
+ };
623
+ }
624
+ },
625
+ {
626
+ key: "_willMethodThrowOnError",
627
+ value: function _willMethodThrowOnError(methodName) {
628
+ var throwOnError = this._options.throwOnError;
629
+ return throwOnError === true || Array.isArray(throwOnError) && throwOnError.includes(methodName);
630
+ }
631
+ },
632
+ {
633
+ key: "_createLoose",
634
+ value: function _createLoose(initialState) {
635
+ var _this = this;
636
+ var _this__options = this._options, tmp = _this__options.timeout, timeoutMs = tmp === void 0 ? this._defaultTimeoutMs : tmp, onFallback = _this__options.onFallback;
637
+ return new Proxy(initialState, {
638
+ get: function(target, methodName) {
639
+ if (methodName in target && ![
640
+ "isWebViewBridgeAvailable",
641
+ "isNativeMethodAvailable"
642
+ ].includes(methodName)) {
643
+ return target[methodName];
644
+ }
645
+ return _this._createNativeMethod(methodName, _this._willMethodThrowOnError(methodName), timeoutMs, onFallback);
646
+ }
647
+ });
648
+ }
649
+ },
650
+ {
651
+ key: "_hydrate",
652
+ value: function _hydrate(bridgeMethods) {
653
+ var _this = this;
654
+ var nativeInitialState = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
655
+ 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;
656
+ var initialMethods = Object.entries(initialBridge).filter(function(param) {
657
+ var _param = _sliced_to_array(param, 2), _ = _param[0], bridge = _param[1];
658
+ return typeof bridge === "function";
659
+ });
660
+ var initialBridgeMethodNames = initialMethods.map(function(param) {
661
+ var _param = _sliced_to_array(param, 1), methodName = _param[0];
662
+ return methodName;
663
+ });
664
+ Object.defineProperties(this, Object.fromEntries(initialMethods.map(function(param) {
665
+ var _param = _sliced_to_array(param, 2), methodName = _param[0], value = _param[1];
666
+ return [
667
+ methodName,
668
+ {
669
+ value: value,
670
+ writable: true
671
+ }
672
+ ];
673
+ })));
674
+ this._bridgeMethods = _to_consumable_array(bridgeMethods).concat(_to_consumable_array(initialBridgeMethodNames));
675
+ this._nativeInitialState = nativeInitialState;
676
+ var nativeMethods = bridgeMethods.reduce(function(acc, methodName) {
677
+ if (!_this.isReactNativeWebView) {
678
+ return acc;
679
+ }
680
+ var nativeMethod = _this._createNativeMethod(methodName, _this._willMethodThrowOnError(methodName), timeoutMs, onFallback);
681
+ Object.defineProperty(_this, methodName, {
682
+ value: nativeMethod,
683
+ writable: false
684
+ });
685
+ return _object_spread_props(_object_spread({}, acc), _define_property({}, methodName, nativeMethod));
686
+ }, initialBridge);
687
+ this.loose = this._createLoose(nativeMethods);
688
+ this.store = linkBridgeStore(this._emitter, _object_spread({}, nativeMethods, nativeInitialState));
689
+ if (!this._isListenerRegistered) {
690
+ var handleVisibilityChange = function() {
691
+ if (document.visibilityState === "visible") {
692
+ _this._postMessage("getBridgeState");
693
+ }
694
+ };
695
+ document.addEventListener("visibilitychange", handleVisibilityChange);
696
+ this._isListenerRegistered = true;
697
+ }
698
+ this._postMessage("getBridgeState");
699
+ var _window_nativeBatchedEvents;
700
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
701
+ try {
702
+ for(var _iterator = ((_window_nativeBatchedEvents = window.nativeBatchedEvents) !== null && _window_nativeBatchedEvents !== void 0 ? _window_nativeBatchedEvents : [])[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
703
+ var _step_value = _to_array(_step.value), eventName = _step_value[0], args = _step_value.slice(1);
704
+ var _this__emitter;
705
+ (_this__emitter = this._emitter).emit.apply(_this__emitter, [
706
+ eventName
707
+ ].concat(_to_consumable_array(args)));
708
+ }
709
+ } catch (err) {
710
+ _didIteratorError = true;
711
+ _iteratorError = err;
712
+ } finally{
713
+ try {
714
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
715
+ _iterator.return();
716
+ }
717
+ } finally{
718
+ if (_didIteratorError) {
719
+ throw _iteratorError;
720
+ }
721
+ }
722
+ }
723
+ window.nativeBatchedEvents = [];
724
+ return true;
725
+ }
726
+ }
727
+ ]);
728
+ return BridgeInstance;
729
+ }();
730
+ // src/linkBridge.ts
731
+ var linkBridge = function() {
732
+ var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {
733
+ timeout: 2e3,
734
+ throwOnError: false
735
+ };
736
+ if (typeof window === "undefined") {
737
+ var _options_initialBridge;
738
+ var initialBridge = (_options_initialBridge = options === null || options === void 0 ? void 0 : options.initialBridge) !== null && _options_initialBridge !== void 0 ? _options_initialBridge : {};
739
+ var initialMethods = Object.entries(initialBridge).filter(function(param) {
740
+ var _param = _sliced_to_array(param, 2), _ = _param[0], bridge = _param[1];
741
+ return typeof bridge === "function";
742
+ });
743
+ var initialBridgeMethodNames = initialMethods.map(function(param) {
744
+ var _param = _sliced_to_array(param, 1), methodName = _param[0];
745
+ return methodName;
746
+ });
747
+ return {
748
+ addEventListener: function(_eventName, _listener) {
749
+ return function() {};
750
+ },
751
+ loose: {},
752
+ isWebViewBridgeAvailable: initialBridgeMethodNames.length > 0,
753
+ isNativeMethodAvailable: function(method) {
754
+ return initialBridgeMethodNames.includes(method);
755
+ },
756
+ store: mockStore(options === null || options === void 0 ? void 0 : options.initialBridge)
757
+ };
758
+ }
759
+ if (!window.ReactNativeWebView) {
760
+ console.warn("[WebViewBridge] Not in a WebView environment");
761
+ }
762
+ var emitter = (0, import_utils3.createEvents)();
763
+ if (!window.nativeEmitter) {
764
+ window.nativeEmitter = emitter;
765
+ }
766
+ var _window___bridgeMethods__;
767
+ var bridgeMethods = (_window___bridgeMethods__ = window.__bridgeMethods__) !== null && _window___bridgeMethods__ !== void 0 ? _window___bridgeMethods__ : [];
768
+ var _window___bridgeInitialState__;
769
+ var nativeInitialState = (_window___bridgeInitialState__ = window.__bridgeInitialState__) !== null && _window___bridgeInitialState__ !== void 0 ? _window___bridgeInitialState__ : {};
770
+ var instance = new BridgeInstance(options, emitter, bridgeMethods, nativeInitialState);
771
+ if (bridgeMethods.length === 0) {
772
+ var unsubscribe = emitter.on("hydrate", function(param) {
773
+ var bridgeMethods2 = param.bridgeMethods, nativeInitialState2 = param.nativeInitialState;
774
+ instance._hydrate(bridgeMethods2, nativeInitialState2);
775
+ unsubscribe();
776
+ });
777
+ }
778
+ var onFallback = options.onFallback, onReady = options.onReady;
779
+ var proxy = new Proxy(instance, {
780
+ get: function(target, methodName, proxy2) {
781
+ if (methodName in target) {
782
+ return target[methodName];
783
+ }
784
+ proxy2._postMessage("fallback", {
785
+ method: methodName
786
+ });
787
+ if (proxy2._willMethodThrowOnError(methodName)) {
788
+ return function() {
789
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
790
+ args[_key] = arguments[_key];
791
+ }
792
+ onFallback === null || onFallback === void 0 ? void 0 : onFallback(methodName, args);
793
+ return Promise.reject(new MethodNotFoundError(methodName));
794
+ };
795
+ } else {
796
+ console.warn("[WebViewBridge] ".concat(methodName, " is not defined, using fallback."));
797
+ }
798
+ return function() {
799
+ return Promise.resolve();
800
+ };
801
+ }
802
+ });
803
+ onReady === null || onReady === void 0 ? void 0 : onReady(proxy);
804
+ return proxy;
805
+ };
806
+ // src/registerWebMethod.ts
807
+ var import_utils4 = require("@webview-bridge/utils");
808
+ var registerWebMethod = function(bridge) {
809
+ if (typeof window !== "undefined" && !window.ReactNativeWebView) {
810
+ console.warn("[WebViewBridge] Not in a WebView environment");
811
+ return bridge;
812
+ }
813
+ var bridgeEntries = Object.entries(bridge);
814
+ var bridgeNames = Object.keys(bridge);
815
+ var emitter = (0, import_utils4.createEvents)();
816
+ window.webEmitter = emitter;
817
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
818
+ try {
819
+ var _loop = function() {
820
+ var _step_value = _sliced_to_array(_step.value, 2), funcName = _step_value[0], func = _step_value[1];
821
+ var $func = /*#__PURE__*/ function() {
822
+ var _ref = _async_to_generator(function(eventId, args) {
823
+ var _window_ReactNativeWebView, value, e, _window_ReactNativeWebView1;
824
+ return _ts_generator(this, function(_state) {
825
+ switch(_state.label){
826
+ case 0:
827
+ _state.trys.push([
828
+ 0,
829
+ 2,
830
+ ,
831
+ 3
832
+ ]);
833
+ return [
834
+ 4,
835
+ func.apply(void 0, _to_consumable_array(args))
836
+ ];
837
+ case 1:
838
+ value = _state.sent();
839
+ (_window_ReactNativeWebView = window.ReactNativeWebView) === null || _window_ReactNativeWebView === void 0 ? void 0 : _window_ReactNativeWebView.postMessage(JSON.stringify({
840
+ type: "webMethodResponse",
841
+ body: {
842
+ funcName: funcName,
843
+ eventId: eventId,
844
+ value: value
845
+ }
846
+ }));
847
+ return [
848
+ 3,
849
+ 3
850
+ ];
851
+ case 2:
852
+ e = _state.sent();
853
+ (_window_ReactNativeWebView1 = window.ReactNativeWebView) === null || _window_ReactNativeWebView1 === void 0 ? void 0 : _window_ReactNativeWebView1.postMessage(JSON.stringify({
854
+ type: "webMethodError",
855
+ body: {
856
+ funcName: funcName,
857
+ eventId: eventId,
858
+ error: JSON.stringify(e)
859
+ }
860
+ }));
861
+ return [
862
+ 3,
863
+ 3
864
+ ];
865
+ case 3:
866
+ return [
867
+ 2
868
+ ];
869
+ }
870
+ });
871
+ });
872
+ return function $func(eventId, args) {
873
+ return _ref.apply(this, arguments);
874
+ };
875
+ }();
876
+ emitter.on(funcName, $func);
877
+ };
878
+ for(var _iterator = bridgeEntries[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
879
+ } catch (err) {
880
+ _didIteratorError = true;
881
+ _iteratorError = err;
882
+ } finally{
883
+ try {
884
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
885
+ _iterator.return();
886
+ }
887
+ } finally{
888
+ if (_didIteratorError) {
889
+ throw _iteratorError;
890
+ }
891
+ }
892
+ }
893
+ var register = function() {
894
+ var _window_ReactNativeWebView;
895
+ (_window_ReactNativeWebView = window.ReactNativeWebView) === null || _window_ReactNativeWebView === void 0 ? void 0 : _window_ReactNativeWebView.postMessage(JSON.stringify({
896
+ type: "registerWebMethod",
897
+ body: {
898
+ bridgeNames: bridgeNames
899
+ }
900
+ }));
901
+ window.removeEventListener("DOMContentLoaded", register);
902
+ };
903
+ if (!window.ReactNativeWebView) {
904
+ window.addEventListener("DOMContentLoaded", register);
905
+ return bridge;
906
+ }
907
+ document.addEventListener("visibilitychange", function() {
908
+ if (document.visibilityState === "visible") {
909
+ var _window_ReactNativeWebView;
910
+ (_window_ReactNativeWebView = window.ReactNativeWebView) === null || _window_ReactNativeWebView === void 0 ? void 0 : _window_ReactNativeWebView.postMessage(JSON.stringify({
911
+ type: "registerWebMethod",
912
+ body: {
913
+ bridgeNames: bridgeNames
914
+ }
915
+ }));
916
+ }
917
+ });
918
+ register();
919
+ return bridge;
920
+ };
921
+ // Annotate the CommonJS export names for ESM import in node:
922
+ 0 && (module.exports = {
923
+ MethodNotFoundError: MethodNotFoundError,
924
+ NativeMethodError: NativeMethodError,
925
+ linkBridge: linkBridge,
926
+ registerWebMethod: registerWebMethod
927
+ });