Skip to content

Commit

Permalink
fix(store): fix limitation on records sync
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Jun 9, 2018
1 parent 0fabac0 commit 81c471b
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/background/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,34 @@ module.exports = class P2PStore extends EventEmitter {
}

if(records.length > 0)
callback({records})
callback({
records,
index: this.id
})
})
})
}

sync()
{
console.log('sync db on version', this.id)
this.p2p.emit('dbSync', {id: this.id}, (data) => {
const processSync = (data, nil, peer) => {
if(!data || !data.records)
return

const oldIndex = this.id
data.records.forEach(record => this._syncRecord(record))
})

// peer also can contain another part of store records, try to sync them all
if(data.index >= 0
&& oldIndex < this.id // last sync update of store must be successful, otherwise no point to try sync db from this peer
&& this.id < data.index)
{
console.log('continue sync store from', this.id, 'index', 'peer', peer.peerId)
peer.emit('dbSync', {id: this.id}, processSync)
}
}
this.p2p.emit('dbSync', {id: this.id}, processSync)
this.synchronized = true
}

Expand Down

0 comments on commit 81c471b

Please sign in to comment.