git ssb

7+

dinoworm 🐛 / patchcore



Commit 5968fb325860b8675c105c04324b66c5858d36e5

sbot: use `gossip.changes` instead of polling `gossip.peers`

Matt McKegg committed on 6/30/2017, 10:21:40 AM
Parent: 1a60fe5ccf3b5627afee9860687652a1478e4f3a

Files changed

sbot.jschanged
sbot.jsView
@@ -1,13 +1,12 @@
11 var pull = require('pull-stream')
22 var defer = require('pull-defer')
3-var { onceTrue } = require('mutant')
3 +var { Value, onceTrue, watch, Set: MutantSet } = require('mutant')
44 var ref = require('ssb-ref')
55 var Reconnect = require('pull-reconnect')
66 var createClient = require('ssb-client')
77 var createFeed = require('ssb-feed')
88 var nest = require('depnest')
9-var Value = require('mutant/value')
109 var ssbKeys = require('ssb-keys')
1110
1211 exports.needs = nest({
1312 'config.sync.load': 'first',
@@ -52,13 +51,11 @@
5251
5352 var sbot = null
5453 var connection = Value()
5554 var connectionStatus = Value()
56- var connectedPeers = Value([])
57- var localPeers = Value([])
55 + var connectedPeers = MutantSet()
56 + var localPeers = MutantSet()
5857
59- setInterval(refreshPeers, 1e3)
60-
6158 var rec = Reconnect(function (isConn) {
6259 function notify (value) {
6360 isConn(value); connectionStatus.set(value)
6461 }
@@ -76,9 +73,8 @@
7673 })
7774
7875 connection.set(sbot)
7976 notify()
80- refreshPeers()
8177 })
8278 })
8379
8480 var internal = {
@@ -89,8 +85,38 @@
8985 sbot.add(msg, cb)
9086 })
9187 }
9288
89 + watch(connection, (sbot) => {
90 + if (sbot) {
91 + sbot.gossip.peers((err, peers) => {
92 + if (err) return console.error(err)
93 + connectedPeers.set(peers.filter(x => x.state === 'connected').map(x => x.key))
94 + localPeers.set(peers.filter(x => x.source === 'local').map(x => x.key))
95 + })
96 + pull(
97 + sbot.gossip.changes(),
98 + pull.drain(data => {
99 + if (data.peer) {
100 + if (data.type === 'remove') {
101 + connectedPeers.delete(data.peer.key)
102 + localPeers.delete(data.peer.key)
103 + } else {
104 + if (data.peer.source === 'local') {
105 + localPeers.add(data.peer.key)
106 + }
107 + if (data.peer.state === 'connected') {
108 + connectedPeers.add(data.peer.key)
109 + } else {
110 + connectedPeers.delete(data.peer.key)
111 + }
112 + }
113 + }
114 + })
115 + )
116 + }
117 + })
118 +
93119 var feed = createFeed(internal, keys, {remote: true})
94120
95121 return {
96122 sbot: {
@@ -223,18 +249,8 @@
223249 // cache[msg.key] = msg.value
224250 // api.sbot.hook.feed(msg)
225251 }
226252 }
227-
228- function refreshPeers () {
229- if (sbot) {
230- sbot.gossip.peers((err, peers) => {
231- if (err) return console.error(err)
232- connectedPeers.set(peers.filter(x => x.state === 'connected').map(x => x.key))
233- localPeers.set(peers.filter(x => x.source === 'local').map(x => x.key))
234- })
235- }
236- }
237253 }
238254
239255 function Hash (onHash) {
240256 var buffers = []

Built with git-ssb-web