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.
@@ -45,6 +45,7 @@ function createStorageEventChannel(options) {
45
45
  if (!storage || !win) return null;
46
46
  const key = `${STORAGE_CHANNEL_PREFIX}${name}`;
47
47
  const listeners = /* @__PURE__ */ new Set();
48
+ const senderId = randomId();
48
49
  let sequence = 0;
49
50
  const onStorage = (event) => {
50
51
  if (event.key !== key || event.newValue === null) return;
@@ -57,7 +58,12 @@ function createStorageEventChannel(options) {
57
58
  return;
58
59
  }
59
60
  const event_ = { data: message };
60
- for (const listener of [...listeners]) listener(event_);
61
+ for (const listener of [...listeners]) {
62
+ try {
63
+ listener(event_);
64
+ } catch {
65
+ }
66
+ }
61
67
  };
62
68
  win.addEventListener("storage", onStorage);
63
69
  let closed = false;
@@ -71,7 +77,7 @@ function createStorageEventChannel(options) {
71
77
  postMessage(message) {
72
78
  if (closed) return;
73
79
  sequence += 1;
74
- storage.setItem(key, JSON.stringify({ seq: sequence, message }));
80
+ storage.setItem(key, JSON.stringify({ senderId, seq: sequence, message }));
75
81
  },
76
82
  close() {
77
83
  closed = true;
@@ -85,6 +91,7 @@ function createStorageEventChannel(options) {
85
91
  };
86
92
  }
87
93
  var tabIdentityInitialized = false;
94
+ var cachedTabId = null;
88
95
  function createBrowserEnvironment(options) {
89
96
  const channelFallback = options?.channelFallback ?? CHANNEL_FALLBACK.NONE;
90
97
  return {
@@ -130,8 +137,9 @@ function canUseStorage(storage, probeKey) {
130
137
  if (!storage) return false;
131
138
  try {
132
139
  storage.setItem(probeKey, "1");
140
+ const readable = storage.getItem(probeKey) === "1";
133
141
  storage.removeItem(probeKey);
134
- return true;
142
+ return readable;
135
143
  } catch {
136
144
  return false;
137
145
  }
@@ -143,14 +151,21 @@ function getOrCreateTabId(environment, key = TAB_ID_STORAGE_KEY) {
143
151
  const hasOpener = typeof window !== "undefined" && Boolean(window.opener);
144
152
  if (existing && (!hasOpener || tabIdentityInitialized)) {
145
153
  tabIdentityInitialized = true;
154
+ cachedTabId = existing;
146
155
  return existing;
147
156
  }
157
+ if (tabIdentityInitialized && cachedTabId) return cachedTabId;
148
158
  const created = `tab-${environment.randomId()}`;
149
159
  storage?.setItem(key, created);
150
160
  tabIdentityInitialized = true;
161
+ cachedTabId = created;
151
162
  return created;
152
163
  } catch {
153
- return `tab-${environment.randomId()}`;
164
+ if (cachedTabId) return cachedTabId;
165
+ const created = `tab-${environment.randomId()}`;
166
+ tabIdentityInitialized = true;
167
+ cachedTabId = created;
168
+ return created;
154
169
  }
155
170
  }
156
171
 
@@ -428,7 +443,17 @@ var BatchingStorageWriter = class {
428
443
  clear() {
429
444
  this.pending.clear();
430
445
  this.flushScheduled = false;
446
+ this.cancelRetry();
447
+ this.retryCount.clear();
448
+ this.retryDelayMs = INITIAL_RETRY_DELAY_MS;
431
449
  this.storage.clear();
450
+ }
451
+ /** Drop queued mutations and cancel retry state without clearing storage.
452
+ * Used when the owning runtime is torn down: final best-effort writes have
453
+ * already been flushed, and failed writes must not keep timers alive. */
454
+ discardPending() {
455
+ this.pending.clear();
456
+ this.flushScheduled = false;
432
457
  this.cancelRetry();
433
458
  this.retryCount.clear();
434
459
  this.retryDelayMs = INITIAL_RETRY_DELAY_MS;
@@ -688,7 +713,15 @@ var WorkerClusterRuntime = class {
688
713
  activate() {
689
714
  if (this.started) return;
690
715
  this.started = true;
691
- this.channel = this.storage ? this.environment.createChannel(this.channelName) : null;
716
+ if (this.storage) {
717
+ try {
718
+ this.channel = this.environment.createChannel(this.channelName);
719
+ } catch {
720
+ this.channel = null;
721
+ }
722
+ } else {
723
+ this.channel = null;
724
+ }
692
725
  if (!this.channel) this.storage = null;
693
726
  this.channel?.addEventListener("message", this.handleMessage);
694
727
  const now = this.environment.now();
@@ -734,6 +767,7 @@ var WorkerClusterRuntime = class {
734
767
  this.removeStorage(this.workerStorageKey(this.workerId));
735
768
  this.flushStorage();
736
769
  this.notifyRegistry();
770
+ if (this.storage instanceof BatchingStorageWriter) this.storage.discardPending();
737
771
  const channel = this.channel;
738
772
  this.channel = null;
739
773
  this.handlers.onSuspend?.();
@@ -2308,7 +2342,7 @@ var DedupManager = class {
2308
2342
  };
2309
2343
 
2310
2344
  // src/core/version.ts
2311
- var SDK_VERSION = true ? "0.20.92" : "";
2345
+ var SDK_VERSION = true ? "0.20.94" : "";
2312
2346
 
2313
2347
  // src/core/data-bus.ts
2314
2348
  var DEFAULT_REPLAY_MAX_PER_TOPIC = 100;
@@ -3500,4 +3534,4 @@ export {
3500
3534
  parseDataBusPublication,
3501
3535
  selectWorkerBackend
3502
3536
  };
3503
- //# sourceMappingURL=chunk-QB74C4EH.js.map
3537
+ //# sourceMappingURL=chunk-RTTTA6JS.js.map