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:
-rw-r--r--index.js86
-rw-r--r--lib/torrent.js5
2 files changed, 48 insertions, 43 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
diff --git a/lib/torrent.js b/lib/torrent.js
index 8e7815b..8992857 100644
--- a/lib/torrent.js
+++ b/lib/torrent.js
@@ -54,13 +54,14 @@ inherits(Torrent, EventEmitter)
/**
* @param {string|Buffer|Object} torrentId
+ * @param {WebTorrent} client
* @param {Object=} opts
*/
-function Torrent (torrentId, opts) {
+function Torrent (torrentId, client, opts) {
EventEmitter.call(this)
if (!debug.enabled) this.setMaxListeners(0)
- this.client = opts.client
+ this.client = client
this._debugId = this.client.peerId.slice(32)
this._debug('new torrent')