Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
refactor(async): remove query callback methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis committed May 23, 2019
1 parent 2fcbd0b commit f70be26
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ class KadDHT extends EventEmitter {

// run our query
timeout((_cb) => {
promiseToCallback(query.runAsync(rtp))(_cb)
promiseToCallback(query.run(rtp))(_cb)
}, options.timeout)((err, res) => {
query.stop()
cb(err, res)
Expand Down Expand Up @@ -439,7 +439,7 @@ class KadDHT extends EventEmitter {
}
})

promiseToCallback(q.runAsync(tablePeers))((err, res) => {
promiseToCallback(q.run(tablePeers))((err, res) => {
if (err) {
return callback(err)
}
Expand Down Expand Up @@ -674,7 +674,7 @@ class KadDHT extends EventEmitter {
})

timeout((_cb) => {
promiseToCallback(query.runAsync(peers))(_cb)
promiseToCallback(query.run(peers))(_cb)
}, options.timeout)((err, res) => {
query.stop()
cb(err, res)
Expand Down
2 changes: 1 addition & 1 deletion src/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ module.exports = (dht) => ({

try {
await promisify(callback => timeout((cb) => {
promiseToCallback(query.runAsync(peers))(cb)
promiseToCallback(query.run(peers))(cb)
}, providerTimeout)(callback))()
} catch (err) {
if (err.code !== 'ETIMEDOUT' || out.length === 0) {
Expand Down
9 changes: 2 additions & 7 deletions src/query/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,9 @@ class Query {
* Run this query, start with the given list of peers first.
*
* @param {Array<PeerId>} peers
* @param {function(Error, Object)} callback
* @returns {void}
* @returns {Promise}
*/
run (peers, callback) {
promiseToCallback(this.runAsync(peers))(callback)
}

async runAsync (peers) {
async run (peers) {
if (!this.dht._queryManager.running) {
this._log.error('Attempt to run query after shutdown')
return { finalSet: new Set(), paths: [] }
Expand Down
30 changes: 15 additions & 15 deletions test/query.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('Query', () => {
}

const q = new Query(dht, peer.id.id, () => query)
promiseToCallback(q.runAsync([peerInfos[1].id]))((err, res) => {
promiseToCallback(q.run([peerInfos[1].id]))((err, res) => {
expect(err).to.not.exist()
expect(res.paths[0].value).to.eql(Buffer.from('cool'))
expect(res.paths[0].success).to.eql(true)
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('Query', () => {
}

const q = new Query(dht, peer.id.id, () => query)
promiseToCallback(q.runAsync([peerInfos[1].id]))((err, res) => {
promiseToCallback(q.run([peerInfos[1].id]))((err, res) => {
expect(err).not.to.exist()

// Should have visited
Expand All @@ -129,7 +129,7 @@ describe('Query', () => {
const query = (p, cb) => cb(new Error('fail'))

const q = new Query(dht, peer.id.id, () => query)
promiseToCallback(q.runAsync([peerInfos[1].id]))((err, res) => {
promiseToCallback(q.run([peerInfos[1].id]))((err, res) => {
expect(err).to.exist()
expect(err.message).to.eql('fail')
done()
Expand All @@ -142,7 +142,7 @@ describe('Query', () => {
const query = (p, cb) => {}

const q = new Query(dht, peer.id.id, () => query)
promiseToCallback(q.runAsync([]))((err, res) => {
promiseToCallback(q.run([]))((err, res) => {
if (err) console.error(err)
expect(err).to.not.exist()

Expand All @@ -167,7 +167,7 @@ describe('Query', () => {
}

const q = new Query(dht, peer.id.id, () => query)
promiseToCallback(q.runAsync([peerInfos[1].id]))((err, res) => {
promiseToCallback(q.run([peerInfos[1].id]))((err, res) => {
expect(err).to.not.exist()
expect(res.finalSet.size).to.eql(2)
done()
Expand Down Expand Up @@ -216,7 +216,7 @@ describe('Query', () => {
}

const q = new Query(dht, peer.id.id, () => query)
promiseToCallback(q.runAsync([peerInfos[1].id, peerInfos[2].id, peerInfos[3].id]))((err, res) => {
promiseToCallback(q.run([peerInfos[1].id, peerInfos[2].id, peerInfos[3].id]))((err, res) => {
expect(err).to.not.exist()

// Should visit all peers
Expand Down Expand Up @@ -257,7 +257,7 @@ describe('Query', () => {
}

const q = new Query(dht, peer.id.id, () => query)
promiseToCallback(q.runAsync([peerInfos[1].id]))((err, res) => {
promiseToCallback(q.run([peerInfos[1].id]))((err, res) => {
expect(err).to.not.exist()

// Should complete successfully
Expand Down Expand Up @@ -317,7 +317,7 @@ describe('Query', () => {
}

const q = new Query(dhtA, peer.id.id, () => query)
promiseToCallback(q.runAsync([peerInfos[1].id]))((err, res) => {
promiseToCallback(q.run([peerInfos[1].id]))((err, res) => {
expect(err).to.not.exist()
})

Expand Down Expand Up @@ -361,7 +361,7 @@ describe('Query', () => {
const q = new Query(dhtA, peer.id.id, () => query)

dhtA.stop(() => {
promiseToCallback(q.runAsync([peerInfos[1].id]))((err, res) => {
promiseToCallback(q.run([peerInfos[1].id]))((err, res) => {
expect(err).to.not.exist()

// Should not visit any peers
Expand Down Expand Up @@ -420,7 +420,7 @@ describe('Query', () => {
}

const q = new Query(dht, peer.id.id, () => query)
promiseToCallback(q.runAsync([peerInfos[1].id, peerInfos[4].id]))((err, res) => {
promiseToCallback(q.run([peerInfos[1].id, peerInfos[4].id]))((err, res) => {
expect(err).to.not.exist()

// We should get back the values from both paths
Expand Down Expand Up @@ -489,7 +489,7 @@ describe('Query', () => {
}

const q = new Query(dht, peer.id.id, () => query)
promiseToCallback(q.runAsync([peerInfos[1].id, peerInfos[4].id]))((err, res) => {
promiseToCallback(q.run([peerInfos[1].id, peerInfos[4].id]))((err, res) => {
expect(err).to.not.exist()

// We should only get back the value from the path 4 -> 5
Expand Down Expand Up @@ -568,7 +568,7 @@ describe('Query', () => {
}

const q = new Query(dht, peer.id.id, () => query)
promiseToCallback(q.runAsync([peerInfos[1].id, peerInfos[4].id]))((err, res) => {
promiseToCallback(q.run([peerInfos[1].id, peerInfos[4].id]))((err, res) => {
expect(err).to.not.exist()

// We should only get back the value from the path 1 -> 2 -> 3
Expand Down Expand Up @@ -649,7 +649,7 @@ describe('Query', () => {
}

const q = new Query(dht, peerInfos[0].id.id, () => query)
promiseToCallback(q.runAsync(initial))((err, res) => {
promiseToCallback(q.run(initial))((err, res) => {
expect(err).to.not.exist()

// Should query 19 peers, then find some peers closer to the key, and
Expand Down Expand Up @@ -723,7 +723,7 @@ describe('Query', () => {
q.concurrency = 1
// due to round-robin allocation of peers from starts, first
// path is good, second bad
promiseToCallback(q.runAsync(starts))((err, res) => {
promiseToCallback(q.run(starts))((err, res) => {
expect(err).to.not.exist()
// we should reach the target node
expect(targetVisited).to.eql(true)
Expand All @@ -749,7 +749,7 @@ describe('Query', () => {
}

const q = new Query(dht, peer.id.id, () => query)
promiseToCallback(q.runAsync([peerInfos[1].id]))((err, res) => {
promiseToCallback(q.run([peerInfos[1].id]))((err, res) => {
expect(err).to.not.exist()
})

Expand Down

0 comments on commit f70be26

Please sign in to comment.