@@ -23,13 +23,22 @@ export class MermaidState {
23
23
type ?: string ;
24
24
transition ?: string | Specification . Transition ;
25
25
end ?: boolean | Specification . End ;
26
+ compensatedBy ?: string ;
26
27
onErrors ?: Specification . Error [ ] ;
28
+ usedForCompensation ?: boolean ;
27
29
} ,
28
30
private isFirstState : boolean = false
29
31
) { }
30
32
31
33
sourceCode ( ) {
32
- return this . definitions ( ) + '\n' + this . transitions ( ) ;
34
+ const stateDefinition = this . definitions ( ) ;
35
+ const stateTransitions = this . transitions ( ) ;
36
+
37
+ const stateDescription = stateTransitions . reduce ( ( p , c ) => {
38
+ return p + '\n' + c ;
39
+ } , stateDefinition ) ;
40
+
41
+ return stateDescription ;
33
42
}
34
43
35
44
private definitions ( ) : string {
@@ -41,19 +50,18 @@ export class MermaidState {
41
50
) ;
42
51
}
43
52
44
- private transitions ( ) : string {
53
+ private transitions ( ) : string [ ] {
45
54
const transitions : string [ ] = [ ] ;
46
55
47
56
transitions . push ( ...this . startTransition ( ) ) ;
48
57
transitions . push ( ...this . dataConditionsTransitions ( ) ) ;
49
58
transitions . push ( ...this . eventConditionsTransition ( ) ) ;
50
59
transitions . push ( ...this . errorTransitions ( ) ) ;
51
60
transitions . push ( ...this . naturalTransition ( this . stateKeyDiagram ( this . state . name ) , this . state . transition ) ) ;
61
+ transitions . push ( ...this . compensatedByTransition ( ) ) ;
52
62
transitions . push ( ...this . endTransition ( ) ) ;
53
63
54
- return transitions . reduce ( ( p , c ) => {
55
- return p + '\n' + c ;
56
- } ) ;
64
+ return transitions ;
57
65
}
58
66
59
67
private stateKeyDiagram ( name : string | undefined ) {
@@ -178,34 +186,61 @@ export class MermaidState {
178
186
return transitions ;
179
187
}
180
188
189
+ private compensatedByTransition ( ) {
190
+ const transitions : string [ ] = [ ] ;
191
+
192
+ if ( this . state . compensatedBy ) {
193
+ transitions . push ( ...this . naturalTransition ( this . state . name , this . state . compensatedBy , 'compensated by' ) ) ;
194
+ }
195
+ return transitions ;
196
+ }
197
+
181
198
private definitionDetails ( ) {
199
+ let definition : string | undefined ;
200
+
182
201
switch ( this . state . type ) {
183
202
case 'sleep' :
184
- return this . sleepStateDetails ( ) ;
203
+ definition = this . sleepStateDetails ( ) ;
204
+ break ;
185
205
case 'event' :
186
- return undefined ; //NOTHING
206
+ // NOTHING
207
+ break ;
187
208
case 'operation' :
188
- return this . operationStateDetails ( ) ;
209
+ definition = this . operationStateDetails ( ) ;
210
+ break ;
189
211
case 'parallel' :
190
- return this . parallelStateDetails ( ) ;
212
+ definition = this . parallelStateDetails ( ) ;
213
+ break ;
191
214
case 'switch' :
192
215
const switchState : any = this . state ;
193
216
if ( switchState . dataConditions ) {
194
- return this . dataBasedSwitchStateDetails ( ) ;
217
+ definition = this . dataBasedSwitchStateDetails ( ) ;
218
+ break ;
195
219
}
196
220
if ( switchState . eventConditions ) {
197
- return this . eventBasedSwitchStateDetails ( ) ;
221
+ definition = this . eventBasedSwitchStateDetails ( ) ;
222
+ break ;
198
223
}
199
224
throw new Error ( `Unexpected switch type; \n state value= ${ JSON . stringify ( this . state , null , 4 ) } ` ) ;
200
225
case 'inject' :
201
- return undefined ; // NOTHING
226
+ // NOTHING
227
+ break ;
202
228
case 'foreach' :
203
- return this . foreachStateDetails ( ) ;
229
+ definition = this . foreachStateDetails ( ) ;
230
+ break ;
204
231
case 'callback' :
205
- return this . callbackStateDetails ( ) ;
232
+ definition = this . callbackStateDetails ( ) ;
233
+ break ;
206
234
default :
207
235
throw new Error ( `Unexpected type= ${ this . state . type } ; \n state value= ${ JSON . stringify ( this . state , null , 4 ) } ` ) ;
208
236
}
237
+
238
+ if ( this . state . usedForCompensation ) {
239
+ definition = definition ? definition : '' ;
240
+ definition = this . stateDescription ( this . stateKeyDiagram ( this . state . name ) , 'usedForCompensation\n' ) + definition ;
241
+ }
242
+
243
+ return definition ? definition : undefined ;
209
244
}
210
245
211
246
private definitionType ( ) {
@@ -349,7 +384,7 @@ export class MermaidState {
349
384
return this . stateKeyDiagram ( source ) + ' --> ' + this . stateKeyDiagram ( target ) + ( label ? ' : ' + label : '' ) ;
350
385
}
351
386
352
- private stateDescription ( stateName : string | undefined , description : string , value : string ) {
353
- return stateName + ` : ${ description } = ${ value } ` ;
387
+ private stateDescription ( stateName : string | undefined , description : string , value ? : string ) {
388
+ return stateName + ` : ${ description } ${ value !== undefined ? ' = ' + value : '' } ` ;
354
389
}
355
390
}
0 commit comments