feat(store): live sync store records on connection to every peer
This commit is contained in:
parent
2d653caaa7
commit
19380dbd9f
@ -7,6 +7,10 @@ module.exports = class P2PStore extends EventEmitter {
|
|||||||
{
|
{
|
||||||
super()
|
super()
|
||||||
this.id = 0
|
this.id = 0
|
||||||
|
Object.defineProperty(p2p.info, 'store', {
|
||||||
|
enumerable: true,
|
||||||
|
get: () => this.id
|
||||||
|
});
|
||||||
this.synchronized = false
|
this.synchronized = false
|
||||||
|
|
||||||
console.log('connect p2p store...')
|
console.log('connect p2p store...')
|
||||||
@ -22,12 +26,14 @@ module.exports = class P2PStore extends EventEmitter {
|
|||||||
|
|
||||||
console.log('store db index', this.id)
|
console.log('store db index', this.id)
|
||||||
|
|
||||||
let lock = false
|
this.p2p.events.on('peer', (peer) => {
|
||||||
this.p2p.events.on('peer', () => {
|
if(peer.info && peer.info.store)
|
||||||
if(lock)
|
{
|
||||||
return
|
if(peer.info.store > this.id)
|
||||||
lock = true
|
this.sync(peer) // sync db
|
||||||
setTimeout(() => this.sync(), 1000)
|
else if(peer.info.store === this.id)
|
||||||
|
this.synchronized = true
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -69,9 +75,9 @@ module.exports = class P2PStore extends EventEmitter {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
sync()
|
sync(peer)
|
||||||
{
|
{
|
||||||
console.log('sync db on version', this.id)
|
console.log('sync db on version', this.id, peer ? `from peer ${peer.peerId}` : '')
|
||||||
const processSync = (data, nil, peer) => {
|
const processSync = (data, nil, peer) => {
|
||||||
if(!data || !data.records)
|
if(!data || !data.records)
|
||||||
return
|
return
|
||||||
@ -88,7 +94,11 @@ module.exports = class P2PStore extends EventEmitter {
|
|||||||
peer.emit('dbSync', {id: this.id}, processSync)
|
peer.emit('dbSync', {id: this.id}, processSync)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.p2p.emit('dbSync', {id: this.id}, processSync)
|
if(peer)
|
||||||
|
peer.emit('dbSync', {id: this.id}, processSync)
|
||||||
|
else
|
||||||
|
this.p2p.emit('dbSync', {id: this.id}, processSync)
|
||||||
|
|
||||||
this.synchronized = true
|
this.synchronized = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user