From cec718dfdac636200018513f17b3b506319c4db4 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 9 Feb 2016 23:11:51 -0800 Subject: consistency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- index.js | 86 +++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 45 insertions(+), 41 deletions(-) (limited to 'index.js') 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.} 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 -- cgit v1.2.3