1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-03-11 21:38:38 +01:00

moving to event emitters. Solved warnings

This commit is contained in:
Enrico Fasoli 2015-11-14 18:26:56 +01:00
parent 8ec8edc0f3
commit 0fd0141068
2 changed files with 34 additions and 24 deletions

View File

@ -6,6 +6,7 @@ particular application. Let's hope it turns out decent
Needs to be browserified to work in the browser
*/
// EventEmitter used to communicate with clients
var EventEmitter = require('wolfy87-eventemitter')
function asObj(str,done){
@ -52,33 +53,35 @@ function BoardsAPI(ipfs){
this.users = {} // userID : profileHash
this.posts = {} // boardName : postsList
this.comments = {} // objectID : comments
this.ee = new EventEmitter()
}
// This function works but needs a little rethinking.
// TODO: convert it to emitters and periodically check. Emit events if the IPNS updates
// TODO: this SUCKS and is SLOW. NEEDS TO BE FIXED
BoardsAPI.prototype.resolveIPNS = function(n,done){
// Rewrote this to use event emitters. Should also add periodic rechecking
BoardsAPI.prototype.resolveIPNS = function(n,handler){
this.ee.on(n,handler)
var cached = this.users[n]
//console.log('Cached is',cached)
if(cached){
//console.log('Returning cached',n,'is',this.users[n])
done(null,cached)
this.ee.emit(n,cached)
}
this.ipfs.name.resolve(n,(err,r) => {
if(!err) console.log('Resolved',n,'to',r.Path)
if(err){
done(err)
// Communicate error
this.ee.emit(n,undefined,err)
} else if(!cached){
//console.log('oldcache',this.users)
//console.log('Setting cache for',n,'to',r.Path)
this.users[n] = r.Path
done(err,r.Path)
this.ee.emit(n,r.Path)
} else if(cached !== r.Path){
// Update cache
// Update cache, emit new value to listeners
//console.log('Setting cache for',n,'from',this.users[n],'to',r.Path)
this.users[n] = r.Path
this.ee.emit(n,r.Path)
}
})
return this.ee
}
BoardsAPI.prototype.isUserProfile = function(addr,done){
@ -96,7 +99,7 @@ BoardsAPI.prototype.isUser = function(s,done){
var ss = s.split('/')
var addr = ss[ss.length-1]
// Try to see if they run IPFS Boards
this.resolveIPNS(addr,(err,url) => {
this.resolveIPNS(addr,(url,err) => {
if(err){
if(done) done(false)
return console.log('Cannot resolve',addr,':',err)
@ -108,11 +111,11 @@ BoardsAPI.prototype.isUser = function(s,done){
if(done) done(true,addr,url)
} else if(done) done(false)
})
return true // remove myself from listeners
})
}
BoardsAPI.prototype.searchUsers = function(){
var ee = new EventEmitter()
// Look at our peers
this.ipfs.swarm.peers((err,r) => {
if(err) return console.log(err)
@ -121,19 +124,18 @@ BoardsAPI.prototype.searchUsers = function(){
reply.Strings.forEach(item => {
this.isUser(item,(isit,addr,url) => {
if(isit){
ee.emit('found user',addr,url)
this.ee.emit('found user',addr,url)
}
})
})
})
})
return ee
return this.ee
}
BoardsAPI.prototype.getProfile = function(userID,done){
var ee = new EventEmitter()
console.log('profile requested for',userID)
this.resolveIPNS(userID,(err,url) => {
this.resolveIPNS(userID,(url,err) => {
if(err){
done(err,null)
} else {
@ -153,16 +155,17 @@ BoardsAPI.prototype.getProfile = function(userID,done){
var l = res.Objects[0].Links.map(i => {
return { name: i.Name, hash: i.Hash }
})
ee.emit('boards',l)
this.ee.emit('boards',l)
} else console.log(err2)
})
}
return true // remove myself from listeners
})
return ee
return this.ee
}
BoardsAPI.prototype.getBoardSettings = function(userID,board,done){
this.resolveIPNS(userID,(e,r) => {
this.resolveIPNS(userID,(r,e) => {
if(e){
done(e)
} else {
@ -176,6 +179,7 @@ BoardsAPI.prototype.getBoardSettings = function(userID,board,done){
}
})
}
return true // remove myself from listeners
})
}
@ -186,7 +190,7 @@ BoardsAPI.prototype.getPostsInBoard = function(adminID,board){
// For now we only list admin's posts
})
*/
var ee = this.getUserPostListInBoard(adminID,board,(err,res) =>{
this.getUserPostListInBoard(adminID,board,(err,res) =>{
if(err){
console.log(err)
} else res.forEach(item => {
@ -195,17 +199,17 @@ BoardsAPI.prototype.getPostsInBoard = function(adminID,board){
console.log('Could not download post',item,'of',board+'@'+adminID)
} else {
// It already returns a JSON?
ee.emit('post',r)
this.ee.emit('post',r,item.hash)
}
})
})
})
return ee
return this.ee
}
BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){
var ee = new EventEmitter()
this.resolveIPNS(user,(err,url) => {
this.resolveIPNS(user,(url,err) => {
if(err){
done(err)
} else this.ipfs.ls(url+'/posts/'+board,(e,r) => {
@ -220,6 +224,7 @@ BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){
done(null,l)
}
})
return true // remove myself from listeners
})
return ee
}

View File

@ -55,6 +55,7 @@ var Profile = React.createClass({
},
componentDidMount: function(){
var ee = boards.getProfile(this.props.params.userid, (err,res) => {
if(!this.isMounted()) return true
if(err){
console.log(err)
this.setState({
@ -67,6 +68,7 @@ var Profile = React.createClass({
}
})
ee.on('boards',l => {
if(!this.isMounted()) return true
this.setState({ boards: l })
})
},
@ -91,8 +93,9 @@ var PostList = React.createClass({
return { posts: [] }
},
componentDidMount: function(){
var ee = boards.getPostsInBoard(this.props.admin,this.props.board)
ee.on('post',(post) => {
console.log('Initial POSTS',this.state.posts.length)
boards.getPostsInBoard(this.props.admin,this.props.board).on('post',(post,hash) => {
if(!this.isMounted()) return true
var posts = this.state.posts
posts.push(post)
this.setState({ posts })
@ -118,6 +121,7 @@ var UserID = React.createClass({
},
componentDidMount: function(){
boards.getProfile(this.props.id, (err,res) => {
if(!this.isMounted()) return true
if(!err) {
this.setState({ name: '@'+res.name.trim() })
}
@ -138,6 +142,7 @@ var Board = React.createClass({
},
componentDidMount: function(){
boards.getBoardSettings(this.props.params.userid,this.props.params.boardname, (err,res) => {
if(!this.isMounted()) return true
if(err) {
console.log('Huh? Invalid board settings?',err)
} else {