From 5715bae7fd5f7a338a971ee4c69aa36c44fed523 Mon Sep 17 00:00:00 2001
From: Jordan Lee <jordan@transmissionbt.com>
Date: Fri, 26 Aug 2011 22:49:57 +0000
Subject: [PATCH] (trunk web) Reduce the torrent-get RPC wrapper down to a
 single function. Annotate the torrent bootstrap process in Transmission.js.

---
 web/javascript/transmission.js        | 45 ++++++++++++---------
 web/javascript/transmission.remote.js | 58 +++++----------------------
 2 files changed, 36 insertions(+), 67 deletions(-)

diff --git a/web/javascript/transmission.js b/web/javascript/transmission.js
index 9bd1cbb46..5806cdad2 100644
--- a/web/javascript/transmission.js
+++ b/web/javascript/transmission.js
@@ -132,7 +132,7 @@ Transmission.prototype =
 		var async = false;
 		this.loadDaemonPrefs(async);
 		this.loadDaemonStats(async);
-		this.initializeAllTorrents();
+		this.initializeTorrents();
 		this.refreshTorrents();
 		this.togglePeriodicSessionRefresh(true);
 
@@ -1055,15 +1055,17 @@ Transmission.prototype =
 			if ((t = this._torrents[id]))
 				t.refresh(o);
 			else {
-				t = this._torrents[id] = new Torrent(o);
+				var tr = this;
+				t = tr._torrents[id] = new Torrent(o);
 				$(t).bind('dataChanged',function(ev) {tr.onTorrentChanged(ev);});
 				new_ids.push(id);
 			}
 		}
 
 		if (new_ids.length) {
-			var tr = this;
-			this.remote.getTorrentInitial(new_ids, function(a,b){tr.updateFromTorrentGet(a,b);});
+			// whee, new torrents! get their initial information.
+			var fields = ['id'].concat(Torrent.Fields.Metadata, Torrent.Fields.Stats);
+	        	this.remote.updateTorrents(new_ids, fields, this.updateFromTorrentGet, this);
 			this.refilterSoon();
 		}
 
@@ -1076,26 +1078,33 @@ Transmission.prototype =
 	refreshTorrents: function()
 	{
 		// send a request right now
-		var tr = this;
-		this.remote.getTorrentStats('recently-active', function(a,b){tr.updateFromTorrentGet(a,b);});
+		var fields = ['id'].concat(Torrent.Fields.Stats);
+		this.remote.updateTorrents('recently-active', fields, this.updateFromTorrentGet, this);
 
 		// schedule the next request
 		clearTimeout(this.refreshTorrentsTimeout);
-                this.refreshTorrentsTimeout = setTimeout(function(){tr.refreshTorrents();}, tr[Prefs._RefreshRate]*1000);
-	},
-	initializeAllTorrents: function() {
 		var tr = this;
-		this.remote.getTorrentInitial(null, function(a,b){tr.updateFromTorrentGet(a,b);});
+		this.refreshTorrentsTimeout = setTimeout(function(){tr.refreshTorrents();}, tr[Prefs._RefreshRate]*1000);
 	},
-	refreshMetadata: function(ids) {
-		var tr = this;
-		this.remote.getTorrentMetadata(ids, function(a,b){tr.updateFromTorrentGet(a,b);});
+
+	initializeTorrents: function()
+	{
+		// to bootstrap, we only need to ask for the servers's torrents' ids.
+		// updateFromTorrentGet() automatically asks for the rest of the info when it gets a new id.
+	        this.remote.updateTorrents(null, ['id'], this.updateFromTorrentGet, this);
 	},
-	refreshInspectorTorrents: function(full) {
-		var tr = this;
-		var ids = tr.getSelectedTorrentIds();
-		if (ids.length > 0)
-			this.remote.getTorrentDetails(ids, full, function(a,b){tr.updateFromTorrentGet(a,b);});
+
+	refreshInspectorTorrents: function(full)
+	{
+		// some torrent fields are only used by the inspector, so we defer loading them
+		// until the user is viewing the torrent in the inspector.
+		if ($('#torrent_inspector').is(':visible')) {
+			var ids = this.getSelectedTorrentIds();
+			if (ids && ids.length) {
+				var fields = ['id'].concat(Torrent.Fields.StatsExtra);
+				this.remote.updateTorrents(ids, fields, this.updateFromTorrentGet, this);
+			}
+		}
 	},
 
 	onRowClicked: function(ev, row)
diff --git a/web/javascript/transmission.remote.js b/web/javascript/transmission.remote.js
index 325e176b5..c63270421 100644
--- a/web/javascript/transmission.remote.js
+++ b/web/javascript/transmission.remote.js
@@ -133,59 +133,19 @@ TransmissionRemote.prototype =
 		this.sendRequest(o, callback, async);
 	},
 
-	getTorrentInitial: function(torrent_ids, callback) {
+	updateTorrents: function(torrentIds, fields, callback, context) {
 		var o = {
 			method: 'torrent-get',
-			arguments: {
-				fields: ['id'].concat(Torrent.Fields.Metadata, Torrent.Fields.Stats)
-			}
-		};
-
-		if (torrent_ids)
-			o.arguments.ids = torrent_ids;
-
-		this.sendRequest(o, function(data){ callback(data.arguments.torrents, data.arguments.removed);});
-	},
-
-	getTorrentMetadata: function(torrent_ids, callback) {
-		var o = {
-			method: 'torrent-get',
-			arguments: {
-				fields: ['id'].concat(Torrent.Fields.Metadata)
-			}
-		};
-
-		if (torrent_ids)
-			o.arguments.ids = torrent_ids;
-
-		this.sendRequest(o, function(data) {callback(data.arguments.torrents)});
-	},
-
-	getTorrentStats: function(torrent_ids, callback) {
-		var o = {
-			method: 'torrent-get',
-			arguments: {
-				'ids': torrent_ids,
-				fields: ['id'].concat(Torrent.Fields.Stats)
-			}
-		};
-
-		this.sendRequest(o, function(data) {callback(data.arguments.torrents, data.arguments.removed);});
-	},
-
-	/* called for the torrents in the inspector aka details dialog */
-	getTorrentDetails: function(torrent_ids, full, callback) {
-		var f = ['id'].concat(Torrent.Fields.StatsExtra);
-		if (full) // these only need to be loaded once...
-			f = f.concat(Torrent.Fields.InfoExtra);
-		var o = {
-			method: 'torrent-get',
-			arguments: {
-				'ids': torrent_ids,
-				fields: f,
+			'arguments': {
+				'fields': fields,
 			}
 		};
-		this.sendRequest(o, function(data) {callback(data.arguments.torrents,null)});
+		if (torrentIds)
+			o['arguments'].ids = torrentIds;
+		this.sendRequest(o, function(response) {
+			var args = response['arguments'];
+			callback.call(context,args.torrents,args.removed);
+		});
 	},
 
 	changeFileCommand: function(command, rows) {
-- 
2.40.0