From 274e0a03bfb89729fd075bf060d6ad0548ea5b5e Mon Sep 17 00:00:00 2001 From: Anders Rune Jensen Date: Thu, 4 Feb 2021 22:01:47 +0100 Subject: [PATCH] Make sure hops() auto drains the contacts index --- index.js | 14 +++++++++----- test/db2.js | 42 ++++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index d012c23..c574447 100644 --- a/index.js +++ b/index.js @@ -100,12 +100,16 @@ exports.init = function (sbot, config) { // legacy, debugging hops (opts, cb) { + if (typeof opts === 'function') { + cb = opts + opts = {} + } + layered.onReady(function () { - if (typeof opts === 'function') { - cb = opts - opts = {} - } - cb(null, layered.getHops(opts)) + if (sbot.db) + sbot.db.onDrain('contacts', () => cb(null, layered.getHops(opts))) + else + cb(null, layered.getHops(opts)) }) }, help: () => help, diff --git a/test/db2.js b/test/db2.js index 920343b..37a34d2 100644 --- a/test/db2.js +++ b/test/db2.js @@ -84,31 +84,29 @@ tape('db2 friends test', function (t) { }), addMsg(sbot.db, carol, u.follow(alice.id)) ])(function (err, results) { - sbot.db.onDrain('contacts', () => { - sbot.friends.hops(function (err, hops) { - if (err) throw err - t.deepEqual(live, hops) - - sbot.close(() => { - sbot = Server({ - keys: alice, - db2: true, - path: dir - }) - live = liveFriends(sbot) + sbot.friends.hops(function (err, hops) { + if (err) throw err + t.deepEqual(live, hops) + + sbot.close(() => { + sbot = Server({ + keys: alice, + db2: true, + path: dir + }) + live = liveFriends(sbot) - addMsg(sbot.db, bob, { - type: 'contact', - contact: carol.id, - following: true - })((err) => { - if (err) throw err + addMsg(sbot.db, bob, { + type: 'contact', + contact: carol.id, + following: true + })((err) => { + if (err) throw err - sbot.db.onDrain('contacts', () => { - t.deepEqual(live, hops) + sbot.db.onDrain('contacts', () => { + t.deepEqual(live, hops) - sbot.close(t.end) - }) + sbot.close(t.end) }) }) })