Skip to content

Commit 108c1ce

Browse files
committed
Graph view: prevent possibilty of race condition on layout
1 parent 814c014 commit 108c1ce

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/views/Graph.vue

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,10 @@ export default {
294294
}
295295
},
296296
297-
async mounted () {
297+
mounted () {
298298
// compile & instantiate graphviz wasm
299-
this.graphviz = await Graphviz.load()
299+
/** @type {Promise<Graphviz>} */
300+
this.graphviz = Graphviz.load()
300301
// allow render to happen before we go configuring svgPanZoom
301302
this.$nextTick(() => {
302303
this.updateTimer()
@@ -420,9 +421,15 @@ export default {
420421
}
421422
return ret
422423
},
424+
/**
425+
* Get the dimensions of currently rendered graph nodes
426+
* (we feed these dimensions into the GraphViz dot code to improve layout).
427+
*
428+
* @param {Object[]} nodes
429+
* @returns {{ [id: string]: SVGRect }} mapping of node IDs to their
430+
* bounding boxes.
431+
*/
423432
getNodeDimensions (nodes) {
424-
// get the dimensions of currently rendered graph nodes
425-
// (we feed these dimensions into the GraphViz dot code to improve layout)
426433
const ret = {}
427434
let bbox
428435
for (const node of nodes) {
@@ -587,13 +594,13 @@ export default {
587594
588595
// layout the graph
589596
try {
590-
this.layout(nodes, edges, nodeDimensions)
597+
await this.layout(nodes, edges, nodeDimensions)
591598
} catch (e) {
592599
// something went wrong, allow the layout to retry later
593600
this.graphID = null
594601
this.updating = false
595602
// eslint-disable-next-line no-console
596-
console.warn(e)
603+
console.error(e)
597604
return
598605
}
599606
@@ -624,13 +631,19 @@ export default {
624631
await this.$nextTick()
625632
}
626633
},
627-
/** re-layout the graph after any new nodes have been rendered */
628-
layout (nodes, edges, nodeDimensions) {
634+
/**
635+
* Re-layout the graph after any new nodes have been rendered.
636+
*
637+
* @param {Object[]} nodes
638+
* @param {Object[]} edges
639+
* @param {{ [id: string]: SVGRect }} nodeDimensions
640+
*/
641+
async layout (nodes, edges, nodeDimensions) {
629642
// generate the GraphViz dot code
630643
const dotCode = this.getDotCode(nodeDimensions, nodes, edges)
631644
632645
// run the layout algorithm
633-
const jsonString = this.graphviz.layout(dotCode, 'json')
646+
const jsonString = (await this.graphviz).layout(dotCode, 'json')
634647
const json = JSON.parse(jsonString)
635648
636649
// update graph node positions

0 commit comments

Comments
 (0)