From f945d4dcdb5ef14f682b2e5ffcbc5b0b86586f88 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 7 May 2022 16:54:28 +0200 Subject: [PATCH] chore: factor out recorder Signed-off-by: Matej Focko --- index.html | 1 + recorder.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ visualization.js | 63 ++------------------------------------- 3 files changed, 79 insertions(+), 61 deletions(-) create mode 100644 recorder.js diff --git a/index.html b/index.html index 0e83754..0c5db4c 100755 --- a/index.html +++ b/index.html @@ -48,5 +48,6 @@ + diff --git a/recorder.js b/recorder.js new file mode 100644 index 0000000..eb88514 --- /dev/null +++ b/recorder.js @@ -0,0 +1,76 @@ +const DURATION = 500; + +class Recorder { + constructor(graph, id) { + this.graph = graph; + this.id = id; + + this.rendered = -1; + this.rendering = false; + this.atOnce = false; + + this.states = new Array(); + + this.initGraph(); + } + + renderAtOnce() { + this.atOnce = true; + return this; + } + + unblockRendering() { + console.log(this); + console.log(`Unblocked renderer #${this.id}`); + this.rendering = false; + } + + initGraph() { + this.graph.transition(() => + d3 + .transition("main") + .ease(d3.easeLinear) + .on("end", this.unblockRendering.bind(this)) + .delay(500) + .duration(DURATION) + ); + } + + render() { + if (this.atOnce) { + if (this.rendered != this.states.length - 1) { + this.rendered = this.states.length - 1; + this.graph.renderDot(this.states[this.rendered]); + } + + return; + } + + if ( + this.rendering || + !this.states || + this.rendered == this.states.length - 1 + ) { + return; + } + + this.rendering = true; + + this.rendered++; + this.graph.renderDot(this.states[this.rendered]); + } + + previous() { + throw "not implemented!"; + } + + next() { + throw "not implemented!"; + } + + record(tree) { + // TODO: adjust join if necessary + this.states.push(tree.toDot().join("")); + this.render(); + } +} diff --git a/visualization.js b/visualization.js index f69b7d7..019d62f 100644 --- a/visualization.js +++ b/visualization.js @@ -1,65 +1,6 @@ -const DURATION = 500; +let recorder = new Recorder(d3.select("#graph").graphviz(), "graph"); -class Recorder { - constructor(graph) { - this.graph = graph; - - this.rendered = -1; - this.rendering = false; - - this.states = new Array(); - - this.unblockRendering = ((recorder) => () => { - recorder.rendering = false; - })(this); - - this.initGraph(); - } - - initGraph() { - this.graph.transition(() => - d3 - .transition("main") - .ease(d3.easeLinear) - .on("end", this.unblockRendering) - .delay(500) - .duration(DURATION) - ); - } - - render() { - if ( - this.rendering || - !this.states || - this.rendered == this.states.length - 1 - ) { - return; - } - - this.rendering = true; - - this.rendered++; - this.graph.renderDot(this.states[this.rendered]); - } - - previous() { - throw "not implemented!"; - } - - next() { - throw "not implemented!"; - } - - record(tree) { - // TODO: adjust join if necessary - this.states.push(tree.toDot().join("")); - this.render(); - } -} - -let recorder = new Recorder(d3.select("#graph").graphviz()); - -let tree = new AVLTree(); +let tree = new WAVLTree(); tree.recorder = recorder; function insertCallback() {