@@ -9,38 +9,9 @@ const async = require('async')
9
9
// It uses an internal `datastore.Datastore` instance to store values.
10
10
function BlockService ( ipfsRepo , exchange ) {
11
11
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 )
15
13
16
14
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
- } )
44
15
ws . end ( )
45
16
}
46
17
0 commit comments