Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFeross Aboukhadijeh <feross@feross.org>2016-04-21 09:31:38 +0300
committerFeross Aboukhadijeh <feross@feross.org>2016-04-21 09:31:38 +0300
commit6085dc79c577ceab5f9f5f305748af5a7d5b33e7 (patch)
treee5cadfcf7da560fc7b3b75f2c8749dd169f22875
parent3daee2c66cbf752b9e6e49b99492b8c1914a4a58 (diff)
torrent: remove _onError, add _destroy(err, cb)
-rw-r--r--lib/torrent.js44
1 files changed, 21 insertions, 23 deletions
diff --git a/lib/torrent.js b/lib/torrent.js
index a5dc4c2..6756633 100644
--- a/lib/torrent.js
+++ b/lib/torrent.js
@@ -1,4 +1,3 @@
-// TODO: remove _onError, add _destroy(err, cb)
// TODO: cleanup event listeners
// TODO: Remove all inline docs, and move to docs/api.md
@@ -238,7 +237,7 @@ Torrent.prototype._onTorrentId = function (torrentId) {
// operation, i.e. http/https link, filesystem path, or Blob.
parseTorrent.remote(torrentId, function (err, parsedTorrent) {
if (self.destroyed) return
- if (err) return self._onError(err)
+ if (err) return self._destroy(err)
self._onParsedTorrent(parsedTorrent)
})
}
@@ -247,12 +246,11 @@ Torrent.prototype._onTorrentId = function (torrentId) {
Torrent.prototype._onParsedTorrent = function (parsedTorrent) {
var self = this
if (self.destroyed) return
- console.log('on parsed torrent')
self._processParsedTorrent(parsedTorrent)
if (!self.infoHash) {
- return self._onError(new Error('Malformed torrent data: No info hash'))
+ return self._destroy(new Error('Malformed torrent data: No info hash'))
}
if (!self.path) self.path = path.join(TMP, self.infoHash)
@@ -296,13 +294,11 @@ Torrent.prototype._processParsedTorrent = function (parsedTorrent) {
this.magnetURI = parseTorrent.toMagnetURI(parsedTorrent)
this.torrentFile = parseTorrent.toTorrentFile(parsedTorrent)
- console.log('process done')
}
Torrent.prototype._onListening = function () {
var self = this
if (self.discovery || self.destroyed) return
- console.log('on listening')
var trackerOpts = {
rtcConfig: self.client._rtcConfig,
wrtc: self.client._wrtc,
@@ -327,7 +323,7 @@ Torrent.prototype._onListening = function () {
port: self.client.torrentPort
})
self.discovery.on('error', function (err) {
- self._onError(err)
+ self._destroy(err)
})
self.discovery.on('peer', function (peer) {
// Don't create new outgoing TCP connections when torrent is done
@@ -367,7 +363,7 @@ Torrent.prototype._onMetadata = function (metadata) {
try {
parsedTorrent = parseTorrent(metadata)
} catch (err) {
- return self._onError(err)
+ return self._destroy(err)
}
}
@@ -426,7 +422,7 @@ Torrent.prototype._onMetadata = function (metadata) {
if (self._fileModtimes && self._store === FSChunkStore) {
// don't verify if the files haven't been modified since we last checked
self.getFileModtimes(function (err, fileModtimes) {
- if (err) return self._onError(err)
+ if (err) return self._destroy(err)
var unchanged = self.files.map(function (_, index) {
return fileModtimes[index] === self._fileModtimes[index]
@@ -490,7 +486,7 @@ Torrent.prototype._verifyPieces = function () {
})
}
}), FILESYSTEM_CONCURRENCY, function (err) {
- if (err) return self._onError(err)
+ if (err) return self._destroy(err)
self._debug('done verifying')
self._onStore()
})
@@ -528,6 +524,11 @@ Torrent.prototype._onStore = function () {
*/
Torrent.prototype.destroy = function (cb) {
var self = this
+ self._destroy(null, cb)
+}
+
+Torrent.prototype._destroy = function (err, cb) {
+ var self = this
if (self.destroyed) return
self.destroyed = true
self._debug('destroy')
@@ -562,6 +563,16 @@ Torrent.prototype.destroy = function (cb) {
}
parallel(tasks, cb)
+
+ if (err) {
+ // When there is no `torrent.on('error')` listener, emit `client.on('error')` instead.
+ // The more-specific, torrent error handler is preferred.
+ if (self.listenerCount('error') === 0) {
+ self.client.emit('error', err)
+ } else {
+ self.emit('error', err)
+ }
+ }
}
/**
@@ -574,8 +585,6 @@ Torrent.prototype.addPeer = function (peer) {
var self = this
if (self.destroyed) throw new Error('torrent is destroyed')
if (!self.infoHash) throw new Error('addPeer() must not be called before the `infoHash` event')
- console.log('addPeer:', peer)
- console.log(self.infoHash)
if (self.client.blocked) {
var host
@@ -1541,17 +1550,6 @@ Torrent.prototype.resume = function () {
this._drain()
}
-Torrent.prototype._onError = function (err) {
- var self = this
- self._debug('torrent error: %s', err.message || err)
- self.destroy()
- if (self.listenerCount('error') === 0) {
- self.client.emit('error', err)
- } else {
- self.emit('error', err)
- }
-}
-
Torrent.prototype._debug = function () {
var args = [].slice.call(arguments)
args[0] = '[' + this._debugId + '] ' + args[0]