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

Commit 47d8166

Browse files
committed
Merge pull request #14 from dignifiedquire/fix/cb
fix(block-service): use correct cb interface for writes
2 parents ba62423 + 83b6e16 commit 47d8166

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

Diff for: 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

Diff for: test/block-service-test.js

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

0 commit comments

Comments
 (0)