Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Change flowDB to class based architecture. #6161

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from

Conversation

saurabhg772244
Copy link
Collaborator

@saurabhg772244 saurabhg772244 commented Jan 2, 2025

📑 Summary

  • Refactors the existing flowDb implementation from a functional approach to a class-based architecture to improve code organization, maintainability, and extensibility.
  • Return a new DB object when diagramObject.db is called.
  • Introduces new FlowDB class to encapsulate database operations and state
  • Migrates existing functions to class methods
  • Maintains existing API interface for backwards compatibility
  • Following functions still uses common DB:
    • defaultConfig
    • setAccTitle
    • setAccDescription
    • setDiagramTitle
    • getAccTitle
    • getAccDescription
    • getDiagramTitle
  • Reason why above functions still uses common DB: Refactor: Change flowDB to class based architecture. #6161 (comment)

📏 Design Decisions

  • The flowDb module previously relied on functions and global variables. Consequently, each call to mermaid.parse or mermaid.render resulted in changes to global variables.
  • This behavior caused issues when rendering multiple diagrams using the same Mermaid API, as the database object (DB) for the diagrams would get mixed up.
  • To address this, flowDb was refactored into a class-based structure. This change ensures that each diagramObject.db call creates a new instance of the database (DB), preventing conflicts between diagrams.
  • Arrow Functions are used instead of class Fields as it is required by flow.jison

📋 Tasks

Make sure you

  • 📖 have read the contribution guidelines
  • 💻 have added necessary unit/e2e tests.
  • 📓 have added documentation. Make sure MERMAID_RELEASE_VERSION is used for all new features.
  • 🦋 If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@github-actions github-actions bot added the Type: Other Not an enhancement or a bug label Jan 2, 2025
Copy link

netlify bot commented Jan 2, 2025

Deploy Preview for mermaid-js ready!

Name Link
🔨 Latest commit f7fe8f2
🔍 Latest deploy log https://app.netlify.com/sites/mermaid-js/deploys/6788d5d0c104e80008c378e7
😎 Deploy Preview https://deploy-preview-6161--mermaid-js.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

pkg-pr-new bot commented Jan 2, 2025

Open in Stackblitz

npm i https://pkg.pr.new/mermaid-js/mermaid@6161
npm i https://pkg.pr.new/mermaid-js/mermaid/@mermaid-js/layout-elk@6161
npm i https://pkg.pr.new/mermaid-js/mermaid/@mermaid-js/mermaid-zenuml@6161
npm i https://pkg.pr.new/mermaid-js/mermaid/@mermaid-js/parser@6161

commit: f7fe8f2

Copy link

codecov bot commented Jan 2, 2025

Codecov Report

Attention: Patch coverage is 0% with 965 lines in your changes missing coverage. Please review.

Project coverage is 4.48%. Comparing base (1d9c2aa) to head (f7fe8f2).

Files with missing lines Patch % Lines
packages/mermaid/src/diagrams/flowchart/flowDb.ts 0.00% 960 Missing ⚠️
...ages/mermaid/src/diagrams/flowchart/flowDiagram.ts 0.00% 4 Missing ⚠️
.../src/diagrams/flowchart/flowRenderer-v3-unified.ts 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           develop   #6161   +/-   ##
=======================================
  Coverage     4.47%   4.48%           
=======================================
  Files          383     383           
  Lines        54144   54124   -20     
  Branches       622     622           
=======================================
  Hits          2425    2425           
+ Misses       51719   51699   -20     
Flag Coverage Δ
unit 4.48% <0.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
.../src/diagrams/flowchart/flowRenderer-v3-unified.ts 0.00% <0.00%> (ø)
...ages/mermaid/src/diagrams/flowchart/flowDiagram.ts 0.00% <0.00%> (ø)
packages/mermaid/src/diagrams/flowchart/flowDb.ts 0.00% <0.00%> (ø)

Copy link

argos-ci bot commented Jan 2, 2025

The latest updates on your projects. Learn more about Argos notifications ↗︎

Awaiting the start of a new Argos build…

Copy link

changeset-bot bot commented Jan 3, 2025

🦋 Changeset detected

Latest commit: f7fe8f2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
mermaid Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@saurabhg772244 saurabhg772244 marked this pull request as ready for review January 3, 2025 06:17
Copy link
Collaborator

@knsv knsv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Copy link
Member

@aloisklink aloisklink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thank you for making this PR as easy to review as possible!

My biggest issues that I've spotted are:

  • We're using class fields and arrow functions instead of class methods, which is a bit strange and not as well supported on browsers. Why can't we just use normal class methods?
  • I think we need a changeset entry. Maybe we can make a fix: XXXXX or patch changeset release? Saying that the db returned from mermaid.mermaidAPI.getDiagramFromText() is now unique for flowcharts? From doing a search, it looks like ~27 people are using mermaidAPI.getDiagramFromText() already: https://github.com/search?q=mermaidAPI.getDiagramFromText&type=code

packages/mermaid/src/mermaidAPI.spec.ts Outdated Show resolved Hide resolved
packages/mermaid/src/mermaidAPI.spec.ts Outdated Show resolved Hide resolved
packages/mermaid/src/mermaidAPI.spec.ts Outdated Show resolved Hide resolved
packages/mermaid/src/mermaidAPI.spec.ts Show resolved Hide resolved
cypress/platform/saurabh.html Outdated Show resolved Hide resolved
packages/mermaid/src/diagrams/flowchart/flowDb.ts Outdated Show resolved Hide resolved
@ashishjain0512 ashishjain0512 changed the title Refactor: Change flwoDb to class based architecture. Refactor: Change flowDB to class based architecture. Jan 7, 2025
packages/mermaid/src/diagrams/flowchart/flowDb.ts Outdated Show resolved Hide resolved
this.vertexCounter++;

