Skip to content

Commit 25dafc9

Browse files
olizillaAlan Shaw
authored andcommitted
fix: update vis once per dag (#1)
- accumlates nodes, then renders - avoids flickering nodes in the top-left corner while we add hundreds of nodes to the vis before doing a layout License: MIT Signed-off-by: Oli Evans <[email protected]>
1 parent c6a40eb commit 25dafc9

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/index.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ const graphOpts = {
5757
const ipfs = new Ipfs()
5858
const { Buffer } = Ipfs
5959

60-
const addToVis = async (cy, cid) => {
61-
if (cy.getElementById(cid.toString()).length) return
60+
const getAllNodes = async (nodeMap, cid) => {
61+
if (nodeMap.get(cid.toString())) return
6262

6363
const { value: source } = await ipfs.dag.get(cid)
6464

@@ -73,10 +73,10 @@ const addToVis = async (cy, cid) => {
7373
}
7474

7575
for (let i = 0; i < source.links.length; i++) {
76-
await addToVis(cy, source.links[i].cid)
76+
await getAllNodes(nodeMap, source.links[i].cid)
7777
}
7878

79-
cy.add({
79+
nodeMap.set(cid.toString(), {
8080
group: 'nodes',
8181
data: {
8282
id: cid.toString(),
@@ -85,13 +85,15 @@ const addToVis = async (cy, cid) => {
8585
classes: source.links.length ? [] : ['leaf']
8686
})
8787

88-
cy.add(source.links.map(link => ({
89-
group: 'edges',
90-
data: {
91-
source: cid.toString(),
92-
target: link.cid.toString()
93-
}
94-
})))
88+
source.links.map(link => {
89+
nodeMap.set(cid.toString() + '->' + link.cid.toString(), {
90+
group: 'edges',
91+
data: {
92+
source: cid.toString(),
93+
target: link.cid.toString()
94+
}
95+
})
96+
})
9597
}
9698

9799
const show = el => { el.style.visibility = 'visible' }
@@ -153,14 +155,17 @@ ipfs.on('ready', () => {
153155

154156
console.log('added', res[res.length - 1].hash)
155157

158+
const nodeMap = new Map()
159+
await getAllNodes(nodeMap, res[res.length - 1].hash)
160+
renderVis(nodeMap)
161+
}
162+
163+
function renderVis (nodeMap) {
156164
const container = document.createElement('div')
157165
container.style.height = '100%'
158166
rootEl.appendChild(container)
159-
160-
const cy = cytoscape({ elements: [], container, ...graphOpts })
161-
162-
await addToVis(cy, res[res.length - 1].hash)
163-
167+
const elements = Array.from(nodeMap.values())
168+
const cy = cytoscape({ elements, container, ...graphOpts })
164169
cy.layout(graphOpts.layout).run()
165170
vis = cy
166171
}

0 commit comments

Comments
 (0)