Refresh cache on changes and handle access to evicted peer

This commit is contained in:
Yoann Ciabaud 2016-06-09 02:10:07 +02:00
parent 1dbc95cbdc
commit 5dcc932247

View File

@ -21,15 +21,18 @@ Swarm.prototype.announce = function (params, cb) {
// Mark the source peer as recently used in cache
var peer = self.peers.get(id)
if (params.event === 'started') {
// Get the peer back in swarm if missing
if (params.event === 'started' || !peer) {
self._onAnnounceStarted(params, peer)
} else if (params.event === 'stopped') {
}
if (params.event === 'stopped') {
self._onAnnounceStopped(params, peer)
} else if (params.event === 'completed') {
self._onAnnounceCompleted(params, peer)
} else if (params.event === 'update') {
self._onAnnounceUpdate(params, peer)
} else {
} else if (params.event !== 'started') {
cb(new Error('invalid event'))
return
}
@ -91,6 +94,8 @@ Swarm.prototype._onAnnounceCompleted = function (params, peer) {
this.complete += 1
this.incomplete -= 1
peer.complete = true
var id = params.type === 'ws' ? params.peer_id : params.addr
this.peers.set(id, peer)
}
Swarm.prototype._onAnnounceUpdate = function (params, peer) {
@ -103,6 +108,8 @@ Swarm.prototype._onAnnounceUpdate = function (params, peer) {
this.complete += 1
this.incomplete -= 1
peer.complete = true
var id = params.type === 'ws' ? params.peer_id : params.addr
this.peers.set(id, peer)
}
}