Skip to content

Commit

Permalink
refactor(scxml): replaces module global with a proper counter class
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij committed Dec 25, 2024
1 parent 199cd71 commit 5b4ecf8
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 107 deletions.
20 changes: 10 additions & 10 deletions dist/parse/scxml/index.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<meta property="og:image:type" content="image/png" />
<link rel="canonical" href="https://state-machine-cat.js.org">
<script nonce="known-inline-script">let LOG = false;</script>
<script src="smcat-online-interpreter.min.js" type="module" defer integrity="sha512-al0prNllJWvqcRP4KL92g31HS0UFScSjat2Z/gNZ/y1XHo2V36JDhYI+dLFaIqujcCJKeyiXK0aA0VR3qaTWFg=="></script>
<script src="smcat-online-interpreter.min.js" type="module" defer integrity="sha512-bNaySAes5WU/PELPbHEbuokRRzTu12F81U8KJz5czxl4jIcXVPw9a4UvkUfMYfpO5hJGDogb38DCTARlJXIC7A=="></script>
<script defer src="https://code.getmdl.io/1.3.0/material.min.js" async></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="purple">
Expand Down
2 changes: 1 addition & 1 deletion docs/inpage.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
src="state-machine-cat-inpage.min.js"
type="module"
defer
integrity="sha512-12WmjzUUso9QyOVws/FxWSeM8JEabqFWu2j2xB2193mYrjPnFHDLpJc54Y/4UHSjGkewBlOnl3BblJNeNdMtzA=="
integrity="sha512-hXI4LlpsUxkExo2T9/L4lzdiPM8aHNlke0TEjMvvtOdLveywHUvOTDUjbjh5ExFoNIQq4XJ50x4+JswrvLfP1Q=="
></script>
<style>
body { font-family: sans-serif; margin: 0 auto; max-width: 799px;
Expand Down
52 changes: 26 additions & 26 deletions docs/smcat-online-interpreter.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/smcat-online-interpreter.min.js.map

Large diffs are not rendered by default.

102 changes: 51 additions & 51 deletions docs/state-machine-cat-inpage.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/state-machine-cat-inpage.min.js.map

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions src/parse/scxml/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import fastxml from "fast-xml-parser";
import he from "he";
import traverse from "neotraverse";
import utl from "../../transform/utl.mjs";
import { Counter } from "../../counter.mjs";
import parserHelpers from "../parser-helpers.mjs";
import utl from "../../transform/utl.mjs";
import { castArray } from "./utl.mjs";
import { normalizeMachine } from "./normalize-machine.mjs";

Expand Down Expand Up @@ -146,8 +147,9 @@ function extractTransitionAttributes(pTransition) {

/**
* @param {import("./scxml").INormalizedSCXMLState} pState
* @param {import("../../counter.mjs").Counter} pCounter
*/
function reduceTransition(pState) {
function reduceTransition(pState, pCounter) {
/**
* @param {import("./scxml").ISCXMLTransition[]} pAllTransitions
* @param {import("./scxml").ISCXMLTransition} pTransition
Expand All @@ -161,7 +163,7 @@ function reduceTransition(pState) {

return pAllTransitions.concat(
lTargets.map((pTarget) => ({
id: parserHelpers.nextTransitionId(),
id: pCounter.next(),
from: pState.id,
// a 'target-less transition' is typically
// a self-transition
Expand All @@ -174,15 +176,16 @@ function reduceTransition(pState) {

/**
* @param {import("./scxml").INormalizedSCXMLState[]} pStates
* @param {import("../../counter.mjs").Counter} pCounter
* @returns {import("../../../types/state-machine-cat").ITransition[]}
*/
function extractTransitions(pStates) {
function extractTransitions(pStates, pCounter) {
return pStates
.filter((pState) => Object.hasOwn(pState, "transition"))
.reduce((pAllTransitions, pThisState) => {
const lTransitionAsArray = castArray(pThisState.transition);
return pAllTransitions.concat(
lTransitionAsArray.reduce(reduceTransition(pThisState), []),
lTransitionAsArray.reduce(reduceTransition(pThisState, pCounter), []),
);
}, []);
}
Expand All @@ -201,16 +204,15 @@ function mapMachine(pSCXMLStateMachine) {
.concat(lNormalizedMachine.history.map(mapState("history")))
.concat(lNormalizedMachine.final.map(mapState("final"))),
};
parserHelpers.resetTransitionId();
const lCounter = new Counter();

const lTransitions = extractTransitions(lNormalizedMachine.initial)
.concat(extractTransitions(lNormalizedMachine.state))
.concat(extractTransitions(lNormalizedMachine.parallel));
const lTransitions = extractTransitions(lNormalizedMachine.initial, lCounter)
.concat(extractTransitions(lNormalizedMachine.state, lCounter))
.concat(extractTransitions(lNormalizedMachine.parallel, lCounter));

if (lTransitions.length > 0) {
lReturnValue.transitions = lTransitions;
}
parserHelpers.resetTransitionId();

return lReturnValue;
}
Expand Down

0 comments on commit 5b4ecf8

Please sign in to comment.