]> granicus.if.org Git - transmission/commitdiff
(trunk web) Reduce the torrent-get RPC wrapper down to a single function. Annotate...
authorJordan Lee <jordan@transmissionbt.com>
Fri, 26 Aug 2011 22:49:57 +0000 (22:49 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Fri, 26 Aug 2011 22:49:57 +0000 (22:49 +0000)
web/javascript/transmission.js
web/javascript/transmission.remote.js

index 9bd1cbb46d9f31c9754cb7b4b08c82c3a3c23f01..5806cdad2d1c3b4e923d23ede2733eea6ca5fe3f 100644 (file)
@@ -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)
index 325e176b54bdb44369c98052938ed41a5a2c582d..c63270421d03a5460b8bbb9f276d6c64e0dbe69c 100644 (file)
@@ -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) {