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.
@@ -224,6 +224,7 @@ function createStorageEventChannel(options) {
224
224
  };
225
225
  }
226
226
  var tabIdentityInitialized = false;
227
+ var cachedTabId = null;
227
228
  function createBrowserEnvironment(options) {
228
229
  const channelFallback = options?.channelFallback ?? CHANNEL_FALLBACK.NONE;
229
230
  return {
@@ -269,8 +270,9 @@ function canUseStorage(storage, probeKey) {
269
270
  if (!storage) return false;
270
271
  try {
271
272
  storage.setItem(probeKey, "1");
273
+ const readable = storage.getItem(probeKey) === "1";
272
274
  storage.removeItem(probeKey);
273
- return true;
275
+ return readable;
274
276
  } catch {
275
277
  return false;
276
278
  }
@@ -282,14 +284,21 @@ function getOrCreateTabId(environment, key = TAB_ID_STORAGE_KEY) {
282
284
  const hasOpener = typeof window !== "undefined" && Boolean(window.opener);
283
285
  if (existing && (!hasOpener || tabIdentityInitialized)) {
284
286
  tabIdentityInitialized = true;
287
+ cachedTabId = existing;
285
288
  return existing;
286
289
  }
290
+ if (tabIdentityInitialized && cachedTabId) return cachedTabId;
287
291
  const created = `tab-${environment.randomId()}`;
288
292
  storage?.setItem(key, created);
289
293
  tabIdentityInitialized = true;
294
+ cachedTabId = created;
290
295
  return created;
291
296
  } catch {
292
- return `tab-${environment.randomId()}`;
297
+ if (cachedTabId) return cachedTabId;
298
+ const created = `tab-${environment.randomId()}`;
299
+ tabIdentityInitialized = true;
300
+ cachedTabId = created;
301
+ return created;
293
302
  }
294
303
  }
295
304
 
@@ -439,7 +448,17 @@ var BatchingStorageWriter = class {
439
448
  clear() {
440
449
  this.pending.clear();
441
450
  this.flushScheduled = false;
451
+ this.cancelRetry();
452
+ this.retryCount.clear();
453
+ this.retryDelayMs = INITIAL_RETRY_DELAY_MS;
442
454
  this.storage.clear();
455
+ }
456
+ /** Drop queued mutations and cancel retry state without clearing storage.
457
+ * Used when the owning runtime is torn down: final best-effort writes have
458
+ * already been flushed, and failed writes must not keep timers alive. */
459
+ discardPending() {
460
+ this.pending.clear();
461
+ this.flushScheduled = false;
443
462
  this.cancelRetry();
444
463
  this.retryCount.clear();
445
464
  this.retryDelayMs = INITIAL_RETRY_DELAY_MS;
@@ -816,7 +835,15 @@ var WorkerClusterRuntime = class {
816
835
  activate() {
817
836
  if (this.started) return;
818
837
  this.started = true;
819
- this.channel = this.storage ? this.environment.createChannel(this.channelName) : null;
838
+ if (this.storage) {
839
+ try {
840
+ this.channel = this.environment.createChannel(this.channelName);
841
+ } catch {
842
+ this.channel = null;
843
+ }
844
+ } else {
845
+ this.channel = null;
846
+ }
820
847
  if (!this.channel) this.storage = null;
821
848
  this.channel?.addEventListener("message", this.handleMessage);
822
849
  const now = this.environment.now();
@@ -862,6 +889,7 @@ var WorkerClusterRuntime = class {
862
889
  this.removeStorage(this.workerStorageKey(this.workerId));
863
890
  this.flushStorage();
864
891
  this.notifyRegistry();
892
+ if (this.storage instanceof BatchingStorageWriter) this.storage.discardPending();
865
893
  const channel = this.channel;
866
894
  this.channel = null;
867
895
  this.handlers.onSuspend?.();
@@ -2436,7 +2464,7 @@ var DedupManager = class {
2436
2464
  };
2437
2465
 
2438
2466
  // src/core/version.ts
2439
- var SDK_VERSION = true ? "0.20.92" : "";
2467
+ var SDK_VERSION = true ? "0.20.93" : "";
2440
2468
 
2441
2469
  // src/core/data-bus.ts
2442
2470
  var DEFAULT_REPLAY_MAX_PER_TOPIC = 100;