2023-01-03 01:26:55 -06:00
|
|
|
import mermaid from 'https://unpkg.com/mermaid@9.2.2/dist/mermaid.esm.min.mjs';
|
|
|
|
import { debounce } from '../util.js';
|
|
|
|
|
2022-11-07 17:44:57 -06:00
|
|
|
export class Vertex {
|
|
|
|
constructor(data) {
|
|
|
|
this.data = data;
|
|
|
|
this.edges = {
|
|
|
|
from: [],
|
|
|
|
to: [],
|
|
|
|
};
|
|
|
|
}
|
2022-11-30 09:13:52 -06:00
|
|
|
|
|
|
|
getEdges(label, away) {
|
2022-12-31 16:08:42 -06:00
|
|
|
return this.edges[away ? 'from' : 'to'].filter(
|
|
|
|
(edge) => edge.label === label,
|
2022-11-30 09:13:52 -06:00
|
|
|
);
|
|
|
|
}
|
2022-11-07 17:44:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Edge {
|
|
|
|
constructor(label, from, to, data) {
|
|
|
|
this.from = from;
|
|
|
|
this.to = to;
|
|
|
|
this.label = label;
|
|
|
|
this.data = data;
|
|
|
|
}
|
|
|
|
}
|
2022-11-11 16:52:57 -06:00
|
|
|
|
2022-11-30 09:13:52 -06:00
|
|
|
export class CategorizedEdges {}
|
2022-11-11 16:52:57 -06:00
|
|
|
|
2022-11-07 17:44:57 -06:00
|
|
|
export class Graph {
|
|
|
|
constructor() {
|
|
|
|
this.vertices = new Map();
|
|
|
|
this.edgeLabels = new Map();
|
|
|
|
this.nextVertexId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
addVertex(id, data) {
|
|
|
|
// Support simple case of auto-incremented numeric ids
|
2022-12-31 16:08:42 -06:00
|
|
|
if (typeof id === 'object') {
|
2022-11-07 17:44:57 -06:00
|
|
|
data = id;
|
|
|
|
id = this.nextVertexId++;
|
|
|
|
}
|
2023-01-03 01:26:55 -06:00
|
|
|
if (this.vertices.has(id)) {
|
|
|
|
throw new Error(`Vertex already exists with id: ${id}`);
|
|
|
|
}
|
2022-11-07 17:44:57 -06:00
|
|
|
const vertex = new Vertex(data);
|
2023-01-03 01:26:55 -06:00
|
|
|
console.log('addVertex', vertex);
|
2022-11-07 17:44:57 -06:00
|
|
|
this.vertices.set(id, vertex);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
getVertex(id) {
|
|
|
|
return this.vertices.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
getVertexData(id) {
|
|
|
|
return this.getVertex(id)?.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
getVertices() {
|
|
|
|
return Array.from(this.vertices.values()).map(({ data }) => data);
|
|
|
|
}
|
|
|
|
|
|
|
|
getEdge(label, from, to) {
|
|
|
|
const edges = this.edgeLabels.get(label);
|
|
|
|
return edges?.get(JSON.stringify({ from, to }));
|
|
|
|
}
|
|
|
|
|
|
|
|
setEdge(label, from, to, edge) {
|
|
|
|
let edges = this.edgeLabels.get(label);
|
|
|
|
if (!edges) {
|
|
|
|
edges = new Map();
|
|
|
|
this.edgeLabels.set(label, edges);
|
|
|
|
}
|
|
|
|
edges.set(JSON.stringify({ from, to }), edge);
|
|
|
|
}
|
|
|
|
|
|
|
|
addEdge(label, from, to, data) {
|
2023-01-03 01:26:55 -06:00
|
|
|
console.log('addEdge', { from, to });
|
2022-11-07 17:44:57 -06:00
|
|
|
if (this.getEdge(label, from, to)) {
|
|
|
|
throw new Error(`Edge ${label} from ${from} to ${to} already exists`);
|
|
|
|
}
|
|
|
|
const edge = new Edge(label, from, to, data);
|
|
|
|
this.setEdge(label, from, to, edge);
|
|
|
|
this.getVertex(from).edges.from.push(edge);
|
|
|
|
this.getVertex(to).edges.to.push(edge);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
getEdges(label, from, to) {
|
|
|
|
const edgeLabels = label ? [label] : Array.from(this.edgeLabels.keys());
|
|
|
|
return edgeLabels.flatMap((edgeLabel) => {
|
|
|
|
const edges = this.edgeLabels.get(edgeLabel);
|
|
|
|
return Array.from(edges?.values() || []).filter((edge) => {
|
2022-12-31 16:08:42 -06:00
|
|
|
const matchFrom = from === null || from === undefined || from === edge.from;
|
2022-11-07 17:44:57 -06:00
|
|
|
const matchTo = to === null || to === undefined || to === edge.to;
|
|
|
|
return matchFrom && matchTo;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2023-01-03 01:26:55 -06:00
|
|
|
|
|
|
|
countVertices() {
|
|
|
|
return this.vertices.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
async renderGraph() {
|
|
|
|
const render = async () => {
|
|
|
|
const dateStart = new Date();
|
|
|
|
const graph = await mermaid.mermaidAPI.render(
|
|
|
|
this.seqDiagramElement.getId(),
|
|
|
|
this.logBox.getInnerText(),
|
|
|
|
);
|
|
|
|
this.seqDiagramBox.setInnerHTML(graph);
|
|
|
|
if (!this.dateLastRender) {
|
|
|
|
this.dateLastRender = new Date();
|
|
|
|
}
|
|
|
|
this.dateLastRender = dateStart;
|
|
|
|
};
|
|
|
|
debounce(render, 100);
|
|
|
|
}
|
2022-11-07 17:44:57 -06:00
|
|
|
}
|