diff --git a/src/random-walk.js b/src/random-walk.js index b6a75dad..7d7b18c5 100644 --- a/src/random-walk.js +++ b/src/random-walk.js @@ -8,6 +8,7 @@ const assert = require('assert') const AbortController = require('abort-controller') const errcode = require('err-code') const times = require('p-times') +const setImmediate = require('async/setImmediate') const c = require('./constants') const { logger } = require('./utils') @@ -36,9 +37,10 @@ class RandomWalk { * every interval requesting random data. This is done to keep the dht * healthy over time. * + * @param {function()} callback * @returns {void} */ - start () { + start (callback) { // Don't run twice if (this._timeoutId || !this._options.enabled) { return } @@ -47,20 +49,23 @@ class RandomWalk { // Start runner immediately this._runPeriodically() }, this._options.delay) + setImmediate(callback || (() => {})) } /** * Stop the random-walk process. Any active * queries will be aborted. * + * @param {function()} callback * @returns {void} */ - stop () { + stop (callback) { if (this._timeoutId) { clearTimeout(this._timeoutId) this._timeoutId = undefined } this._controller && this._controller.abort() + setImmediate(callback || (() => {})) } /**