cross-tab-worker-databus 0.20.92 → 0.20.94

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.
@@ -189,6 +189,7 @@ function createStorageEventChannel(options) {
189
189
  if (!storage || !win) return null;
190
190
  const key = `${STORAGE_CHANNEL_PREFIX}${name}`;
191
191
  const listeners = /* @__PURE__ */ new Set();
192
+ const senderId = randomId();
192
193
  let sequence = 0;
193
194
  const onStorage = (event) => {
194
195
  if (event.key !== key || event.newValue === null) return;
@@ -201,7 +202,12 @@ function createStorageEventChannel(options) {
201
202
  return;
202
203
  }
203
204
  const event_ = { data: message };
204
- for (const listener of [...listeners]) listener(event_);
205
+ for (const listener of [...listeners]) {
206
+ try {
207
+ listener(event_);
208
+ } catch {
209
+ }
210
+ }
205
211
  };
206
212
  win.addEventListener("storage", onStorage);
207
213
  let closed = false;
@@ -215,7 +221,7 @@ function createStorageEventChannel(options) {
215
221
  postMessage(message) {
216
222
  if (closed) return;
217
223
  sequence += 1;
218
- storage.setItem(key, JSON.stringify({ seq: sequence, message }));
224
+ storage.setItem(key, JSON.stringify({ senderId, seq: sequence, message }));
219
225
  },
220
226
  close() {
221
227
  closed = true;
@@ -229,6 +235,7 @@ function createStorageEventChannel(options) {
229
235
  };
230
236
  }
231
237
  var tabIdentityInitialized = false;
238
+ var cachedTabId = null;
232
239
  function createBrowserEnvironment(options) {
233
240
  const channelFallback = options?.channelFallback ?? CHANNEL_FALLBACK.NONE;
234
241
  return {
@@ -274,8 +281,9 @@ function canUseStorage(storage, probeKey) {
274
281
  if (!storage) return false;
275
282
  try {
276
283
  storage.setItem(probeKey, "1");
284
+ const readable = storage.getItem(probeKey) === "1";
277
285
  storage.removeItem(probeKey);
278
- return true;
286
+ return readable;
279
287
  } catch {
280
288
  return false;
281
289
  }
@@ -287,14 +295,21 @@ function getOrCreateTabId(environment, key = TAB_ID_STORAGE_KEY) {
287
295
  const hasOpener = typeof window !== "undefined" && Boolean(window.opener);
288
296
  if (existing && (!hasOpener || tabIdentityInitialized)) {
289
297
  tabIdentityInitialized = true;
298
+ cachedTabId = existing;
290
299
  return existing;
291
300
  }
301
+ if (tabIdentityInitialized && cachedTabId) return cachedTabId;
292
302
  const created = `tab-${environment.randomId()}`;
293
303
  storage?.setItem(key, created);
294
304
  tabIdentityInitialized = true;
305
+ cachedTabId = created;
295
306
  return created;
296
307
  } catch {
297
- return `tab-${environment.randomId()}`;
308
+ if (cachedTabId) return cachedTabId;
309
+ const created = `tab-${environment.randomId()}`;
310
+ tabIdentityInitialized = true;
311
+ cachedTabId = created;
312
+ return created;
298
313
  }
299
314
  }
300
315
 
@@ -455,7 +470,17 @@ var BatchingStorageWriter = class {
455
470
  clear() {
456
471
  this.pending.clear();
457
472
  this.flushScheduled = false;
473
+ this.cancelRetry();
474
+ this.retryCount.clear();
475
+ this.retryDelayMs = INITIAL_RETRY_DELAY_MS;
458
476
  this.storage.clear();
477
+ }
478
+ /** Drop queued mutations and cancel retry state without clearing storage.
479
+ * Used when the owning runtime is torn down: final best-effort writes have
480
+ * already been flushed, and failed writes must not keep timers alive. */
481
+ discardPending() {
482
+ this.pending.clear();
483
+ this.flushScheduled = false;
459
484
  this.cancelRetry();
460
485
  this.retryCount.clear();
461
486
  this.retryDelayMs = INITIAL_RETRY_DELAY_MS;
@@ -814,7 +839,15 @@ var WorkerClusterRuntime = class {
814
839
  activate() {
815
840
  if (this.started) return;
816
841
  this.started = true;
817
- this.channel = this.storage ? this.environment.createChannel(this.channelName) : null;
842
+ if (this.storage) {
843
+ try {
844
+ this.channel = this.environment.createChannel(this.channelName);
845
+ } catch {
846
+ this.channel = null;
847
+ }
848
+ } else {
849
+ this.channel = null;
850
+ }
818
851
  if (!this.channel) this.storage = null;
819
852
  this.channel?.addEventListener("message", this.handleMessage);
820
853
  const now = this.environment.now();
@@ -860,6 +893,7 @@ var WorkerClusterRuntime = class {
860
893
  this.removeStorage(this.workerStorageKey(this.workerId));
861
894
  this.flushStorage();
862
895
  this.notifyRegistry();
896
+ if (this.storage instanceof BatchingStorageWriter) this.storage.discardPending();
863
897
  const channel = this.channel;
864
898
  this.channel = null;
865
899
  this.handlers.onSuspend?.();
@@ -2434,7 +2468,7 @@ var DedupManager = class {
2434
2468
  };
2435
2469
 
2436
2470
  // src/core/version.ts
2437
- var SDK_VERSION = true ? "0.20.92" : "";
2471
+ var SDK_VERSION = true ? "0.20.94" : "";
2438
2472
 
2439
2473
  // src/core/data-bus.ts
2440
2474
  var DEFAULT_REPLAY_MAX_PER_TOPIC = 100;