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.
@@ -184,6 +184,7 @@ function createStorageEventChannel(options) {
184
184
  if (!storage || !win) return null;
185
185
  const key = `${STORAGE_CHANNEL_PREFIX}${name}`;
186
186
  const listeners = /* @__PURE__ */ new Set();
187
+ const senderId = randomId();
187
188
  let sequence = 0;
188
189
  const onStorage = (event) => {
189
190
  if (event.key !== key || event.newValue === null) return;
@@ -196,7 +197,12 @@ function createStorageEventChannel(options) {
196
197
  return;
197
198
  }
198
199
  const event_ = { data: message };
199
- for (const listener of [...listeners]) listener(event_);
200
+ for (const listener of [...listeners]) {
201
+ try {
202
+ listener(event_);
203
+ } catch {
204
+ }
205
+ }
200
206
  };
201
207
  win.addEventListener("storage", onStorage);
202
208
  let closed = false;
@@ -210,7 +216,7 @@ function createStorageEventChannel(options) {
210
216
  postMessage(message) {
211
217
  if (closed) return;
212
218
  sequence += 1;
213
- storage.setItem(key, JSON.stringify({ seq: sequence, message }));
219
+ storage.setItem(key, JSON.stringify({ senderId, seq: sequence, message }));
214
220
  },
215
221
  close() {
216
222
  closed = true;
@@ -224,6 +230,7 @@ function createStorageEventChannel(options) {
224
230
  };
225
231
  }
226
232
  var tabIdentityInitialized = false;
233
+ var cachedTabId = null;
227
234
  function createBrowserEnvironment(options) {
228
235
  const channelFallback = options?.channelFallback ?? CHANNEL_FALLBACK.NONE;
229
236
  return {
@@ -269,8 +276,9 @@ function canUseStorage(storage, probeKey) {
269
276
  if (!storage) return false;
270
277
  try {
271
278
  storage.setItem(probeKey, "1");
279
+ const readable = storage.getItem(probeKey) === "1";
272
280
  storage.removeItem(probeKey);
273
- return true;
281
+ return readable;
274
282
  } catch {
275
283
  return false;
276
284
  }
@@ -282,14 +290,21 @@ function getOrCreateTabId(environment, key = TAB_ID_STORAGE_KEY) {
282
290
  const hasOpener = typeof window !== "undefined" && Boolean(window.opener);
283
291
  if (existing && (!hasOpener || tabIdentityInitialized)) {
284
292
  tabIdentityInitialized = true;
293
+ cachedTabId = existing;
285
294
  return existing;
286
295
  }
296
+ if (tabIdentityInitialized && cachedTabId) return cachedTabId;
287
297
  const created = `tab-${environment.randomId()}`;
288
298
  storage?.setItem(key, created);
289
299
  tabIdentityInitialized = true;
300
+ cachedTabId = created;
290
301
  return created;
291
302
  } catch {
292
- return `tab-${environment.randomId()}`;
303
+ if (cachedTabId) return cachedTabId;
304
+ const created = `tab-${environment.randomId()}`;
305
+ tabIdentityInitialized = true;
306
+ cachedTabId = created;
307
+ return created;
293
308
  }
294
309
  }
295
310
 
@@ -439,7 +454,17 @@ var BatchingStorageWriter = class {
439
454
  clear() {
440
455
  this.pending.clear();
441
456
  this.flushScheduled = false;
457
+ this.cancelRetry();
458
+ this.retryCount.clear();
459
+ this.retryDelayMs = INITIAL_RETRY_DELAY_MS;
442
460
  this.storage.clear();
461
+ }
462
+ /** Drop queued mutations and cancel retry state without clearing storage.
463
+ * Used when the owning runtime is torn down: final best-effort writes have
464
+ * already been flushed, and failed writes must not keep timers alive. */
465
+ discardPending() {
466
+ this.pending.clear();
467
+ this.flushScheduled = false;
443
468
  this.cancelRetry();
444
469
  this.retryCount.clear();
445
470
  this.retryDelayMs = INITIAL_RETRY_DELAY_MS;
@@ -816,7 +841,15 @@ var WorkerClusterRuntime = class {
816
841
  activate() {
817
842
  if (this.started) return;
818
843
  this.started = true;
819
- this.channel = this.storage ? this.environment.createChannel(this.channelName) : null;
844
+ if (this.storage) {
845
+ try {
846
+ this.channel = this.environment.createChannel(this.channelName);
847
+ } catch {
848
+ this.channel = null;
849
+ }
850
+ } else {
851
+ this.channel = null;
852
+ }
820
853
  if (!this.channel) this.storage = null;
821
854
  this.channel?.addEventListener("message", this.handleMessage);
822
855
  const now = this.environment.now();
@@ -862,6 +895,7 @@ var WorkerClusterRuntime = class {
862
895
  this.removeStorage(this.workerStorageKey(this.workerId));
863
896
  this.flushStorage();
864
897
  this.notifyRegistry();
898
+ if (this.storage instanceof BatchingStorageWriter) this.storage.discardPending();
865
899
  const channel = this.channel;
866
900
  this.channel = null;
867
901
  this.handlers.onSuspend?.();
@@ -2436,7 +2470,7 @@ var DedupManager = class {
2436
2470
  };
2437
2471
 
2438
2472
  // src/core/version.ts
2439
- var SDK_VERSION = true ? "0.20.92" : "";
2473
+ var SDK_VERSION = true ? "0.20.94" : "";
2440
2474
 
2441
2475
  // src/core/data-bus.ts
2442
2476
  var DEFAULT_REPLAY_MAX_PER_TOPIC = 100;