diff options
-rw-r--r-- | lib/torrent.js | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/lib/torrent.js b/lib/torrent.js index 89ad91d..ae3e256 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -893,7 +893,7 @@ Torrent.prototype.deselect = function (start, end, priority) { for (var i = 0; i < self._selections.length; ++i) { var s = self._selections[i] if (s.from === start && s.to === end && s.priority === priority) { - self._selections.splice(i--, 1) + self._selections.splice(i, 1) break } } @@ -1131,13 +1131,16 @@ Torrent.prototype._updateSelections = function () { Torrent.prototype._gcSelections = function () { var self = this - for (var i = 0; i < self._selections.length; i++) { + var i + var toRemove = [] + + for (i = 0; i < self._selections.length; ++i) { var s = self._selections[i] var oldOffset = s.offset // check for newly downloaded pieces in selection while (self.bitfield.get(s.from + s.offset) && s.from + s.offset < s.to) { - s.offset++ + s.offset += 1 } if (oldOffset !== s.offset) s.notify() @@ -1145,8 +1148,16 @@ Torrent.prototype._gcSelections = function () { if (!self.bitfield.get(s.from + s.offset)) continue // remove fully downloaded selection - self._selections.splice(i--, 1) // decrement i to offset splice - s.notify() // TODO: this may notify twice in a row. is this a problem? + toRemove.push(i) + } + + if (toRemove.length > 0) { + // Since self._selections is being modified in-place, it's necessary to + // traverse `toRemove` in reverse. + for (i = toRemove.length - 1; i >= 0; --i) { + self._selections.splice(i, 1) + } + s.notify() self._updateInterest() } |