@braze/web-sdk 6.10.2 → 6.12.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.
Files changed (109) hide show
  1. package/index.d.ts +61 -6
  2. package/package.json +1 -1
  3. package/shared-lib/encoding-utils.js +3 -3
  4. package/shared-lib/event-types.js +12 -12
  5. package/shared-lib/indexed-db-adapter.js +52 -52
  6. package/shared-lib/logger.js +7 -7
  7. package/shared-lib/supported-options.js +10 -10
  8. package/src/Core/logout.js +2 -2
  9. package/src/DUST/dust-config-store.js +89 -0
  10. package/src/DUST/dust-connection-core.js +7 -21
  11. package/src/DUST/dust-message-dedup.js +33 -0
  12. package/src/DUST/dust-provider.js +367 -397
  13. package/src/DUST/dust-tab-identity.js +30 -0
  14. package/src/DUST/index.js +7 -9
  15. package/src/DUST/subscribe-to-dust.js +5 -5
  16. package/src/FeatureFlags/feature-flag-factory.js +4 -4
  17. package/src/FeatureFlags/feature-flag.js +5 -5
  18. package/src/FeatureFlags/feature-flags-provider-factory.js +8 -1
  19. package/src/FeatureFlags/feature-flags-provider.js +74 -74
  20. package/src/FeatureFlags/get-all-feature-flags.js +2 -2
  21. package/src/FeatureFlags/get-feature-flag.js +2 -2
  22. package/src/FeatureFlags/log-feature-flag-impression.js +3 -3
  23. package/src/FeatureFlags/subscribe-to-feature-flags-updates.js +1 -1
  24. package/src/InAppMessage/constants.js +39 -47
  25. package/src/InAppMessage/defer-in-app-message.js +2 -2
  26. package/src/InAppMessage/display/get-animation-effect.js +1 -1
  27. package/src/InAppMessage/display/html-message-display-utils.js +4 -4
  28. package/src/InAppMessage/display/html-message-to-html.js +10 -10
  29. package/src/InAppMessage/display/in-app-message-to-html.js +68 -66
  30. package/src/InAppMessage/display/modal-utils.js +14 -14
  31. package/src/InAppMessage/get-deferred-in-app-message.js +2 -2
  32. package/src/InAppMessage/in-app-message-factory.js +6 -6
  33. package/src/InAppMessage/in-app-message-manager-factory.js +5 -5
  34. package/src/InAppMessage/in-app-message-manager.js +101 -80
  35. package/src/InAppMessage/log-in-app-message-button-click.js +2 -2
  36. package/src/InAppMessage/log-in-app-message-click.js +2 -2
  37. package/src/InAppMessage/log-in-app-message-html-click.js +4 -4
  38. package/src/InAppMessage/log-in-app-message-impression.js +3 -3
  39. package/src/InAppMessage/models/full-screen-message.js +25 -25
  40. package/src/InAppMessage/models/html-message.js +14 -14
  41. package/src/InAppMessage/models/in-app-message-button.js +4 -4
  42. package/src/InAppMessage/models/in-app-message.js +100 -98
  43. package/src/InAppMessage/models/modal-message.js +23 -23
  44. package/src/InAppMessage/models/slide-up-message.js +29 -29
  45. package/src/InAppMessage/models/templated-in-app-message.js +8 -8
  46. package/src/InAppMessage/subscribe-to-in-app-message.js +2 -2
  47. package/src/InAppMessage/ui/automatically-show-in-app-messages.js +2 -2
  48. package/src/InAppMessage/ui/show-in-app-message.js +42 -40
  49. package/src/Push/index.js +1 -0
  50. package/src/Push/is-push-blocked.js +2 -2
  51. package/src/Push/is-push-permission-granted.js +2 -2
  52. package/src/Push/is-push-supported.js +2 -2
  53. package/src/Push/push-manager-factory.js +6 -6
  54. package/src/Push/push-manager.js +228 -185
  55. package/src/Push/register-push.js +9 -0
  56. package/src/Push/unregister-push-token-on-server.js +4 -4
  57. package/src/Push/utils/push-utils.js +16 -16
  58. package/src/User/user-manager.js +19 -19
  59. package/src/User/user.js +36 -36
  60. package/src/common/constants.js +1 -1
  61. package/src/common/content-cards-display.js +21 -21
  62. package/src/common/event-logger.js +2 -2
  63. package/src/l10n/l10n-manager-factory.js +7 -7
  64. package/src/l10n/l10n-manager.js +5 -5
  65. package/src/managers/auth-manager.js +1 -1
  66. package/src/managers/braze-instance.js +70 -66
  67. package/src/managers/device-manager.js +12 -12
  68. package/src/managers/network-manager.js +118 -118
  69. package/src/managers/server-config-manager.js +84 -84
  70. package/src/managers/session-manager.js +10 -10
  71. package/src/managers/storage-manager-factory.js +4 -4
  72. package/src/managers/storage-manager.js +138 -136
  73. package/src/managers/subscription-manager.js +1 -1
  74. package/src/managers/utils.js +1 -1
  75. package/src/models/backend-errors.js +5 -5
  76. package/src/models/braze-event.js +7 -7
  77. package/src/models/device.js +2 -2
  78. package/src/models/identifier.js +6 -6
  79. package/src/models/push-token.js +6 -6
  80. package/src/models/server-config.js +31 -31
  81. package/src/request-controller.js +91 -91
  82. package/src/triggers/models/filter.js +58 -58
  83. package/src/triggers/models/in-app-message-click-data.js +4 -4
  84. package/src/triggers/models/push-click-data.js +2 -2
  85. package/src/triggers/models/trigger-condition.js +35 -35
  86. package/src/triggers/models/trigger.js +37 -37
  87. package/src/triggers/triggers-provider-factory.js +3 -3
  88. package/src/triggers/triggers-provider.js +61 -53
  89. package/src/types.js +1 -1
  90. package/src/ui/js/attach-css.js +1 -1
  91. package/src/ui/js/feed-css.js +2 -2
  92. package/src/ui/js/iam-css.js +2 -2
  93. package/src/util/base-device-parser.js +1 -1
  94. package/src/util/braze-actions.js +4 -4
  95. package/src/util/browser-detector.js +13 -13
  96. package/src/util/client-hints-parser.js +4 -4
  97. package/src/util/component-utils.js +4 -4
  98. package/src/util/dom-utils.js +5 -5
  99. package/src/util/ecommerce-event-validation.js +77 -48
  100. package/src/util/key-codes.js +1 -1
  101. package/src/util/net.js +4 -4
  102. package/src/util/request-header-utils.js +20 -20
  103. package/src/util/string-utils.js +5 -0
  104. package/src/util/user-agent-parser.js +15 -15
  105. package/src/util/validation-utils.js +9 -9
  106. package/src/util/window-utils.js +2 -2
  107. package/src/DUST/dust-shared-worker-code.js +0 -2
  108. package/src/DUST/dust-shared-worker-impl.js +0 -234
  109. package/src/DUST/dust-worker-bridge.js +0 -151
