@@ -13,7 +13,41 @@ exports.manifest = {
1313}
1414
1515exports . init = function ( ssb , config ) {
16- var view = ssb . _flumeUse ( 'patchwork-contacts' , FlumeReduce ( 0 , reduce , map ) )
16+ var values = { }
17+
18+ var view = ssb . _flumeUse ( 'patchwork-contacts' , FlumeReduce ( 0 , function reduce ( result , item ) {
19+ // used by the reducer view
20+ if ( ! result ) result = { }
21+ if ( item ) {
22+ for ( let author in item ) {
23+ for ( let contact in item [ author ] ) {
24+ if ( ! result [ author ] ) result [ author ] = { }
25+ result [ author ] [ contact ] = item [ author ] [ contact ]
26+ }
27+ }
28+ }
29+
30+ // always make sure values is the latest result
31+ // hack around result being null on index initialize
32+ values = result
33+ return result
34+ } , function map ( msg ) {
35+ // used by the reducer view
36+ if ( msg . value && msg . value . content && msg . value . content . type === 'contact' && ref . isFeed ( msg . value . content . contact ) ) {
37+ return {
38+ [ msg . value . author ] : {
39+ [ msg . value . content . contact ] : getContactState ( msg . value . content )
40+ }
41+ }
42+ }
43+ } ) )
44+
45+ view . get ( ( err , result ) => {
46+ if ( ! err && result ) {
47+ // initialize values
48+ values = result
49+ }
50+ } )
1751
1852 return {
1953 // expose raw view to other plugins (not over rpc)
@@ -22,15 +56,15 @@ exports.init = function (ssb, config) {
2256 isFollowing : function ( { source, dest } , cb ) {
2357 view . get ( ( err , graph ) => {
2458 if ( err ) return cb ( err )
25- var following = graph [ source ] && graph [ source ] [ dest ] === true
59+ var following = graph && graph [ source ] && graph [ source ] [ dest ] === true
2660 cb ( null , following )
2761 } )
2862 } ,
2963
3064 isBlocking : function ( { source, dest } , cb ) {
3165 view . get ( ( err , graph ) => {
3266 if ( err ) return cb ( err )
33- var blocking = graph [ source ] && graph [ source ] [ dest ] === false
67+ var blocking = graph && graph [ source ] && graph [ source ] [ dest ] === false
3468 cb ( null , blocking )
3569 } )
3670 } ,
@@ -170,7 +204,6 @@ exports.init = function (ssb, config) {
170204 var stream = PullPushAbort ( )
171205
172206 var lastResolvedValues = { }
173- var values = { }
174207
175208 var timer = null
176209 var queued = false
@@ -200,7 +233,6 @@ exports.init = function (ssb, config) {
200233 if ( ! sync ) {
201234 // we'll store the incoming values (they will be updated as the view updates so
202235 // do not need to be manually patched)
203- values = msg
204236 sync = true
205237 update ( )
206238
@@ -264,28 +296,3 @@ function resolveValues (values, yourId) {
264296 }
265297 return result
266298}
267-
268- function reduce ( result , item ) {
269- // used by the reducer view
270- if ( ! result ) result = { }
271- if ( item ) {
272- for ( let author in item ) {
273- for ( let contact in item [ author ] ) {
274- if ( ! result [ author ] ) result [ author ] = { }
275- result [ author ] [ contact ] = item [ author ] [ contact ]
276- }
277- }
278- }
279- return result
280- }
281-
282- function map ( msg ) {
283- // used by the reducer view
284- if ( msg . value && msg . value . content && msg . value . content . type === 'contact' && ref . isFeed ( msg . value . content . contact ) ) {
285- return {
286- [ msg . value . author ] : {
287- [ msg . value . content . contact ] : getContactState ( msg . value . content )
288- }
289- }
290- }
291- }
0 commit comments