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

optimizations, implemented local storage cache

This commit is contained in:
Enrico Fasoli 2015-11-14 23:55:55 +01:00
parent 6c377ff54e
commit 1fb06cac4b

View File

@ -53,6 +53,25 @@ function BoardsAPI(ipfs){
this.users = {} // userID : profileHash
this.resolving_ipns = {} // to check if a resolve is already in progress
this.ee = new EventEmitter()
if(localStorage !== undefined){
// Use localStorage to store the IPNS cache
var stored = localStorage.getItem('ipfs-boards-user-cache')
try {
this.users = JSON.parse(stored)
if(this.users === null || this.users === undefined){
this.users = {}
}
} catch(e){
this.users = {}
}
}
}
BoardsAPI.prototype.backupCache = function(){
if(localStorage !== undefined){
// Use localStorage to store the IPNS cache
localStorage.setItem('ipfs-boards-user-cache',JSON.stringify(this.users))
}
}
// Rewrote this to use event emitters. Should also add periodic rechecking
@ -74,15 +93,11 @@ BoardsAPI.prototype.resolveIPNS = function(n,handler){
if(err){
// Communicate error
this.ee.emit(n,undefined,err)
} else if(!cached){
} else if(cached !== r.Path){
//console.log('oldcache',this.users)
//console.log('Setting cache for',n,'to',r.Path)
this.users[n] = r.Path
this.ee.emit(n,r.Path)
} else if(cached !== r.Path){
// 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.backupCache()
this.ee.emit(n,r.Path)
}
})
@ -90,10 +105,6 @@ BoardsAPI.prototype.resolveIPNS = function(n,handler){
return this.ee
}
BoardsAPI.prototype.getIPNS = function(){
}
BoardsAPI.prototype.isUserProfile = function(addr,done){
this.ipfs.cat(addr+'/ipfs-boards-version.txt',(err,r) => {
if(err) return done(false)
@ -239,25 +250,11 @@ BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){
return ee
}
BoardsAPI.prototype.getCommentsFor = function(parent,done){
BoardsAPI.prototype.getCommentsFor = function(parent){
// Create an EventEmitter, start looking and emit an event for every new comment
}
// Work only in writable mode:
BoardsAPI.prototype.createPost = function(post,board,done){
}
BoardsAPI.prototype.createComment = function(parent,comment,done){
}
BoardsAPI.prototype.createUpvote = function(parent,done){
}
// API for managing the administrations to be done later
// API for publishing content and managing to be done later...
// Initialize API
BoardsAPI.prototype.init = function(done){
@ -265,7 +262,7 @@ BoardsAPI.prototype.init = function(done){
if(err){
console.log(err)
done(err)
} else {
} else if(res.ID){
console.log('I am',res.ID)
this.id = res.ID
this.isUser(res.ID)
@ -273,10 +270,12 @@ BoardsAPI.prototype.init = function(done){
this.ipfs.add(new Buffer('ipfs:boards:version:'+this.version),(err,r) => {
if(err){
console.log('Error while calculating version hash:',err)
done(err)
} else {
this.version_hash = r[0].Hash
console.log('Version hash is',this.version_hash)
done(null)
}
this.version_hash = r[0].Hash
console.log('Version hash is',this.version_hash)
done(null)
})
}
})