Skip to content

Commit 895f9d4

Browse files
committed
6691: update packet diagram to use new class-based DB approach
on-behalf-of: @Mermaid-Chart <hello@mermaidchart.com>
1 parent 5986189 commit 895f9d4

File tree

5 files changed

+69
-58
lines changed

5 files changed

+69
-58
lines changed

.changeset/vast-buses-see.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'mermaid': minor
3+
---
4+
5+
chore: Update packet diagram to use new class-based database structure
Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getConfig as commonGetConfig } from '../../config.js';
22
import type { PacketDiagramConfig } from '../../config.type.js';
33
import DEFAULT_CONFIG from '../../defaultConfig.js';
4+
import type { DiagramDB } from '../../diagram-api/types.js';
45
import { cleanAndMerge } from '../../utils.js';
56
import {
67
clear as commonClear,
@@ -11,49 +12,42 @@ import {
1112
setAccTitle,
1213
setDiagramTitle,
1314
} from '../common/commonDb.js';
14-
import type { PacketDB, PacketData, PacketWord } from './types.js';
15-
16-
const defaultPacketData: PacketData = {
17-
packet: [],
18-
};
19-
20-
let data: PacketData = structuredClone(defaultPacketData);
21-
15+
import type { PacketWord } from './types.js';
2216
const DEFAULT_PACKET_CONFIG: Required<PacketDiagramConfig> = DEFAULT_CONFIG.packet;
2317

24-
const getConfig = (): Required<PacketDiagramConfig> => {
25-
const config = cleanAndMerge({
26-
...DEFAULT_PACKET_CONFIG,
27-
...commonGetConfig().packet,
28-
});
29-
if (config.showBits) {
30-
config.paddingY += 10;
18+
export class PacketDB implements DiagramDB {
19+
private packet: PacketWord[] = [];
20+
21+
public getConfig() {
22+
const config = cleanAndMerge({
23+
...DEFAULT_PACKET_CONFIG,
24+
...commonGetConfig().packet,
25+
});
26+
if (config.showBits) {
27+
config.paddingY += 10;
28+
}
29+
return config;
3130
}
32-
return config;
33-
};
3431

35-
const getPacket = (): PacketWord[] => data.packet;
32+
public getPacket() {
33+
return this.packet;
34+
}
3635

37-
const pushWord = (word: PacketWord) => {
38-
if (word.length > 0) {
39-
data.packet.push(word);
36+
public pushWord(word: PacketWord) {
37+
if (word.length > 0) {
38+
this.packet.push(word);
39+
}
4040
}
41-
};
4241

43-
const clear = () => {
44-
commonClear();
45-
data = structuredClone(defaultPacketData);
46-
};
42+
public clear() {
43+
commonClear();
44+
this.packet = [];
45+
}
4746

48-
export const db: PacketDB = {
49-
pushWord,
50-
getPacket,
51-
getConfig,
52-
clear,
53-
setAccTitle,
54-
getAccTitle,
55-
setDiagramTitle,
56-
getDiagramTitle,
57-
getAccDescription,
58-
setAccDescription,
59-
};
47+
public setAccTitle = setAccTitle;
48+
public getAccTitle = getAccTitle;
49+
public setDiagramTitle = setDiagramTitle;
50+
public getDiagramTitle = getDiagramTitle;
51+
public getAccDescription = getAccDescription;
52+
public setAccDescription = setAccDescription;
53+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { DiagramDefinition } from '../../diagram-api/types.js';
2-
import { db } from './db.js';
2+
import { PacketDB } from './db.js';
33
import { parser } from './parser.js';
44
import { renderer } from './renderer.js';
55
import { styles } from './styles.js';
66

77
export const diagram: DiagramDefinition = {
88
parser,
9-
db,
9+
get db() {
10+
return new PacketDB();
11+
},
1012
renderer,
1113
styles,
1214
};

packages/mermaid/src/diagrams/packet/packet.spec.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
import { it, describe, expect } from 'vitest';
2-
import { db } from './db.js';
2+
import { PacketDB } from './db.js';
33
import { parser } from './parser.js';
44

5-
const { clear, getPacket, getDiagramTitle, getAccTitle, getAccDescription } = db;
6-
75
describe('packet diagrams', () => {
6+
let db: PacketDB;
87
beforeEach(() => {
9-
clear();
8+
db = new PacketDB();
9+
if (parser.parser) {
10+
parser.parser.yy = db;
11+
}
1012
});
1113

1214
it('should handle a packet-beta definition', async () => {
1315
const str = `packet-beta`;
1416
await expect(parser.parse(str)).resolves.not.toThrow();
15-
expect(getPacket()).toMatchInlineSnapshot('[]');
17+
expect(db.getPacket()).toMatchInlineSnapshot('[]');
1618
});
1719

1820
it('should handle a packet definition', async () => {
1921
const str = `packet`;
2022
await expect(parser.parse(str)).resolves.not.toThrow();
21-
expect(getPacket()).toMatchInlineSnapshot('[]');
23+
expect(db.getPacket()).toMatchInlineSnapshot('[]');
2224
});
2325

2426
it('should handle diagram with data and title', async () => {
@@ -29,10 +31,10 @@ describe('packet diagrams', () => {
2931
0-10: "test"
3032
`;
3133
await expect(parser.parse(str)).resolves.not.toThrow();
32-
expect(getDiagramTitle()).toMatchInlineSnapshot('"Packet diagram"');
33-
expect(getAccTitle()).toMatchInlineSnapshot('"Packet accTitle"');
34-
expect(getAccDescription()).toMatchInlineSnapshot('"Packet accDescription"');
35-
expect(getPacket()).toMatchInlineSnapshot(`
34+
expect(db.getDiagramTitle()).toMatchInlineSnapshot('"Packet diagram"');
35+
expect(db.getAccTitle()).toMatchInlineSnapshot('"Packet accTitle"');
36+
expect(db.getAccDescription()).toMatchInlineSnapshot('"Packet accDescription"');
37+
expect(db.getPacket()).toMatchInlineSnapshot(`
3638
[
3739
[
3840
{
@@ -52,7 +54,7 @@ describe('packet diagrams', () => {
5254
11: "single"
5355
`;
5456
await expect(parser.parse(str)).resolves.not.toThrow();
55-
expect(getPacket()).toMatchInlineSnapshot(`
57+
expect(db.getPacket()).toMatchInlineSnapshot(`
5658
[
5759
[
5860
{
@@ -78,7 +80,7 @@ describe('packet diagrams', () => {
7880
+16: "word"
7981
`;
8082
await expect(parser.parse(str)).resolves.not.toThrow();
81-
expect(getPacket()).toMatchInlineSnapshot(`
83+
expect(db.getPacket()).toMatchInlineSnapshot(`
8284
[
8385
[
8486
{
@@ -104,7 +106,7 @@ describe('packet diagrams', () => {
104106
+16: "word"
105107
`;
106108
await expect(parser.parse(str)).resolves.not.toThrow();
107-
expect(getPacket()).toMatchInlineSnapshot(`
109+
expect(db.getPacket()).toMatchInlineSnapshot(`
108110
[
109111
[
110112
{
@@ -130,7 +132,7 @@ describe('packet diagrams', () => {
130132
11-90: "multiple"
131133
`;
132134
await expect(parser.parse(str)).resolves.not.toThrow();
133-
expect(getPacket()).toMatchInlineSnapshot(`
135+
expect(db.getPacket()).toMatchInlineSnapshot(`
134136
[
135137
[
136138
{
@@ -172,7 +174,7 @@ describe('packet diagrams', () => {
172174
17-63: "multiple"
173175
`;
174176
await expect(parser.parse(str)).resolves.not.toThrow();
175-
expect(getPacket()).toMatchInlineSnapshot(`
177+
expect(db.getPacket()).toMatchInlineSnapshot(`
176178
[
177179
[
178180
{

packages/mermaid/src/diagrams/packet/parser.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { parse } from '@mermaid-js/parser';
33
import type { ParserDefinition } from '../../diagram-api/types.js';
44
import { log } from '../../logger.js';
55
import { populateCommonDb } from '../common/populateCommonDb.js';
6-
import { db } from './db.js';
6+
import { PacketDB } from './db.js';
77
import type { PacketBlock, PacketWord } from './types.js';
88

99
const maxPacketSize = 10_000;
1010

11-
const populate = (ast: Packet) => {
11+
const populate = (ast: Packet, db: PacketDB) => {
1212
populateCommonDb(ast, db);
1313
let lastBit = -1;
1414
let word: PacketWord = [];
@@ -91,9 +91,17 @@ const getNextFittingBlock = (
9191
};
9292

9393
export const parser: ParserDefinition = {
94+
// @ts-expect-error - PacketDB is not assignable to DiagramDB
95+
parser: { yy: undefined },
9496
parse: async (input: string): Promise<void> => {
9597
const ast: Packet = await parse('packet', input);
98+
const db = parser.parser?.yy;
99+
if (!(db instanceof PacketDB)) {
100+
throw new Error(
101+
'parser.parser?.yy was not a PacketDB. This is due to a bug within Mermaid, please report this issue at https://github.com/mermaid-js/mermaid/issues.'
102+
);
103+
}
96104
log.debug(ast);
97-
populate(ast);
105+
populate(ast, db);
98106
},
99107
};

0 commit comments

Comments
 (0)