From b164fef0c5d8fb4dcdbd15038a612378cf5cfa67 Mon Sep 17 00:00:00 2001 From: Damien Churchill Date: Mon, 14 Sep 2009 15:55:36 +0000 Subject: [PATCH] extend the Deluge.Events class to include support for subscribing to the remote events --- deluge/ui/web/js/Deluge.Events.js | 34 +++++++++++++++++++++++++++++-- deluge/ui/web/js/Deluge.UI.js | 11 +++++----- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/deluge/ui/web/js/Deluge.Events.js b/deluge/ui/web/js/Deluge.Events.js index 8eef3b2cb..8edbf2518 100644 --- a/deluge/ui/web/js/Deluge.Events.js +++ b/deluge/ui/web/js/Deluge.Events.js @@ -39,7 +39,6 @@ Copyright: */ (function() { - Events = Ext.extend(Ext.util.Observable, { constructor: function() { Events.superclass.constructor.call(this); @@ -47,8 +46,39 @@ Copyright: addListener: function(eventName, fn, scope, o) { this.addEvents(eventName); + if (/[A-Z]/.test(eventName.substring(0, 1))) { + Deluge.Client.web.register_event_listener(eventName); + } Events.superclass.addListener.call(this, eventName, fn, scope, o); - } + }, + + poll: function() { + Deluge.Client.web.get_events({ + success: this.onPollSuccess, + scope: this + }); + }, + + start: function() { + this.poll = this.poll.bind(this); + this.running = setInterval(this.poll, 2000); + this.poll(); + }, + + stop: function() { + if (this.running) { + clearInterval(this.running); + } + }, + + onPollSuccess: function(events) { + if (!events) return; + Ext.each(events, function(event) { + var name = event[0], args = event[1]; + args.splice(0, 0, name); + this.fireEvent.apply(this, args); + }, this); + } }); Events.prototype.on = Events.prototype.addListener Events.prototype.fire = Events.prototype.fireEvent diff --git a/deluge/ui/web/js/Deluge.UI.js b/deluge/ui/web/js/Deluge.UI.js index 3c2d53460..6798f2ca2 100644 --- a/deluge/ui/web/js/Deluge.UI.js +++ b/deluge/ui/web/js/Deluge.UI.js @@ -82,6 +82,7 @@ Deluge.UI = { Deluge.Client.on('connected', function(e) { Deluge.Login.show(); + Deluge.Events.start(); }, this, {single: true}); this.update = this.update.bind(this); @@ -156,11 +157,11 @@ Deluge.UI = { * Stop the Deluge UI polling the server and clear the interface. */ stop: function() { - if (this.running) { - clearInterval(this.running); - this.running = false; - Deluge.Torrents.getStore().loadData([]); - } + if (this.running) { + clearInterval(this.running); + this.running = false; + Deluge.Torrents.getStore().loadData([]); + } } }