From 0ea039fafcd0966fc8d14eaed84439d3d0791711 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Mon, 28 Dec 2015 16:50:02 +0100 Subject: [PATCH] Fix torrent.done state when there are deselected files (fix #316) Supersedes PR https://github.com/feross/webtorrent/pull/533 --- lib/torrent.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/torrent.js b/lib/torrent.js index 3d0ba168..71ee9dcb 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -1154,8 +1154,20 @@ Torrent.prototype._checkDone = function () { debug('file done: ' + file.name) }) - // is the torrent done? - if (self.files.every(function (file) { return file.done })) { + // is the torrent done? (if all current selections are satisfied, or there are + // no selections, then torrent is done) + var done = true + for (var i = 0; i < self._selections.length; i++) { + var selection = self._selections[i] + for (var piece = selection.from; piece <= selection.to; piece++) { + if (!self.bitfield.get(piece)) { + done = false + break + } + } + if (!done) break + } + if (!self.done && done) { self.done = true self.emit('done') debug('torrent done: ' + self.infoHash)