diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b85b504..5d09b84a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # WebTorrent Version History +## UNRELEASED + +- When a duplicate torrent is added, don't emit the 'infoHash' event after 'error'. The 'error' event should be the last event. + ## v0.93.1 - 2016-05-08 - Remove `path-exists` dependency. diff --git a/index.js b/index.js index f8194da5..3a69d3c9 100644 --- a/index.js +++ b/index.js @@ -240,7 +240,7 @@ WebTorrent.prototype.add = function (torrentId, opts, ontorrent) { var torrent = new Torrent(torrentId, self, opts) self.torrents.push(torrent) - torrent.once('infoHash', onInfoHash) + torrent.once('_infoHash', onInfoHash) torrent.once('ready', onReady) torrent.once('close', onClose) @@ -262,7 +262,7 @@ WebTorrent.prototype.add = function (torrentId, opts, ontorrent) { } function onClose () { - torrent.removeListener('infoHash', onInfoHash) + torrent.removeListener('_infoHash', onInfoHash) torrent.removeListener('ready', onReady) torrent.removeListener('close', onClose) } diff --git a/lib/torrent.js b/lib/torrent.js index a765ef8b..2e082b90 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -253,8 +253,14 @@ Torrent.prototype._onParsedTorrent = function (parsedTorrent) { }, RECHOKE_INTERVAL) if (self._rechokeIntervalId.unref) self._rechokeIntervalId.unref() + // Private 'infoHash' event allows client.add to check for duplicate torrents and + // destroy them before the normal 'infoHash' event is emitted. Prevents user + // applications from needing to deal with duplicate 'infoHash' events. + self.emit('_infoHash', self.infoHash) + if (self.destroyed) return + self.emit('infoHash', self.infoHash) - if (self.destroyed) return // user might destroy torrent in `infoHash` event handler + if (self.destroyed) return // user might destroy torrent in event handler if (self.client.listening) { self._onListening()