@@ -18,22 +18,25 @@ const attachEndNode = (g: graphlib.Graph, stateName: string) => {
1818 g . setEdge ( stateName , magicEndNodeName ) ;
1919} ;
2020
21- const ensureUnspecifiedNodes = ( g : graphlib . Graph ) => {
21+ const createMissingNodes = ( g : graphlib . Graph ) => {
22+ const style = "fill: #ff0000;" ;
23+ const makeLabel = ( edgePointer ) => `${ edgePointer } (Missing)` ;
2224 g . edges ( ) . forEach ( ( edge ) => {
2325 if ( ! g . node ( edge . v ) ) {
24- g . setNode ( edge . v , { label : ` ${ edge . v } (Missing)` , style : "fill: #ff0000;" } ) ;
26+ g . setNode ( edge . v , { label : makeLabel ( edge . v ) , style } ) ;
2527 }
2628 if ( ! g . node ( edge . w ) ) {
27- g . setNode ( edge . w , { label : ` ${ edge . w } (Missing)` , style : "fill: #ff0000;" } ) ;
29+ g . setNode ( edge . w , { label : makeLabel ( edge . w ) , style } ) ;
2830 }
2931 } ) ;
3032} ;
3133
3234const roundNodes = ( g : graphlib . Graph ) => {
3335 g . nodes ( ) . forEach ( function ( v ) {
34- var node = g . node ( v ) ;
36+ const node = g . node ( v ) ;
3537 if ( node ) {
36- node . rx = node . ry = 5 ;
38+ node . rx = 5 ;
39+ node . ry = 5 ;
3740 }
3841 } ) ;
3942} ;
@@ -51,10 +54,11 @@ const getNodeOptions = (state) => {
5154} ;
5255
5356export function buildGraph ( stepFunction : StepFunction ) {
54- var g = new graphlib . Graph ( { compound : true , multigraph : true } ) . setGraph ( { } ) . setDefaultEdgeLabel ( ( ) => ( { } ) ) ;
57+ const g = new graphlib . Graph ( { compound : true , multigraph : true } ) . setGraph ( { } ) . setDefaultEdgeLabel ( ( ) => ( { } ) ) ;
5558
56- function traverse ( stepFunction : StepFunction , g : graphlib . Graph , groupName ?: string ) {
59+ const traverse = ( stepFunction : StepFunction , g : graphlib . Graph , groupName ?: string ) => {
5760 const startAtName = stepFunction . StartAt ;
61+ const isRootLevel = ! groupName ;
5862
5963 if ( groupName ) {
6064 g . setParent ( startAtName , groupName ) ;
@@ -65,10 +69,10 @@ export function buildGraph(stepFunction: StepFunction) {
6569 R . toPairs ( stepFunction . States ) . forEach ( ( [ stateName , state ] ) => {
6670 g . setNode ( stateName , { label : stateName , ...getNodeOptions ( state ) } ) ;
6771
68- if ( stateName === startAtName && ! groupName ) {
72+ if ( stateName === startAtName && isRootLevel ) {
6973 attachStartNode ( g , stateName ) ;
7074 }
71- if ( state . End && ! groupName ) {
75+ if ( state . End && isRootLevel ) {
7276 attachEndNode ( g , stateName ) ;
7377 }
7478
@@ -102,20 +106,17 @@ export function buildGraph(stepFunction: StepFunction) {
102106 g . setParent ( newGroupName , groupName ) ;
103107 }
104108
109+ const edgeOptions = { labelStyle : "font-style: italic;" } ;
110+
105111 state . Choices . forEach ( ( choice : Operator ) => {
106- g . setEdge ( stateName , choice . Next , {
107- label : stringifyChoiceOperator ( choice ) ,
108- labelStyle : "font-style: italic;" ,
109- } ) ;
112+ const label = stringifyChoiceOperator ( choice ) ;
113+ g . setEdge ( stateName , choice . Next , { label, ...edgeOptions } ) ;
110114 g . setParent ( choice . Next , newGroupName ) ;
111115 statesToAddToParent . delete ( choice . Next ) ;
112116 } ) ;
113-
114117 if ( state . Default ) {
115- g . setEdge ( stateName , state . Default , {
116- label : "Default" ,
117- labelStyle : "font-style: italic;" ,
118- } ) ;
118+ const label = "Default" ;
119+ g . setEdge ( stateName , state . Default , { label, ...edgeOptions } ) ;
119120 g . setParent ( state . Default , newGroupName ) ;
120121 statesToAddToParent . delete ( state . Default ) ;
121122 }
@@ -149,24 +150,14 @@ export function buildGraph(stepFunction: StepFunction) {
149150
150151 if ( state . Catch ) {
151152 state . Catch . forEach ( ( catcher ) => {
152- g . setEdge ( stateName , catcher . Next , {
153- label : ( catcher . ErrorEquals || [ ] ) . join ( " or " ) ,
154- labelStyle : "font-style: italic;" ,
155- } ) ;
153+ const label = ( catcher . ErrorEquals || [ ] ) . join ( " or " ) ;
154+ g . setEdge ( stateName , catcher . Next , { label, labelStyle : "font-style: italic;" } ) ;
156155 } ) ;
157156 }
158157 if ( state . Retry ) {
159- const edgeName = `Edge_${ uuidv4 ( ) } ` ;
160158 const conditionsLength = ( state . Retry || [ ] ) . length ;
161- g . setEdge (
162- stateName ,
163- stateName ,
164- {
165- label : `(${ conditionsLength } condition${ conditionsLength > 1 ? "s" : "" } )` ,
166- labelStyle : "font-style: italic;" ,
167- } ,
168- edgeName
169- ) ;
159+ const label = `(${ conditionsLength } condition${ conditionsLength > 1 ? "s" : "" } )` ;
160+ g . setEdge ( stateName , stateName , { label, labelStyle : "font-style: italic;" } ) ;
170161 }
171162 } ) ;
172163
@@ -175,10 +166,10 @@ export function buildGraph(stepFunction: StepFunction) {
175166 g . setParent ( stateName , groupName ) ;
176167 } ) ;
177168 }
178- }
169+ } ;
179170
180171 traverse ( stepFunction , g ) ;
181- ensureUnspecifiedNodes ( g ) ;
172+ createMissingNodes ( g ) ;
182173 roundNodes ( g ) ;
183174
184175 return JSON . stringify ( graphlib . json . write ( g ) ) ;
0 commit comments