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.
@@ -378,6 +378,18 @@ var WorkerClusterRuntime = class {
378
378
  // (or local self-subscribe), never via the reverse cache.
379
379
  assignedTopics = /* @__PURE__ */ new Map();
380
380
  routeOwnerCache = /* @__PURE__ */ new Map();
381
+ routeOwnerCacheMax;
382
+ routeOwnerCacheHits = 0;
383
+ routeOwnerCacheMisses = 0;
384
+ touchRouteOwnerCache(topicKey, value) {
385
+ if (this.routeOwnerCache.has(topicKey)) this.routeOwnerCache.delete(topicKey);
386
+ this.routeOwnerCache.set(topicKey, value);
387
+ while (this.routeOwnerCache.size > this.routeOwnerCacheMax) {
388
+ const oldest = this.routeOwnerCache.keys().next().value;
389
+ if (oldest === void 0) break;
390
+ this.routeOwnerCache.delete(oldest);
391
+ }
392
+ }
381
393
  wildcardPublishCache = /* @__PURE__ */ new Map();
382
394
  // Reverse mapping: opaque topicKey → plaintext topic. A bounded cache with
383
395
  // FIFO eviction — NOT authoritative. It can hold a topicKey that is also in
@@ -396,6 +408,7 @@ var WorkerClusterRuntime = class {
396
408
  this.handlers = options.handlers;
397
409
  this.maxActiveWorkers = options.maxActiveWorkers ?? DEFAULT_MAX_ACTIVE_WORKERS;
398
410
  this.heartbeatIntervalMs = options.heartbeatIntervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS;
411
+ this.routeOwnerCacheMax = options.routeOwnerCacheMax ?? 256;
399
412
  this.workerTtlMs = options.workerTtlMs ?? DEFAULT_WORKER_TTL_MS;
400
413
  const clusterHash = createOpaqueKey(options.clusterKey || "__default__");
401
414
  const prefix = options.storagePrefix ?? DEFAULT_STORAGE_PREFIX;
@@ -588,25 +601,26 @@ var WorkerClusterRuntime = class {
588
601
  return this.sendControl(this.workerId, "PUBLISH", topic, topicKey, data, metadata);
589
602
  }
590
603
  const cachedPattern = this.wildcardPublishCache.get(topic);
591
- if (cachedPattern !== void 0) {
592
- if (cachedPattern === null || this.assignedTopics.has(cachedPattern)) {
593
- return this.sendControl(this.workerId, "PUBLISH", topic, topicKey, data, metadata);
594
- }
595
- this.wildcardPublishCache.delete(topic);
604
+ if (cachedPattern !== void 0 && cachedPattern !== null && this.assignedTopics.has(cachedPattern)) {
605
+ return this.sendControl(this.workerId, "PUBLISH", topic, topicKey, data, metadata);
596
606
  }
597
- for (const pattern of this.assignedTopics.values()) {
598
- if (pattern !== topic && topicMatchesPattern(pattern, topic)) {
599
- this.wildcardPublishCache.set(topic, pattern);
600
- return this.sendControl(this.workerId, "PUBLISH", topic, topicKey, data, metadata);
607
+ if (cachedPattern === void 0) {
608
+ for (const pattern of this.assignedTopics.values()) {
609
+ if (pattern !== topic && topicMatchesPattern(pattern, topic)) {
610
+ this.wildcardPublishCache.set(topic, pattern);
611
+ return this.sendControl(this.workerId, "PUBLISH", topic, topicKey, data, metadata);
612
+ }
601
613
  }
614
+ this.wildcardPublishCache.set(topic, null);
602
615
  }
603
- this.wildcardPublishCache.set(topic, null);
604
616
  const workers = this.readWorkers();
605
617
  const route = this.readRoute(topicKey);
606
618
  const cached = this.routeOwnerCache.get(topicKey);
607
619
  const cachedLive = cached && route && route.generation === cached.generation && route.workerId === cached.workerId && workers.some((worker) => worker.workerId === cached.workerId);
620
+ if (cachedLive) this.routeOwnerCacheHits += 1;
621
+ else this.routeOwnerCacheMisses += 1;
608
622
  const target = cachedLive ? cached.workerId : this.routeOwnerIsLive(route, workers) ? route?.workerId ?? this.workerId : this.workerId;
609
- if (route && target === route.workerId) this.routeOwnerCache.set(topicKey, { workerId: route.workerId, generation: route.generation });
623
+ if (route && target === route.workerId) this.touchRouteOwnerCache(topicKey, { workerId: route.workerId, generation: route.generation });
610
624
  else this.routeOwnerCache.delete(topicKey);
611
625
  return this.sendControl(target, "PUBLISH", topic, topicKey, data, metadata);
612
626
  }
@@ -663,7 +677,8 @@ var WorkerClusterRuntime = class {
663
677
  routes,
664
678
  subscribedTopics: Array.from(this.subscribedTopics),
665
679
  assignedTopics: Array.from(this.assignedTopics.values()),
666
- knownTopics: Array.from(this.knownTopics.entries(), ([topicKey, topic]) => ({ topicKey, topic }))
680
+ knownTopics: Array.from(this.knownTopics.entries(), ([topicKey, topic]) => ({ topicKey, topic })),
681
+ routeOwnerCache: { size: this.routeOwnerCache.size, max: this.routeOwnerCacheMax, hits: this.routeOwnerCacheHits, misses: this.routeOwnerCacheMisses }
667
682
  };
668
683
  }
669
684
  handlePageHide = () => this.pause();