@@ -1,6 +1,5 @@
1
1
  import t from "../common/base-provider.js";
2
2
  import r from "../managers/braze-instance.js";
3
- import { STORAGE_KEYS as s } from "../managers/storage-manager.js";
4
3
  import f from "../managers/subscription-manager.js";
5
4
  import l from "../util/net.js";
6
5
  import h from "../util/request-header-utils.js";
@@ -12,16 +11,17 @@ import {
12
11
  } from "../common/constants.js";
13
12
  import { randomInclusive as a } from "../util/math.js";
14
13
  import {
15
- CONNECTION_VALID_THRESHOLD_MS as ii,
16
- MAX_RETRIES as ti,
17
- buildSseUrl as si,
14
+ MAX_RETRIES as ii,
15
+ buildSseUrl as ti,
18
16
  } from "./dust-connection-core.js";
17
+ import lt, { isConfigExpired as si } from "./dust-config-store.js";
18
+ import at from "./dust-message-dedup.js";
19
19
  import {
20
- DustWorkerBridge as ei,
21
- isSharedWorkerSupported as ni,
22
- } from "./dust-worker-bridge.js";
20
+ buildTabMite as ei,
21
+ generateIndividual as ni,
22
+ } from "./dust-tab-identity.js";
23
23
  export default class sr extends t {
24
- constructor(i, t, s, e, n = !0) {
24
+ constructor(i, t, s, e) {
25
25
  super(),
26
26
  (this.B = i),
27
27
  (this.j = t),
@@ -30,198 +30,171 @@ export default class sr extends t {
30
30
  (this.j = t),
31
31
  (this.h = s),
32
32
  (this.mite = null),
33
- (this.Wi = null),
34
- (this.Gi = null),
35
- (this.Hi = null),
36
- (this.Oi = e || null),
37
- (this.Ki = null),
38
- (this.Vi = null),
39
- (this.T = null),
40
- (this._i = 0),
41
- (this.Qi = ti),
42
- (this.Yi = null),
43
- (this.Zi = null),
44
- (this.we = !0),
45
- (this.Se = null),
46
- (this.ke = null),
47
33
  (this.ye = null),
34
+ (this.Se = null),
48
35
  (this.Re = null),
49
- (this.De = !1),
50
- (this.Me = new Map()),
51
- (this.xe = null),
52
- (this.We = null),
36
+ (this.ke = e || null),
37
+ (this.Me = null),
53
38
  (this.Fe = null),
54
- (this.Ue = this.Ne(n)),
55
- (this.Be = !1),
39
+ (this.Ne = null),
40
+ (this.T = null),
56
41
  (this.Te = 0),
57
- (this.ze = null),
58
- this.Ae();
59
- }
60
- Ne(i) {
61
- return i
62
- ? ni()
63
- ? "sharedworker"
64
- : (b.info(
65
- "SharedWorker not supported, using direct EventSource (multi-tab will gracefully degrade)",
66
- ),
67
- "direct")
68
- : (b.info("Shared connection disabled, using direct EventSource"),
69
- "direct");
70
- }
71
- Ge() {
72
- this.Fe = new ei({
73
- Pe: (i) => {
74
- this.qe(i);
75
- },
76
- He: () => {
77
- this.Je(), (this.ze = new Date().valueOf());
78
- },
79
- Le: () => {
80
- this.ze = null;
81
- },
82
- Oe: (i) => {
83
- b.error(`Real-time messaging SharedWorker error: ${i}`);
84
- },
85
- });
86
- this.Fe.initialize() ||
87
- (b.info(
88
- "SharedWorker initialization failed, falling back to direct EventSource",
89
- ),
90
- (this.Ue = "direct"),
91
- (this.Fe = null));
92
- }
93
- Je() {
94
- (this._i = 0),
95
- (this.Se = null),
96
- (this.we = !0),
42
+ (this.De = ii),
43
+ (this.Ue = null),
44
+ (this.Ae = null),
45
+ (this.Ge = null),
46
+ (this.Ie = !0),
47
+ (this.Je = null),
48
+ (this.Oe = null),
49
+ (this.Pe = null),
50
+ (this.qe = null),
97
51
  (this.Be = !1),
98
- (this.Te = 0);
99
- }
100
- Ie() {
101
- var i;
102
- return (
103
- !(
104
- !("sharedworker" === this.Ue
105
- ? Boolean(null === (i = this.Fe) || void 0 === i ? void 0 : i.Ke())
106
- : Boolean(this.Vi)) || null === this.ze
107
- ) && new Date().valueOf() - this.ze >= ii
108
- );
109
- }
110
- qe(i) {
111
- const t = i.type;
112
- if (!t)
52
+ (this.ze = new Map()),
53
+ (this.He = null),
54
+ (this.Ke = null),
55
+ (this.Ve = null),
56
+ (this.We = null),
57
+ (this._e = new lt(t)),
58
+ (this.Qe = null),
59
+ (this.Xe = new at(t)),
60
+ (this.Ye = !1),
61
+ this.Ze();
62
+ }
63
+ sn() {
64
+ return this.We || (this.We = ni()), this.We;
65
+ }
66
+ en() {
67
+ (this.Te = 0), (this.Je = null), (this.Ie = !0);
68
+ }
69
+ hn() {
70
+ return null !== this.Fe && this.Fe === this.Ve;
71
+ }
72
+ on(i, t) {
73
+ const s = i.type;
74
+ if (!s)
113
75
  return void b.warn(
114
76
  `Received real-time message without type: ${JSON.stringify(i)}`,
115
77
  );
116
- const s = "sharedworker" === this.Ue ? "SharedWorker" : "Direct";
117
- b.info(`Received real-time message of type '${t}' via ${s}`);
118
- const e = this.Me.get(t);
119
- if (e && e.Ve() > 0)
78
+ if ((b.info(`Received real-time message of type '${s}'`), "cir" === s))
79
+ return void this.ln(t || this.Fe);
80
+ if (!this.Xe.Mr(s))
81
+ return void b.info(
82
+ `Another tab is handling the real-time message of type '${s}', skipping it`,
83
+ );
84
+ const e = this.ze.get(s);
85
+ if (e && e.an() > 0)
120
86
  try {
121
87
  e.A(i);
122
88
  } catch (i) {
123
- b.error(`Error invoking subscription for message type '${t}': ${i}`);
89
+ b.error(`Error invoking subscription for message type '${s}': ${i}`);
124
90
  }
125
- else b.info(`No subscribers for real-time message type '${t}'`);
126
- }
127
- _e() {
128
- return "sharedworker" === this.Ue;
129
- }
130
- Qe() {
131
- return this.Ue;
132
- }
133
- Ae() {
134
- if (this.j) {
135
- const i = this.j.St(s.It.Xe),
136
- t = this.j.St(s.It.Ye),
137
- e = this.j.St(s.It.Ze),
138
- n = this.j.St(s.It.sn);
139
- i && t
140
- ? ((this.mite = i),
141
- (this.Wi = t),
142
- (this.Gi = e),
143
- (this.Hi = n),
144
- b.info("Restored real-time messaging configuration from storage"))
145
- : (i || t) &&
146
- (b.warn(
147
- "Incomplete real-time messaging configuration in storage, clearing",
148
- ),
149
- this.en());
150
- }
91
+ else b.info(`No subscribers for real-time message type '${s}'`);
92
+ }
93
+ ln(i) {
94
+ i &&
95
+ this.cn(i) &&
96
+ (b.info("Real-time messaging connection is ready"), (this.Ve = i));
97
+ }
98
+ un(i) {
99
+ this.Ve === i && (this.Ve = null);
100
+ }
101
+ Ze() {
102
+ if (!this.j) return;
103
+ const i = this._e.read();
104
+ i.mite && i.host
105
+ ? ((this.mite = i.mite),
106
+ (this.ye = i.host),
107
+ (this.Se = i.auth),
108
+ (this.Re = i.expiration),
109
+ b.info("Restored real-time messaging configuration from storage"))
110
+ : (i.mite || i.host) &&
111
+ (b.warn(
112
+ "Incomplete real-time messaging configuration in storage, clearing",
113
+ ),
114
+ this.gn());
115
+ }
116
+ dn() {
117
+ const i = this._e.read();
118
+ i.mite &&
119
+ i.host &&
120
+ (i.auth !== this.Se &&
121
+ b.info(
122
+ "Using real-time messaging configuration refreshed by another tab",
123
+ ),
124
+ (this.mite = i.mite),
125
+ (this.ye = i.host),
126
+ (this.Se = i.auth),
127
+ (this.Re = i.expiration));
151
128
  }
152
129
  Hr() {
153
- this.xe ||
154
- this.We ||
155
- ((this.xe = this.on("ddr", (i) => {
130
+ this.He ||
131
+ this.Ke ||
132
+ ((this.He = this.mn("ddr", (i) => {
156
133
  var t, s, e;
157
134
  const n = i.body;
158
135
  if (!n) return;
159
- const r = n.r_ms;
160
- if ("number" != typeof r) return;
136
+ const h = n.r_ms;
137
+ if ("number" != typeof h) return;
161
138
  const o = n.e,
162
- h = (null === (t = this.h) || void 0 === t ? void 0 : t.vt()) || d,
139
+ r = (null === (t = this.h) || void 0 === t ? void 0 : t.vt()) || d,
163
140
  l = (null === (s = this.h) || void 0 === s ? void 0 : s.gt()) || m,
164
141
  u = (null === (e = this.h) || void 0 === e ? void 0 : e.bt()) || c,
165
- g = Math.min(u, a(h, h * l)),
166
- f = Math.round(r + g),
142
+ g = Math.min(u, a(r, r * l)),
143
+ f = Math.round(h + g),
167
144
  p = o ? ` (${o})` : "";
168
145
  b.info(
169
- `Admin requested disconnect${p}, reconnecting in ${f}ms (r_ms=${r} + backoff=${g})`,
146
+ `Admin requested disconnect${p}, reconnecting in ${f}ms (r_ms=${h} + backoff=${g})`,
170
147
  ),
171
- this.hn(),
172
- setTimeout(() => this.an(), f);
148
+ this.fn(),
149
+ (this.Ge = window.setTimeout(() => {
150
+ (this.Ge = null), this.pn();
151
+ }, f));
173
152
  })),
174
- (this.We = this.on("ttl", (i) => {
153
+ (this.Ke = this.mn("ttl", (i) => {
175
154
  const t = i.body;
176
155
  if (!t) return;
156
+ b.info(`ttl message: ${JSON.stringify(t)}`);
177
157
  const s = t.t_ms;
178
158
  if ("number" != typeof s) return;
179
159
  const e = s;
180
- if (
181
- (b.info(`Time to live set to ${e}ms, will reconnect when expired`),
182
- "sharedworker" === this.Ue)
183
- )
184
- return;
160
+ b.info(`Time to live set to ${e}ms, will reconnect when expired`);
185
161
  const n = t.rcs;
186
- "string" == typeof n && (this.Ki = n),
187
- null !== this.Zi && window.clearTimeout(this.Zi),
188
- (this.Zi = window.setTimeout(() => {
189
- (this.Zi = null),
162
+ "string" == typeof n && (this.Me = n),
163
+ null !== this.Ae && window.clearTimeout(this.Ae),
164
+ (this.Ae = window.setTimeout(() => {
165
+ (this.Ae = null),
190
166
  b.info("Time to live expired, performing gapless reconnection"),
191
- this.ln();
167
+ this.vn();
192
168
  }, e));
193
169
  })));
194
170
  }
195
- cn() {
196
- this.ke ||
197
- this.ye ||
198
- ((this.Re = () => {
199
- (this.De = !0), (this.we = !1);
171
+ wn() {
172
+ this.Oe ||
173
+ this.Pe ||
174
+ ((this.qe = () => {
175
+ (this.Be = !0), (this.Ie = !1);
200
176
  }),
201
- window.addEventListener("beforeunload", this.Re),
202
- (this.ke = () => {
203
- var i;
204
- this.De = !0;
205
- (this.Vi ||
206
- (null === (i = this.Fe) || void 0 === i ? void 0 : i.Ke())) &&
207
- (b.info("Page unloading, closing real-time connection gracefully"),
208
- (this.we = !1),
209
- this.hn());
177
+ window.addEventListener("beforeunload", this.qe),
178
+ (this.Oe = () => {
179
+ (this.Be = !0),
180
+ this.we(),
181
+ (this.Fe || this.Ne) &&
182
+ (b.info("Page unloading, closing real-time connection gracefully"),
183
+ (this.Ie = !1),
184
+ this.fn());
210
185
  }),
211
- window.addEventListener("pagehide", this.ke),
212
- (this.ye = (i) => {
213
- var t;
214
- const s =
215
- this.Vi || (null === (t = this.Fe) || void 0 === t ? void 0 : t.Ke());
186
+ window.addEventListener("pagehide", this.Oe),
187
+ (this.Pe = (i) => {
216
188
  i.persisted &&
217
- this.un() &&
218
- !s &&
189
+ this.bn() &&
190
+ !this.Fe &&
191
+ !this.Ne &&
219
192
  (b.info("Page restored from bfcache, reconnecting"),
220
- (this.De = !1),
221
- this.Je(),
222
- this.an());
193
+ (this.Be = !1),
194
+ this.en(),
195
+ this.pn());
223
196
  }),
224
- window.addEventListener("pageshow", this.ye));
197
+ window.addEventListener("pageshow", this.Pe));
225
198
  }
226
199
  Et() {
227
200
  return this.T;
@@ -229,218 +202,213 @@ export default class sr extends t {
229
202
  Lt(i) {
230
203
  this.T = i;
231
204
  }
232
- on(i, t) {
205
+ mn(i, t) {
233
206
  if ("function" != typeof t) return null;
234
- let s = this.Me.get(i);
235
- return s || ((s = new f()), this.Me.set(i, s), r.S(s)), s.Ut(t);
207
+ let s = this.ze.get(i);
208
+ return s || ((s = new f()), this.ze.set(i, s), r.S(s)), s.Ut(t);
236
209
  }
237
- dn(i, t) {
238
- const s = this.Me.get(i);
210
+ yn(i, t) {
211
+ const s = this.ze.get(i);
239
212
  s && s.removeSubscription(t);
240
213
  }
241
- un() {
242
- return Boolean(this.mite && this.Wi);
214
+ bn() {
215
+ return Boolean(this.mite && this.ye);
216
+ }
217
+ Sn() {
218
+ return si(this.Re);
243
219
  }
244
220
  gn() {
245
- if (!this.Hi) return !1;
246
- return Math.floor(new Date().valueOf() / 1e3) >= this.Hi;
247
- }
248
- en() {
249
221
  (this.mite = null),
250
- (this.Wi = null),
251
- (this.Gi = null),
252
- (this.Hi = null),
253
- (this.Ki = null),
254
- this.j &&
255
- (this.j.Vt(s.It.Xe),
256
- this.j.Vt(s.It.Ye),
257
- this.j.Vt(s.It.Ze),
258
- this.j.Vt(s.It.sn));
222
+ (this.ye = null),
223
+ (this.Se = null),
224
+ (this.Re = null),
225
+ (this.Me = null),
226
+ this._e.clear();
259
227
  }
260
- mn(i, t) {
228
+ Rn(i, t, s = !1) {
261
229
  const e = () => {
262
230
  "function" == typeof t && t();
263
231
  },
264
- n = this.B,
232
+ n = () => {
233
+ this.pn(), "function" == typeof i && i();
234
+ };
235
+ if (this.Ye) return void e();
236
+ const o = this.B,
265
237
  r = this.j;
266
- if (!n || !r)
238
+ if (!o || !r)
267
239
  return (
268
240
  b.error("NetworkManager or StorageManager not available"), void e()
269
241
  );
270
- if (!this.h || !this.h.fn())
242
+ if (!this.h || !this.h.$n())
271
243
  return (
272
244
  b.info("Real-time messaging is not enabled, skipping refresh"), void e()
273
245
  );
274
- this.un()
275
- ? b.info("Refreshing real-time messaging configuration")
276
- : b.info("Fetching initial real-time messaging configuration");
277
- const o = n.Z({}, !0),
278
- a = n.tt(o, h.it.pn),
279
- c = new Date().valueOf();
280
- h.nt(r, h.it.pn, c),
281
- l.ot({
282
- url: `${n.ht()}/dust/config`,
283
- headers: a,
284
- data: o,
285
- lt: (t) => {
286
- if (!n.ut(o, t, a))
287
- return (
288
- b.error(
289
- "Failed to validate server response for real-time messaging configuration",
290
- ),
291
- void e()
292
- );
293
- n.ct(),
294
- t.mite && t.host
295
- ? ((this.mite = t.mite),
296
- (this.Wi = t.host),
297
- (this.Gi = t.auth || null),
298
- (this.Hi = t.expiration || null),
299
- b.info(
300
- "Received real-time messaging configuration from server",
301
- ),
302
- r.Pt(s.It.Xe, t.mite),
303
- r.Pt(s.It.Ye, t.host),
304
- t.auth ? r.Pt(s.It.Ze, t.auth) : r.Vt(s.It.Ze),
305
- t.expiration ? r.Pt(s.It.sn, t.expiration) : r.Vt(s.It.sn),
306
- this.an(),
307
- "function" == typeof i && i())
308
- : (b.info(
309
- "Real-time messaging configuration not available - this SDK version may not be supported",
310
- ),
311
- this.en(),
312
- e());
313
- },
314
- error: (i) => {
315
- n.dt(i, "retrieving DUST config"), e();
316
- },
317
- });
318
- }
319
- an() {
320
- if (!this.h || !this.h.fn()) return;
321
- if (!this.un())
322
- return void b.error(
323
- "Cannot start real-time subscription without configuration",
324
- );
325
- if (this.gn())
246
+ const a = this.Qe || this._e.Yi();
247
+ if (!a && !s)
326
248
  return (
327
249
  b.info(
328
- "Real-time messaging auth token has expired, refreshing configuration",
250
+ "Another tab is refreshing real-time messaging configuration, waiting for the result",
329
251
  ),
330
- void this.mn(
331
- () => {
332
- this.an();
333
- },
252
+ void this._e.xe(
334
253
  () => {
335
- b.error(
336
- "Failed to refresh expired real-time messaging configuration",
337
- );
254
+ this.dn(), n();
338
255
  },
256
+ () => this.Rn(i, t, !0),
339
257
  )
340
258
  );
341
- const i = this.mite,
342
- t = this.Oi || this.Wi;
343
- if (i && t)
344
- switch (this.Ue) {
345
- case "sharedworker":
346
- this.vn(i, t);
347
- break;
348
- case "direct":
349
- this.Vi &&
350
- (b.info(
351
- "Real-time connection already exists, closing before starting new subscription",
352
- ),
353
- this.hn()),
354
- this.wn();
259
+ (this.Qe = a),
260
+ this.bn()
261
+ ? b.info("Refreshing real-time messaging configuration")
262
+ : b.info("Fetching initial real-time messaging configuration");
263
+ const c = o.Z({}, !0),
264
+ u = o.tt(c, h.it.Cn),
265
+ g = new Date().valueOf();
266
+ h.nt(r, h.it.Cn, g),
267
+ l.ot({
268
+ url: `${o.ht()}/dust/config`,
269
+ headers: u,
270
+ data: c,
271
+ lt: (i) => {
272
+ if (!this.Ye) {
273
+ if ((this.we(), !o.ut(c, i, u)))
274
+ return (
275
+ b.error(
276
+ "Failed to validate server response for real-time messaging configuration",
277
+ ),
278
+ void e()
279
+ );
280
+ o.ct(),
281
+ i.mite && i.host
282
+ ? ((this.mite = i.mite),
283
+ (this.ye = i.host),
284
+ (this.Se = i.auth || null),
285
+ (this.Re = i.expiration || null),
286
+ b.info(
287
+ "Received real-time messaging configuration from server",
288
+ ),
289
+ this._e.write({
290
+ mite: this.mite,
291
+ host: this.ye,
292
+ auth: this.Se,
293
+ expiration: this.Re,
294
+ }),
295
+ n())
296
+ : (b.info(
297
+ "Real-time messaging configuration not available - this SDK version may not be supported",
298
+ ),
299
+ this.gn(),
300
+ e());
301
+ }
302
+ },
303
+ error: (i) => {
304
+ this.Ye || (this.we(), o.dt(i, "retrieving DUST config"), e());
305
+ },
306
+ });
307
+ }
308
+ we() {
309
+ this.Qe && (this._e.we(this.Qe), (this.Qe = null));
310
+ }
311
+ pn() {
312
+ if (!this.Ye && this.h && this.h.$n())
313
+ if (this.Ne)
314
+ b.info("Real-time messaging connection attempt already in progress");
315
+ else {
316
+ if ((this.dn(), this.bn()))
317
+ return this.Sn()
318
+ ? (b.info(
319
+ "Real-time messaging auth token has expired, refreshing configuration",
320
+ ),
321
+ void this.Rn(void 0, () => {
322
+ b.error(
323
+ "Failed to refresh expired real-time messaging configuration",
324
+ );
325
+ }))
326
+ : void (
327
+ this.kn() &&
328
+ (this.ke || this.ye) &&
329
+ (this.Fe &&
330
+ (b.info(
331
+ "Real-time connection already exists, closing before starting new subscription",
332
+ ),
333
+ this.fn()),
334
+ this.jn())
335
+ );
336
+ b.error("Cannot start real-time subscription without configuration");
355
337
  }
356
338
  }
357
- bn() {
358
- const i = this.mite,
359
- t = this.Oi || this.Wi;
339
+ kn() {
340
+ return this.mite ? ei(this.mite, this.sn()) : null;
341
+ }
342
+ xn() {
343
+ const i = this.kn(),
344
+ t = this.ke || this.ye;
360
345
  return i && t
361
- ? si(t, i, this._i, this.Gi || void 0, this.Ki || void 0)
346
+ ? ti(t, i, this.Te, this.Se || void 0, this.Me || void 0)
362
347
  : null;
363
348
  }
364
- vn(i, t) {
365
- var s, e, n;
366
- if ((this.Fe || this.Ge(), !this.Fe))
367
- return (
368
- b.info(
369
- "SharedWorker initialization failed, falling back to direct EventSource",
370
- ),
371
- (this.Ue = "direct"),
372
- void this.Sn()
373
- );
374
- this.Oi && b.info(`Using custom real-time messaging host: ${this.Oi}`),
375
- b.info("Starting real-time subscription via SharedWorker");
376
- const r = (null === (s = this.h) || void 0 === s ? void 0 : s.vt()) || d,
377
- o = (null === (e = this.h) || void 0 === e ? void 0 : e.bt()) || c,
378
- h = (null === (n = this.h) || void 0 === n ? void 0 : n.gt()) || m;
379
- this.Fe.connect({
380
- mite: i,
381
- dustHost: t,
382
- auth: this.Gi || void 0,
383
- backoff: { minSleepMs: r, maxSleepMs: o, scaleFactor: h },
384
- });
385
- }
386
- Sn() {
387
- this.Vi &&
388
- (b.info(
389
- "Real-time connection already exists, closing before starting new subscription",
390
- ),
391
- this.kn()),
392
- this.wn();
349
+ cn(i) {
350
+ return this.Fe === i || this.Ne === i;
393
351
  }
394
- wn(i) {
395
- const t = this.bn();
352
+ jn(i) {
353
+ if (this.Ye) return;
354
+ const t = this.xn();
396
355
  if (t) {
397
- this.Oi && b.info(`Using custom real-time messaging host: ${this.Oi}`);
356
+ this.ke && b.info(`Using custom real-time messaging host: ${this.ke}`);
398
357
  try {
399
358
  const s = new EventSource(t);
400
- (s.onopen = () => {
401
- i
402
- ? (b.info(
403
- "Gapless reconnection: new connection established, closing old connection",
404
- ),
405
- i.close())
406
- : b.info("Real-time messaging connection established"),
407
- (this.Vi = s),
408
- (this._i = 0),
409
- (this.Se = null),
410
- (this.we = !0),
411
- (this.Be = !0),
412
- (this.ze = new Date().valueOf());
413
- }),
359
+ (this.Ne = s),
360
+ b.info(`createConnection: ${t}`),
361
+ (s.onopen = () => {
362
+ this.cn(s)
363
+ ? (this.Ne === s && (this.Ne = null),
364
+ i
365
+ ? (b.info(
366
+ "Gapless reconnection: new connection established, closing old connection",
367
+ ),
368
+ i.close())
369
+ : b.info("Real-time messaging connection established"),
370
+ (this.Fe = s),
371
+ this.Ve !== s && (this.Ve = null),
372
+ (this.Te = 0),
373
+ (this.Je = null),
374
+ (this.Ie = !0))
375
+ : s.close();
376
+ }),
414
377
  s.addEventListener("msg", (i) => {
415
- this.yn(i.data);
378
+ if (!this.cn(s))
379
+ return (
380
+ b.info("Ignoring real-time message from superseded connection"),
381
+ void s.close()
382
+ );
383
+ this.Mn(i.data, s);
416
384
  }),
417
385
  (s.onerror = () => {
418
386
  const t = s.readyState;
419
- return (
420
- this.De ||
421
- (0 === t
422
- ? b.info("Real-time messaging failed to connect")
423
- : b.info("Real-time messaging connection lost")),
424
- i && this.Vi !== s
425
- ? (b.info(
426
- "Gapless reconnection failed, keeping old connection",
427
- ),
387
+ if (this.cn(s)) {
388
+ if (
389
+ (this.Be ||
390
+ (0 === t
391
+ ? b.info("Real-time messaging failed to connect")
392
+ : b.info("Real-time messaging connection lost")),
393
+ i && this.Fe !== s)
394
+ )
395
+ return (
396
+ b.info("Gapless reconnection failed, keeping old connection"),
428
397
  s.close(),
429
- void (this.we && this._i < this.Qi && this.$n()))
430
- : (this.kn(),
431
- this.Be && (this.Te++, this.Te > 1)
432
- ? (b.info(
433
- "Real-time messaging connection lost twice after successful connect (likely multi-tab conflict), yielding to other tab",
398
+ this.Ne === s && (this.Ne = null),
399
+ void (this.Ie && this.Te < this.De && this.Fn())
400
+ );
401
+ if (this.Ne && this.Fe === s)
402
+ return s.close(), (this.Fe = null), void this.un(s);
403
+ this.fn(),
404
+ this.Ie && this.Te < this.De
405
+ ? this.Fn()
406
+ : (this.Te >= this.De &&
407
+ b.error(
408
+ `Max retry attempts (${this.De}) reached for real-time messaging, giving up for current session`,
434
409
  ),
435
- void (this.we = !1))
436
- : void (this.we && this._i < this.Qi
437
- ? this.$n()
438
- : (this._i >= this.Qi &&
439
- b.error(
440
- `Max retry attempts (${this.Qi}) reached for real-time messaging, giving up for current session`,
441
- ),
442
- (this.we = !1))))
443
- );
410
+ (this.Ie = !1));
411
+ } else s.close();
444
412
  });
445
413
  } catch (i) {
446
414
  b.error(
@@ -451,60 +419,59 @@ export default class sr extends t {
451
419
  }
452
420
  }
453
421
  }
454
- $n() {
422
+ Fn() {
455
423
  var i, t, s;
456
- this._i++;
424
+ this.Te++;
457
425
  const e = (null === (i = this.h) || void 0 === i ? void 0 : i.vt()) || d,
458
426
  n = (null === (t = this.h) || void 0 === t ? void 0 : t.gt()) || m,
459
- r = (null === (s = this.h) || void 0 === s ? void 0 : s.bt()) || c;
460
- let o = this.Se;
427
+ h = (null === (s = this.h) || void 0 === s ? void 0 : s.bt()) || c;
428
+ let o = this.Je;
461
429
  (null == o || o < e) && (o = e);
462
- const h = Math.min(r, a(e, o * n));
463
- (this.Se = h),
430
+ const r = Math.min(h, a(e, o * n));
431
+ (this.Je = r),
464
432
  b.info(
465
- `Retrying real-time messaging connection in ${h}ms (attempt ${this._i}/${this.Qi})`,
433
+ `Retrying real-time messaging connection in ${r}ms (attempt ${this.Te}/${this.De})`,
466
434
  ),
467
- (this.Yi = window.setTimeout(() => {
468
- (this.Yi = null), this.an();
469
- }, h));
470
- }
471
- ln() {
472
- if (this.un()) {
473
- if (this.gn())
474
- return (
475
- b.info(
476
- "Auth token expired during gapless reconnection, falling back to regular reconnection",
477
- ),
478
- this.hn(),
479
- void this.an()
480
- );
481
- null !== this.Zi && (window.clearTimeout(this.Zi), (this.Zi = null)),
482
- this.wn(this.Vi || void 0);
483
- } else b.error("Cannot perform gapless reconnection without configuration");
484
- }
485
- hn() {
486
- var i;
487
- switch (((this.ze = null), this.Ue)) {
488
- case "sharedworker":
489
- null === (i = this.Fe) || void 0 === i || i.disconnect();
490
- break;
491
- case "direct":
492
- this.kn();
435
+ (this.Ue = window.setTimeout(() => {
436
+ (this.Ue = null), this.pn();
437
+ }, r));
438
+ }
439
+ vn() {
440
+ if (this.Ne)
441
+ b.info("Real-time messaging connection attempt already in progress");
442
+ else {
443
+ if ((this.dn(), this.bn()))
444
+ return this.Sn()
445
+ ? (b.info(
446
+ "Auth token expired during gapless reconnection, falling back to regular reconnection",
447
+ ),
448
+ this.fn(),
449
+ void this.pn())
450
+ : void (this.Ne
451
+ ? b.info(
452
+ "Real-time connection attempt already in progress, skipping gapless reconnection",
453
+ )
454
+ : (null !== this.Ae &&
455
+ (window.clearTimeout(this.Ae), (this.Ae = null)),
456
+ this.jn(this.Fe || void 0)));
457
+ b.error("Cannot perform gapless reconnection without configuration");
493
458
  }
494
459
  }
495
- kn() {
496
- null !== this.Yi && (window.clearTimeout(this.Yi), (this.Yi = null)),
497
- null !== this.Zi && (window.clearTimeout(this.Zi), (this.Zi = null)),
498
- (this.ze = null),
499
- this.Vi &&
500
- (this.Vi.close(),
501
- (this.Vi = null),
460
+ fn() {
461
+ null !== this.Ue && (window.clearTimeout(this.Ue), (this.Ue = null)),
462
+ null !== this.Ae && (window.clearTimeout(this.Ae), (this.Ae = null)),
463
+ null !== this.Ge && (window.clearTimeout(this.Ge), (this.Ge = null)),
464
+ (this.Ve = null),
465
+ this.Ne && (this.Ne.close(), (this.Ne = null)),
466
+ this.Fe &&
467
+ (this.Fe.close(),
468
+ (this.Fe = null),
502
469
  b.info("Real-time messaging connection closed"));
503
470
  }
504
- yn(i) {
471
+ Mn(i, t) {
505
472
  try {
506
- const t = JSON.parse(i);
507
- this.qe(t);
473
+ const s = JSON.parse(i);
474
+ this.on(s, t);
508
475
  } catch (i) {
509
476
  b.warn(
510
477
  `Failed to parse real-time message: ${
@@ -514,39 +481,42 @@ export default class sr extends t {
514
481
  }
515
482
  }
516
483
  changeUser(i = !1) {
517
- this.hn(),
484
+ this.fn(),
518
485
  i ||
519
- (this.un() &&
486
+ (this.bn() &&
520
487
  b.info(
521
488
  "Clearing cached real-time messaging configuration for user change",
522
489
  ),
523
- this.en()),
524
- this.Je();
490
+ this.gn()),
491
+ this.en();
525
492
  }
526
493
  clearData(i = !1) {
527
- (this.we = !1),
528
- this.hn(),
494
+ (this.Ie = !1),
495
+ this.fn(),
529
496
  i &&
530
- (this.un() &&
497
+ (this.bn() &&
531
498
  b.info(
532
499
  "Clearing cached real-time messaging configuration (wipeData)",
533
500
  ),
534
- this.en()),
535
- this.Je();
501
+ this.gn(),
502
+ this.Xe.Wr()),
503
+ this.en();
536
504
  }
537
505
  destroy() {
538
- (this.we = !1),
539
- this.hn(),
540
- this.en(),
541
- this.Fe && (this.Fe.destroy(), (this.Fe = null)),
542
- this.xe && (this.dn("ddr", this.xe), (this.xe = null)),
543
- this.We && (this.dn("ttl", this.We), (this.We = null)),
544
- this.Re &&
545
- (window.removeEventListener("beforeunload", this.Re), (this.Re = null)),
546
- this.ke &&
547
- (window.removeEventListener("pagehide", this.ke), (this.ke = null)),
548
- this.ye &&
549
- (window.removeEventListener("pageshow", this.ye), (this.ye = null)),
506
+ (this.Ye = !0),
507
+ (this.Ie = !1),
508
+ this.fn(),
509
+ this.we(),
510
+ this._e.destroy(),
511
+ this.gn(),
512
+ this.He && (this.yn("ddr", this.He), (this.He = null)),
513
+ this.Ke && (this.yn("ttl", this.Ke), (this.Ke = null)),
514
+ this.qe &&
515
+ (window.removeEventListener("beforeunload", this.qe), (this.qe = null)),
516
+ this.Oe &&
517
+ (window.removeEventListener("pagehide", this.Oe), (this.Oe = null)),
518
+ this.Pe &&
519
+ (window.removeEventListener("pageshow", this.Pe), (this.Pe = null)),
550
520
  this.T && (r.removeSubscription(this.T), (this.T = null));
551
521
  }
552
522
  }