@rotorsoft/act 1.33.20 → 1.34.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
@@ -40,7 +40,7 @@ import {
40
40
  sleep,
41
41
  store,
42
42
  validate
43
- } from "./chunk-5Z53Y7SG.js";
43
+ } from "./chunk-5F2TQ4PT.js";
44
44
  import {
45
45
  ActorSchema,
46
46
  CausationEventSchema,
@@ -70,7 +70,7 @@ import {
70
70
  pii_fields,
71
71
  pii_schemas,
72
72
  sensitive
73
- } from "./chunk-HEHGZHHA.js";
73
+ } from "./chunk-BKCNU6FF.js";
74
74
 
75
75
  // src/signals.ts
76
76
  process.once("SIGINT", async (arg) => {
@@ -1924,6 +1924,19 @@ async function action(me, action2, target, payload, options, patch_fn = bare_pat
1924
1924
  );
1925
1925
  });
1926
1926
  }
1927
+ if (me.requires) {
1928
+ const preconditions = me.requires[action2] || [];
1929
+ for (const { holds, description } of preconditions)
1930
+ if (!holds(validated, snapshot.state, actor))
1931
+ throw new InvariantError(
1932
+ action2,
1933
+ validated,
1934
+ target,
1935
+ snapshot,
1936
+ description,
1937
+ "precondition"
1938
+ );
1939
+ }
1927
1940
  const result = me.on[action2](validated, snapshot, target);
1928
1941
  if (!result) return [snapshot];
1929
1942
  if (Array.isArray(result) && result.length === 0) {
@@ -4239,6 +4252,7 @@ function merge_into_existing(state2, existing, states, actions, events) {
4239
4252
  patch: merged_patch,
4240
4253
  on: { ...existing.on, ...state2.on },
4241
4254
  given: { ...existing.given, ...state2.given },
4255
+ requires: { ...existing.requires, ...state2.requires },
4242
4256
  snap: pick_declared(existing.snap, state2.snap, "snap strategy", state2.name),
4243
4257
  // Per-action retry policy is keyed by action name, so the two partials'
4244
4258
  // maps combine. Left `undefined` when neither declared one, so the
@@ -5006,6 +5020,11 @@ function action_builder(state2) {
5006
5020
  function given(rules) {
5007
5021
  internal.given ??= {};
5008
5022
  internal.given[action2] = rules;
5023
+ return { requires, emit };
5024
+ }
5025
+ function requires(rules) {
5026
+ internal.requires ??= {};
5027
+ internal.requires[action2] = rules;
5009
5028
  return { emit };
5010
5029
  }
5011
5030
  function emit(handler) {
@@ -5023,7 +5042,7 @@ function action_builder(state2) {
5023
5042
  }
5024
5043
  return builder;
5025
5044
  }
5026
- return { given, emit };
5045
+ return { given, requires, emit };
5027
5046
  },
5028
5047
  snap(snap2) {
5029
5048
  internal.snap = snap2;