Commit 143a1d94732e8892d9f8d93f3fc65691d3d2580f
don't assume latestSequence is strictly async
Dominic Tarr committed on 5/21/2017, 9:03:48 AMParent: 41acb7e418bf9228b56b84133488f53d7c718fce
Files changed
test/random.js | changed |
test/random.js | |||
---|---|---|---|
@@ -24,8 +24,17 @@ | |||
24 | 24 … | function isNumber (n) { | |
25 | 25 … | return typeof n === 'number' | |
26 | 26 … | } | |
27 | 27 … | ||
28 … | +function once (fn) { | ||
29 … | + var called = false | ||
30 … | + return function () { | ||
31 … | + if(called) throw new Error('called twice!') | ||
32 … | + called = true | ||
33 … | + fn.apply(this, arguments) | ||
34 … | + } | ||
35 … | +} | ||
36 … | + | ||
28 | 37 … | var createSbot = require('../') | |
29 | 38 … | .use(require('../plugins/friends')) | |
30 | 39 … | .use(require('../plugins/replicate')) | |
31 | 40 … | .use(require('../plugins/gossip')) | |
@@ -93,20 +102,19 @@ | |||
93 | 102 … | ||
94 | 103 … | } | |
95 | 104 … | ||
96 | 105 … | function latest (sbot, cb) { | |
97 | - sbot.friends.hops({hops: 3}, function (err, keys) { | ||
106 … | + sbot.friends.hops({hops: 3}, once(function (err, keys) { | ||
98 | 107 … | if(err) return cb(err) | |
99 | - var n = 0, map = {} | ||
108 … | + var n = Object.keys(keys).length, map = {} | ||
100 | 109 … | for(var k in keys) (function (key) { | |
101 | - n++ | ||
102 | - sbot.latestSequence(key, function (err, value) { | ||
110 … | + sbot.latestSequence(key, once(function (err, value) { | ||
103 | 111 … | map[key] = isNumber(value) ? value : value.sequence | |
104 | 112 … | if(--n) return | |
105 | 113 … | cb(null, map) | |
106 | - }) | ||
114 … | + })) | ||
107 | 115 … | })(k) | |
108 | - }) | ||
116 … | + })) | ||
109 | 117 … | } | |
110 | 118 … | ||
111 | 119 … | var alice = ssbKeys.generate() | |
112 | 120 … | var bob = ssbKeys.generate() | |
@@ -274,9 +282,4 @@ | |||
274 | 282 … | animalNetwork.close(true) | |
275 | 283 … | t.end() | |
276 | 284 … | }) | |
277 | 285 … | ||
278 | - | ||
279 | - | ||
280 | - | ||
281 | - | ||
282 | - |
Built with git-ssb-web