cross-tab-worker-databus 0.20.92 → 0.20.93

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.
@@ -229,6 +229,7 @@ function createStorageEventChannel(options) {
229
229
  };
230
230
  }
231
231
  var tabIdentityInitialized = false;
232
+ var cachedTabId = null;
232
233
  function createBrowserEnvironment(options) {
233
234
  const channelFallback = options?.channelFallback ?? CHANNEL_FALLBACK.NONE;
234
235
  return {
@@ -274,8 +275,9 @@ function canUseStorage(storage, probeKey) {
274
275
  if (!storage) return false;
275
276
  try {
276
277
  storage.setItem(probeKey, "1");
278
+ const readable = storage.getItem(probeKey) === "1";
277
279
  storage.removeItem(probeKey);
278
- return true;
280
+ return readable;
279
281
  } catch {
280
282
  return false;
281
283
  }
@@ -287,14 +289,21 @@ function getOrCreateTabId(environment, key = TAB_ID_STORAGE_KEY) {
287
289
  const hasOpener = typeof window !== "undefined" && Boolean(window.opener);
288
290
  if (existing && (!hasOpener || tabIdentityInitialized)) {
289
291
  tabIdentityInitialized = true;
292
+ cachedTabId = existing;
290
293
  return existing;
291
294
  }
295
+ if (tabIdentityInitialized && cachedTabId) return cachedTabId;
292
296
  const created = `tab-${environment.randomId()}`;
293
297
  storage?.setItem(key, created);
294
298
  tabIdentityInitialized = true;
299
+ cachedTabId = created;
295
300
  return created;
296
301
  } catch {
297
- return `tab-${environment.randomId()}`;
302
+ if (cachedTabId) return cachedTabId;
303
+ const created = `tab-${environment.randomId()}`;
304
+ tabIdentityInitialized = true;
305
+ cachedTabId = created;
306
+ return created;
298
307
  }
299
308
  }
300
309
 
@@ -455,7 +464,17 @@ var BatchingStorageWriter = class {
455
464
  clear() {
456
465
  this.pending.clear();
457
466
  this.flushScheduled = false;
467
+ this.cancelRetry();
468
+ this.retryCount.clear();
469
+ this.retryDelayMs = INITIAL_RETRY_DELAY_MS;
458
470
  this.storage.clear();
471
+ }
472
+ /** Drop queued mutations and cancel retry state without clearing storage.
473
+ * Used when the owning runtime is torn down: final best-effort writes have
474
+ * already been flushed, and failed writes must not keep timers alive. */
475
+ discardPending() {
476
+ this.pending.clear();
477
+ this.flushScheduled = false;
459
478
  this.cancelRetry();
460
479
  this.retryCount.clear();
461
480
  this.retryDelayMs = INITIAL_RETRY_DELAY_MS;
@@ -814,7 +833,15 @@ var WorkerClusterRuntime = class {
814
833
  activate() {
815
834
  if (this.started) return;
816
835
  this.started = true;
817
- this.channel = this.storage ? this.environment.createChannel(this.channelName) : null;
836
+ if (this.storage) {
837
+ try {
838
+ this.channel = this.environment.createChannel(this.channelName);
839
+ } catch {
840
+ this.channel = null;
841
+ }
842
+ } else {
843
+ this.channel = null;
844
+ }
818
845
  if (!this.channel) this.storage = null;
819
846
  this.channel?.addEventListener("message", this.handleMessage);
820
847
  const now = this.environment.now();
@@ -860,6 +887,7 @@ var WorkerClusterRuntime = class {
860
887
  this.removeStorage(this.workerStorageKey(this.workerId));
861
888
  this.flushStorage();
862
889
  this.notifyRegistry();
890
+ if (this.storage instanceof BatchingStorageWriter) this.storage.discardPending();
863
891
  const channel = this.channel;
864
892
  this.channel = null;
865
893
  this.handlers.onSuspend?.();
@@ -2434,7 +2462,7 @@ var DedupManager = class {
2434
2462
  };
2435
2463
 
2436
2464
  // src/core/version.ts
2437
- var SDK_VERSION = true ? "0.20.92" : "";
2465
+ var SDK_VERSION = true ? "0.20.93" : "";
2438
2466
 
2439
2467
  // src/core/data-bus.ts
2440
2468
  var DEFAULT_REPLAY_MAX_PER_TOPIC = 100;