@rotorsoft/act-diagram 1.1.8 → 1.2.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.cjs CHANGED
@@ -347,10 +347,15 @@ function build_state(model, st) {
347
347
  for (const action_name of Object.keys(raw_actions)) {
348
348
  if (action_name.startsWith("__emits_")) continue;
349
349
  const emits = raw_actions[`__emits_${action_name}`] ?? [];
350
- const invariants = (st.given?.[action_name] ?? []).map(
351
- (inv) => inv.description ?? ""
352
- );
353
- action_nodes.push({ name: action_name, emits, invariants });
350
+ const describe = (rule) => rule.description ?? "";
351
+ const invariants = (st.given?.[action_name] ?? []).map(describe);
352
+ const preconditions = (st.requires?.[action_name] ?? []).map(describe);
353
+ action_nodes.push({
354
+ name: action_name,
355
+ emits,
356
+ invariants,
357
+ ...preconditions.length ? { preconditions } : {}
358
+ });
354
359
  }
355
360
  const node = {
356
361
  name: domain_name,
@@ -664,6 +669,7 @@ function mock_state(entry, on_build) {
664
669
  events: {},
665
670
  actions: {},
666
671
  given: {},
672
+ requires: {},
667
673
  patches: /* @__PURE__ */ new Set(),
668
674
  _tag: "State"
669
675
  };
@@ -671,20 +677,21 @@ function mock_state(entry, on_build) {
671
677
  on(action_entry) {
672
678
  const action_name = Object.keys(action_entry)[0];
673
679
  info.actions[action_name] = action_entry[action_name];
680
+ const emit = (handler) => {
681
+ attach_emit(info, action_name, handler);
682
+ return action_builder(action_name);
683
+ };
684
+ const requires = (rules) => {
685
+ if (rules) info.requires[action_name] = rules;
686
+ return { emit };
687
+ };
674
688
  return {
675
689
  given(rules) {
676
690
  if (rules) info.given[action_name] = rules;
677
- return {
678
- emit(handler) {
679
- attach_emit(info, action_name, handler);
680
- return action_builder(action_name);
681
- }
682
- };
691
+ return { requires, emit };
683
692
  },
684
- emit(handler) {
685
- attach_emit(info, action_name, handler);
686
- return action_builder(action_name);
687
- }
693
+ requires,
694
+ emit
688
695
  };
689
696
  },
690
697
  snap: () => action_builder(_currentAction),
@@ -1270,11 +1277,13 @@ function build_patterns(esc, type) {
1270
1277
  ];
1271
1278
  const guard_patterns = [
1272
1279
  // Guard definition — prefer jumping to the declaration
1273
- new RegExp(`(?:const|let|var)\\s+(${esc})\\s*(?::\\s*Invariant)?\\s*=`),
1280
+ new RegExp(
1281
+ `(?:const|let|var)\\s+(${esc})\\s*(?::\\s*(?:Invariant|Precondition)\\b[^=]*)?\\s*=`
1282
+ ),
1274
1283
  new RegExp(`rule\\(\\s*["'\`](${esc})["'\`]`),
1275
1284
  new RegExp(`description:\\s*["'\`](${esc})["'\`]`),
1276
- // .given() usage in the state builder — last resort
1277
- new RegExp(`\\.given\\([\\s\\S]*?(${esc})`)
1285
+ // .given() or .requires() usage in the state builder — last resort
1286
+ new RegExp(`\\.(?:given|requires)\\([\\s\\S]*?(${esc})`)
1278
1287
  ];
1279
1288
  const slice_patterns = [
1280
1289
  new RegExp(`(?:const|let|var)\\s+(${esc})\\s*=\\s*slice\\s*\\(`)
@@ -1547,6 +1556,10 @@ var import_lucide_react2 = require("lucide-react");
1547
1556
  var import_react2 = require("react");
1548
1557
 
1549
1558
  // src/client/lib/layout.ts
1559
+ function rules_of(action) {
1560
+ const rules = [...action.invariants, ...action.preconditions ?? []];
1561
+ return rules.length ? rules : void 0;
1562
+ }
1550
1563
  var W = 100;
1551
1564
  var H = 36;
1552
1565
  var STATE_W = 80;
@@ -1628,8 +1641,8 @@ function place_chain(measure, rDef, reactionY, rX, triggered_from_x, triggered_f
1628
1641
  pos: dap,
1629
1642
  type: "action",
1630
1643
  label: row.an,
1631
- sub: dispatched_action && dispatched_action.invariants.length > 0 ? "guarded" : void 0,
1632
- guards: dispatched_action && dispatched_action.invariants.length > 0 ? dispatched_action.invariants : void 0,
1644
+ sub: dispatched_action && rules_of(dispatched_action) ? "guarded" : void 0,
1645
+ guards: dispatched_action ? rules_of(dispatched_action) : void 0,
1633
1646
  file: row.target_state?.file,
1634
1647
  emits: dispatched_action?.emits
1635
1648
  });
@@ -1949,8 +1962,8 @@ function compute_layout(view_model) {
1949
1962
  pos: { x: cx, y: actY },
1950
1963
  type: "action",
1951
1964
  label: action.name,
1952
- sub: action.invariants.length > 0 ? "guarded" : void 0,
1953
- guards: action.invariants.length > 0 ? action.invariants : void 0,
1965
+ sub: rules_of(action) ? "guarded" : void 0,
1966
+ guards: rules_of(action),
1954
1967
  file: action_file_map.get(action.name) ?? st.file,
1955
1968
  emits: action.emits
1956
1969
  });
@@ -2095,8 +2108,8 @@ function compute_layout(view_model) {
2095
2108
  pos: { x: cx, y: actY },
2096
2109
  type: "action",
2097
2110
  label: action.name,
2098
- sub: action.invariants.length > 0 ? "guarded" : void 0,
2099
- guards: action.invariants.length > 0 ? action.invariants : void 0,
2111
+ sub: rules_of(action) ? "guarded" : void 0,
2112
+ guards: rules_of(action),
2100
2113
  emits: action.emits
2101
2114
  });
2102
2115
  for (const en of action.emits) {
@@ -2214,6 +2227,8 @@ function formatModelTree(model) {
2214
2227
  let line = ` ${a.name} \u2192 [${a.emits.join(", ")}]`;
2215
2228
  if (a.invariants.length)
2216
2229
  line += ` guards: [${a.invariants.join(", ")}]`;
2230
+ if (a.preconditions?.length)
2231
+ line += ` requires: [${a.preconditions.join(", ")}]`;
2217
2232
  lines.push(line);
2218
2233
  }
2219
2234
  }
@@ -2464,7 +2479,7 @@ function Diagram({
2464
2479
  title: "@rotorsoft/act-diagram version",
2465
2480
  children: [
2466
2481
  "v",
2467
- "1.1.7"
2482
+ "1.1.8"
2468
2483
  ]
2469
2484
  }
2470
2485
  )