From 5dad3443b8929186df5892d9acf5fa2adbb79b81 Mon Sep 17 00:00:00 2001 From: andrycodestuffs Date: Sat, 24 Aug 2019 13:31:15 +0200 Subject: [PATCH] fix: match start() and stop() libp2p/interface-peer-discovery spec --- src/random-walk.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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 || (() => {})) } /**