diff options
Diffstat (limited to 'lib/torrent.js')
-rw-r--r-- | lib/torrent.js | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/lib/torrent.js b/lib/torrent.js index 7f583c4..0e83e56 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -251,6 +251,11 @@ Torrent.prototype._onParsedTorrent = function (parsedTorrent) { self._onSwarmListening() }) + self._rechokeIntervalId = setInterval(function () { + self._rechoke() + }, RECHOKE_INTERVAL) + if (self._rechokeIntervalId.unref) self._rechokeIntervalId.unref() + self.emit('infoHash', self.infoHash) } @@ -496,15 +501,14 @@ Torrent.prototype._onStore = function () { // start off selecting the entire torrent with low priority self.select(0, self.pieces.length - 1, false) - self._rechokeIntervalId = setInterval(function () { - self._rechoke() - }, RECHOKE_INTERVAL) - if (self._rechokeIntervalId.unref) self._rechokeIntervalId.unref() - self.ready = true self.emit('ready') + // Files may start out done if the file was already in the store self._checkDone() + + // In case any selections were made before torrent was ready + self._updateSelections() } /** @@ -518,10 +522,7 @@ Torrent.prototype.destroy = function (cb) { self.client.remove(self) - if (self._rechokeIntervalId) { - clearInterval(self._rechokeIntervalId) - self._rechokeIntervalId = null - } + clearInterval(self._rechokeIntervalId) var tasks = [] @@ -849,13 +850,7 @@ Torrent.prototype._onWireWithMetadata = function (wire) { */ Torrent.prototype._updateSelections = function () { var self = this - if (!self.swarm || self.destroyed) return - if (!self.metadata) { - self.once('metadata', function () { - self._updateSelections() - }) - return - } + if (!self.ready || self.destroyed) return process.nextTick(function () { self._gcSelections() @@ -1080,6 +1075,7 @@ Torrent.prototype._updateWire = function (wire) { */ Torrent.prototype._rechoke = function () { var self = this + if (!self.ready) return if (self._rechokeOptimisticTime > 0) self._rechokeOptimisticTime -= 1 else self._rechokeOptimisticWire = null |