@webview-bridge/web 1.5.0 → 1.5.1-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.
@@ -1,8 +1,25 @@
1
1
  "use strict";
2
2
  var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
6
23
  var __export = (target, all) => {
7
24
  for (var name in all)
8
25
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -16,6 +33,26 @@ var __copyProps = (to, from, except, desc) => {
16
33
  return to;
17
34
  };
18
35
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
+ var __async = (__this, __arguments, generator) => {
37
+ return new Promise((resolve, reject) => {
38
+ var fulfilled = (value) => {
39
+ try {
40
+ step(generator.next(value));
41
+ } catch (e) {
42
+ reject(e);
43
+ }
44
+ };
45
+ var rejected = (value) => {
46
+ try {
47
+ step(generator.throw(value));
48
+ } catch (e) {
49
+ reject(e);
50
+ }
51
+ };
52
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
53
+ step((generator = generator.apply(__this, __arguments)).next());
54
+ });
55
+ };
19
56
 
20
57
  // src/index.ts
21
58
  var src_exports = {};
@@ -52,9 +89,11 @@ var createEvents = () => ({
52
89
  }
53
90
  },
54
91
  on(event, cb) {
55
- this.events[event]?.push(cb) || (this.events[event] = [cb]);
92
+ var _a;
93
+ ((_a = this.events[event]) == null ? void 0 : _a.push(cb)) || (this.events[event] = [cb]);
56
94
  return () => {
57
- this.events[event] = this.events[event]?.filter((i) => cb !== i);
95
+ var _a2;
96
+ this.events[event] = (_a2 = this.events[event]) == null ? void 0 : _a2.filter((i) => cb !== i);
58
97
  };
59
98
  }
60
99
  });
