@rotorsoft/act-diagram 0.1.2 → 0.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.js CHANGED
@@ -377,7 +377,10 @@ function captureDispatches(handler) {
377
377
  if (typeof actionName === "string" && !dispatches.includes(actionName))
378
378
  dispatches.push(actionName);
379
379
  return Promise.resolve([]);
380
- }
380
+ },
381
+ load: () => Promise.resolve({}),
382
+ query: () => Promise.resolve({ count: 0 }),
383
+ query_array: () => Promise.resolve([])
381
384
  };
382
385
  const mockEvent = new Proxy({}, {
383
386
  get: (_, prop) => prop === "stream" ? "mock" : prop === "data" ? new Proxy({}, { get: () => "" }) : ""
@@ -670,6 +673,15 @@ function resolve(imp, fromPath, byKey) {
670
673
  }
671
674
 
672
675
  // src/client/lib/evaluate.ts
676
+ var stripNonCode = (src, level = "full") => {
677
+ const blank = (m) => m.replace(/[^\n]/g, " ");
678
+ let result = src.replace(/\/\/[^\n]*/g, blank).replace(/\/\*[\s\S]*?\*\//g, blank).replace(/`(?:\\[\s\S]|[^`])*`/g, blank);
679
+ if (level === "full") {
680
+ result = result.replace(/"(?:\\[\s\S]|[^"])*"/g, blank).replace(/'(?:\\[\s\S]|[^'])*'/g, blank);
681
+ }
682
+ return result;
683
+ };
684
+ var isSourceFile = (f) => f.path.endsWith(".ts") && !f.path.endsWith(".d.ts") && !f.path.endsWith(".tsx") && !f.path.endsWith(".spec.ts") && !f.path.endsWith(".test.ts") && !f.path.includes("node_modules/") && !f.path.includes("__tests__/") && !f.path.includes("/test/") && !f.path.startsWith("test/");
673
685
  function transpile(code) {
674
686
  try {
675
687
  const safe = code.replace(/\bimport\.meta\.\w+/g, "'__import_meta__'");
@@ -691,7 +703,8 @@ function execute(files) {
691
703
  projections: [],
692
704
  acts: [],
693
705
  error: void 0,
694
- fileErrors: /* @__PURE__ */ new Map()
706
+ fileErrors: /* @__PURE__ */ new Map(),
707
+ expectedSlices: /* @__PURE__ */ new Map()
695
708
  };
696
709
  try {
697
710
  const __built__ = {
@@ -743,11 +756,7 @@ function execute(files) {
743
756
  }
744
757
  return unknownModuleProxy();
745
758
  };
746
- const sorted = topoSort(
747
- files.filter(
748
- (f) => f.path.endsWith(".ts") && !f.path.endsWith(".d.ts") && !f.path.endsWith(".tsx") && !f.path.endsWith(".spec.ts") && !f.path.endsWith(".test.ts") && !f.path.includes("node_modules/") && !f.path.includes("__tests__/") && !f.path.includes("/test/") && !f.path.startsWith("test/")
749
- )
750
- );
759
+ const sorted = topoSort(files.filter(isSourceFile));
751
760
  for (const file of sorted) {
752
761
  _currentFile = file.path;
753
762
  const js = transpile(file.content);
@@ -757,10 +766,19 @@ function execute(files) {
757
766
  const sliceNamesInAct = [];
758
767
  const wsRe = /\.withSlice\(\s*(?:\w+\.)*(\w+)\s*\)/g;
759
768
  let wsm;
760
- while ((wsm = wsRe.exec(js)) !== null) {
769
+ const codeOnly = stripNonCode(js);
770
+ while ((wsm = wsRe.exec(codeOnly)) !== null) {
761
771
  if (!sliceNamesInAct.includes(wsm[1])) sliceNamesInAct.push(wsm[1]);
762
772
  }
763
- const fileRequire = (mod) => resolveModule(mod, key);
773
+ for (const m of codeOnly.matchAll(
774
+ /(?:exports\.)?(\w+)\s*=\s*(?:\w+\.)?slice(?:\s*\(\s*\)|\.call\(void 0,\s*\))/g
775
+ )) {
776
+ result.expectedSlices.set(m[1], file.path);
777
+ }
778
+ const fileRequire = Object.assign(
779
+ (mod) => resolveModule(mod, key),
780
+ { resolve: (mod) => mod, cache: {}, main: void 0 }
781
+ );
764
782
  try {
765
783
  const cleanJs = js.replace(/\bconst\s+__dirname\b/g, "var __dirname").replace(/\bconst\s+__filename\b/g, "var __filename");
766
784
  const fn = new Function(
@@ -806,48 +824,10 @@ function execute(files) {
806
824
  }
807
825
  function extractModel(files) {
808
826
  const result = execute(files);
809
- const expectedSlices = /* @__PURE__ */ new Map();
810
- for (const file of files) {
811
- if (!file.path.endsWith(".ts") || file.path.endsWith(".d.ts") || file.path.endsWith(".spec.ts") || file.path.endsWith(".test.ts") || file.path.includes("node_modules/"))
812
- continue;
813
- for (const m of file.content.matchAll(
814
- /(?:export\s+)?(?:const|let|var)\s+(\w+)\s*=\s*slice\s*\(\s*\)/g
815
- )) {
816
- expectedSlices.set(m[1], file.path);
817
- }
818
- }
819
- return buildModel(result, files, expectedSlices);
827
+ return buildModel(result, files, result.expectedSlices);
820
828
  }
821
829
 
822
830
  // src/client/lib/navigate.ts
823
- function isInsideComment(content, matchIndex) {
824
- const before = content.slice(0, matchIndex);
825
- const lastNl = before.lastIndexOf("\n");
826
- const lineStart = lastNl >= 0 ? lastNl + 1 : 0;
827
- const lineEnd = content.indexOf("\n", matchIndex);
828
- const lineText = content.slice(lineStart, lineEnd >= 0 ? lineEnd : content.length).trimStart();
829
- if (lineText.startsWith("//") || lineText.startsWith("*") || lineText.startsWith("/*")) {
830
- return true;
831
- }
832
- const lastBlockOpen = before.lastIndexOf("/*");
833
- if (lastBlockOpen >= 0) {
834
- const lastBlockClose = before.lastIndexOf("*/");
835
- if (lastBlockClose < lastBlockOpen) return true;
836
- }
837
- return false;
838
- }
839
- function findNonCommentMatch(content, re, startFrom = 0) {
840
- const globalRe = new RegExp(
841
- re.source,
842
- re.flags.includes("g") ? re.flags : re.flags + "g"
843
- );
844
- globalRe.lastIndex = startFrom;
845
- let match;
846
- while ((match = globalRe.exec(content)) !== null) {
847
- if (!isInsideComment(content, match.index)) return match.index;
848
- }
849
- return -1;
850
- }
851
831
  function positionAt(content, index) {
852
832
  const before = content.slice(0, index);
853
833
  const line = before.split("\n").length;
@@ -923,10 +903,19 @@ function buildPatterns(esc, type) {
923
903
  }
924
904
  }
925
905
  function navigateToCode(files, name, type, targetFile) {
906
+ const stripped = /* @__PURE__ */ new Map();
907
+ const code = (f) => {
908
+ let c = stripped.get(f.path);
909
+ if (c === void 0) {
910
+ c = stripNonCode(f.content, "nav");
911
+ stripped.set(f.path, c);
912
+ }
913
+ return c;
914
+ };
926
915
  if (type === "file") {
927
916
  const file = files.find((f) => f.path === name || f.path.endsWith(name));
928
917
  if (file) {
929
- const actMatch = /\bact\s*\(\s*\)/.exec(file.content);
918
+ const actMatch = /\bact\s*\(\s*\)/.exec(code(file));
930
919
  if (actMatch) {
931
920
  const { line, col } = positionAt(file.content, actMatch.index);
932
921
  return { file: file.path, line, col };
@@ -938,31 +927,35 @@ function navigateToCode(files, name, type, targetFile) {
938
927
  if (targetFile) {
939
928
  const file = files.find((f) => f.path === targetFile);
940
929
  if (file) {
930
+ const c = code(file);
941
931
  const esc2 = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
942
932
  if (type === "event") {
943
933
  const emitsBlockRe = new RegExp(
944
934
  `\\.emits\\([\\s\\S]*?(${esc2})\\s*[,:}]`
945
935
  );
946
- const blockMatch = emitsBlockRe.exec(file.content);
936
+ const blockMatch = emitsBlockRe.exec(c);
947
937
  if (blockMatch) {
948
938
  const nameIdx = blockMatch.index + blockMatch[0].lastIndexOf(name);
949
939
  const { line, col } = positionAt(file.content, nameIdx);
950
940
  return { file: file.path, line, col };
951
941
  }
952
942
  const nameRe2 = new RegExp(`\\b${esc2}\\b`, "g");
953
- if (findNonCommentMatch(file.content, nameRe2) >= 0) {
954
- const emitsIdx = findNonCommentMatch(file.content, /\.emits\s*\(/);
955
- if (emitsIdx >= 0) {
956
- const { line, col } = positionAt(file.content, emitsIdx + 1);
943
+ if (nameRe2.exec(c)) {
944
+ const emitsMatch = /\.emits\s*\(/.exec(c);
945
+ if (emitsMatch) {
946
+ const { line, col } = positionAt(
947
+ file.content,
948
+ emitsMatch.index + 1
949
+ );
957
950
  return { file: file.path, line, col };
958
951
  }
959
952
  }
960
953
  }
961
954
  if (type === "guard") {
962
955
  const descRe = new RegExp(`description:\\s*["'\`]${esc2}["'\`]`);
963
- const descMatch = descRe.exec(file.content);
956
+ const descMatch = descRe.exec(c);
964
957
  if (descMatch) {
965
- const before = file.content.slice(0, descMatch.index);
958
+ const before = c.slice(0, descMatch.index);
966
959
  const declRe = /(?:export\s+)?(?:const|let|var)\s+(\w+)\s*(?::\s*Invariant[^=]*)?\s*=/g;
967
960
  let lastDecl = null;
968
961
  let m;
@@ -980,19 +973,20 @@ function navigateToCode(files, name, type, targetFile) {
980
973
  }
981
974
  if (type === "action") {
982
975
  const nameRe2 = new RegExp(`\\b${esc2}\\b`, "g");
983
- const blockStart = file.content.indexOf(".on(");
976
+ const blockStart = c.indexOf(".on(");
984
977
  if (blockStart >= 0) {
985
- const idx2 = findNonCommentMatch(file.content, nameRe2, blockStart);
986
- if (idx2 >= 0) {
987
- const { line, col } = positionAt(file.content, idx2);
978
+ nameRe2.lastIndex = blockStart;
979
+ const match2 = nameRe2.exec(c);
980
+ if (match2) {
981
+ const { line, col } = positionAt(file.content, match2.index);
988
982
  return { file: file.path, line, col };
989
983
  }
990
984
  }
991
985
  }
992
- const nameRe = new RegExp(`\\b${esc2}\\b`, "g");
993
- const idx = findNonCommentMatch(file.content, nameRe);
994
- if (idx >= 0) {
995
- const { line, col } = positionAt(file.content, idx);
986
+ const nameRe = new RegExp(`\\b${esc2}\\b`);
987
+ const match = nameRe.exec(c);
988
+ if (match) {
989
+ const { line, col } = positionAt(file.content, match.index);
996
990
  return { file: file.path, line, col };
997
991
  }
998
992
  }
@@ -1007,9 +1001,10 @@ function navigateToCode(files, name, type, targetFile) {
1007
1001
  const descRe = new RegExp(`description:\\s*["'\`]${esc}["'\`]`);
1008
1002
  for (const f of sortedFiles) {
1009
1003
  if (!/\.tsx?$/.test(f.path)) continue;
1010
- const descMatch = descRe.exec(f.content);
1011
- if (descMatch && !isInsideComment(f.content, descMatch.index)) {
1012
- const before = f.content.slice(0, descMatch.index);
1004
+ const c = code(f);
1005
+ const descMatch = descRe.exec(c);
1006
+ if (descMatch) {
1007
+ const before = c.slice(0, descMatch.index);
1013
1008
  const declRe = /(?:export\s+)?(?:const|let|var)\s+(\w+)\s*(?::\s*Invariant[^=]*)?\s*=/g;
1014
1009
  let lastDecl = null;
1015
1010
  let m;
@@ -1026,18 +1021,17 @@ function navigateToCode(files, name, type, targetFile) {
1026
1021
  }
1027
1022
  const patterns = buildPatterns(esc, type);
1028
1023
  for (const re of patterns) {
1029
- for (let i = 0; i < sortedFiles.length; i++) {
1030
- if (!/\.tsx?$/.test(sortedFiles[i].path)) continue;
1031
- const content = sortedFiles[i].content;
1024
+ for (const f of sortedFiles) {
1025
+ if (!/\.tsx?$/.test(f.path)) continue;
1026
+ const c = code(f);
1032
1027
  const globalRe = new RegExp(re.source, re.flags + "g");
1033
1028
  let match;
1034
- while ((match = globalRe.exec(content)) !== null) {
1035
- if (isInsideComment(content, match.index)) continue;
1029
+ while ((match = globalRe.exec(c)) !== null) {
1036
1030
  const matchText = match[0];
1037
1031
  const nameOffsetInMatch = matchText.lastIndexOf(name);
1038
1032
  const nameStart = match.index + Math.max(0, nameOffsetInMatch);
1039
- const { line, col } = positionAt(content, nameStart);
1040
- return { file: sortedFiles[i].path, line, col };
1033
+ const { line, col } = positionAt(f.content, nameStart);
1034
+ return { file: f.path, line, col };
1041
1035
  }
1042
1036
  }
1043
1037
  }
@@ -1254,7 +1248,9 @@ function placeChain(measure, rDef, reactionY, rX, triggeredFromX, triggeredFromY
1254
1248
  nextX += STATE_W + GAP;
1255
1249
  }
1256
1250
  const evtX = nextX;
1257
- let emitY = rowCenterY - row.emitsH / 2;
1251
+ const nEvents = row.emitInfos.length;
1252
+ const eventsOnlyH = nEvents * H + (nEvents - 1) * (GAP / 2);
1253
+ let emitY = rowCenterY - eventsOnlyH / 2;
1258
1254
  for (const emit of row.emitInfos) {
1259
1255
  ns.push({
1260
1256
  key: `e:${emit.name}:dispatched:${rDef.handlerName}:${row.an}`,
@@ -1284,7 +1280,7 @@ function placeChain(measure, rDef, reactionY, rX, triggeredFromX, triggeredFromY
1284
1280
  sliceRightXRef
1285
1281
  );
1286
1282
  }
1287
- emitY += emit.h;
1283
+ emitY += H + GAP / 2;
1288
1284
  }
1289
1285
  if (row.emitInfos.length > 0) nextX = evtX + W + GAP;
1290
1286
  sliceRightXRef.value = Math.max(sliceRightXRef.value, nextX);
@@ -1416,28 +1412,27 @@ function computeLayout(viewModel) {
1416
1412
  const measuredEvents = [];
1417
1413
  let evtBlockH = 0;
1418
1414
  for (const { eventName: en, actionName } of eventRows) {
1419
- const rDef = sliceReactionByEvent.get(en);
1420
- let eventH = H;
1421
- let chain;
1422
- let chainReaction;
1423
- if (rDef && !visitedEvents.has(en) && !visitedReactions.has(rDef.handlerName)) {
1424
- visitedEvents.add(en);
1425
- visitedReactions.add(rDef.handlerName);
1426
- chain = measureChain(
1427
- rDef,
1428
- sliceReactionByEvent,
1429
- visitedReactions,
1430
- viewModel.states
1431
- );
1432
- chainReaction = rDef;
1433
- eventH = Math.max(H, chain.groupH);
1415
+ const chains = [];
1416
+ if (!visitedEvents.has(en)) {
1417
+ const rDefs = sliceReactionsByEvent.get(en) ?? [];
1418
+ for (const rDef of rDefs) {
1419
+ if (visitedReactions.has(rDef.handlerName)) continue;
1420
+ visitedReactions.add(rDef.handlerName);
1421
+ const chain = measureChain(
1422
+ rDef,
1423
+ sliceReactionByEvent,
1424
+ visitedReactions,
1425
+ viewModel.states
1426
+ );
1427
+ chains.push({ chain, reaction: rDef });
1428
+ }
1429
+ if (rDefs.length > 0) visitedEvents.add(en);
1434
1430
  }
1435
1431
  measuredEvents.push({
1436
1432
  eventName: en,
1437
1433
  actionName,
1438
- h: eventH,
1439
- chain,
1440
- chainReaction
1434
+ h: H,
1435
+ chains
1441
1436
  });
1442
1437
  if (evtBlockH > 0) evtBlockH += GAP / 2;
1443
1438
  evtBlockH += H;
@@ -1459,12 +1454,7 @@ function computeLayout(viewModel) {
1459
1454
  let chainWatermark = -Infinity;
1460
1455
  const eventYMap = /* @__PURE__ */ new Map();
1461
1456
  for (let ei = 0; ei < measuredEvents.length; ei++) {
1462
- const {
1463
- eventName: en,
1464
- actionName,
1465
- chain,
1466
- chainReaction
1467
- } = measuredEvents[ei];
1457
+ const { eventName: en, actionName, chains } = measuredEvents[ei];
1468
1458
  ns.push({
1469
1459
  key: `e:${en}:${slice.name}:${actionName}`,
1470
1460
  pos: { x: eventColX, y: evtY },
@@ -1475,7 +1465,7 @@ function computeLayout(viewModel) {
1475
1465
  reactions: eventReactions.get(en)
1476
1466
  });
1477
1467
  eventYMap.set(en, evtY);
1478
- if (chain && chainReaction) {
1468
+ for (const { chain, reaction: chainReaction } of chains) {
1479
1469
  const rX = eventColX + W + GAP * 3;
1480
1470
  const minReactionY = chainWatermark + chain.dispatchBlockH / 2 - H / 2;
1481
1471
  const reactionY = Math.max(evtY, minReactionY);
@@ -1557,33 +1547,18 @@ function computeLayout(viewModel) {
1557
1547
  y += primaryBlockH + GAP / 2;
1558
1548
  partIdx++;
1559
1549
  }
1560
- const remainingYByEvent = /* @__PURE__ */ new Map();
1561
1550
  for (const r of slice.reactions) {
1562
1551
  if (r.isVoid || visitedReactions.has(r.handlerName)) continue;
1563
- const trigNode = ns.find(
1564
- (n) => n.type === "event" && n.label === r.event && n.key.includes(slice.name)
1565
- );
1566
- const baseY = trigNode ? trigNode.pos.y : y;
1567
- const rY = remainingYByEvent.get(r.event) ?? baseY;
1568
1552
  const rX = sliceRightX + GAP * 2;
1569
- const rp = { x: rX, y: rY };
1553
+ const rp = { x: rX, y };
1570
1554
  ns.push({
1571
1555
  key: `r:${r.handlerName}:${slice.name}`,
1572
1556
  pos: rp,
1573
1557
  type: "reaction",
1574
1558
  label: r.handlerName
1575
1559
  });
1576
- if (trigNode) {
1577
- es.push({
1578
- from: { x: trigNode.pos.x + W, y: trigNode.pos.y + H / 2 },
1579
- to: { x: rp.x, y: rp.y + H / 2 },
1580
- color: COLORS.reaction.border,
1581
- dash: true
1582
- });
1583
- }
1584
- remainingYByEvent.set(r.event, rY + H + GAP / 2);
1585
1560
  sliceRightX = Math.max(sliceRightX, rX + W + GAP);
1586
- y = Math.max(y, rY + H + GAP / 2);
1561
+ y += H + GAP / 2;
1587
1562
  }
1588
1563
  const bbox = {
1589
1564
  minX: Infinity,