diff options
-rw-r--r-- | README.md | 6 | ||||
-rwxr-xr-x | bin/cmd.js | 3 | ||||
-rw-r--r-- | index.js | 24 | ||||
-rw-r--r-- | lib/torrent.js | 36 |
4 files changed, 36 insertions, 33 deletions
@@ -369,11 +369,11 @@ Get the total progress from 0 to 1. Get the torrent ratio (seeded/downloaded). -#### `torrent.downloadSpeed()` +#### `torrent.downloadSpeed` Returns the download speed. -#### `torrent.uploadSpeed()` +#### `torrent.uploadSpeed` Returns the current upload speed. @@ -471,7 +471,7 @@ Emitted every time a new chunk of data arrives, it's useful for reporting the cu torrent.on('download', function(chunkSize){ console.log('chunk size: ' + chunkSize); console.log('total downloaded: ' + torrent.downloaded); - console.log('download speed: ' + torrent.downloadSpeed()); + console.log('download speed: ' + torrent.downloadSpeed); console.log('progress: ' + torrent.progress); console.log('======'); }) @@ -446,7 +446,6 @@ function runDownload (torrentId) { device.play(href, 0, function () {}) }) .start() - // TODO: handle case where user closes airplay. do same thing as when VLC is closed } if (argv.chromecast) { @@ -505,7 +504,7 @@ function drawTorrent (torrent) { }) var linesRemaining = clivas.height var peerslisted = 0 - var speed = torrent.downloadSpeed() + var speed = torrent.downloadSpeed var estimate = moment.duration(torrent.timeRemaining / 1000, 'seconds').humanize() clivas.clear() @@ -64,8 +64,8 @@ function WebTorrent (opts) { self.torrents = [] - self.downloadSpeed = speedometer() - self.uploadSpeed = speedometer() + self._downloadSpeed = speedometer() + self._uploadSpeed = speedometer() self.maxConns = opts.maxConns @@ -104,23 +104,29 @@ function WebTorrent (opts) { } } -/** - * Seed ratio for all torrents in the client. - * @type {number} - */ +// Seed ratio for all torrents (uploaded / downloaded) Object.defineProperty(WebTorrent.prototype, 'ratio', { get: function () { - var self = this - var uploaded = self.torrents.reduce(function (total, torrent) { + var uploaded = this.torrents.reduce(function (total, torrent) { return total + torrent.uploaded }, 0) - var downloaded = self.torrents.reduce(function (total, torrent) { + var downloaded = this.torrents.reduce(function (total, torrent) { return total + torrent.downloaded }, 0) || 1 return uploaded / downloaded } }) +// Download speed in bytes/sec +Object.defineProperty(WebTorrent.prototype, 'downloadSpeed', { + get: function () { return this._downloadSpeed() } +}) + +// Upload speed in bytes/sec +Object.defineProperty(WebTorrent.prototype, 'uploadSpeed', { + get: function () { return this._uploadSpeed() } +}) + /** * Returns the torrent with the given `torrentId`. Convenience method. Easier than * searching through the `client.torrents` array. Returns `null` if no matching torrent diff --git a/lib/torrent.js b/lib/torrent.js index 84fabf2..62a0c2d 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -93,7 +93,7 @@ function Torrent (torrentId, opts) { if (torrentId != null) this._onTorrentId(torrentId) } -// Time remaining (in milliseconds) +// Time remaining in milliseconds Object.defineProperty(Torrent.prototype, 'timeRemaining', { get: function () { if (this.done) return 0 @@ -102,7 +102,7 @@ Object.defineProperty(Torrent.prototype, 'timeRemaining', { } }) -// Bytes completed (excluding invalid data) +// Bytes completed, excluding invalid data Object.defineProperty(Torrent.prototype, 'downloaded', { get: function () { if (!this.bitfield) return 0 @@ -119,7 +119,7 @@ Object.defineProperty(Torrent.prototype, 'downloaded', { } }) -// Bytes received from peers (including invalid data) +// Bytes received from peers, including invalid data Object.defineProperty(Torrent.prototype, 'received', { get: function () { return this.swarm ? this.swarm.downloaded : 0 } }) @@ -129,9 +129,7 @@ Object.defineProperty(Torrent.prototype, 'uploaded', { get: function () { return this.swarm ? this.swarm.uploaded : 0 } }) -/** - * The number of missing pieces. Used to implement 'end game' mode. - */ +// The number of missing pieces. Used to implement 'end game' mode. // Object.defineProperty(Storage.prototype, 'numMissing', { // get: function () { // var self = this @@ -148,11 +146,21 @@ Object.defineProperty(Torrent.prototype, 'progress', { get: function () { return this.length ? this.downloaded / this.length : 0 } }) -// Seed ratio +// Seed ratio (uploaded / downloaded) Object.defineProperty(Torrent.prototype, 'ratio', { get: function () { return this.uploaded / (this.downloaded || 1) } }) +// Download speed in bytes/sec +Object.defineProperty(Torrent.prototype, 'downloadSpeed', { + get: function () { return this.swarm ? this.swarm.downloadSpeed() : 0 } +}) + +// Upload speed in bytes/sec +Object.defineProperty(Torrent.prototype, 'uploadSpeed', { + get: function () { return this.swarm ? this.swarm.uploadSpeed() : 0 } +}) + // Number of peers Object.defineProperty(Torrent.prototype, 'numPeers', { get: function () { return this.swarm ? this.swarm.numPeers : 0 } @@ -168,16 +176,6 @@ Object.defineProperty(Torrent.prototype, 'torrentFileURL', { } }) -// TODO: Make this into a property -Torrent.prototype.downloadSpeed = function () { - return this.swarm ? this.swarm.downloadSpeed() : 0 -} - -// TODO: Make this into a property -Torrent.prototype.uploadSpeed = function () { - return this.swarm ? this.swarm.uploadSpeed() : 0 -} - Torrent.prototype._onTorrentId = function (torrentId) { var self = this if (self.destroyed) return @@ -211,13 +209,13 @@ Torrent.prototype._onParsedTorrent = function (parsedTorrent) { self.swarm.on('wire', self._onWire.bind(self)) self.swarm.on('download', function (downloaded) { - self.client.downloadSpeed(downloaded) // update overall client stats + self.client._downloadSpeed(downloaded) // update overall client stats self.client.emit('download', downloaded) self.emit('download', downloaded) }) self.swarm.on('upload', function (uploaded) { - self.client.uploadSpeed(uploaded) // update overall client stats + self.client._uploadSpeed(uploaded) // update overall client stats self.client.emit('upload', uploaded) self.emit('upload', uploaded) }) |