Skip to content

Commit 87f0094

Browse files
author
Alan Shaw
committed
feat: make good
1 parent 466dc1a commit 87f0094

File tree

2 files changed

+78
-66
lines changed

2 files changed

+78
-66
lines changed

src/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ <h1 class="w-50 ma0 tr f3 fw2 montserrat aqua">DAG builder</h1>
1515
<section class="flex-none pa3 bg-white">
1616
<input type="file" id="file" style="visibility:hidden" class="input avenir" />
1717
<select id="chunker">
18+
<option value="size-32">32 byte chunks</option>
1819
<option value="size-512" selected>512 byte chunks</option>
1920
<option value="size-1024">1,024 byte chunks</option>
2021
<option value="size-16384">16,384 byte chunks</option>
2122
<option value="size-262144">26,2144 byte chunks</option>
22-
<option value="size-262144">26,2144 byte chunks</option>
2323
</select>
2424
<select id="strategy">
2525
<option value="balanced" selected>Balanced DAG</option>
@@ -34,7 +34,7 @@ <h1 class="w-50 ma0 tr f3 fw2 montserrat aqua">DAG builder</h1>
3434
<select id="layer-repeat" style="visibility:hidden">
3535
<option value="1">1 layer repeat</option>
3636
<option value="4" selected>4 layer repeats</option>
37-
<option value="16">4 layer repeats</option>
37+
<option value="16">16 layer repeats</option>
3838
</select>
3939
</section>
4040
<div class="flex-auto" id="root" style="background: pink;"></div>

src/index.js

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

60-
const cy = cytoscape({
61-
elements: [],
62-
container: document.getElementById('root'),
63-
...graphOpts
64-
})
60+
const addToVis = async (cy, cid, seen = []) => {
61+
const { value: source } = await ipfs.dag.get(cid)
62+
63+
let nodeData = {}
6564

66-
const slowMap = async (items, fn, delay = 10) => {
67-
const res = []
68-
for (let i = 0; i < items.length; i++) {
69-
res.push(await fn(items[i], i))
70-
await new Promise(resolve => setTimeout(resolve, delay))
65+
try {
66+
// it's a unix system?
67+
nodeData = unixfs.unmarshal(source.data)
68+
} catch (err) {
69+
// dag-pb but not a unixfs.
70+
console.log(err)
7171
}
72-
return res
73-
}
7472

75-
const addToVis = async (cy, cid) => {
76-
const { value: source } = await ipfs.dag.get(cid)
77-
await slowMap(source.links, async (link) => {
78-
const { value: target } = await ipfs.dag.get(link.cid)
79-
let nodeData = {}
80-
try {
81-
// it's a unix system?
82-
nodeData = unixfs.unmarshal(target.data)
83-
} catch (err) {
84-
// dag-pb but not a unixfs.
85-
console.log(err)
86-
}
87-
console.log(`adding leaf ${link.cid} to parent ${cid}`, nodeData)
88-
cy.add([{
73+
for (let i = 0; i < source.links.length; i++) {
74+
await addToVis(cy, source.links[i].cid)
75+
}
76+
77+
if (!cy.getElementById(cid.toString()).length) {
78+
cy.add({
8979
group: 'nodes',
9080
data: {
91-
id: link.cid.toString(),
81+
id: cid.toString(),
9282
...nodeData
9383
},
94-
classes: [
95-
nodeData.blockSizes.length ? 'meta' : 'leaf'
96-
]
97-
}, {
98-
group: 'edges',
99-
data: {
100-
source: cid.toString(),
101-
target: link.cid.toString()
102-
}
103-
}])
104-
105-
cy.layout(graphOpts.layout).run()
84+
classes: source.links.length ? [] : ['leaf']
85+
})
86+
}
10687

107-
return addToVis(cy, link.cid)
108-
})
88+
cy.add(source.links.map(link => ({
89+
group: 'edges',
90+
data: {
91+
source: cid.toString(),
92+
target: link.cid.toString()
93+
}
94+
})))
10995
}
11096

11197
const show = el => { el.style.visibility = 'visible' }
11298
const hide = el => { el.style.visibility = 'hidden' }
11399

100+
const rootEl = document.getElementById('root')
114101
const fileEl = document.getElementById('file')
115102
const chunkerEl = document.getElementById('chunker')
116103
const strategyEl = document.getElementById('strategy')
117104
const maxChildrenEl = document.getElementById('max-children')
118105
const layerRepeatEl = document.getElementById('layer-repeat')
119106

120-
strategyEl.addEventListener('change', e => {
121-
if (['balanced', 'trickle'].includes(e.target.value)) {
107+
function updateAvailableOptions () {
108+
if (['balanced', 'trickle'].includes(strategyEl.value)) {
122109
show(maxChildrenEl)
123110
} else {
124111
hide(maxChildrenEl)
125112
}
126113

127-
if (e.target.value === 'trickle') {
114+
if (strategyEl.value === 'trickle') {
128115
show(layerRepeatEl)
129116
} else {
130117
hide(layerRepeatEl)
131118
}
132-
})
119+
}
120+
121+
updateAvailableOptions()
122+
123+
strategyEl.addEventListener('change', updateAvailableOptions)
133124

134125
ipfs.on('ready', () => {
135126
console.log('IPFS is ready!')
136127

137128
show(fileEl)
138129

130+
let vis
131+
const rawFiles = []
132+
133+
;[chunkerEl, strategyEl, maxChildrenEl, layerRepeatEl].forEach(el => {
134+
el.addEventListener('change', addAndRender)
135+
})
136+
137+
async function addAndRender () {
138+
if (!rawFiles.length) return
139+
140+
if (vis) {
141+
vis.container().parentNode.removeChild(vis.container())
142+
vis = null
143+
}
144+
145+
const files = rawFiles.map(({ path, content }) => ({ path, content }))
146+
const res = await ipfs.add(files, {
147+
chunker: chunkerEl.value,
148+
strategy: strategyEl.value,
149+
maxChildrenPerNode: parseInt(maxChildrenEl.value),
150+
layerRepeat: parseInt(layerRepeatEl.value),
151+
wrapWithDirectory: files.length > 1
152+
})
153+
154+
console.log('added', res[res.length - 1].hash)
155+
156+
const container = document.createElement('div')
157+
container.style.height = '100%'
158+
rootEl.appendChild(container)
159+
160+
const cy = cytoscape({ elements: [], container, ...graphOpts })
161+
162+
await addToVis(cy, res[res.length - 1].hash)
163+
164+
cy.layout(graphOpts.layout).run()
165+
vis = cy
166+
}
167+
139168
fileEl.addEventListener('change', e => {
140169
const file = e.target.files[0]
141170
const fileReader = new FileReader()
142171

143172
fileReader.onload = async e => {
144-
const path = file.name
145-
const content = Buffer.from(e.target.result)
146-
const res = await ipfs.add({ path, content }, {
147-
chunker: chunkerEl.value,
148-
strategy: strategyEl.value,
149-
maxChildrenPerNode: parseInt(maxChildrenEl.value),
150-
layerRepeat: parseInt(layerRepeatEl.value)
151-
})
152-
153-
console.log('added', res[0].hash)
154-
155-
cy.add({
156-
group: 'nodes',
157-
data: {
158-
id: res[0].hash
159-
}
160-
})
161-
162-
addToVis(cy, res[0].hash)
173+
rawFiles.push({ path: file.name, content: Buffer.from(e.target.result) })
174+
addAndRender()
163175
}
164176

165177
fileReader.readAsArrayBuffer(file)

0 commit comments

Comments
 (0)