dao-governance-framework/forum-network/public/classes/scene.js

88 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-12-31 16:08:42 -06:00
import mermaid from 'https://unpkg.com/mermaid@9.2.2/dist/mermaid.esm.min.mjs';
import { Actor } from './actor.js';
import { Action } from './action.js';
import { debounce } from '../util.js';
export class Scene {
constructor(name, rootBox) {
this.name = name;
this.box = rootBox.addBox(name);
this.titleBox = this.box.addBox().setInnerHTML(name);
2022-12-31 16:08:42 -06:00
this.box.addBox('Spacer').setInnerHTML(' ');
this.displayValuesBox = this.box.addBox(`${this.name}-values`);
2022-12-31 16:08:42 -06:00
this.box.addBox('Spacer').setInnerHTML(' ');
2022-11-13 12:23:30 -06:00
this.actors = new Set();
2022-11-30 09:13:52 -06:00
this.seqDiagramContainer = this.box.addBox(
2022-12-31 16:08:42 -06:00
`${this.name}-seq-diagram-container`,
2022-11-30 09:13:52 -06:00
);
this.seqDiagramElement = this.box
.addBox(`${this.name}-seq-diagram-element`)
.setId();
this.seqDiagramBox = this.box.addBox(`${this.name}-seq-diagram`);
2022-12-31 16:08:42 -06:00
this.box.addBox('Spacer').setInnerHTML(' ');
this.logBox = this.box.addBox(`${this.name}-log`);
2023-01-03 11:59:44 -06:00
mermaid.mermaidAPI.initialize({
startOnLoad: false,
theme: 'base',
themeVariables: {
darkMode: true,
primaryColor: '#2a5b6c',
primaryTextColor: '#b6b6b6',
fontFamily: 'monospace',
noteBkgColor: '#516f77',
noteTextColor: '#cecece',
},
});
this.dateLastRender = null;
}
addActor(name) {
const actor = new Actor(name, this);
return actor;
}
2022-11-13 12:23:30 -06:00
registerActor(actor) {
this.actors.add(actor);
}
addAction(name) {
const action = new Action(name, this);
return action;
}
addDisplayValue(name) {
const dv = this.displayValuesBox.addDisplayValue(name);
return dv;
}
log(msg) {
this.logBox.addBox().setInnerHTML(msg).monospace();
this.renderSequenceDiagram();
return this;
}
2022-11-11 16:52:57 -06:00
2022-11-13 12:23:30 -06:00
deactivateAll() {
for (const actor of this.actors.values()) {
while (actor.active) {
actor.deactivate();
}
}
}
async renderSequenceDiagram() {
const render = async () => {
const dateStart = new Date();
const graph = await mermaid.mermaidAPI.render(
this.seqDiagramElement.getId(),
2022-12-31 16:08:42 -06:00
this.logBox.getInnerText(),
);
this.seqDiagramBox.setInnerHTML(graph);
if (!this.dateLastRender) {
this.dateLastRender = new Date();
}
this.dateLastRender = dateStart;
};
debounce(render, 100);
}
}