git ssb

4+

Dominic / scuttlebot



Commit c1997c8a400cf294934a5e97a93d35b3705dec55

get flume style friends plugin working right, still needs block though

Dominic Tarr committed on 5/24/2017, 11:24:54 AM
Parent: e56dd0fc65220e4f40d8a41f95306b8d8bb09369

Files changed

plugins/friends.jschanged
plugins/friends.jsView
@@ -29,17 +29,12 @@
2929 exports.version = '1.0.0'
3030 exports.manifest = mdm.manifest(apidoc)
3131
3232 exports.init = function (sbot, config) {
33- var g = null
34-
35- var index = sbot._flumeUse('friends', Reduce(1, function (g, rel) {
36- if(!g) g = {}
33+ var g = {}
34+ var index = sbot._flumeUse('friends', Reduce(1, function (_, rel) {
35+ //if(!g) g = {}
3736 if(!ref.isFeed(rel.from)) throw new Error('FROM is not id')
38- if(!ref.isFeed(rel.to)) {
39- console.log('???', rel)
40- throw new Error('TO is not id')
41- }
4237 G.addEdge(g, rel.from, rel.to, rel.value)
4338 return g
4439 }, function (data) {
4540 if(data.value.content.type === 'contact' && ref.isFeed(data.value.content.contact)) {
@@ -62,25 +57,32 @@
6257 var live = opts.live === true
6358 var meta = opts.meta === true
6459 var start = opts.start || sbot.id
6560 var first = true
66- var reachable = {}
67- g = g || {}
61+ var reachable
62+ if(!g) throw new Error('not initialized')
63+ //g = g || {}
6864 return pull(
6965 index.stream(opts),
7066 FlatMap(function (v) {
67+ if(!v) return []
68+
7169 //this code handles real time streaming of the hops map.
7270 function push (to, hops) {
7371 out.push(meta ? {id: to, hops: hops} : to)
7472 }
7573 var out = []
76- if(!v) return []
7774 if(v.from && v.to) {
78- //add edge from->to (value)
79- G.addEdge(g, v.from, v.to, v.value)
75+ if(!reachable) {
76+ //this is is hack...
77+ reachable = {}
78+ reachable[sbot.id] = 0
79+ push(sbot.id, 0)
80+ }
8081 //recalculate the portion of the graph, reachable in opts.hops
81- var _reachable = G.hops(g, start, 0, opts.hops || 3, reachable)
82- //for each node currently reachable
82+ //(but only the portion not already reachable)
83+ var _reachable = G.hops(g, v.from, reachable[v.from], opts.hops || 3, reachable)
84+
8385 for(var k in _reachable) {
8486 //check if it has _become_ reachable just now.
8587 //if so add to the set
8688 if(reachable[k] == null)
@@ -91,10 +93,10 @@
9193 //else, we where already able to reach this node.
9294 }
9395 }
9496 else {
95- g = v
96- reachable = G.hops(g, start, 0, opts.hops || 3)
97+ var _g = v
98+ reachable = G.hops(_g, start, 0, opts.hops || 3)
9799 for(var k in reachable)
98100 push(k, reachable[k])
99101 }
100102 if(first) {
@@ -102,15 +104,18 @@
102104 if(live) {
103105 out.push({sync: true})
104106 }
105107 }
108+
106109 return out
107110 })
108111
109112 )
110113 }, 'createFriendStreamOpts?'),
111114
112115 hops: function (opts, cb) {
116+ if(isFunction(opts))
117+ cb = opts, opts = {}
113118 opts = opts || {}
114119 if(isString(opts))
115120 opts = {start: opts}
116121 index.get(null, function (err, g) {
@@ -121,5 +126,4 @@
121126 }
122127 }
123128
124129
125-

Built with git-ssb-web