@rotorsoft/act 0.27.0 → 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
@@ -744,8 +744,9 @@ async function snap(snapshot) {
744
744
  logger2.error(error);
745
745
  }
746
746
  }
747
- async function load(me, stream, callback) {
748
- 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);
749
750
  let state2 = cached?.state ?? (me.init ? me.init() : {});
750
751
  let patches = cached?.patches ?? 0;
751
752
  let snaps = cached?.snaps ?? 0;
@@ -763,11 +764,15 @@ async function load(me, stream, callback) {
763
764
  }
764
765
  callback && callback({ event, state: state2, patches, snaps });
765
766
  },
766
- { 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
+ }
767
772
  );
768
773
  logger2.trace(
769
774
  state2,
770
- `\u{1F7E2} load ${stream}${cached && count === 0 ? " (cached)" : ""}`
775
+ `\u{1F7E2} load ${stream}${cached && count === 0 ? " (cached)" : ""}${timeTravel ? " (as-of)" : ""}`
771
776
  );
772
777
  return { event, state: state2, patches, snaps };
773
778
  }
@@ -1027,7 +1032,7 @@ var Act = class {
1027
1032
  this.emit("committed", snapshots);
1028
1033
  return snapshots;
1029
1034
  }
1030
- async load(stateOrName, stream, callback) {
1035
+ async load(stateOrName, stream, callback, asOf) {
1031
1036
  let merged;
1032
1037
  if (typeof stateOrName === "string") {
1033
1038
  const found = this._states.get(stateOrName);
@@ -1036,7 +1041,7 @@ var Act = class {
1036
1041
  } else {
1037
1042
  merged = this._states.get(stateOrName.name) || stateOrName;
1038
1043
  }
1039
- return await load(merged, stream, callback);
1044
+ return await load(merged, stream, callback, asOf);
1040
1045
  }
1041
1046
  /**
1042
1047
  * Queries the event store for events matching a filter.