@rotorsoft/act 1.25.0 → 1.25.2

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
@@ -22,7 +22,7 @@ import {
22
22
  sleep,
23
23
  store,
24
24
  validate
25
- } from "./chunk-IYXAAZMQ.js";
25
+ } from "./chunk-NHJQCCBS.js";
26
26
  import {
27
27
  ActorSchema,
28
28
  CausationEventSchema,
@@ -2806,6 +2806,7 @@ var DrainController = class {
2806
2806
  };
2807
2807
 
2808
2808
  // src/internal/projection-fold.ts
2809
+ var FOLD_RESET = /* @__PURE__ */ Symbol("act.fold.reset");
2809
2810
  function make_fold_handler(me, flush, config2, patch_fn = bare_patch, sensitive_fields) {
2810
2811
  const load_me = {
2811
2812
  ...me,
@@ -2829,7 +2830,27 @@ function make_fold_handler(me, flush, config2, patch_fn = bare_patch, sensitive_
2829
2830
  await flush(rows);
2830
2831
  for (const f of flushed) f.dirty = false;
2831
2832
  };
2832
- return async (events) => {
2833
+ const first_sight = async (stream) => {
2834
+ if (cache2.size >= config2.maxCachedStates) {
2835
+ const oldest = cache2.keys().next().value;
2836
+ const evictee = cache2.get(oldest);
2837
+ if (evictee.dirty) await flush([row(evictee)]);
2838
+ cache2.delete(oldest);
2839
+ }
2840
+ const snapshot = await load(load_me, { stream }, void 0, patch_fn);
2841
+ const seated = {
2842
+ stream,
2843
+ state: snapshot.state,
2844
+ version: snapshot.version,
2845
+ event_id: snapshot.id,
2846
+ patches: snapshot.patches,
2847
+ snaps: snapshot.snaps,
2848
+ dirty: true
2849
+ };
2850
+ cache2.set(stream, seated);
2851
+ return seated;
2852
+ };
2853
+ const handler = async (events) => {
2833
2854
  let folded_since_flush = 0;
2834
2855
  for (const event of events) {
2835
2856
  const stream = event.stream;
@@ -2838,23 +2859,12 @@ function make_fold_handler(me, flush, config2, patch_fn = bare_patch, sensitive_
2838
2859
  cache2.delete(stream);
2839
2860
  cache2.set(stream, fold);
2840
2861
  } else {
2841
- if (cache2.size >= config2.maxCachedStates) {
2842
- const oldest = cache2.keys().next().value;
2843
- const evictee = cache2.get(oldest);
2844
- if (evictee.dirty) await flush([row(evictee)]);
2845
- cache2.delete(oldest);
2846
- }
2847
- const snapshot = await load(load_me, { stream }, void 0, patch_fn);
2848
- fold = {
2849
- stream,
2850
- state: snapshot.state,
2851
- version: snapshot.version,
2852
- event_id: snapshot.id,
2853
- patches: snapshot.patches,
2854
- snaps: snapshot.snaps,
2855
- dirty: true
2856
- };
2857
- cache2.set(stream, fold);
2862
+ fold = await first_sight(stream);
2863
+ }
2864
+ const contiguous = event.version === fold.version + 1;
2865
+ if (event.id > fold.event_id && !contiguous) {
2866
+ cache2.delete(stream);
2867
+ fold = await first_sight(stream);
2858
2868
  }
2859
2869
  if (event.id > fold.event_id) {
2860
2870
  const reducer = me.patch[event.name];
@@ -2878,6 +2888,8 @@ function make_fold_handler(me, flush, config2, patch_fn = bare_patch, sensitive_
2878
2888
  }
2879
2889
  await flush_dirty();
2880
2890
  };
2891
+ Object.defineProperty(handler, FOLD_RESET, { value: () => cache2.clear() });
2892
+ return handler;
2881
2893
  }
2882
2894
 
2883
2895
  // src/internal/reactions.ts
@@ -4179,6 +4191,8 @@ var Act = class {
4179
4191
  async reset(input) {
4180
4192
  return this._scoped(async () => {
4181
4193
  const count = await store().reset(input);
4194
+ for (const handler of this._batch_handlers.values())
4195
+ handler[FOLD_RESET]?.();
4182
4196
  if (count > 0 && this._reactive_events.size > 0) this._arm_all();
4183
4197
  return count;
4184
4198
  });
@@ -4582,12 +4596,17 @@ function act() {
4582
4596
  const fold_specs = [];
4583
4597
  const lanes = [];
4584
4598
  let _built = false;
4585
- const pii_wrap = (original) => async (events, stream) => {
4586
- const stripped = events.map((e) => {
4587
- const f = _sf.get(e.name);
4588
- return f ? pii_strip(e, f) : e;
4589
- });
4590
- return original(stripped, stream);
4599
+ const pii_wrap = (original) => {
4600
+ const wrapped = async (events, stream) => {
4601
+ const stripped = events.map((e) => {
4602
+ const f = _sf.get(e.name);
4603
+ return f ? pii_strip(e, f) : e;
4604
+ });
4605
+ return original(stripped, stream);
4606
+ };
4607
+ const reset = original[FOLD_RESET];
4608
+ if (reset) Object.defineProperty(wrapped, FOLD_RESET, { value: reset });
4609
+ return wrapped;
4591
4610
  };
4592
4611
  const make_batch_handlers = (patch_fn) => {
4593
4612
  const handlers = new Map(batch_handlers);