prevent negative complete/incomplete numbers

Fixes #65
Fixes #72
This commit is contained in:
Feross Aboukhadijeh 2015-05-03 16:38:16 -07:00
parent 71c5cf5fb6
commit 1338282cc9

View File

@ -19,8 +19,6 @@ Swarm.prototype.announce = function (params, cb) {
if (self[fn]) {
self[fn](params, peer) // process event
if (params.left === 0 && peer) peer.complete = true
cb(null, {
complete: self.complete,
incomplete: self.incomplete,
@ -86,6 +84,12 @@ Swarm.prototype._onAnnounce_update = function (params, peer) {
debug('unexpected `update` event from peer that is not in swarm')
return this._onAnnounce_started(params, peer) // treat as a start
}
if (!peer.complete && params.left === 0) {
this.complete += 1
this.incomplete -= 1
peer.complete = true
}
}
// TODO: randomize the peers that are given out