1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-10 12:24:20 +01:00

removed cache layer for IPNS

This commit is contained in:
Enrico Fasoli 2015-12-12 11:57:51 +01:00
parent 947ec377f4
commit 7c6360389b
2 changed files with 28 additions and 29 deletions

View File

@ -60,8 +60,8 @@ function BoardsAPI(ipfs){
this.ipfs = ipfs this.ipfs = ipfs
this.version = 'ipfs:boards:version:dev' this.version = 'ipfs:boards:version:dev'
this.baseurl = '/ipfs-boards-profile/' this.baseurl = '/ipfs-boards-profile/'
this.users = {} // userID : profileHash this.users = [] // list of IPNS names
this.resolving_ipns = {} // to check if a resolve is already in progress this.resolvingIPNS = {}
this.ee = new EventEmitter() this.ee = new EventEmitter()
if(localStorage !== undefined){ if(localStorage !== undefined){
// Use localStorage to store the IPNS cache // Use localStorage to store the IPNS cache
@ -69,10 +69,10 @@ function BoardsAPI(ipfs){
try { try {
this.users = JSON.parse(stored) this.users = JSON.parse(stored)
if(this.users === null || this.users === undefined){ if(this.users === null || this.users === undefined){
this.users = {} this.users = []
} }
} catch(e){ } catch(e){
this.users = {} this.users = []
} }
} }
} }
@ -87,16 +87,10 @@ BoardsAPI.prototype.backupCache = function(){
// Rewrote this to use event emitters. Should also add periodic rechecking // Rewrote this to use event emitters. Should also add periodic rechecking
BoardsAPI.prototype.resolveIPNS = function(n,handler){ BoardsAPI.prototype.resolveIPNS = function(n,handler){
if(handler && handler.apply) this.ee.on(n,handler) if(handler && handler.apply) this.ee.on(n,handler)
var cached = this.users[n] if(!this.resolvingIPNS[n]){
if(cached){ this.resolvingIPNS[n] = true
this.ee.emit(n,cached)
//console.log(n,'was cached',cached)
} else {
console.log(n,'not cached')
}
if(this.resolving_ipns[n] != true){
this.resolving_ipns[n] = true
this.ipfs.name.resolve(n,(err,r) => { this.ipfs.name.resolve(n,(err,r) => {
delete this.resolvingIPNS[n]
if(err){ if(err){
// Communicate error // Communicate error
this.ee.emit('error',err) this.ee.emit('error',err)
@ -105,20 +99,25 @@ BoardsAPI.prototype.resolveIPNS = function(n,handler){
if(url === undefined){ if(url === undefined){
console.log('Could not resolve',n) console.log('Could not resolve',n)
this.ee.emit('error',r.Message) this.ee.emit('error',r.Message)
} else if(this.users[n] != url) this.isUserProfile(url,(isit,err) => { } else if(this.users.indexOf(n) < 0){ // new find
if(isit){ this.isUserProfile(url,(isit,err) => {
console.log(n,'is a user') if(isit){
if(this.users[n] === undefined) this.ee.emit('user',n,url) console.log(n,'is a user')
this.users[n] = url this.ee.emit(n,url)
this.ee.emit(n,url) if(this.users.indexOf(n) < 0){
this.backupCache() this.ee.emit('user',n,url)
} else { this.users.push(n)
console.log(n,'not a valid profile:',err) this.backupCache()
this.ee.emit(n,undefined,'not a valid profile: '+err) }
} } else {
this.resolving_ipns[n] = false console.log(n,'not a valid profile:',err)
return true // Remove from listeners this.ee.emit(n,undefined,'not a valid profile: '+err)
}) }
return true // Remove from listeners
})
} else { // Already known
this.ee.emit(n,url)
}
} }
}) })
} }
@ -497,7 +496,7 @@ BoardsAPI.prototype.getEventEmitter = function(){
} }
BoardsAPI.prototype.getUsers = function(){ BoardsAPI.prototype.getUsers = function(){
return Object.keys(this.users) return this.users
} }
BoardsAPI.prototype.getMyID = function(){ BoardsAPI.prototype.getMyID = function(){

View File

@ -59,7 +59,7 @@ module.exports = function(boardsAPI){
<Markdown source={this.state.description} skipHtml={true} /> <Markdown source={this.state.description} skipHtml={true} />
{this.props.params.userid?<h5><UserID id={this.props.params.userid} api={this.state.boards} /></h5>:<p></p>} {this.props.params.userid?<h5><UserID id={this.props.params.userid} api={this.state.boards} /></h5>:<p></p>}
<div className="whitelist"> <div className="whitelist">
{this.state.whitelist.map(i => <UserID id={i} api={this.state.boards} />)} {this.state.whitelist.map(i => <UserID id={i} key={i} api={this.state.boards} />)}
</div> </div>
<hr /> <hr />
<PostList board={this.props.params.boardname} admin={this.props.params.userid} api={this.state.boards} /> <PostList board={this.props.params.boardname} admin={this.props.params.userid} api={this.state.boards} />