Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit bec44b3

Browse files
fix(block-service): use correct cb interface for writes
fixes #13
1 parent ba62423 commit bec44b3

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

src/block-service.js

+1-30
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,9 @@ const async = require('async')
99
// It uses an internal `datastore.Datastore` instance to store values.
1010
function BlockService (ipfsRepo, exchange) {
1111
this.addBlock = (block, callback) => {
12-
const ws = ipfsRepo.datastore.createWriteStream(block.key, block.extension)
13-
14-
let done = false
12+
const ws = ipfsRepo.datastore.createWriteStream(block.key, block.extension, callback)
1513

1614
ws.write(block.data)
17-
18-
ws.once('error', (err) => {
19-
done = true
20-
callback(err)
21-
})
22-
23-
ws.once('finish', () => {
24-
if (!done) {
25-
// Important to note: Writing to a stream
26-
// isn't an atomic process, because streams can be
27-
// piped, and the finish of one only represents that
28-
// the data was buffered to the next one.
29-
// This is something known and 'accepted' on the
30-
// streams API, however, since we expose a callback
31-
// interface on BlockService and a streams one,
32-
// the users will expect for the callback to be fired
33-
// when the final write was concluded. We add a
34-
// timeout to ensure that.
35-
// TODO: Create an elegant way to understand when
36-
// the block was actually flushed to disk. This
37-
// means changing how the blob-stores and repo are
38-
// implemented.
39-
// One option, is polling till we check it
40-
// is written.
41-
setTimeout(callback, 150)
42-
}
43-
})
4415
ws.end()
4516
}
4617

test/block-service-test.js

+19
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,24 @@ module.exports = (repo) => {
193193
done()
194194
})
195195
})
196+
197+
it('stores and gets lots of blocks', (done) => {
198+
const blocks = []
199+
const count = 1000
200+
while (blocks.length < count) {
201+
blocks.push(new Block('hello-' + Math.random()))
202+
}
203+
204+
bs.addBlocks(blocks, (err) => {
205+
expect(err).to.not.exist
206+
207+
bs.getBlocks(blocks.map((b) => b.key), (err, res) => {
208+
expect(err).to.not.exist
209+
expect(Object.keys(res)).to.have.length(count)
210+
211+
done()
212+
})
213+
})
214+
})
196215
})
197216
}

0 commit comments

Comments
 (0)