@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.cjs CHANGED
@@ -1011,7 +1011,6 @@ var InMemoryStore = class {
1011
1011
  async claim(lagging, leading, by, millis, lane) {
1012
1012
  await sleep();
1013
1013
  const has_work = (s) => {
1014
- if (s.at < 0) return true;
1015
1014
  if (!s.source) return s.at < this._max_non_snap_event_id;
1016
1015
  if (is_literal_source(s.source)) {
1017
1016
  const max_id = this._max_event_id_by_stream.get(s.source);
@@ -4374,6 +4373,7 @@ var DrainController = class {
4374
4373
  };
4375
4374
 
4376
4375
  // src/internal/projection-fold.ts
4376
+ var FOLD_RESET = /* @__PURE__ */ Symbol("act.fold.reset");
4377
4377
  function make_fold_handler(me, flush, config2, patch_fn = bare_patch, sensitive_fields) {
4378
4378
  const load_me = {
4379
4379
  ...me,
@@ -4397,7 +4397,27 @@ function make_fold_handler(me, flush, config2, patch_fn = bare_patch, sensitive_
4397
4397
  await flush(rows);
4398
4398
  for (const f of flushed) f.dirty = false;
4399
4399
  };
4400
- return async (events) => {
4400
+ const first_sight = async (stream) => {
4401
+ if (cache3.size >= config2.maxCachedStates) {
4402
+ const oldest = cache3.keys().next().value;
4403
+ const evictee = cache3.get(oldest);
4404
+ if (evictee.dirty) await flush([row(evictee)]);
4405
+ cache3.delete(oldest);
4406
+ }
4407
+ const snapshot = await load(load_me, { stream }, void 0, patch_fn);
4408
+ const seated = {
4409
+ stream,
4410
+ state: snapshot.state,
4411
+ version: snapshot.version,
4412
+ event_id: snapshot.id,
4413
+ patches: snapshot.patches,
4414
+ snaps: snapshot.snaps,
4415
+ dirty: true
4416
+ };
4417
+ cache3.set(stream, seated);
4418
+ return seated;
4419
+ };
4420
+ const handler = async (events) => {
4401
4421
  let folded_since_flush = 0;
4402
4422
  for (const event of events) {
4403
4423
  const stream = event.stream;
@@ -4406,23 +4426,12 @@ function make_fold_handler(me, flush, config2, patch_fn = bare_patch, sensitive_
4406
4426
  cache3.delete(stream);
4407
4427
  cache3.set(stream, fold);
4408
4428
  } else {
4409
- if (cache3.size >= config2.maxCachedStates) {
4410
- const oldest = cache3.keys().next().value;
4411
- const evictee = cache3.get(oldest);
4412
- if (evictee.dirty) await flush([row(evictee)]);
4413
- cache3.delete(oldest);
4414
- }
4415
- const snapshot = await load(load_me, { stream }, void 0, patch_fn);
4416
- fold = {
4417
- stream,
4418
- state: snapshot.state,
4419
- version: snapshot.version,
4420
- event_id: snapshot.id,
4421
- patches: snapshot.patches,
4422
- snaps: snapshot.snaps,
4423
- dirty: true
4424
- };
4425
- cache3.set(stream, fold);
4429
+ fold = await first_sight(stream);
4430
+ }
4431
+ const contiguous = event.version === fold.version + 1;
4432
+ if (event.id > fold.event_id && !contiguous) {
4433
+ cache3.delete(stream);
4434
+ fold = await first_sight(stream);
4426
4435
  }
4427
4436
  if (event.id > fold.event_id) {
4428
4437
  const reducer = me.patch[event.name];
@@ -4446,6 +4455,8 @@ function make_fold_handler(me, flush, config2, patch_fn = bare_patch, sensitive_
4446
4455
  }
4447
4456
  await flush_dirty();
4448
4457
  };
4458
+ Object.defineProperty(handler, FOLD_RESET, { value: () => cache3.clear() });
4459
+ return handler;
4449
4460
  }
4450
4461
 
4451
4462
  // src/internal/reactions.ts
@@ -5747,6 +5758,8 @@ var Act = class {
5747
5758
  async reset(input) {
5748
5759
  return this._scoped(async () => {
5749
5760
  const count = await store2().reset(input);
5761
+ for (const handler of this._batch_handlers.values())
5762
+ handler[FOLD_RESET]?.();
5750
5763
  if (count > 0 && this._reactive_events.size > 0) this._arm_all();
5751
5764
  return count;
5752
5765
  });
@@ -6150,12 +6163,17 @@ function act() {
6150
6163
  const fold_specs = [];
6151
6164
  const lanes = [];
6152
6165
  let _built = false;
6153
- const pii_wrap = (original) => async (events, stream) => {
6154
- const stripped = events.map((e) => {
6155
- const f = _sf.get(e.name);
6156
- return f ? pii_strip(e, f) : e;
6157
- });
6158
- return original(stripped, stream);
6166
+ const pii_wrap = (original) => {
6167
+ const wrapped = async (events, stream) => {
6168
+ const stripped = events.map((e) => {
6169
+ const f = _sf.get(e.name);
6170
+ return f ? pii_strip(e, f) : e;
6171
+ });
6172
+ return original(stripped, stream);
6173
+ };
6174
+ const reset = original[FOLD_RESET];
6175
+ if (reset) Object.defineProperty(wrapped, FOLD_RESET, { value: reset });
6176
+ return wrapped;
6159
6177
  };
6160
6178
  const make_batch_handlers = (patch_fn) => {
6161
6179
  const handlers = new Map(batch_handlers);