cross-tab-worker-databus 0.4.0 → 0.5.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.
@@ -1201,6 +1201,8 @@ var CrossTabDataBus = class _CrossTabDataBus {
1201
1201
  // replay is enabled — buffering is opt-in and must cost nothing otherwise.
1202
1202
  replayBuffers;
1203
1203
  replayMaxPerTopic;
1204
+ replayPersistence;
1205
+ replayHydration;
1204
1206
  initialConfig;
1205
1207
  hasInitialConfig;
1206
1208
  trace;
@@ -1240,6 +1242,8 @@ var CrossTabDataBus = class _CrossTabDataBus {
1240
1242
  }
1241
1243
  this.replayMaxPerTopic = replay?.maxPerTopic ?? DEFAULT_REPLAY_MAX_PER_TOPIC;
1242
1244
  this.replayBuffers = replay ? /* @__PURE__ */ new Map() : null;
1245
+ this.replayPersistence = replay?.persistence ?? null;
1246
+ this.replayHydration = this.hydrateReplay();
1243
1247
  const { autoStart, initialConfig, trace, transport, ...clusterOptions } = options;
1244
1248
  this.transport = transport;
1245
1249
  this.initialConfig = initialConfig;
@@ -1408,7 +1412,13 @@ var CrossTabDataBus = class _CrossTabDataBus {
1408
1412
  typeof options.replay === "number" ? Math.floor(options.replay) : this.replayMaxPerTopic,
1409
1413
  this.replayMaxPerTopic
1410
1414
  );
1411
- this.deliverReplay(topic, limit, handler);
1415
+ if (this.replayPersistence) {
1416
+ void this.replayHydration.then(() => {
1417
+ if (this.topicHandlers.get(topic)?.has(handler)) this.deliverReplay(topic, limit, handler);
1418
+ });
1419
+ } else {
1420
+ this.deliverReplay(topic, limit, handler);
1421
+ }
1412
1422
  }
1413
1423
  return () => this.unsubscribe(topic, handler);
1414
1424
  }
@@ -1532,6 +1542,27 @@ var CrossTabDataBus = class _CrossTabDataBus {
1532
1542
  }
1533
1543
  buffer.push(message);
1534
1544
  if (buffer.length > this.replayMaxPerTopic) buffer.shift();
1545
+ if (this.replayPersistence) {
1546
+ void this.replayPersistence.append(message).catch((error) => this.reportError(error));
1547
+ }
1548
+ }
1549
+ async hydrateReplay() {
1550
+ if (!this.replayBuffers || !this.replayPersistence) {
1551
+ return;
1552
+ }
1553
+ try {
1554
+ for (const message of await this.replayPersistence.load()) {
1555
+ let buffer = this.replayBuffers.get(message.topic);
1556
+ if (!buffer) {
1557
+ buffer = [];
1558
+ this.replayBuffers.set(message.topic, buffer);
1559
+ }
1560
+ buffer.push(message);
1561
+ if (buffer.length > this.replayMaxPerTopic) buffer.shift();
1562
+ }
1563
+ } catch (error) {
1564
+ this.reportError(error);
1565
+ }
1535
1566
  }
1536
1567
  /** Deliver buffered history to a newly-registered handler. For an exact
1537
1568
  * topic this is that topic's ring; for a wildcard subscription every