@rotorsoft/act 1.21.0 → 1.22.1

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
@@ -538,6 +538,14 @@ function in_autoclose_window(window, now) {
538
538
  const hour = hour_in_zone(now, window.timeZone);
539
539
  return window.start < window.end ? hour >= window.start && hour < window.end : hour >= window.start || hour < window.end;
540
540
  }
541
+ function next_window_open(window, now) {
542
+ for (let h = 0; h <= 48; h++) {
543
+ const candidate = new Date(now.getTime() + h * 36e5);
544
+ if (hour_in_zone(candidate, window.timeZone) === window.start)
545
+ return candidate;
546
+ }
547
+ return new Date(now.getTime() + 864e5);
548
+ }
541
549
 
542
550
  // src/internal/autoclose-policy.ts
543
551
  import { z as z2 } from "zod";
@@ -707,7 +715,7 @@ function synthesize_autoclose_reactions(registry, states, config2) {
707
715
  const aggregate = event.stream;
708
716
  if (!in_autoclose_window(config2.autocloseWindow, /* @__PURE__ */ new Date()))
709
717
  throw new DeferSignal({
710
- at: new Date(Date.now() + config2.autocloseCycleMinutes * 6e4)
718
+ at: next_window_open(config2.autocloseWindow, /* @__PURE__ */ new Date())
711
719
  });
712
720
  const stats = await store().query_stats([aggregate], {
713
721
  count: true,
@@ -2393,10 +2401,15 @@ function build_handle(deps) {
2393
2401
  return async (lease, payloads) => {
2394
2402
  if (payloads.length === 0) return { lease, handled: 0, acked_at: lease.at };
2395
2403
  const stream = lease.stream;
2396
- let at = payloads.at(0).event.id;
2404
+ const last_index_of = /* @__PURE__ */ new Map();
2405
+ for (let i = 0; i < payloads.length; i++)
2406
+ last_index_of.set(payloads[i].event.id, i);
2407
+ let at = lease.at;
2397
2408
  let handled = 0;
2398
2409
  if (lease.retry > 0)
2399
- logger.warn(`Retrying ${stream}@${at} (${lease.retry}).`);
2410
+ logger.warn(
2411
+ `Retrying ${stream}@${payloads.at(0).event.id} (${lease.retry}).`
2412
+ );
2400
2413
  const scoped_app = {
2401
2414
  do: bound_do,
2402
2415
  load: bound_load,
@@ -2404,7 +2417,8 @@ function build_handle(deps) {
2404
2417
  query_array: bound_query_array,
2405
2418
  forget: bound_forget
2406
2419
  };
2407
- for (const payload of payloads) {
2420
+ for (let i = 0; i < payloads.length; i++) {
2421
+ const payload = payloads[i];
2408
2422
  const { event, handler } = payload;
2409
2423
  scoped_app.do = (action2, target, action_payload, options) => bound_do(action2, target, action_payload, {
2410
2424
  ...options,
@@ -2412,8 +2426,10 @@ function build_handle(deps) {
2412
2426
  });
2413
2427
  try {
2414
2428
  await handler(event, stream, scoped_app);
2415
- at = event.id;
2416
- handled++;
2429
+ if (last_index_of.get(event.id) === i) {
2430
+ at = event.id;
2431
+ handled++;
2432
+ }
2417
2433
  } catch (error) {
2418
2434
  if (error instanceof DeferSignal)
2419
2435
  return {