@vbotma/bridge 2.2.5 → 2.2.7

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.js CHANGED
@@ -1,35 +1,35 @@
1
- import { is as L, looseObject as m, function as Y, nullish as v, boolean as j, number as U, string as E, optional as I, unknown as me, parse as B, any as we } from "valibot";
2
- import { throwifyFpFn as A, getStorageValue as de, setStorageValue as X, createLogger as be, throwifyAnyEither as C, createCbCollector as Z, BetterTaskEither as ee } from "@vbotma/toolkit";
3
- import { createLogger as yt, deepSnakeToCamelObjKeys as vt } from "@vbotma/toolkit";
4
- import { BetterPromise as te, TimeoutError as he } from "better-promises";
5
- import { CancelledError as Pt, TimeoutError as kt } from "better-promises";
6
- import { function as l, either as c, option as F, taskEither as w, json as ge } from "fp-ts";
7
- import { errorClassWithData as ye, errorClass as k } from "error-kid";
8
- import { parseLaunchParamsQueryFp as re, themeParams as ve, pipeJsonToSchema as ne, miniAppsMessage as oe, serializeLaunchParamsQuery as Ee, parseLaunchParamsQuery as Pe } from "@vbotma/transformers";
9
- import { signal as h, computed as ae } from "@vbotma/signals";
10
- import ke from "mitt";
11
- function se(e) {
12
- return L(
13
- m({ VBotWebviewProxy: m({ postEvent: Y() }) }),
1
+ import { is as O, looseObject as w, function as X, nullish as y, boolean as U, number as I, string as E, optional as $, unknown as me, parse as L, any as be } from "valibot";
2
+ import { throwifyFpFn as A, getStorageValue as he, setStorageValue as Z, createLogger as ge, throwifyAnyEither as C, createCbCollector as ee, BetterTaskEither as te } from "@vbotma/toolkit";
3
+ import { createLogger as Pt, deepSnakeToCamelObjKeys as kt } from "@vbotma/toolkit";
4
+ import { BetterPromise as re, TimeoutError as ve } from "better-promises";
5
+ import { CancelledError as St, TimeoutError as Bt } from "better-promises";
6
+ import { function as l, either as c, option as F, taskEither as d, json as ye } from "fp-ts";
7
+ import { errorClassWithData as Ee, errorClass as x } from "error-kid";
8
+ import { parseLaunchParamsQueryFp as ne, themeParams as Pe, pipeJsonToSchema as oe, miniAppsMessage as ae, serializeLaunchParamsQuery as ke, parseLaunchParamsQuery as xe } from "@vbotma/transformers";
9
+ import { signal as h, computed as se } from "@vbotma/signals";
10
+ import Se from "mitt";
11
+ function ie(e) {
12
+ return O(
13
+ w({ VBotWebviewProxy: w({ postEvent: X() }) }),
14
14
  e
15
15
  );
16
16
  }
17
- function ie() {
17
+ function pe() {
18
18
  try {
19
19
  return window.self !== window.top;
20
20
  } catch {
21
21
  return !0;
22
22
  }
23
23
  }
24
- class xe extends (/* @__PURE__ */ k("MethodUnsupportedError", (t, r) => [
24
+ class Be extends (/* @__PURE__ */ x("MethodUnsupportedError", (t, r) => [
25
25
  `Method "${t}" is unsupported in Mini Apps version ${r}`
26
26
  ])) {
27
27
  }
28
- class Se extends (/* @__PURE__ */ k("MethodParameterUnsupportedError", (t, r, n) => [
28
+ class Ae extends (/* @__PURE__ */ x("MethodParameterUnsupportedError", (t, r, n) => [
29
29
  `Parameter "${r}" of "${t}" method is unsupported in Mini Apps version ${n}`
30
30
  ])) {
31
31
  }
32
- class Ae extends (/* @__PURE__ */ ye(
32
+ class Ce extends (/* @__PURE__ */ Ee(
33
33
  "LaunchParamsRetrieveError",
34
34
  (t) => ({ errors: t }),
35
35
  (t) => [
@@ -45,30 +45,30 @@ class Ae extends (/* @__PURE__ */ ye(
45
45
  ]
46
46
  )) {
47
47
  }
48
- class Ce extends (/* @__PURE__ */ k("InvalidLaunchParamsError", (t, r) => [
48
+ class Me extends (/* @__PURE__ */ x("InvalidLaunchParamsError", (t, r) => [
49
49
  `Invalid value for launch params: ${t}`,
50
50
  { cause: r }
51
51
  ])) {
52
52
  }
53
- class pe extends (/* @__PURE__ */ k("UnknownEnvError")) {
53
+ class ce extends (/* @__PURE__ */ x("UnknownEnvError")) {
54
54
  }
55
- class Me extends (/* @__PURE__ */ k(
55
+ class Ve extends (/* @__PURE__ */ x(
56
56
  "InvokeCustomMethodError",
57
57
  (t) => [`Server returned error: ${t}`]
58
58
  )) {
59
59
  }
60
- const $ = "launchParams";
61
- function z(e) {
60
+ const z = "launchParams";
61
+ function G(e) {
62
62
  return e.replace(/^[^?#]*[?#]/, "").replace(/[?#]/g, "&");
63
63
  }
64
- function qe() {
64
+ function We() {
65
65
  return l.pipe(
66
66
  M(),
67
- c.chainW(re)
67
+ c.chainW(ne)
68
68
  );
69
69
  }
70
- const tt = A(qe);
71
- function Ve() {
70
+ const rt = A(We);
71
+ function qe() {
72
72
  return l.pipe(
73
73
  M(),
74
74
  c.map((e) => {
@@ -77,9 +77,9 @@ function Ve() {
77
77
  })
78
78
  );
79
79
  }
80
- function rt() {
80
+ function nt() {
81
81
  return l.pipe(
82
- Ve(),
82
+ qe(),
83
83
  c.fold((e) => {
84
84
  throw e;
85
85
  }, (e) => e),
@@ -93,14 +93,14 @@ function M() {
93
93
  // Try to retrieve launch parameters from the current location. This method
94
94
  // can return nothing in case, location was changed, and then the page was
95
95
  // reloaded.
96
- [() => z(window.location.href), "window.location.href"],
96
+ [() => G(window.location.href), "window.location.href"],
97
97
  // Then, try using the lower level API - window.performance.
98
98
  [() => {
99
99
  const n = performance.getEntriesByType("navigation")[0];
100
- return n && z(n.name);
100
+ return n && G(n.name);
101
101
  }, "performance navigation entries"],
102
102
  // Finally, try using the session storage.
103
- [() => de($), "local storage"]
103
+ [() => he(z), "local storage"]
104
104
  ]) {
105
105
  const n = t();
106
106
  if (!n) {
@@ -108,43 +108,43 @@ function M() {
108
108
  continue;
109
109
  }
110
110
  const a = l.pipe(
111
- re(n),
111
+ ne(n),
112
112
  c.foldW((o) => o, () => !0)
113
113
  );
114
114
  if (typeof a != "boolean") {
115
115
  e.push({ source: r, error: a });
116
116
  continue;
117
117
  }
118
- return X($, n), c.right(n);
118
+ return Z(z, n), c.right(n);
119
119
  }
120
- return c.left(new Ae(e));
120
+ return c.left(new Ce(e));
121
121
  }
122
- const nt = A(M);
123
- function We(e, t) {
124
- const r = /* @__PURE__ */ new Map(), n = ke(), a = (o, s, p = !1) => {
122
+ const ot = A(M);
123
+ function _e(e, t) {
124
+ const r = /* @__PURE__ */ new Map(), n = Se(), a = (o, s, i = !1) => {
125
125
  const _ = r.get(o) || /* @__PURE__ */ new Map();
126
126
  r.set(o, _);
127
- const i = _.get(s) || [];
128
- _.set(s, i);
129
- const u = i.findIndex((f) => f[1] === p);
130
- if (u >= 0 && (n.off(o, i[u][0]), i.splice(u, 1), !i.length && (_.delete(s), !_.size))) {
127
+ const p = _.get(s) || [];
128
+ _.set(s, p);
129
+ const u = p.findIndex((f) => f[1] === i);
130
+ if (u >= 0 && (n.off(o, p[u][0]), p.splice(u, 1), !p.length && (_.delete(s), !_.size))) {
131
131
  const f = r.size;
132
132
  r.delete(o), f && !r.size && t();
133
133
  }
134
134
  };
135
135
  return {
136
- on(o, s, p) {
136
+ on(o, s, i) {
137
137
  !r.size && e();
138
138
  const _ = () => {
139
- a(o, s, p);
140
- }, i = (...d) => {
141
- p && _(), o === "*" ? s({ name: d[0], payload: d[1] }) : s(...d);
139
+ a(o, s, i);
140
+ }, p = (...m) => {
141
+ i && _(), o === "*" ? s({ name: m[0], payload: m[1] }) : s(...m);
142
142
  };
143
- n.on(o, i);
143
+ n.on(o, p);
144
144
  const u = r.get(o) || /* @__PURE__ */ new Map();
145
145
  r.set(o, u);
146
146
  const f = u.get(s) || [];
147
- return u.set(s, f), f.push([i, p || !1]), _;
147
+ return u.set(s, f), f.push([p, i || !1]), _;
148
148
  },
149
149
  off: a,
150
150
  // eslint-disable-next-line @typescript-eslint/unbound-method
@@ -155,7 +155,7 @@ function We(e, t) {
155
155
  }
156
156
  };
157
157
  }
158
- function V(e, t) {
158
+ function W(e, t) {
159
159
  window.dispatchEvent(new MessageEvent("message", {
160
160
  data: JSON.stringify({ eventType: e, eventData: t }),
161
161
  // We specify this kind of source here in order to allow the package's "on" function to
@@ -164,66 +164,66 @@ function V(e, t) {
164
164
  source: window.parent
165
165
  }));
166
166
  }
167
- const S = h(!1), T = h("https://web.telegram.org"), Be = (e) => {
167
+ const B = h(!1), T = h("https://web.telegram.org"), Le = (e) => {
168
168
  b().log("Event received:", e);
169
- }, ce = ae(S);
170
- function ot(e) {
171
- e !== S() && (S.set(e), (e ? O : Le)("*", Be));
172
- }
173
- const _e = ae(T);
169
+ }, ue = se(B);
174
170
  function at(e) {
171
+ e !== B() && (B.set(e), (e ? R : Oe)("*", Le));
172
+ }
173
+ const fe = se(T);
174
+ function st(e) {
175
175
  T.set(e), b().log("New target origin set", e);
176
176
  }
177
177
  const P = h((...e) => {
178
178
  window.parent.postMessage(...e);
179
- }), b = h(be("Bridge", {
179
+ }), b = h(ge("Bridge", {
180
180
  bgColor: "#9147ff",
181
181
  textColor: "white",
182
- shouldLog: ce
182
+ shouldLog: ue
183
183
  }));
184
- function st() {
185
- Oe(), [P, T, _e, S, ce, b].forEach((e) => {
184
+ function it() {
185
+ Re(), [P, T, fe, B, ue, b].forEach((e) => {
186
186
  e.unsubAll(), "reset" in e && e.reset();
187
187
  });
188
188
  }
189
- function W(e, t, r) {
189
+ function q(e, t, r) {
190
190
  const n = [r], a = e[t];
191
191
  typeof a == "function" && n.push(a);
192
- const o = (...p) => {
193
- n.forEach((_) => _(...p));
194
- }, s = Object.assign((...p) => {
195
- o(...p);
192
+ const o = (...i) => {
193
+ n.forEach((_) => _(...i));
194
+ }, s = Object.assign((...i) => {
195
+ o(...i);
196
196
  }, {
197
197
  // Unwraps the composer.
198
198
  unwrap() {
199
- const { length: p } = n;
200
- if (p === 1) {
199
+ const { length: i } = n;
200
+ if (i === 1) {
201
201
  delete e[t];
202
202
  return;
203
203
  }
204
- if (p === 2) {
205
- D(e, t, n[1]);
204
+ if (i === 2) {
205
+ H(e, t, n[1]);
206
206
  return;
207
207
  }
208
- n.unshift(1), D(e, t, o);
208
+ n.unshift(1), H(e, t, o);
209
209
  }
210
210
  });
211
- ue(
211
+ le(
212
212
  e,
213
213
  t,
214
214
  () => s,
215
- (p) => n.push(p)
215
+ (i) => n.push(i)
216
216
  );
217
217
  }
218
- function G(e, t) {
218
+ function D(e, t) {
219
219
  const r = e[t];
220
- ue(e, t, () => r, (n) => {
220
+ le(e, t, () => r, (n) => {
221
221
  Object.entries(n).forEach(([a, o]) => {
222
222
  r[a] = o;
223
223
  });
224
224
  });
225
225
  }
226
- function ue(e, t, r, n) {
226
+ function le(e, t, r, n) {
227
227
  Object.defineProperty(e, t, {
228
228
  enumerable: !0,
229
229
  configurable: !0,
@@ -231,7 +231,7 @@ function ue(e, t, r, n) {
231
231
  set: n
232
232
  });
233
233
  }
234
- function D(e, t, r) {
234
+ function H(e, t, r) {
235
235
  Object.defineProperty(e, t, {
236
236
  enumerable: !0,
237
237
  configurable: !0,
@@ -240,26 +240,26 @@ function D(e, t, r) {
240
240
  });
241
241
  }
242
242
  const Fe = {
243
- clipboard_text_received: m({
243
+ clipboard_text_received: w({
244
244
  req_id: E(),
245
- data: v(E())
245
+ data: y(E())
246
246
  }),
247
- custom_method_invoked: m({
247
+ custom_method_invoked: w({
248
248
  req_id: E(),
249
- result: I(me()),
250
- error: I(E())
249
+ result: $(me()),
250
+ error: $(E())
251
251
  }),
252
- popup_closed: v(
253
- m({ button_id: v(E(), () => {
252
+ popup_closed: y(
253
+ w({ button_id: y(E(), () => {
254
254
  }) }),
255
255
  {}
256
256
  ),
257
- viewport_changed: v(
258
- m({
259
- height: U(),
260
- width: v(U(), () => window.innerWidth),
261
- is_state_stable: j(),
262
- is_expanded: j()
257
+ viewport_changed: y(
258
+ w({
259
+ height: I(),
260
+ width: y(I(), () => window.innerWidth),
261
+ is_state_stable: U(),
262
+ is_expanded: U()
263
263
  }),
264
264
  // TODO: At the moment, macOS has a bug with the invalid event payload - it is always equal to
265
265
  // null. Leaving this default value until the bug is fixed.
@@ -269,8 +269,8 @@ const Fe = {
269
269
  is_expanded: !0
270
270
  })
271
271
  ),
272
- theme_changed: m({
273
- theme_params: ve()
272
+ theme_changed: w({
273
+ theme_params: Pe()
274
274
  })
275
275
  };
276
276
  function J(e) {
@@ -278,14 +278,14 @@ function J(e) {
278
278
  return;
279
279
  let t;
280
280
  try {
281
- t = B(ne(oe()), e.data);
281
+ t = L(oe(ae()), e.data);
282
282
  } catch {
283
283
  return;
284
284
  }
285
285
  const { eventType: r, eventData: n } = t, a = Fe[r];
286
286
  let o;
287
287
  try {
288
- o = a ? B(a, n) : n;
288
+ o = a ? L(a, n) : n;
289
289
  } catch (s) {
290
290
  return b().forceError(
291
291
  [
@@ -301,20 +301,20 @@ function J(e) {
301
301
  Te(r, o);
302
302
  }
303
303
  const {
304
- on: O,
305
- off: Le,
304
+ on: R,
305
+ off: Oe,
306
306
  emit: Te,
307
- clear: Oe
308
- } = We(
307
+ clear: Re
308
+ } = _e(
309
309
  () => {
310
310
  const e = window;
311
- !e.TelegramGameProxy && (e.TelegramGameProxy = {}), W(e.TelegramGameProxy, "receiveEvent", V), G(e, "TelegramGameProxy"), !e.Telegram && (e.Telegram = {}), !e.VBot.WebView && (e.VBot.WebView = {}), W(e.VBot.WebView, "receiveEvent", V), G(e.Telegram, "WebView"), W(e, "TelegramGameProxy_receiveEvent", V), window.addEventListener("message", J);
311
+ !e.TelegramGameProxy && (e.TelegramGameProxy = {}), q(e.TelegramGameProxy, "receiveEvent", W), D(e, "TelegramGameProxy"), !e.VBot && (e.VBot = {}), !e.VBot.WebView && (e.VBot.WebView = {}), q(e.VBot.WebView, "receiveEvent", W), D(e.VBot, "WebView"), q(e, "TelegramGameProxy_receiveEvent", W), window.addEventListener("message", J);
312
312
  },
313
313
  () => {
314
314
  [
315
315
  ["TelegramGameProxy_receiveEvent"],
316
316
  ["TelegramGameProxy", "receiveEvent"],
317
- ["Telegram", "WebView", "receiveEvent"]
317
+ ["VBot", "WebView", "receiveEvent"]
318
318
  ].forEach((e) => {
319
319
  const t = window;
320
320
  let r = [void 0, t];
@@ -325,10 +325,10 @@ const {
325
325
  "unwrap" in a && (a.unwrap(), n && n !== t && !Object.keys(n).length && delete t[e[0]]);
326
326
  }), window.removeEventListener("message", J);
327
327
  }
328
- ), Re = (...e) => P()(...e);
329
- function je(e, t) {
328
+ ), je = (...e) => P()(...e);
329
+ function Ue(e, t) {
330
330
  l.pipe(
331
- x(
331
+ S(
332
332
  // @ts-expect-error It's ok, TS can't determine a specific override.
333
333
  e,
334
334
  t
@@ -338,49 +338,49 @@ function je(e, t) {
338
338
  })
339
339
  );
340
340
  }
341
- function x(e, t) {
341
+ function S(e, t) {
342
342
  b().log("Posting event:", t ? { eventType: e, eventData: t } : { eventType: e });
343
343
  const r = window, n = JSON.stringify({ eventType: e, eventData: t });
344
- return ie() ? (Re(n, _e()), c.right(void 0)) : se(r) ? (r.VBotWebviewProxy.postEvent(e, JSON.stringify(t)), c.right(void 0)) : L(m({ external: m({ notify: Y() }) }), r) ? (r.external.notify(n), c.right(void 0)) : c.left(new pe());
344
+ return pe() ? (je(n, fe()), c.right(void 0)) : ie(r) ? (r.VBotWebviewProxy.postEvent(e, JSON.stringify(t)), c.right(void 0)) : O(w({ external: w({ notify: X() }) }), r) ? (r.external.notify(n), c.right(void 0)) : c.left(new ce());
345
345
  }
346
- function R(e, t, r = {}) {
346
+ function j(e, t, r = {}) {
347
347
  const {
348
348
  // If no capture function was passed, we capture the first compatible event.
349
349
  capture: n = () => !0,
350
- postEvent: a = x
351
- } = r, o = h(), [s, p] = Z();
352
- (Array.isArray(t) ? t : [t]).forEach((i) => {
350
+ postEvent: a = S
351
+ } = r, o = h(), [s, i] = ee();
352
+ (Array.isArray(t) ? t : [t]).forEach((p) => {
353
353
  s(
354
- O(i, (u) => {
354
+ R(p, (u) => {
355
355
  const f = Array.isArray(t);
356
- n(f ? { event: i, payload: u } : u) && o.set([
357
- f ? { event: i, payload: u } : u
356
+ n(f ? { event: p, payload: u } : u) && o.set([
357
+ f ? { event: p, payload: u } : u
358
358
  ]);
359
359
  })
360
360
  );
361
361
  });
362
- const _ = (i) => (p(), i);
362
+ const _ = (p) => (i(), p);
363
363
  return l.pipe(
364
364
  async () => a(e, r.params),
365
- w.chainW(() => ee((i, u, f) => {
366
- const d = o();
367
- if (d)
368
- return i(d[0]);
369
- const g = (y) => {
370
- y && i(y[0]);
371
- }, q = () => {
365
+ d.chainW(() => te((p, u, f) => {
366
+ const m = o();
367
+ if (m)
368
+ return p(m[0]);
369
+ const g = (v) => {
370
+ v && p(v[0]);
371
+ }, V = () => {
372
372
  o.unsub(g);
373
373
  };
374
- o.sub(g), f.on("finalized", q);
374
+ o.sub(g), f.on("finalized", V);
375
375
  }, r)),
376
- w.mapBoth(_, _)
376
+ d.mapBoth(_, _)
377
377
  );
378
378
  }
379
- function it(e, t, r) {
379
+ function pt(e, t, r) {
380
380
  const { postEvent: n } = r || {};
381
381
  return C(
382
382
  // @ts-expect-error TypeScript will not be able to handle our overrides here.
383
- R(e, t, {
383
+ j(e, t, {
384
384
  ...r,
385
385
  postEvent: n ? (...a) => {
386
386
  try {
@@ -388,60 +388,60 @@ function it(e, t, r) {
388
388
  } catch (o) {
389
389
  return c.left(o);
390
390
  }
391
- } : x
391
+ } : S
392
392
  })
393
393
  );
394
394
  }
395
- function pt(e, t) {
396
- const r = Ue(
395
+ function ct(e, t) {
396
+ const r = Ie(
397
397
  // @ts-expect-error TS doesn't get what override we are going to use.
398
398
  e,
399
399
  t
400
400
  );
401
- return typeof r == "function" ? te.fn(() => C(r)) : r;
401
+ return typeof r == "function" ? re.fn(() => C(r)) : r;
402
402
  }
403
- function Ue(e, t) {
404
- const r = se(window);
403
+ function Ie(e, t) {
404
+ const r = ie(window);
405
405
  if (!e)
406
406
  return r || l.pipe(M(), c.match(() => !1, () => !0));
407
407
  if (r)
408
- return w.right(!0);
408
+ return d.right(!0);
409
409
  const { timeout: n = 100 } = t || {};
410
410
  return l.pipe(
411
- R("web_app_request_theme", "theme_changed", { ...t, timeout: n }),
412
- w.match(
413
- (a) => he.is(a) || pe.is(a) ? c.right(!1) : c.left(a),
411
+ j("web_app_request_theme", "theme_changed", { ...t, timeout: n }),
412
+ d.match(
413
+ (a) => ve.is(a) || ce.is(a) ? c.right(!1) : c.left(a),
414
414
  () => c.right(!0)
415
415
  )
416
416
  );
417
417
  }
418
- function ct({ launchParams: e, onEvent: t, resetPostMessage: r } = {}) {
418
+ function _t({ launchParams: e, onEvent: t, resetPostMessage: r } = {}) {
419
419
  if (e) {
420
420
  const o = typeof e == "string" || e instanceof URLSearchParams ? e.toString() : (
421
421
  // Here we have to trick serializeLaunchParamsQuery into thinking, it serializes a valid
422
422
  // value. We are doing it because we are working with vbWebAppData presented as a
423
423
  // string, not an object as serializeLaunchParamsQuery requires.
424
- Ee({ ...e, vbWebAppData: void 0 }) + (e.vbWebAppData ? `&vbWebAppData=${encodeURIComponent(e.vbWebAppData.toString())}` : "")
424
+ ke({ ...e, vbWebAppData: void 0 }) + (e.vbWebAppData ? `&vbWebAppData=${encodeURIComponent(e.vbWebAppData.toString())}` : "")
425
425
  );
426
426
  try {
427
- Pe(o);
427
+ xe(o);
428
428
  } catch (s) {
429
- throw new Ce(o, s);
429
+ throw new Me(o, s);
430
430
  }
431
- X("launchParams", o);
431
+ Z("launchParams", o);
432
432
  }
433
- if (ie()) {
433
+ if (pe()) {
434
434
  if (!t)
435
435
  return;
436
436
  r && P.reset();
437
437
  const o = P();
438
438
  P.set((...s) => {
439
- const [p] = s, _ = () => {
439
+ const [i] = s, _ = () => {
440
440
  o(...s);
441
441
  };
442
442
  try {
443
- const i = B(ne(oe()), p);
444
- t({ name: i.eventType, params: i.eventData }, _);
443
+ const p = L(oe(ae()), i);
444
+ t({ name: p.eventType, params: p.eventData }, _);
445
445
  } catch {
446
446
  _();
447
447
  }
@@ -453,17 +453,54 @@ function ct({ launchParams: e, onEvent: t, resetPostMessage: r } = {}) {
453
453
  window.VBotWebviewProxy = {
454
454
  ...n,
455
455
  postEvent(o, s) {
456
- const p = () => {
456
+ const i = () => {
457
457
  a(o, s);
458
458
  };
459
459
  t ? t({
460
460
  name: o,
461
461
  params: s ? JSON.parse(s) : void 0
462
- }, p) : p();
462
+ }, i) : i();
463
463
  }
464
464
  }, b().log("Environment was mocked by the mockVBotEnv function");
465
465
  }
466
- function Ie(e) {
466
+ function ut(e, t = "*") {
467
+ const r = (o) => {
468
+ if (o.source === e.contentWindow)
469
+ try {
470
+ const s = JSON.parse(o.data), { eventType: i, eventData: _ } = s;
471
+ n.emit(i, _);
472
+ } catch {
473
+ }
474
+ }, n = _e(
475
+ () => {
476
+ window.addEventListener("message", r);
477
+ },
478
+ () => {
479
+ window.removeEventListener("message", r);
480
+ }
481
+ ), a = {
482
+ // Spread the emitter methods: on, off, emit, clear
483
+ ...n,
484
+ send: (o, s) => {
485
+ if (e.contentWindow) {
486
+ const i = JSON.stringify({ eventType: o, eventData: s });
487
+ e.contentWindow.postMessage(i, t);
488
+ }
489
+ },
490
+ // We need to override clear to also remove the window listener
491
+ clear: () => {
492
+ n.clear(), k === a && (k = void 0);
493
+ }
494
+ };
495
+ return k = a, a;
496
+ }
497
+ let k;
498
+ function ft() {
499
+ if (!k)
500
+ throw new Error("HostBridge not initialized. Call defineHostBridge() first.");
501
+ return k;
502
+ }
503
+ function $e(e) {
467
504
  return ({ req_id: t }) => t === e;
468
505
  }
469
506
  const N = {
@@ -561,49 +598,49 @@ const N = {
561
598
  function Q(e, t) {
562
599
  return Object.keys(N).find((n) => N[n].some((a) => t ? typeof a == "object" && a.method === e && a.param === t : a === e)) || null;
563
600
  }
564
- function H(e) {
601
+ function K(e) {
565
602
  return e.split(".").map(Number);
566
603
  }
567
- function $e(e, t) {
568
- const r = H(e), n = H(t), a = Math.max(r.length, n.length);
604
+ function ze(e, t) {
605
+ const r = K(e), n = K(t), a = Math.max(r.length, n.length);
569
606
  for (let o = 0; o < a; o += 1) {
570
- const s = r[o] || 0, p = n[o] || 0;
571
- if (s !== p)
572
- return s > p ? 1 : -1;
607
+ const s = r[o] || 0, i = n[o] || 0;
608
+ if (s !== i)
609
+ return s > i ? 1 : -1;
573
610
  }
574
611
  return 0;
575
612
  }
576
- function K(e, t, r) {
613
+ function Y(e, t, r) {
577
614
  const n = r ? Q(
578
615
  e,
579
616
  t
580
617
  ) : Q(e);
581
- return n ? $e(n, r || t) <= 0 : !1;
618
+ return n ? ze(n, r || t) <= 0 : !1;
582
619
  }
583
- function _t(e, t = "strict") {
620
+ function lt(e, t = "strict") {
584
621
  const r = typeof t == "function" ? t : (n) => {
585
- const { method: a, version: o } = n, s = "param" in n ? new Se(a, n.param, o) : new xe(a, o);
622
+ const { method: a, version: o } = n, s = "param" in n ? new Ae(a, n.param, o) : new Be(a, o);
586
623
  if (t === "strict")
587
624
  throw s;
588
625
  return b().forceWarn(s.message);
589
626
  };
590
- return ((n, a) => K(n, e) ? n === "web_app_set_header_color" && L(m({ color: we() }), a) && !K(n, "color", e) ? r({ version: e, method: n, param: "color" }) : je(n, a) : r({ version: e, method: n }));
627
+ return ((n, a) => Y(n, e) ? n === "web_app_set_header_color" && O(w({ color: be() }), a) && !Y(n, "color", e) ? r({ version: e, method: n, param: "color" }) : Ue(n, a) : r({ version: e, method: n }));
591
628
  }
592
- function ze(e, t, r, n) {
629
+ function Ge(e, t, r, n) {
593
630
  return l.pipe(
594
- R("web_app_invoke_custom_method", "custom_method_invoked", {
631
+ j("web_app_invoke_custom_method", "custom_method_invoked", {
595
632
  ...n || {},
596
633
  params: { method: e, params: t, req_id: r },
597
- capture: Ie(r)
634
+ capture: $e(r)
598
635
  }),
599
- w.chain(({ result: a, error: o }) => o ? w.left(new Me(o)) : w.right(a))
636
+ d.chain(({ result: a, error: o }) => o ? d.left(new Ve(o)) : d.right(a))
600
637
  );
601
638
  }
602
- function ut(e, t, r, n) {
603
- return te.fn(() => l.pipe(
639
+ function wt(e, t, r, n) {
640
+ return re.fn(() => l.pipe(
604
641
  // @ts-expect-error TypeScript is unable to determine required override.
605
- ze(e, t, r, n),
606
- w.match(
642
+ Ge(e, t, r, n),
643
+ d.match(
607
644
  (a) => {
608
645
  throw a;
609
646
  },
@@ -611,41 +648,41 @@ function ut(e, t, r, n) {
611
648
  )
612
649
  )());
613
650
  }
614
- function Ge(e, t, r = {}) {
651
+ function De(e, t, r = {}) {
615
652
  const {
616
653
  // If no capture function was passed, we capture the first compatible event.
617
654
  capture: n = () => !0,
618
- postEvent: a = x
619
- } = r, o = h(), [s, p] = Z();
620
- (Array.isArray(t) ? t : [t]).forEach((i) => {
655
+ postEvent: a = S
656
+ } = r, o = h(), [s, i] = ee();
657
+ (Array.isArray(t) ? t : [t]).forEach((p) => {
621
658
  s(
622
- O(i, (u) => {
623
- (Array.isArray(t) ? n({ event: i, payload: u }) : n(u)) && o.set([u]);
659
+ R(p, (u) => {
660
+ (Array.isArray(t) ? n({ event: p, payload: u }) : n(u)) && o.set([u]);
624
661
  })
625
662
  );
626
663
  });
627
- const _ = (i) => (p(), i);
664
+ const _ = (p) => (i(), p);
628
665
  return l.pipe(
629
666
  async () => a(e, r.params),
630
- w.chainW(() => ee((i, u, f) => {
631
- const d = o();
632
- if (d)
633
- return i(d[0]);
634
- const g = (y) => {
635
- y && i(y[0]);
636
- }, q = () => {
667
+ d.chainW(() => te((p, u, f) => {
668
+ const m = o();
669
+ if (m)
670
+ return p(m[0]);
671
+ const g = (v) => {
672
+ v && p(v[0]);
673
+ }, V = () => {
637
674
  o.unsub(g);
638
675
  };
639
- o.sub(g), f.on("finalized", q);
676
+ o.sub(g), f.on("finalized", V);
640
677
  }, r)),
641
- w.mapBoth(_, _)
678
+ d.mapBoth(_, _)
642
679
  );
643
680
  }
644
- function ft(e, t, r) {
681
+ function dt(e, t, r) {
645
682
  const { postEvent: n } = r || {};
646
683
  return C(
647
684
  // @ts-expect-error TypeScript will not be able to handle our overrides here.
648
- Ge(e, t, {
685
+ De(e, t, {
649
686
  ...r,
650
687
  postEvent: n ? (...a) => {
651
688
  try {
@@ -653,34 +690,34 @@ function ft(e, t, r) {
653
690
  } catch (o) {
654
691
  return c.left(o);
655
692
  }
656
- } : x
693
+ } : S
657
694
  })
658
695
  );
659
696
  }
660
- function lt() {
697
+ function mt() {
661
698
  Object.hasOwn || (Object.hasOwn = function(e, t) {
662
699
  return Object.prototype.hasOwnProperty.call(e, t);
663
700
  });
664
701
  }
665
- function fe(e) {
702
+ function we(e) {
666
703
  return c.tryCatch(() => decodeURIComponent(
667
704
  atob(
668
705
  e.replace(/-/g, "+").replace(/_/g, "/")
669
706
  ).split("").map((t) => "%" + ("00" + t.charCodeAt(0).toString(16)).slice(-2)).join("")
670
707
  ), (t) => t);
671
708
  }
672
- const mt = A(fe);
673
- function le(e) {
709
+ const bt = A(we);
710
+ function de(e) {
674
711
  return btoa(
675
712
  encodeURIComponent(e).replace(/%([0-9A-F]{2})/g, (t, r) => String.fromCharCode(parseInt(`0x${r}`)))
676
713
  ).replace(/\+/g, "-").replace(/\//g, "_");
677
714
  }
678
- function De(e) {
679
- const t = le(typeof e == "string" ? e : JSON.stringify(e));
715
+ function He(e) {
716
+ const t = de(typeof e == "string" ? e : JSON.stringify(e));
680
717
  return t.length > 512 ? c.left(new Error("Value is too long for start parameter")) : c.right(t);
681
718
  }
682
- const wt = A(De);
683
- function dt(e, t) {
719
+ const ht = A(He);
720
+ function gt(e, t) {
684
721
  return C(
685
722
  Je(
686
723
  e,
@@ -691,68 +728,70 @@ function dt(e, t) {
691
728
  }
692
729
  function Je(e, t) {
693
730
  return l.pipe(
694
- fe(e),
695
- c.chain((r) => t ? typeof t == "function" ? t(r) : ge.parse(r) : c.right(r))
731
+ we(e),
732
+ c.chain((r) => t ? typeof t == "function" ? t(r) : ye.parse(r) : c.right(r))
696
733
  );
697
734
  }
698
- function bt(e) {
699
- return le(e).length <= 512;
735
+ function vt(e) {
736
+ return de(e).length <= 512;
700
737
  }
701
738
  export {
702
- Pt as CancelledError,
703
- Ce as InvalidLaunchParamsError,
704
- Me as InvokeCustomMethodFailedError,
705
- Ae as LaunchParamsRetrieveError,
706
- Se as MethodParameterUnsupportedError,
707
- xe as MethodUnsupportedError,
708
- kt as TimeoutError,
709
- pe as UnknownEnvError,
710
- lt as applyPolyfills,
711
- Ie as captureSameReq,
712
- $e as compareVersions,
713
- yt as createLogger,
714
- _t as createPostEvent,
715
- wt as createStartParam,
716
- De as createStartParamFp,
717
- ce as debug,
718
- mt as decodeBase64Url,
719
- fe as decodeBase64UrlFp,
720
- dt as decodeStartParam,
739
+ St as CancelledError,
740
+ Me as InvalidLaunchParamsError,
741
+ Ve as InvokeCustomMethodFailedError,
742
+ Ce as LaunchParamsRetrieveError,
743
+ Ae as MethodParameterUnsupportedError,
744
+ Be as MethodUnsupportedError,
745
+ Bt as TimeoutError,
746
+ ce as UnknownEnvError,
747
+ mt as applyPolyfills,
748
+ $e as captureSameReq,
749
+ ze as compareVersions,
750
+ Pt as createLogger,
751
+ lt as createPostEvent,
752
+ ht as createStartParam,
753
+ He as createStartParamFp,
754
+ ue as debug,
755
+ bt as decodeBase64Url,
756
+ we as decodeBase64UrlFp,
757
+ gt as decodeStartParam,
721
758
  Je as decodeStartParamFp,
722
- vt as deepSnakeToCamelObjKeys,
723
- V as emitEvent,
724
- le as encodeBase64Url,
759
+ kt as deepSnakeToCamelObjKeys,
760
+ ut as defineHostBridge,
761
+ W as emitEvent,
762
+ de as encodeBase64Url,
763
+ ft as getHostBridge,
725
764
  Q as getReleaseVersion,
726
- se as hasWebviewProxy,
727
- ut as invokeCustomMethod,
728
- ze as invokeCustomMethodFp,
729
- ie as isIframe,
730
- bt as isSafeToCreateStartParam,
731
- pt as isVBMA,
732
- Ue as isVBMAFp,
765
+ ie as hasWebviewProxy,
766
+ wt as invokeCustomMethod,
767
+ Ge as invokeCustomMethodFp,
768
+ pe as isIframe,
769
+ vt as isSafeToCreateStartParam,
770
+ ct as isVBMA,
771
+ Ie as isVBMAFp,
733
772
  b as logger,
734
- ct as mockVBotEnv,
735
- Le as off,
736
- Oe as offAll,
737
- O as on,
738
- je as postEvent,
739
- x as postEventFp,
740
- Re as postMessage,
773
+ _t as mockVBotEnv,
774
+ Oe as off,
775
+ Re as offAll,
776
+ R as on,
777
+ Ue as postEvent,
778
+ S as postEventFp,
779
+ je as postMessage,
741
780
  P as postMessageImpl,
742
- ft as request,
743
- it as request2,
744
- R as request2Fp,
745
- Ge as requestFp,
746
- st as resetGlobals,
747
- tt as retrieveLaunchParams,
748
- qe as retrieveLaunchParamsFp,
749
- rt as retrieveRawInitData,
750
- Ve as retrieveRawInitDataFp,
751
- nt as retrieveRawLaunchParams,
781
+ dt as request,
782
+ pt as request2,
783
+ j as request2Fp,
784
+ De as requestFp,
785
+ it as resetGlobals,
786
+ rt as retrieveLaunchParams,
787
+ We as retrieveLaunchParamsFp,
788
+ nt as retrieveRawInitData,
789
+ qe as retrieveRawInitDataFp,
790
+ ot as retrieveRawLaunchParams,
752
791
  M as retrieveRawLaunchParamsFp,
753
- ot as setDebug,
754
- at as setTargetOrigin,
755
- K as supports,
756
- _e as targetOrigin
792
+ at as setDebug,
793
+ st as setTargetOrigin,
794
+ Y as supports,
795
+ fe as targetOrigin
757
796
  };
758
797
  //# sourceMappingURL=index.js.map