Skip to content

Commit

Permalink
fix: avoid sync callbacks in async code
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun authored and vmx committed Jan 7, 2019
1 parent 399dc0e commit ddfdd71
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/decision-engine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class DecisionEngine {
cb()
})
} else {
cb()
setImmediate(cb)
}
}, cb)
}
Expand Down Expand Up @@ -179,7 +179,7 @@ class DecisionEngine {
const ledger = this._findOrCreate(peerId)

if (msg.empty) {
return cb()
return setImmediate(cb)
}

// If the message was a full wantlist clear the current one
Expand All @@ -190,7 +190,7 @@ class DecisionEngine {
this._processBlocks(msg.blocks, ledger)

if (msg.wantlist.size === 0) {
return cb()
return setImmediate(cb)
}

let cancels = []
Expand Down Expand Up @@ -219,7 +219,7 @@ class DecisionEngine {
})
}

_addWants (ledger, peerId, entries, cb) {
_addWants (ledger, peerId, entries, callback) {
each(entries, (entry, cb) => {
// If we already have the block, serve it
this.blockstore.has(entry.cid, (err, exists) => {
Expand All @@ -235,7 +235,7 @@ class DecisionEngine {
})
}, () => {
this._outbox()
cb()
callback()
})
}

Expand Down
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const waterfall = require('async/waterfall')
const reject = require('async/reject')
const each = require('async/each')
const series = require('async/series')
const setImmediate = require('async/setImmediate')
const map = require('async/map')

const WantManager = require('./want-manager')
Expand Down Expand Up @@ -108,7 +109,7 @@ class Bitswap {
(has, cb) => {
this._updateReceiveCounters(peerId.toB58String(), block, has)
if (has) {
return cb()
return setImmediate(cb)
}

this._putBlock(block, cb)
Expand Down Expand Up @@ -146,7 +147,7 @@ class Bitswap {
_putBlock (block, callback) {
this.blockstore.put(block, (err) => {
if (err) {
return callback(err)
return setImmediate(() => callback(err))
}

this.notifications.hasBlock(block)
Expand Down Expand Up @@ -310,7 +311,7 @@ class Bitswap {
(cb) => this.blockstore.has(block.cid, cb),
(has, cb) => {
if (has) {
return cb()
return setImmediate(cb)
}

this._putBlock(block, cb)
Expand All @@ -333,7 +334,7 @@ class Bitswap {
}, cb),
(newBlocks, cb) => this.blockstore.putMany(newBlocks, (err) => {
if (err) {
return cb(err)
return setImmediate(() => cb(err))
}

newBlocks.forEach((block) => {
Expand All @@ -345,7 +346,7 @@ class Bitswap {
}
})
})
cb()
setImmediate(cb)
})
], callback)
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/message/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ BitswapMessage.deserialize = (raw, callback) => {
if (decoded.payload.length > 0) {
return each(decoded.payload, (p, cb) => {
if (!p.prefix || !p.data) {
cb()
return setImmediate(cb)
}
const values = vd(p.prefix)
const cidVersion = values[0]
Expand All @@ -203,7 +203,7 @@ BitswapMessage.deserialize = (raw, callback) => {
try {
cid = new CID(cidVersion, codecName[multicodec.toString('16')], hash)
} catch (err) {
return callback(err)
return cb(err)
}

msg.addBlock(new Block(p.data, cid))
Expand Down

0 comments on commit ddfdd71

Please sign in to comment.