Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING: Many fixes; all leaks fixed #762

Merged
merged 12 commits into from Apr 22, 2016

client: cleanup event listeners

  • Loading branch information
feross committed Apr 21, 2016
commit 0a22dc73c15eb9e74a3c0c13f570a76516e41799
@@ -1,5 +1,3 @@
// TODO: cleanup event listeners

module.exports = WebTorrent

var createTorrent = require('create-torrent')
@@ -92,9 +90,11 @@ function WebTorrent (opts) {
if (opts.dht !== false && typeof DHT === 'function' /* browser exclude */) {
// use a single DHT instance for all torrents, so the routing table can be reused
self.dht = new DHT(extend({ nodeId: self.nodeId }, opts.dht))

self.dht.once('error', function (err) {
self._destroy(err)
})

self.dht.once('listening', function () {
var address = self.dht.address()
if (address) self.dhtPort = address.port
@@ -219,24 +219,33 @@ WebTorrent.prototype.add = function (torrentId, opts, ontorrent) {
var torrent = new Torrent(torrentId, self, opts)
self.torrents.push(torrent)

torrent.once('infoHash', function () {
torrent.once('infoHash', onInfoHash)
torrent.once('ready', onReady)
torrent.once('close', onClose)

function onInfoHash () {
if (self.destroyed) return
for (var i = 0, len = self.torrents.length; i < len; i++) {
var t = self.torrents[i]
if (t.infoHash === torrent.infoHash && t !== torrent) {
torrent.removeListener('ready', onReady)
torrent._destroy(new Error('Cannot add duplicate torrent ' + torrent.infoHash))
return
}
}
})
torrent.once('ready', onReady)
}

function onReady () {
if (self.destroyed) return
if (typeof ontorrent === 'function') ontorrent(torrent)
self.emit('torrent', torrent)
}

function onClose () {
torrent.removeListener('infoHash', onInfoHash)
torrent.removeListener('ready', onReady)
torrent.removeListener('close', onClose)
}

return torrent
}

@@ -577,6 +577,8 @@ Torrent.prototype._destroy = function (err, cb) {
}
}

self.emit('close')

self.client = null
self.files = []
self.discovery = null
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.