Skip to content

Commit

Permalink
refactor(render/dot): replaces module global with a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij committed Dec 27, 2024
1 parent 8b11f73 commit 23e96a5
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 142 deletions.
47 changes: 33 additions & 14 deletions dist/render/dot/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-JFcPEOL9zVRvLlspORt02ETHoKUWv8R2XhdmBO7VI0c6my3zL0u0xgeZUcaYPQWDMLvoj1SFuFJ+vU+4GupJQA=="></script>
<script src="smcat-online-interpreter.min.js" type="module" defer integrity="sha512-etmvkaSIz9SFW5+1Bzzn6ieLBYn8JmftNXi2SQJYmJHAr7/jyzkzeZgLI/7MJnqaZoHuO4mYnNrhLrnjn05RhA=="></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-52vZVh/elfXk881aqqod5J6ZLaHea1tt7k0RFATXooP+1NCeIRS0NN0GE8rfDIrMAQWUH0FVxCUKwqaSfGVb2Q=="
integrity="sha512-Q3WVgrYhrnoimmIvsjeTizYqt56f7VRe2kDpZWErZMa3u3KK7n7nmIEEOlwSH9zEOgRU5spqT8M8+IRFXeuyIg=="
></script>
<style>
body { font-family: sans-serif; margin: 0 auto; max-width: 799px;
Expand Down
110 changes: 55 additions & 55 deletions docs/smcat-online-interpreter.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/smcat-online-interpreter.min.js.map

Large diffs are not rendered by default.

108 changes: 54 additions & 54 deletions docs/state-machine-cat-inpage.min.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

39 changes: 28 additions & 11 deletions src/render/dot/index.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-params */
/* eslint-disable max-lines */
/* eslint-disable no-use-before-define */
/* eslint-disable complexity */
Expand Down Expand Up @@ -29,8 +30,6 @@ import {
stateNote,
} from "./utl.mjs";

let gRenderedTransitions: Set<number> = new Set();

function initial(pState: IStateNormalized, pIndent: string): string {
const lActiveAttribute = pState.active ? " penwidth=3.0" : "";

Expand Down Expand Up @@ -94,6 +93,7 @@ function compositeRegular(
pIndent: string,
pOptions: IRenderOptions,
pModel: StateMachineModel,
pRenderedTransitions: Set<number>,
): string {
// eslint-disable-next-line no-nested-ternary
const lPenWidth = pState.isParallelArea
Expand Down Expand Up @@ -121,7 +121,7 @@ ${pIndent} class="${pState.class}" label= <
${lLabelTag}
${pIndent} > style=${lStyle} penwidth=${lPenWidth}${pState.colorAttribute}${pState.fontColorAttribute}
${pIndent} "${pState.name}" [shape=point style=invis margin=0 width=0 height=0 fixedsize=true]
${states(pState?.statemachine?.states ?? [], `${pIndent} `, pOptions, pModel)}
${states(pState?.statemachine?.states ?? [], `${pIndent} `, pOptions, pModel, pRenderedTransitions)}
${pIndent} }${pState.noteText}`;
}

Expand All @@ -130,9 +130,16 @@ function regular(
pIndent: string,
pOptions: IRenderOptions,
pModel: StateMachineModel,
pRenderedTransitions: Set<number>,
): string {
if (pState.statemachine) {
return compositeRegular(pState, pIndent, pOptions, pModel);
return compositeRegular(
pState,
pIndent,
pOptions,
pModel,
pRenderedTransitions,
);
}
return atomicRegular(pState, pIndent);
}
Expand Down Expand Up @@ -240,14 +247,15 @@ function state(
pIndent: string,
pOptions: IRenderOptions,
pModel: StateMachineModel,
pRenderedTransitions: Set<number>,
): string {
const lState = normalizeState(pState, pOptions, pIndent);
const lCandidateTransitions = pModel.findTransitionsToSiblings(
pState.name,
gRenderedTransitions,
pRenderedTransitions,
);
lCandidateTransitions.forEach((pTransition) => {
gRenderedTransitions.add(pTransition.id);
pRenderedTransitions.add(pTransition.id);
});
const lTransitions = transitions(
lCandidateTransitions,
Expand All @@ -263,6 +271,7 @@ function state(
pIndent,
pOptions,
pModel,
pRenderedTransitions,
) +
lTransitions +
"\n"
Expand All @@ -274,9 +283,12 @@ function states(
pIndent: string,
pOptions: IRenderOptions,
pModel: StateMachineModel,
pRenderedTransitions: Set<number>,
): string {
return pStates
.map((pState) => state(pState, pIndent, pOptions, pModel))
.map((pState) =>
state(pState, pIndent, pOptions, pModel, pRenderedTransitions),
)
.join("");
}

Expand Down Expand Up @@ -381,8 +393,14 @@ export default function renderDot(
const lNodeAttributes = buildNodeAttributes(pOptions.dotNodeAttrs || []);
const lEdgeAttributes = buildEdgeAttributes(pOptions.dotEdgeAttrs || []);
const lModel = new StateMachineModel(pStateMachine);
gRenderedTransitions = new Set();
const lStates = states(pStateMachine.states, pIndent, pOptions, lModel);
const lRenderedTransitions: Set<number> = new Set();
const lStates = states(
pStateMachine.states,
pIndent,
pOptions,
lModel,
lRenderedTransitions,
);
// ideally, we render transitions together with the states. However, in graphviz
// that only renders as we want to if we if the transition is _within_ the state.
// In this guy 'a' is rendered within cluster_b, though
Expand Down Expand Up @@ -412,14 +430,13 @@ export default function renderDot(
// 2. Render all other transitions separately (below)
const lRemainingTransitions = transitions(
lModel.flattenedTransitions.filter(
(pTransition) => !gRenderedTransitions.has(pTransition.id),
(pTransition) => !lRenderedTransitions.has(pTransition.id),
),
pIndent,
pOptions,
lModel,
);

gRenderedTransitions = new Set();
return `digraph "state transitions" {
${lGraphAttributes}
node [${lNodeAttributes}]
Expand Down

0 comments on commit 23e96a5

Please sign in to comment.