@rotorsoft/act 0.26.1 → 0.28.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.js CHANGED
@@ -376,6 +376,17 @@ var InMemoryStream = class {
376
376
  };
377
377
  }
378
378
  }
379
+ /**
380
+ * Reset this stream's watermark and state for replay.
381
+ */
382
+ reset() {
383
+ this._at = -1;
384
+ this._retry = 0;
385
+ this._blocked = false;
386
+ this._error = "";
387
+ this._leased_by = void 0;
388
+ this._leased_until = void 0;
389
+ }
379
390
  };
380
391
  var InMemoryStore = class {
381
392
  // stored events
@@ -566,6 +577,24 @@ var InMemoryStore = class {
566
577
  await sleep();
567
578
  return leases.map((l) => this._streams.get(l.stream)?.block(l, l.error)).filter((l) => !!l);
568
579
  }
580
+ /**
581
+ * Reset watermarks for the given streams to -1, clearing retry, blocked,
582
+ * error, and lease state so they can be replayed from the beginning.
583
+ * @param streams - Stream names to reset.
584
+ * @returns Count of streams that were actually reset.
585
+ */
586
+ async reset(streams) {
587
+ await sleep();
588
+ let count = 0;
589
+ for (const name of streams) {
590
+ const s = this._streams.get(name);
591
+ if (s) {
592
+ s.reset();
593
+ count++;
594
+ }
595
+ }
596
+ return count;
597
+ }
569
598
  };
570
599
 
571
600
  // src/ports.ts
@@ -715,8 +744,9 @@ async function snap(snapshot) {
715
744
  logger2.error(error);
716
745
  }
717
746
  }
718
- async function load(me, stream, callback) {
719
- const cached = await cache().get(stream);
747
+ async function load(me, stream, callback, asOf) {
748
+ const timeTravel = asOf && (asOf.before !== void 0 || asOf.created_before !== void 0 || asOf.created_after !== void 0 || asOf.limit !== void 0);
749
+ const cached = timeTravel ? void 0 : await cache().get(stream);
720
750
  let state2 = cached?.state ?? (me.init ? me.init() : {});
721
751
  let patches = cached?.patches ?? 0;
722
752
  let snaps = cached?.snaps ?? 0;
@@ -734,11 +764,15 @@ async function load(me, stream, callback) {
734
764
  }
735
765
  callback && callback({ event, state: state2, patches, snaps });
736
766
  },
737
- { stream, with_snaps: !cached, after: cached?.event_id, stream_exact: true }
767
+ {
768
+ stream,
769
+ stream_exact: true,
770
+ ...cached ? { after: cached.event_id } : { with_snaps: true, ...asOf }
771
+ }
738
772
  );
739
773
  logger2.trace(
740
774
  state2,
741
- `\u{1F7E2} load ${stream}${cached && count === 0 ? " (cached)" : ""}`
775
+ `\u{1F7E2} load ${stream}${cached && count === 0 ? " (cached)" : ""}${timeTravel ? " (as-of)" : ""}`
742
776
  );
743
777
  return { event, state: state2, patches, snaps };
744
778
  }
@@ -998,7 +1032,7 @@ var Act = class {
998
1032
  this.emit("committed", snapshots);
999
1033
  return snapshots;
1000
1034
  }
1001
- async load(stateOrName, stream, callback) {
1035
+ async load(stateOrName, stream, callback, asOf) {
1002
1036
  let merged;
1003
1037
  if (typeof stateOrName === "string") {
1004
1038
  const found = this._states.get(stateOrName);
@@ -1007,7 +1041,7 @@ var Act = class {
1007
1041
  } else {
1008
1042
  merged = this._states.get(stateOrName.name) || stateOrName;
1009
1043
  }
1010
- return await load(merged, stream, callback);
1044
+ return await load(merged, stream, callback, asOf);
1011
1045
  }
1012
1046
  /**
1013
1047
  * Queries the event store for events matching a filter.