cross-tab-worker-databus 0.20.56 → 0.20.58

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.
@@ -353,6 +353,18 @@ var WorkerClusterRuntime = class {
353
353
  // (or local self-subscribe), never via the reverse cache.
354
354
  assignedTopics = /* @__PURE__ */ new Map();
355
355
  routeOwnerCache = /* @__PURE__ */ new Map();
356
+ routeOwnerCacheMax;
357
+ routeOwnerCacheHits = 0;
358
+ routeOwnerCacheMisses = 0;
359
+ touchRouteOwnerCache(topicKey, value) {
360
+ if (this.routeOwnerCache.has(topicKey)) this.routeOwnerCache.delete(topicKey);
361
+ this.routeOwnerCache.set(topicKey, value);
362
+ while (this.routeOwnerCache.size > this.routeOwnerCacheMax) {
363
+ const oldest = this.routeOwnerCache.keys().next().value;
364
+ if (oldest === void 0) break;
365
+ this.routeOwnerCache.delete(oldest);
366
+ }
367
+ }
356
368
  wildcardPublishCache = /* @__PURE__ */ new Map();
357
369
  // Reverse mapping: opaque topicKey → plaintext topic. A bounded cache with
358
370
  // FIFO eviction — NOT authoritative. It can hold a topicKey that is also in
@@ -371,6 +383,7 @@ var WorkerClusterRuntime = class {
371
383
  this.handlers = options.handlers;
372
384
  this.maxActiveWorkers = options.maxActiveWorkers ?? DEFAULT_MAX_ACTIVE_WORKERS;
373
385
  this.heartbeatIntervalMs = options.heartbeatIntervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS;
386
+ this.routeOwnerCacheMax = options.routeOwnerCacheMax ?? 256;
374
387
  this.workerTtlMs = options.workerTtlMs ?? DEFAULT_WORKER_TTL_MS;
375
388
  const clusterHash = createOpaqueKey(options.clusterKey || "__default__");
376
389
  const prefix = options.storagePrefix ?? DEFAULT_STORAGE_PREFIX;
@@ -563,25 +576,26 @@ var WorkerClusterRuntime = class {
563
576
  return this.sendControl(this.workerId, "PUBLISH", topic, topicKey, data, metadata);
564
577
  }
565
578
  const cachedPattern = this.wildcardPublishCache.get(topic);
566
- if (cachedPattern !== void 0) {
567
- if (cachedPattern === null || this.assignedTopics.has(cachedPattern)) {
568
- return this.sendControl(this.workerId, "PUBLISH", topic, topicKey, data, metadata);
569
- }
570
- this.wildcardPublishCache.delete(topic);
579
+ if (cachedPattern !== void 0 && cachedPattern !== null && this.assignedTopics.has(cachedPattern)) {
580
+ return this.sendControl(this.workerId, "PUBLISH", topic, topicKey, data, metadata);
571
581
  }
572
- for (const pattern of this.assignedTopics.values()) {
573
- if (pattern !== topic && topicMatchesPattern(pattern, topic)) {
574
- this.wildcardPublishCache.set(topic, pattern);
575
- return this.sendControl(this.workerId, "PUBLISH", topic, topicKey, data, metadata);
582
+ if (cachedPattern === void 0) {
583
+ for (const pattern of this.assignedTopics.values()) {
584
+ if (pattern !== topic && topicMatchesPattern(pattern, topic)) {
585
+ this.wildcardPublishCache.set(topic, pattern);
586
+ return this.sendControl(this.workerId, "PUBLISH", topic, topicKey, data, metadata);
587
+ }
576
588
  }
589
+ this.wildcardPublishCache.set(topic, null);
577
590
  }
578
- this.wildcardPublishCache.set(topic, null);
579
591
  const workers = this.readWorkers();
580
592
  const route = this.readRoute(topicKey);
581
593
  const cached = this.routeOwnerCache.get(topicKey);
582
594
  const cachedLive = cached && route && route.generation === cached.generation && route.workerId === cached.workerId && workers.some((worker) => worker.workerId === cached.workerId);
595
+ if (cachedLive) this.routeOwnerCacheHits += 1;
596
+ else this.routeOwnerCacheMisses += 1;
583
597
  const target = cachedLive ? cached.workerId : this.routeOwnerIsLive(route, workers) ? route?.workerId ?? this.workerId : this.workerId;
584
- if (route && target === route.workerId) this.routeOwnerCache.set(topicKey, { workerId: route.workerId, generation: route.generation });
598
+ if (route && target === route.workerId) this.touchRouteOwnerCache(topicKey, { workerId: route.workerId, generation: route.generation });
585
599
  else this.routeOwnerCache.delete(topicKey);
586
600
  return this.sendControl(target, "PUBLISH", topic, topicKey, data, metadata);
587
601
  }
@@ -638,7 +652,8 @@ var WorkerClusterRuntime = class {
638
652
  routes,
639
653
  subscribedTopics: Array.from(this.subscribedTopics),
640
654
  assignedTopics: Array.from(this.assignedTopics.values()),
641
- knownTopics: Array.from(this.knownTopics.entries(), ([topicKey, topic]) => ({ topicKey, topic }))
655
+ knownTopics: Array.from(this.knownTopics.entries(), ([topicKey, topic]) => ({ topicKey, topic })),
656
+ routeOwnerCache: { size: this.routeOwnerCache.size, max: this.routeOwnerCacheMax, hits: this.routeOwnerCacheHits, misses: this.routeOwnerCacheMisses }
642
657
  };
643
658
  }
644
659
  handlePageHide = () => this.pause();