@objectstack/metadata 5.1.0 → 5.2.0

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.
package/dist/index.cjs CHANGED
@@ -2392,6 +2392,23 @@ var _MetadataManager = class _MetadataManager {
2392
2392
  this.notifyWatchers(type, legacyEvent);
2393
2393
  }
2394
2394
  notifyWatchers(type, event) {
2395
+ this.notifyWatchersLocal(type, event);
2396
+ if (this.clusterPubSub) {
2397
+ const payload = {
2398
+ originNode: this.clusterNodeId,
2399
+ type,
2400
+ event
2401
+ };
2402
+ const key = `${type}:${event.name ?? ""}`;
2403
+ void this.clusterPubSub.publish(_MetadataManager.CLUSTER_CHANNEL, payload, { partitionKey: key }).catch((err) => {
2404
+ this.logger.error("Cluster metadata publish failed", void 0, {
2405
+ type,
2406
+ error: err instanceof Error ? err.message : String(err)
2407
+ });
2408
+ });
2409
+ }
2410
+ }
2411
+ notifyWatchersLocal(type, event) {
2395
2412
  const callbacks = this.watchCallbacks.get(type);
2396
2413
  if (!callbacks) return;
2397
2414
  for (const callback of callbacks) {
@@ -2405,6 +2422,63 @@ var _MetadataManager = class _MetadataManager {
2405
2422
  }
2406
2423
  }
2407
2424
  }
2425
+ /**
2426
+ * Attach a cluster pub/sub transport so metadata-change events fan
2427
+ * out to peer nodes and remote events replay into local watchers.
2428
+ *
2429
+ * The bridge plugin in @objectstack/service-cluster calls this once
2430
+ * per kernel boot after both cluster and metadata services are
2431
+ * registered. Passing the same MetadataManager twice no-ops; passing
2432
+ * a different transport replaces the prior subscription.
2433
+ *
2434
+ * Pass `nodeId` matching the local cluster's nodeId so loopback
2435
+ * suppression works.
2436
+ *
2437
+ * @returns disposer that unsubscribes from cluster events.
2438
+ */
2439
+ attachClusterPubSub(pubsub, nodeId) {
2440
+ if (this.clusterPubSub === pubsub && this.clusterNodeId === nodeId) {
2441
+ return () => this.detachClusterPubSub();
2442
+ }
2443
+ this.detachClusterPubSub();
2444
+ this.clusterPubSub = pubsub;
2445
+ this.clusterNodeId = nodeId;
2446
+ this.clusterUnsubscribe = pubsub.subscribe(
2447
+ _MetadataManager.CLUSTER_CHANNEL,
2448
+ (msg) => {
2449
+ const p = msg.payload;
2450
+ if (p?.originNode && p.originNode === this.clusterNodeId) return;
2451
+ if (!p?.type || !p.event) return;
2452
+ setImmediate(() => {
2453
+ try {
2454
+ this.notifyWatchersLocal(p.type, p.event);
2455
+ } catch (err) {
2456
+ this.logger.error("Cluster remote replay failed", void 0, {
2457
+ type: p.type,
2458
+ error: err instanceof Error ? err.message : String(err)
2459
+ });
2460
+ }
2461
+ });
2462
+ }
2463
+ );
2464
+ this.logger.info("MetadataManager attached to cluster pubsub", {
2465
+ nodeId,
2466
+ channel: _MetadataManager.CLUSTER_CHANNEL
2467
+ });
2468
+ return () => this.detachClusterPubSub();
2469
+ }
2470
+ /** Tear down cluster wiring. Safe to call multiple times. */
2471
+ detachClusterPubSub() {
2472
+ if (this.clusterUnsubscribe) {
2473
+ try {
2474
+ this.clusterUnsubscribe();
2475
+ } catch {
2476
+ }
2477
+ this.clusterUnsubscribe = void 0;
2478
+ }
2479
+ this.clusterPubSub = void 0;
2480
+ this.clusterNodeId = void 0;
2481
+ }
2408
2482
  // ==========================================
2409
2483
  // Version History & Rollback
2410
2484
  // ==========================================
@@ -2505,6 +2579,7 @@ var _MetadataManager = class _MetadataManager {
2505
2579
  }
2506
2580
  };
2507
2581
  _MetadataManager.LIST_CACHE_TTL_MS = 3e4;
2582
+ _MetadataManager.CLUSTER_CHANNEL = "metadata.changed";
2508
2583
  var MetadataManager = _MetadataManager;
2509
2584
 
2510
2585
  // src/plugin.ts