mirror of
https://github.com/fazo96/ipfs-boards
synced 2025-03-12 21:48:39 +01:00
optimizations, implemented local storage cache
This commit is contained in:
parent
6c377ff54e
commit
1fb06cac4b
@ -53,6 +53,25 @@ function BoardsAPI(ipfs){
|
|||||||
this.users = {} // userID : profileHash
|
this.users = {} // userID : profileHash
|
||||||
this.resolving_ipns = {} // to check if a resolve is already in progress
|
this.resolving_ipns = {} // to check if a resolve is already in progress
|
||||||
this.ee = new EventEmitter()
|
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
|
// Rewrote this to use event emitters. Should also add periodic rechecking
|
||||||
@ -74,15 +93,11 @@ BoardsAPI.prototype.resolveIPNS = function(n,handler){
|
|||||||
if(err){
|
if(err){
|
||||||
// Communicate error
|
// Communicate error
|
||||||
this.ee.emit(n,undefined,err)
|
this.ee.emit(n,undefined,err)
|
||||||
} else if(!cached){
|
} else if(cached !== r.Path){
|
||||||
//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
|
||||||
this.ee.emit(n,r.Path)
|
this.backupCache()
|
||||||
} 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.ee.emit(n,r.Path)
|
this.ee.emit(n,r.Path)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -90,10 +105,6 @@ BoardsAPI.prototype.resolveIPNS = function(n,handler){
|
|||||||
return this.ee
|
return this.ee
|
||||||
}
|
}
|
||||||
|
|
||||||
BoardsAPI.prototype.getIPNS = function(){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
BoardsAPI.prototype.isUserProfile = function(addr,done){
|
BoardsAPI.prototype.isUserProfile = function(addr,done){
|
||||||
this.ipfs.cat(addr+'/ipfs-boards-version.txt',(err,r) => {
|
this.ipfs.cat(addr+'/ipfs-boards-version.txt',(err,r) => {
|
||||||
if(err) return done(false)
|
if(err) return done(false)
|
||||||
@ -239,25 +250,11 @@ BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){
|
|||||||
return ee
|
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:
|
// API for publishing content and managing to be done later...
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
// Initialize API
|
// Initialize API
|
||||||
BoardsAPI.prototype.init = function(done){
|
BoardsAPI.prototype.init = function(done){
|
||||||
@ -265,7 +262,7 @@ BoardsAPI.prototype.init = function(done){
|
|||||||
if(err){
|
if(err){
|
||||||
console.log(err)
|
console.log(err)
|
||||||
done(err)
|
done(err)
|
||||||
} else {
|
} else if(res.ID){
|
||||||
console.log('I am',res.ID)
|
console.log('I am',res.ID)
|
||||||
this.id = res.ID
|
this.id = res.ID
|
||||||
this.isUser(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) => {
|
this.ipfs.add(new Buffer('ipfs:boards:version:'+this.version),(err,r) => {
|
||||||
if(err){
|
if(err){
|
||||||
console.log('Error while calculating version hash:',err)
|
console.log('Error while calculating version hash:',err)
|
||||||
}
|
done(err)
|
||||||
|
} else {
|
||||||
this.version_hash = r[0].Hash
|
this.version_hash = r[0].Hash
|
||||||
console.log('Version hash is',this.version_hash)
|
console.log('Version hash is',this.version_hash)
|
||||||
done(null)
|
done(null)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user