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-02-10 10:11:51 +0300
committerFeross Aboukhadijeh <feross@feross.org>2016-02-10 10:11:51 +0300
commitcec718dfdac636200018513f17b3b506319c4db4 (patch)
tree38f463044ee14ec3cc7e909da5b5d26d1f45aa17 /index.js
parentb11afa9c30b432d384bb477ddbe94b47caf29148 (diff)
consistency
- client.on(‘error’, err, torrent) always includes relevant torrent object for all errors - ‘torrent’ event emitted when duplicate torrent added - ‘seed’ event emitted when duplicate torrent seeded
Diffstat (limited to 'index.js')
-rw-r--r--index.js86
1 files changed, 45 insertions, 41 deletions
diff --git a/index.js b/index.js
index 8a02678..a3c5410 100644
--- a/index.js
+++ b/index.js
@@ -166,36 +166,35 @@ WebTorrent.prototype.download = function (torrentId, opts, ontorrent) {
var self = this
if (self.destroyed) throw new Error('client is destroyed')
if (typeof opts === 'function') return self.add(torrentId, null, opts)
- opts = opts ? extend(opts) : {}
+
debug('add')
+ opts = opts ? extend(opts) : {}
var torrent = self.get(torrentId)
- function _ontorrent () {
- if (typeof ontorrent === 'function') ontorrent(torrent)
- }
-
if (torrent) {
- if (torrent.ready) process.nextTick(_ontorrent)
- else torrent.on('ready', _ontorrent)
+ if (torrent.ready) process.nextTick(onReady)
+ else torrent.once('ready', onReady)
} else {
- opts.client = self
- torrent = new Torrent(torrentId, opts)
+ torrent = new Torrent(torrentId, self, opts)
self.torrents.push(torrent)
- torrent.on('error', function (err) {
+ torrent.once('error', function (err) {
self.emit('error', err, torrent)
self.remove(torrent)
})
- torrent.on('listening', function (port) {
+ torrent.once('listening', function (port) {
self.emit('listening', port, torrent)
})
- torrent.on('ready', function () {
- _ontorrent()
- self.emit('torrent', torrent)
- })
+ torrent.once('ready', onReady)
+ }
+
+ function onReady () {
+ debug('on torrent')
+ if (typeof ontorrent === 'function') ontorrent(torrent)
+ self.emit('torrent', torrent)
}
return torrent
@@ -205,38 +204,23 @@ WebTorrent.prototype.download = function (torrentId, opts, ontorrent) {
* Start seeding a new file/folder.
* @param {string|File|FileList|Buffer|Array.<string|File|Buffer>} input
* @param {Object=} opts
- * @param {function=} onseed
+ * @param {function=} onseed called when torrent is seeding
*/
WebTorrent.prototype.seed = function (input, opts, onseed) {
var self = this
if (self.destroyed) throw new Error('client is destroyed')
if (typeof opts === 'function') return self.seed(input, null, opts)
- opts = opts ? extend(opts) : {}
+
debug('seed')
+ opts = opts ? extend(opts) : {}
- // When seeding from filesystem, initialize store from that path (avoids a copy)
+ // When seeding from fs path, initialize store from that path to avoid a copy
if (typeof input === 'string') opts.path = path.dirname(input)
if (!opts.createdBy) opts.createdBy = 'WebTorrent/' + VERSION_STR
if (!self.tracker) opts.announce = []
+ var torrent = self.add(null, opts, onTorrent)
var streams
- var torrent = self.add(undefined, opts, function (torrent) {
- var tasks = [
- function (cb) {
- torrent.load(streams, cb)
- }
- ]
- if (self.dht) {
- tasks.push(function (cb) {
- torrent.on('dhtAnnounce', cb)
- })
- }
- parallel(tasks, function (err) {
- if (err) return self.emit('error', err)
- _onseed()
- self.emit('seed', torrent)
- })
- })
if (!Array.isArray(input)) input = [ input ]
parallel(input.map(function (item) {
@@ -245,20 +229,21 @@ WebTorrent.prototype.seed = function (input, opts, onseed) {
else cb(null, item)
}
}), function (err, input) {
- if (err) return self.emit('error', err)
+ if (err) return self.emit('error', err, torrent)
+ if (self.destroyed) return
createTorrent.parseInput(input, opts, function (err, files) {
- if (err) return self.emit('error', err)
+ if (err) return self.emit('error', err, torrent)
+ if (self.destroyed) return
streams = files.map(function (file) { return file.getStream })
createTorrent(input, opts, function (err, torrentBuf) {
- if (err) return self.emit('error', err)
+ if (err) return self.emit('error', err, torrent)
if (self.destroyed) return
var existingTorrent = self.get(torrentBuf)
if (existingTorrent) {
torrent.destroy()
- _onseed()
- return
+ _onseed(existingTorrent)
} else {
torrent._onTorrentId(torrentBuf)
}
@@ -266,9 +251,28 @@ WebTorrent.prototype.seed = function (input, opts, onseed) {
})
})
- function _onseed () {
+ function onTorrent (torrent) {
+ var tasks = [
+ function (cb) {
+ torrent.load(streams, cb)
+ }
+ ]
+ if (self.dht) {
+ tasks.push(function (cb) {
+ torrent.once('dhtAnnounce', cb)
+ })
+ }
+ parallel(tasks, function (err) {
+ if (self.destroyed) return
+ if (err) return self.emit('error', err, torrent)
+ _onseed(torrent)
+ })
+ }
+
+ function _onseed (torrent) {
debug('on seed')
if (typeof onseed === 'function') onseed(torrent)
+ self.emit('seed', torrent)
}
return torrent