From acd05deb1512addf2a7208d73af6f998d13a6adf Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Thu, 19 Nov 2015 12:59:19 +0100 Subject: [PATCH] fixed #31 --- lib/boards-api.js | 36 +++++++++++++++++++++++++----------- webapp/app.jsx | 4 ++-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/boards-api.js b/lib/boards-api.js index 5c90a60..e620723 100644 --- a/lib/boards-api.js +++ b/lib/boards-api.js @@ -8,15 +8,21 @@ Needs to be browserified to work in the browser // EventEmitter used to communicate with clients var EventEmitter = require('wolfy87-eventemitter') +var asyncjs = require('async') function asObj(str,done){ - var obj - try { - obj = JSON.parse(str) - } catch (e) { - return done(e,null) + if(typeof str === 'string'){ + var obj + try { + obj = JSON.parse(str) + } catch (e) { + console.log('error parsing:',str,'Error:',e) + return done(e,null) + } + done(null,obj) + } else { + done(null,str) } - done(null,obj) } function replyAsObj(res,isJson,done){ @@ -136,17 +142,22 @@ BoardsAPI.prototype.searchUsers = function(){ this.ipfs.swarm.peers((err,r) => { if(err) return console.log(err) replyAsObj(r,true,(e,reply) => { + if(e){ + this.ee.emit('error',e) + return console.log('There was an error while getting swarm peers:',e) + } console.log('Checking',reply.Strings.length,'peers') - reply.Strings.forEach(item => { + //reply.Strings.forEach(item => { + var f = (item, done) => { var ss = item.split('/') var n = ss[ss.length-1] + this.ee.once(n,(res,err) => done(err)) this.resolveIPNS(n) - }) + } + asyncjs.eachSeries(reply.Strings,f.bind(this)) }) }) // Look for who has the correct version file, they probably have a profile - // Disabled at the moment - /* this.ipfs.dht.findprovs(this.version_hash, (err,res) => { if(err){ console.log('DHT FINDPROVS err',err) @@ -156,7 +167,6 @@ BoardsAPI.prototype.searchUsers = function(){ console.log('DHT FINDPROVS string',res) } }) - */ return this.ee } @@ -362,4 +372,8 @@ BoardsAPI.prototype.getEventEmitter = function(){ return this.ee } +BoardsAPI.prototype.getUsers = function(){ + return Object.keys(this.users) +} + module.exports = BoardsAPI diff --git a/webapp/app.jsx b/webapp/app.jsx index 9589934..10504d3 100644 --- a/webapp/app.jsx +++ b/webapp/app.jsx @@ -258,7 +258,7 @@ var Board = React.createClass({ var Users = React.createClass({ getInitialState: function(){ - return { users: [ boards.id ] } + return { users: boards.getUsers() } }, componentDidMount: function(){ boards.searchUsers().on('user',(id) => { @@ -269,7 +269,7 @@ var Users = React.createClass({ render: function(){ return

Users

-

Found {this.state.users.length} users, looking for more...

+

Found {this.state.users.length} users