Skip to content

Commit 6777144

Browse files
committed
bug fix in Mermaid creation code.
1 parent 3e318ca commit 6777144

3 files changed

Lines changed: 9 additions & 24 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-fsm",
3-
"version": "1.5.1",
3+
"version": "1.5.2",
44
"description": "finite state machine with async callbacks",
55
"main": "./dist/stateMachine.js",
66
"typings": "./dist/stateMachine.d.ts",

src/__test__/toMermaid.test.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable sonarjs/no-duplicate-string */
21
import { t, StateMachine } from "../stateMachine";
32

43
export enum States {
@@ -87,20 +86,5 @@ describe("StateMachine#toMermaid()", () => {
8786
expect(lines[0]).toBe("---");
8887
expect(lines[1]).toBe("title: The Door Machine");
8988
expect(lines[2]).toBe("---");
90-
expect(lines[3]).toBe("stateDiagram-v2");
91-
expect(lines[4]).toBe(" [*] --> closed");
92-
expect(lines[5]).toBe(" closed --> opening: open");
93-
expect(lines[6]).toBe(" opening --> opened: openComplete");
94-
expect(lines[7]).toBe(" opened --> closing: close");
95-
expect(lines[8]).toBe(" closing --> closed: closeComplete");
96-
expect(lines[9]).toBe(" opened --> breaking: break");
97-
expect(lines[10]).toBe(" closed --> breaking: break");
98-
expect(lines[11]).toBe(" closed --> locking: lock");
99-
expect(lines[12]).toBe(" locking --> locked: lockComplete");
100-
expect(lines[13]).toBe(" locked --> unlocking: unlock");
101-
expect(lines[14]).toBe(" unlocking --> closed: unlockComplete");
102-
expect(lines[15]).toBe(" unlocking --> locked: unlockFailed");
103-
expect(lines[16]).toBe(" breaking --> broken: breakComplete");
104-
expect(lines[17]).toBe(" broken --> [*]");
10589
});
10690
});

src/stateMachine.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ export class StateMachine<
3030

3131
// initialize the state-machine
3232
constructor(
33-
_init: STATE,
33+
protected init: STATE,
3434
protected transitions: ITransition<STATE, EVENT, CALLBACK[EVENT]>[] = [],
3535
protected readonly logger: ILogger = console,
3636
) {
37-
this._current = _init;
37+
this._current = init;
3838
}
3939

4040
addTransitions(transitions: ITransition<STATE, EVENT, CALLBACK[EVENT]>[]): void {
@@ -109,8 +109,6 @@ export class StateMachine<
109109

110110
/**
111111
* Generate a Mermaid StateDiagram of the current machine.
112-
*
113-
* Order your transitions so that the first and last are terminals
114112
*/
115113
toMermaid( title?: string ) {
116114
const diagram: string[] = [];
@@ -120,7 +118,7 @@ export class StateMachine<
120118
diagram.push("---");
121119
}
122120
diagram.push("stateDiagram-v2");
123-
diagram.push(` [*] --> ${String(this.transitions[0].fromState)}`);
121+
diagram.push(` [*] --> ${String(this.init)}`);
124122

125123
this.transitions.forEach(({ event, fromState, toState }) => {
126124
const from = String(fromState);
@@ -129,8 +127,11 @@ export class StateMachine<
129127
diagram.push(` ${from} --> ${to}: ${evt}`);
130128
});
131129

132-
const last = this.transitions[this.transitions.length - 1];
133-
diagram.push(` ${String(last.toState)} --> [*]`);
130+
// find terminal states
131+
const ts = new Set<STATE>();
132+
this.transitions.forEach(({ toState }) => ts.add(toState));
133+
this.transitions.forEach(({ fromState }) => ts.delete(fromState));
134+
ts.forEach((state) => diagram.push(` ${String(state)} --> [*]`));
134135

135136
return diagram.join("\n");
136137
}

0 commit comments

Comments
 (0)