if (textObj !== undefined) {
this.config = getConfig();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to update the config when adding every vertex?

I see this is from the old code, so you can leave it in now, and fix later.

Comment on lines 39 to 60
private config = getConfig();
private vertices = new Map<string, FlowVertex>();
private edges: FlowEdge[] & { defaultInterpolate?: string; defaultStyle?: string[] } = [];
private classes = new Map<string, FlowClass>();
private subGraphs: FlowSubGraph[] = [];
private subGraphLookup = new Map<string, FlowSubGraph>();
private tooltips = new Map<string, string>();
private subCount = 0;
private firstGraphFlag = true;
private direction: string | undefined;

private version: string | undefined; // As in graph

private secCount = -1;
private posCrossRef: number[] = [];

// Functions to be run after graph rendering
private funs: ((element: Element) => void)[] = []; // cspell:ignore funs

constructor() {
this.funs.push(this.setupToolTips);
this.clear();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getConfig is called when initailizing, and also inside this.clear function, so one call is redundant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that in order to keep this PR's change surface as small as possible, we could leave this in and fix this in another PR.

With this new class style, we might not even need a .clear() function (except maybe to clear the common DB values?).

Comment on lines +1092 to +1098
public defaultConfig = () => defaultConfig.flowchart;
public setAccTitle = setAccTitle;
public setAccDescription = setAccDescription;
public setDiagramTitle = setDiagramTitle;
public getAccTitle = getAccTitle;
public getAccDescription = getAccDescription;
public getDiagramTitle = getDiagramTitle;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hhhhmmmmmmmmmmmm, this means part of the DB will be tied to a diagram, and some parts will change based on the last rendered diagram.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it's probably something to fix next.

I'd imagine it would be more difficult, though, since I believe the Langium parsers also modify this shared state.

And since it's part of the DiagramAPI in

/**
* DiagramDB with fields that is required for all new diagrams.
*/
export type DiagramDBBase<T extends BaseDiagramConfig> = {
getConfig: () => Required<T>;
} & SetRequired<
DiagramDB,
| 'clear'
| 'getAccTitle'
| 'getDiagramTitle'
| 'getAccDescription'
| 'setAccDescription'
| 'setAccTitle'
| 'setDiagramTitle'
>;
, it might be a breaking change to change it? I'm not 100% sure though.

@saurabhg772244, I know you've already documented in the PR description that you haven't changed these functions. Can you maybe add a link to this comment thread to explain why we haven't changed them in this PR?

Comment on lines +59 to +61
this.funs.push(this.setupToolTips);
this.clear();
this.setGen('gen-2');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already done inside clear, so why do we need to do it here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're copied from the old init call in:

flowDb.clear();
flowDb.setGen('gen-2');

But @saurabhg772244 has changed .clear() to use 'gen-2' by default, see #6161 (comment)

IMO, my gut feeling is that it's probably better to change as little as possible in this PR, and maybe do the gen-1/gen-2 cleanup in a separate PR, to keep this PR as small as possible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We require gen-2 as gen-1 is deprecated and will break sub-graphs. https://mermaidchart.slack.com/archives/C06FGFCQVSR/p1735895537015209

packages/mermaid/src/diagrams/flowchart/flowDb.ts Outdated Show resolved Hide resolved
Copy link
Member

@aloisklink aloisklink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all your code looks good 1, but I'm still not sure if using arrow functions assigned to public instance fields is safe, since it looks like Safari 15.6 still had issues with it, which is a relatively recent browser.

Before merging this, can we make sure that this won't be a breaking change? Otherwise, we'd either want to make a Mermaid v12 release, or we should rewrite this PR so it still works on older browsers.

Footnotes

  1. Maybe other than the gen-1 to gen-2 change in https://github.com/mermaid-js/mermaid/pull/6161#discussion_r1903932273, but that's really minor.

Comment on lines +1092 to +1098
public defaultConfig = () => defaultConfig.flowchart;
public setAccTitle = setAccTitle;
public setAccDescription = setAccDescription;
public setDiagramTitle = setDiagramTitle;
public getAccTitle = getAccTitle;
public getAccDescription = getAccDescription;
public getDiagramTitle = getDiagramTitle;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it's probably something to fix next.

I'd imagine it would be more difficult, though, since I believe the Langium parsers also modify this shared state.

And since it's part of the DiagramAPI in

/**
* DiagramDB with fields that is required for all new diagrams.
*/
export type DiagramDBBase<T extends BaseDiagramConfig> = {
getConfig: () => Required<T>;
} & SetRequired<
DiagramDB,
| 'clear'
| 'getAccTitle'
| 'getDiagramTitle'
| 'getAccDescription'
| 'setAccDescription'
| 'setAccTitle'
| 'setDiagramTitle'
>;
, it might be a breaking change to change it? I'm not 100% sure though.

@saurabhg772244, I know you've already documented in the PR description that you haven't changed these functions. Can you maybe add a link to this comment thread to explain why we haven't changed them in this PR?

Comment on lines +59 to +61
this.funs.push(this.setupToolTips);
this.clear();
this.setGen('gen-2');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're copied from the old init call in:

flowDb.clear();
flowDb.setGen('gen-2');

But @saurabhg772244 has changed .clear() to use 'gen-2' by default, see #6161 (comment)

IMO, my gut feeling is that it's probably better to change as little as possible in this PR, and maybe do the gen-1/gen-2 cleanup in a separate PR, to keep this PR as small as possible.

.changeset/new-kiwis-listen.md Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Other Not an enhancement or a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants