Skip to content

Commit 23e96a5

Browse files
committed
refactor(render/dot): replaces module global with a parameter
1 parent 8b11f73 commit 23e96a5

File tree

8 files changed

+178
-142
lines changed

8 files changed

+178
-142
lines changed

dist/render/dot/index.mjs

Lines changed: 33 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<meta property="og:image:type" content="image/png" />
3636
<link rel="canonical" href="https://state-machine-cat.js.org">
3737
<script nonce="known-inline-script">let LOG = false;</script>
38-
<script src="smcat-online-interpreter.min.js" type="module" defer integrity="sha512-JFcPEOL9zVRvLlspORt02ETHoKUWv8R2XhdmBO7VI0c6my3zL0u0xgeZUcaYPQWDMLvoj1SFuFJ+vU+4GupJQA=="></script>
38+
<script src="smcat-online-interpreter.min.js" type="module" defer integrity="sha512-etmvkaSIz9SFW5+1Bzzn6ieLBYn8JmftNXi2SQJYmJHAr7/jyzkzeZgLI/7MJnqaZoHuO4mYnNrhLrnjn05RhA=="></script>
3939
<script defer src="https://code.getmdl.io/1.3.0/material.min.js" async></script>
4040
<meta name="viewport" content="width=device-width, initial-scale=1.0">
4141
<meta name="theme-color" content="purple">

docs/inpage.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
src="state-machine-cat-inpage.min.js"
2424
type="module"
2525
defer
26-
integrity="sha512-52vZVh/elfXk881aqqod5J6ZLaHea1tt7k0RFATXooP+1NCeIRS0NN0GE8rfDIrMAQWUH0FVxCUKwqaSfGVb2Q=="
26+
integrity="sha512-Q3WVgrYhrnoimmIvsjeTizYqt56f7VRe2kDpZWErZMa3u3KK7n7nmIEEOlwSH9zEOgRU5spqT8M8+IRFXeuyIg=="
2727
></script>
2828
<style>
2929
body { font-family: sans-serif; margin: 0 auto; max-width: 799px;

docs/smcat-online-interpreter.min.js

Lines changed: 55 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/smcat-online-interpreter.min.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/state-machine-cat-inpage.min.js

Lines changed: 54 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/state-machine-cat-inpage.min.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/render/dot/index.mts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-params */
12
/* eslint-disable max-lines */
23
/* eslint-disable no-use-before-define */
34
/* eslint-disable complexity */
@@ -29,8 +30,6 @@ import {
2930
stateNote,
3031
} from "./utl.mjs";
3132

32-
let gRenderedTransitions: Set<number> = new Set();
33-
3433
function initial(pState: IStateNormalized, pIndent: string): string {
3534
const lActiveAttribute = pState.active ? " penwidth=3.0" : "";
3635

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

@@ -130,9 +130,16 @@ function regular(
130130
pIndent: string,
131131
pOptions: IRenderOptions,
132132
pModel: StateMachineModel,
133+
pRenderedTransitions: Set<number>,
133134
): string {
134135
if (pState.statemachine) {
135-
return compositeRegular(pState, pIndent, pOptions, pModel);
136+
return compositeRegular(
137+
pState,
138+
pIndent,
139+
pOptions,
140+
pModel,
141+
pRenderedTransitions,
142+
);
136143
}
137144
return atomicRegular(pState, pIndent);
138145
}
@@ -240,14 +247,15 @@ function state(
240247
pIndent: string,
241248
pOptions: IRenderOptions,
242249
pModel: StateMachineModel,
250+
pRenderedTransitions: Set<number>,
243251
): string {
244252
const lState = normalizeState(pState, pOptions, pIndent);
245253
const lCandidateTransitions = pModel.findTransitionsToSiblings(
246254
pState.name,
247-
gRenderedTransitions,
255+
pRenderedTransitions,
248256
);
249257
lCandidateTransitions.forEach((pTransition) => {
250-
gRenderedTransitions.add(pTransition.id);
258+
pRenderedTransitions.add(pTransition.id);
251259
});
252260
const lTransitions = transitions(
253261
lCandidateTransitions,
@@ -263,6 +271,7 @@ function state(
263271
pIndent,
264272
pOptions,
265273
pModel,
274+
pRenderedTransitions,
266275
) +
267276
lTransitions +
268277
"\n"
@@ -274,9 +283,12 @@ function states(
274283
pIndent: string,
275284
pOptions: IRenderOptions,
276285
pModel: StateMachineModel,
286+
pRenderedTransitions: Set<number>,
277287
): string {
278288
return pStates
279-
.map((pState) => state(pState, pIndent, pOptions, pModel))
289+
.map((pState) =>
290+
state(pState, pIndent, pOptions, pModel, pRenderedTransitions),
291+
)
280292
.join("");
281293
}
282294

@@ -381,8 +393,14 @@ export default function renderDot(
381393
const lNodeAttributes = buildNodeAttributes(pOptions.dotNodeAttrs || []);
382394
const lEdgeAttributes = buildEdgeAttributes(pOptions.dotEdgeAttrs || []);
383395
const lModel = new StateMachineModel(pStateMachine);
384-
gRenderedTransitions = new Set();
385-
const lStates = states(pStateMachine.states, pIndent, pOptions, lModel);
396+
const lRenderedTransitions: Set<number> = new Set();
397+
const lStates = states(
398+
pStateMachine.states,
399+
pIndent,
400+
pOptions,
401+
lModel,
402+
lRenderedTransitions,
403+
);
386404
// ideally, we render transitions together with the states. However, in graphviz
387405
// that only renders as we want to if we if the transition is _within_ the state.
388406
// In this guy 'a' is rendered within cluster_b, though
@@ -412,14 +430,13 @@ export default function renderDot(
412430
// 2. Render all other transitions separately (below)
413431
const lRemainingTransitions = transitions(
414432
lModel.flattenedTransitions.filter(
415-
(pTransition) => !gRenderedTransitions.has(pTransition.id),
433+
(pTransition) => !lRenderedTransitions.has(pTransition.id),
416434
),
417435
pIndent,
418436
pOptions,
419437
lModel,
420438
);
421439

422-
gRenderedTransitions = new Set();
423440
return `digraph "state transitions" {
424441
${lGraphAttributes}
425442
node [${lNodeAttributes}]

0 commit comments

Comments
 (0)