git ssb

4+

Dominic / scuttlebot



Commit 143a1d94732e8892d9f8d93f3fc65691d3d2580f

don't assume latestSequence is strictly async

Dominic Tarr committed on 5/21/2017, 9:03:48 AM
Parent: 41acb7e418bf9228b56b84133488f53d7c718fce

Files changed

test/random.jschanged
test/random.jsView
@@ -24,8 +24,17 @@
2424 function isNumber (n) {
2525 return typeof n === 'number'
2626 }
2727
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 +
2837 var createSbot = require('../')
2938 .use(require('../plugins/friends'))
3039 .use(require('../plugins/replicate'))
3140 .use(require('../plugins/gossip'))
@@ -93,20 +102,19 @@
93102
94103 }
95104
96105 function latest (sbot, cb) {
97- sbot.friends.hops({hops: 3}, function (err, keys) {
106 + sbot.friends.hops({hops: 3}, once(function (err, keys) {
98107 if(err) return cb(err)
99- var n = 0, map = {}
108 + var n = Object.keys(keys).length, map = {}
100109 for(var k in keys) (function (key) {
101- n++
102- sbot.latestSequence(key, function (err, value) {
110 + sbot.latestSequence(key, once(function (err, value) {
103111 map[key] = isNumber(value) ? value : value.sequence
104112 if(--n) return
105113 cb(null, map)
106- })
114 + }))
107115 })(k)
108- })
116 + }))
109117 }
110118
111119 var alice = ssbKeys.generate()
112120 var bob = ssbKeys.generate()
@@ -274,9 +282,4 @@
274282 animalNetwork.close(true)
275283 t.end()
276284 })
277285
278-
279-
280-
281-
282-

Built with git-ssb-web