From 2670b687f9ef7be87f4f8a128c80f0260f1ea131 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 3 Mar 2016 14:24:37 -0800 Subject: client.progress only for *active* torrents --- index.js | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) (limited to 'index.js') diff --git a/index.js b/index.js index 736fc4c..f6f7614 100644 --- a/index.js +++ b/index.js @@ -111,40 +111,39 @@ function WebTorrent (opts) { } } -// Seed ratio for all torrents (uploaded / downloaded) -Object.defineProperty(WebTorrent.prototype, 'ratio', { - get: function () { - var uploaded = this.torrents.reduce(function (total, torrent) { - return total + torrent.uploaded - }, 0) - var downloaded = this.torrents.reduce(function (total, torrent) { - return total + torrent.downloaded - }, 0) || 1 - return uploaded / downloaded - } +Object.defineProperty(WebTorrent.prototype, 'downloadSpeed', { + get: function () { return this._downloadSpeed() } +}) + +Object.defineProperty(WebTorrent.prototype, 'uploadSpeed', { + get: function () { return this._uploadSpeed() } }) -// Percentage complete, represented as a number between 0 and 1 Object.defineProperty(WebTorrent.prototype, 'progress', { get: function () { - var downloaded = this.torrents.reduce(function (total, torrent) { + var torrents = this.torrents.filter(function (torrent) { + return torrent.progress !== 1 + }) + var downloaded = torrents.reduce(function (total, torrent) { return total + torrent.downloaded }, 0) - var length = this.torrents.reduce(function (total, torrent) { + var length = torrents.reduce(function (total, torrent) { return total + (torrent.length || 0) }, 0) || 1 return downloaded / length } }) -// 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() } +Object.defineProperty(WebTorrent.prototype, 'ratio', { + get: function () { + var uploaded = this.torrents.reduce(function (total, torrent) { + return total + torrent.uploaded + }, 0) + var downloaded = this.torrents.reduce(function (total, torrent) { + return total + torrent.downloaded + }, 0) || 1 + return uploaded / downloaded + } }) /** -- cgit v1.2.3