@@ -73,7 +112,7 @@ var createResolver = ({
73
112
  unbind();
74
113
  if (throwOccurred) {
75
114
  if (failHandler instanceof Error) {
76
- onFallback?.();
115
+ onFallback == null ? void 0 : onFallback();
77
116
  reject(failHandler);
78
117
  } else {
79
118
  resolve(void 0);
@@ -177,6 +216,7 @@ var emitter = createEvents();
177
216
 
178
217
  // src/internal/linkBridgeStore.ts
179
218
  var linkBridgeStore = (initialState = {}) => {
219
+ var _a;
180
220
  if (!window.ReactNativeWebView) {
181
221
  console.warn("[WebViewBridge] Not in a WebView environment");
182
222
  }
@@ -185,10 +225,7 @@ var linkBridgeStore = (initialState = {}) => {
185
225
  }
186
226
  const getState = () => state;
187
227
  const setState = (newState) => {
188
- const _newState = {
189
- ...state,
190
- ...removeUndefinedKeys(newState)
191
- };
228
+ const _newState = __spreadValues(__spreadValues({}, state), removeUndefinedKeys(newState));
192
229
  if (equals(state, _newState)) {
193
230
  return;
194
231
  }
@@ -200,20 +237,21 @@ var linkBridgeStore = (initialState = {}) => {
200
237
  setState(data);
201
238
  });
202
239
  document.addEventListener("visibilitychange", () => {
240
+ var _a2;
203
241
  if (document.visibilityState === "visible") {
204
- window.ReactNativeWebView?.postMessage(
242
+ (_a2 = window.ReactNativeWebView) == null ? void 0 : _a2.postMessage(
205
243
  JSON.stringify({
206
244
  type: "getBridgeState"
207
245
  })
208
246
  );
209
247
  }
210
248
  });
211
- window.ReactNativeWebView?.postMessage(
249
+ (_a = window.ReactNativeWebView) == null ? void 0 : _a.postMessage(
212
250
  JSON.stringify({
213
251
  type: "getBridgeState"
214
252
  })
215
253
  );
216
- let state = { ...initialState, ...window.__bridgeInitialState__ };
254
+ let state = __spreadValues(__spreadValues({}, initialState), window.__bridgeInitialState__);
217
255
  const listeners = /* @__PURE__ */ new Set();
218
256
  const emitChange = (newState, prevState) => {
219
257
  for (const listener of listeners) {
@@ -245,7 +283,8 @@ var createNativeMethod = ({
245
283
  methodName,
246
284
  eventId,
247
285
  evaluate: () => {
248
- window.ReactNativeWebView?.postMessage(
286
+ var _a;
287
+ (_a = window.ReactNativeWebView) == null ? void 0 : _a.postMessage(
249
288
  JSON.stringify({
250
289
  type: "bridge",
251
290
  body: {
@@ -257,7 +296,7 @@ var createNativeMethod = ({
257
296
  );
258
297
  },
259
298
  onFallback: () => {
260
- onFallback?.(methodName, args);
299
+ onFallback == null ? void 0 : onFallback(methodName, args);
261
300
  },
262
301
  failHandler: throwOnError && new NativeMethodError(methodName)
263
302
  }),
@@ -269,6 +308,7 @@ var linkBridge = (options = {
269
308
  timeout: 2e3,
270
309
  throwOnError: false
271
310
  }) => {
311
+ var _a, _b;
272
312
  if (typeof window === "undefined") {
273
313
  return {
274
314
  store: {
@@ -286,7 +326,7 @@ var linkBridge = (options = {
286
326
  if (!window.ReactNativeWebView) {
287
327
  console.warn("[WebViewBridge] Not in a WebView environment");
288
328
  }
289
- const bridgeMethods = window.__bridgeMethods__ ?? [];
329
+ const bridgeMethods = (_a = window.__bridgeMethods__) != null ? _a : [];
290
330
  if (!window.nativeEmitter) {
291
331
  window.nativeEmitter = emitter;
292
332
  }
@@ -295,15 +335,14 @@ var linkBridge = (options = {
295
335
  };
296
336
  const target = bridgeMethods.reduce(
297
337
  (acc, methodName) => {
298
- return {
299
- ...acc,
338
+ return __spreadProps(__spreadValues({}, acc), {
300
339
  [methodName]: createNativeMethod({
301
340
  methodName,
302
341
  timeoutMs,
303
342
  throwOnError: willMethodThrowOnError(methodName),
304
343
  onFallback
305
344
  })
306
- };
345
+ });
307
346
  },
308
347
  {}
309
348
  );
@@ -335,10 +374,11 @@ var linkBridge = (options = {
335
374
  });
336
375
  const proxy = new Proxy(target, {
337
376
  get: (target2, methodName) => {
377
+ var _a2;
338
378
  if (methodName in target2) {
339
379
  return target2[methodName];
340
380
  }
341
- window.ReactNativeWebView?.postMessage(
381
+ (_a2 = window.ReactNativeWebView) == null ? void 0 : _a2.postMessage(
342
382
  JSON.stringify({
343
383
  type: "fallback",
344
384
  body: {
@@ -348,7 +388,7 @@ var linkBridge = (options = {
348
388
  );
349
389
  if (willMethodThrowOnError(methodName)) {
350
390
  return (...args) => {
351
- onFallback?.(methodName, args);
391
+ onFallback == null ? void 0 : onFallback(methodName, args);
352
392
  Promise.reject(new MethodNotFoundError(methodName));
353
393
  };
354
394
  } else {
@@ -359,11 +399,11 @@ var linkBridge = (options = {
359
399
  return () => Promise.resolve();
360
400
  }
361
401
  });
362
- for (const [eventName, ...args] of window.nativeBatchedEvents ?? []) {
402
+ for (const [eventName, ...args] of (_b = window.nativeBatchedEvents) != null ? _b : []) {
363
403
  emitter.emit(eventName, ...args);
364
404
  }
365
405
  window.nativeBatchedEvents = [];
366
- onReady?.(proxy);
406
+ onReady == null ? void 0 : onReady(proxy);
367
407
  return proxy;
368
408
  };
369
409
 
@@ -386,28 +426,30 @@ var registerWebMethod = (bridge) => {
386
426
  const emitter2 = createEvents();
387
427
  window.webEmitter = emitter2;
388
428
  for (const [funcName, func] of bridgeEntries) {
389
- const $func = async (eventId, args) => {
429
+ const $func = (eventId, args) => __async(void 0, null, function* () {
430
+ var _a, _b;
390
431
  try {
391
- const value = await func(...args);
392
- window.ReactNativeWebView?.postMessage(
432
+ const value = yield func(...args);
433
+ (_a = window.ReactNativeWebView) == null ? void 0 : _a.postMessage(
393
434
  JSON.stringify({
394
435
  type: "webMethodResponse",
395
436
  body: { funcName, eventId, value }
396
437
  })
397
438
  );
398
439
  } catch (e) {
399
- window.ReactNativeWebView?.postMessage(
440
+ (_b = window.ReactNativeWebView) == null ? void 0 : _b.postMessage(
400
441
  JSON.stringify({
401
442
  type: "webMethodError",
402
443
  body: { funcName, eventId, error: JSON.stringify(e) }
403
444
  })
404
445
  );
405
446
  }
406
- };
447
+ });
407
448
  emitter2.on(funcName, $func);
408
449
  }
409
450
  const register = () => {
410
- window.ReactNativeWebView?.postMessage(
451
+ var _a;
452
+ (_a = window.ReactNativeWebView) == null ? void 0 : _a.postMessage(
411
453
  JSON.stringify({
412
454
  type: "registerWebMethod",
413
455
  body: { bridgeNames }
@@ -420,8 +462,9 @@ var registerWebMethod = (bridge) => {
420
462
  return bridge;
421
463
  }
422
464
  document.addEventListener("visibilitychange", () => {
465
+ var _a;
423
466
  if (document.visibilityState === "visible") {
424
- window.ReactNativeWebView?.postMessage(
467
+ (_a = window.ReactNativeWebView) == null ? void 0 : _a.postMessage(
425
468
  JSON.stringify({
426
469
  type: "registerWebMethod",
427
470
  body: { bridgeNames }
@@ -1,3 +1,43 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __async = (__this, __arguments, generator) => {
21
+ return new Promise((resolve, reject) => {
22
+ var fulfilled = (value) => {
23
+ try {
24
+ step(generator.next(value));
25
+ } catch (e) {
26
+ reject(e);
27
+ }
28
+ };
29
+ var rejected = (value) => {
30
+ try {
31
+ step(generator.throw(value));
32
+ } catch (e) {
33
+ reject(e);
34
+ }
35
+ };
36
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
37
+ step((generator = generator.apply(__this, __arguments)).next());
38
+ });
39
+ };
40
+
1
41
  // src/error.ts
2
42
  var MethodNotFoundError = class extends Error {
3
43
  constructor(methodName) {
@@ -22,9 +62,11 @@ var createEvents = () => ({
22
62
  }
23
63
  },
24
64
  on(event, cb) {
25
- this.events[event]?.push(cb) || (this.events[event] = [cb]);
65
+ var _a;
66
+ ((_a = this.events[event]) == null ? void 0 : _a.push(cb)) || (this.events[event] = [cb]);
26
67
  return () => {
27
- this.events[event] = this.events[event]?.filter((i) => cb !== i);
68
+ var _a2;
69
+ this.events[event] = (_a2 = this.events[event]) == null ? void 0 : _a2.filter((i) => cb !== i);
28
70
  };
29
71
  }
30
72
  });
@@ -43,7 +85,7 @@ var createResolver = ({
43
85
  unbind();
44
86
  if (throwOccurred) {
45
87
  if (failHandler instanceof Error) {
46
- onFallback?.();
88
+ onFallback == null ? void 0 : onFallback();
47
89
  reject(failHandler);
48
90
  } else {
49
91
  resolve(void 0);
@@ -147,6 +189,7 @@ var emitter = createEvents();
147
189
 
148
190
  // src/internal/linkBridgeStore.ts
149
191
  var linkBridgeStore = (initialState = {}) => {
192
+ var _a;
150
193
  if (!window.ReactNativeWebView) {
151
194
  console.warn("[WebViewBridge] Not in a WebView environment");
152
195
  }
@@ -155,10 +198,7 @@ var linkBridgeStore = (initialState = {}) => {
155
198
  }
156
199
  const getState = () => state;
157
200
  const setState = (newState) => {
158
- const _newState = {
159
- ...state,
160
- ...removeUndefinedKeys(newState)
161
- };
201
+ const _newState = __spreadValues(__spreadValues({}, state), removeUndefinedKeys(newState));
162
202
  if (equals(state, _newState)) {
163
203
  return;
164
204
  }
@@ -170,20 +210,21 @@ var linkBridgeStore = (initialState = {}) => {
170
210
  setState(data);
171
211
  });
172
212
  document.addEventListener("visibilitychange", () => {
213
+ var _a2;
173
214
  if (document.visibilityState === "visible") {
174
- window.ReactNativeWebView?.postMessage(
215
+ (_a2 = window.ReactNativeWebView) == null ? void 0 : _a2.postMessage(
175
216
  JSON.stringify({
176
217
  type: "getBridgeState"
177
218
  })
178
219
  );
179
220
  }
180
221
  });
181
- window.ReactNativeWebView?.postMessage(
222
+ (_a = window.ReactNativeWebView) == null ? void 0 : _a.postMessage(
182
223
  JSON.stringify({
183
224
  type: "getBridgeState"
184
225
  })
185
226
  );
186
- let state = { ...initialState, ...window.__bridgeInitialState__ };
227
+ let state = __spreadValues(__spreadValues({}, initialState), window.__bridgeInitialState__);
187
228
  const listeners = /* @__PURE__ */ new Set();
188
229
  const emitChange = (newState, prevState) => {
189
230
  for (const listener of listeners) {
@@ -215,7 +256,8 @@ var createNativeMethod = ({
215
256
  methodName,
216
257
  eventId,
217
258
  evaluate: () => {
218
- window.ReactNativeWebView?.postMessage(
259
+ var _a;
260
+ (_a = window.ReactNativeWebView) == null ? void 0 : _a.postMessage(
219
261
  JSON.stringify({
220
262
  type: "bridge",
221
263
  body: {
@@ -227,7 +269,7 @@ var createNativeMethod = ({
227
269
  );
228
270
  },
229
271
  onFallback: () => {
230
- onFallback?.(methodName, args);
272
+ onFallback == null ? void 0 : onFallback(methodName, args);
231
273
  },
232
274
  failHandler: throwOnError && new NativeMethodError(methodName)
233
275
  }),
@@ -239,6 +281,7 @@ var linkBridge = (options = {
239
281
  timeout: 2e3,
240
282
  throwOnError: false
241
283
  }) => {
284
+ var _a, _b;
242
285
  if (typeof window === "undefined") {
243
286
  return {
244
287
  store: {
@@ -256,7 +299,7 @@ var linkBridge = (options = {
256
299
  if (!window.ReactNativeWebView) {
257
300
  console.warn("[WebViewBridge] Not in a WebView environment");
258
301
  }
259
- const bridgeMethods = window.__bridgeMethods__ ?? [];
302
+ const bridgeMethods = (_a = window.__bridgeMethods__) != null ? _a : [];
260
303
  if (!window.nativeEmitter) {
261
304
  window.nativeEmitter = emitter;
262
305
  }
@@ -265,15 +308,14 @@ var linkBridge = (options = {
265
308
  };
266
309
  const target = bridgeMethods.reduce(
267
310
  (acc, methodName) => {
268
- return {
269
- ...acc,
311
+ return __spreadProps(__spreadValues({}, acc), {
270
312
  [methodName]: createNativeMethod({
271
313
  methodName,
272
314
  timeoutMs,
273
315
  throwOnError: willMethodThrowOnError(methodName),
274
316
  onFallback
275
317
  })
276
- };
318
+ });
277
319
  },
278
320
  {}
279
321
  );
@@ -305,10 +347,11 @@ var linkBridge = (options = {
305
347
  });
306
348
  const proxy = new Proxy(target, {
307
349
  get: (target2, methodName) => {
350
+ var _a2;
308
351
  if (methodName in target2) {
309
352
  return target2[methodName];
310
353
  }
311
- window.ReactNativeWebView?.postMessage(
354
+ (_a2 = window.ReactNativeWebView) == null ? void 0 : _a2.postMessage(
312
355
  JSON.stringify({
313
356
  type: "fallback",
314
357
  body: {
@@ -318,7 +361,7 @@ var linkBridge = (options = {
318
361
  );
319
362
  if (willMethodThrowOnError(methodName)) {
320
363
  return (...args) => {
321
- onFallback?.(methodName, args);
364
+ onFallback == null ? void 0 : onFallback(methodName, args);
322
365
  Promise.reject(new MethodNotFoundError(methodName));
323
366
  };
324
367
  } else {
@@ -329,11 +372,11 @@ var linkBridge = (options = {
329
372
  return () => Promise.resolve();
330
373
  }
331
374
  });
332
- for (const [eventName, ...args] of window.nativeBatchedEvents ?? []) {
375
+ for (const [eventName, ...args] of (_b = window.nativeBatchedEvents) != null ? _b : []) {
333
376
  emitter.emit(eventName, ...args);
334
377
  }
335
378
  window.nativeBatchedEvents = [];
336
- onReady?.(proxy);
379
+ onReady == null ? void 0 : onReady(proxy);
337
380
  return proxy;
338
381
  };
339
382
 
@@ -356,28 +399,30 @@ var registerWebMethod = (bridge) => {
356
399
  const emitter2 = createEvents();
357
400
  window.webEmitter = emitter2;
358
401
  for (const [funcName, func] of bridgeEntries) {
359
- const $func = async (eventId, args) => {
402
+ const $func = (eventId, args) => __async(void 0, null, function* () {
403
+ var _a, _b;
360
404
  try {
361
- const value = await func(...args);
362
- window.ReactNativeWebView?.postMessage(
405
+ const value = yield func(...args);
406
+ (_a = window.ReactNativeWebView) == null ? void 0 : _a.postMessage(
363
407
  JSON.stringify({
364
408
  type: "webMethodResponse",
365
409
  body: { funcName, eventId, value }
366
410
  })
367
411
  );
368
412
  } catch (e) {
369
- window.ReactNativeWebView?.postMessage(
413
+ (_b = window.ReactNativeWebView) == null ? void 0 : _b.postMessage(
370
414
  JSON.stringify({
371
415
  type: "webMethodError",
372
416
  body: { funcName, eventId, error: JSON.stringify(e) }
373
417
  })
374
418
  );
375
419
  }
376
- };
420
+ });
377
421
  emitter2.on(funcName, $func);
378
422
  }
379
423
  const register = () => {
380
- window.ReactNativeWebView?.postMessage(
424
+ var _a;
425
+ (_a = window.ReactNativeWebView) == null ? void 0 : _a.postMessage(
381
426
  JSON.stringify({
382
427
  type: "registerWebMethod",
383
428
  body: { bridgeNames }
@@ -390,8 +435,9 @@ var registerWebMethod = (bridge) => {
390
435
  return bridge;
391
436
  }
392
437
  document.addEventListener("visibilitychange", () => {
438
+ var _a;
393
439
  if (document.visibilityState === "visible") {
394
- window.ReactNativeWebView?.postMessage(
440
+ (_a = window.ReactNativeWebView) == null ? void 0 : _a.postMessage(
395
441
  JSON.stringify({
396
442
  type: "registerWebMethod",
397
443
  body: { bridgeNames }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@webview-bridge/web",
3
3
  "type": "module",
4
- "version": "1.5.0",
4
+ "version": "1.5.1-rc.0",
5
5
  "description": "Fully Type-Safe Integration for React Native WebView and Web",
6
6
  "publishConfig": {
7
7
  "access": "public"