@@ -57,8 +57,8 @@ const graphOpts = {
57
57
const ipfs = new Ipfs ( )
58
58
const { Buffer } = Ipfs
59
59
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
62
62
63
63
const { value : source } = await ipfs . dag . get ( cid )
64
64
@@ -73,10 +73,10 @@ const addToVis = async (cy, cid) => {
73
73
}
74
74
75
75
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 )
77
77
}
78
78
79
- cy . add ( {
79
+ nodeMap . set ( cid . toString ( ) , {
80
80
group : 'nodes' ,
81
81
data : {
82
82
id : cid . toString ( ) ,
@@ -85,13 +85,15 @@ const addToVis = async (cy, cid) => {
85
85
classes : source . links . length ? [ ] : [ 'leaf' ]
86
86
} )
87
87
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
+ } )
95
97
}
96
98
97
99
const show = el => { el . style . visibility = 'visible' }
@@ -153,14 +155,17 @@ ipfs.on('ready', () => {
153
155
154
156
console . log ( 'added' , res [ res . length - 1 ] . hash )
155
157
158
+ const nodeMap = new Map ( )
159
+ await getAllNodes ( nodeMap , res [ res . length - 1 ] . hash )
160
+ renderVis ( nodeMap )
161
+ }
162
+
163
+ function renderVis ( nodeMap ) {
156
164
const container = document . createElement ( 'div' )
157
165
container . style . height = '100%'
158
166
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 } )
164
169
cy . layout ( graphOpts . layout ) . run ( )
165
170
vis = cy
166
171
}
0 commit comments