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

View File

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