implement a better remote events system

This commit is contained in:
Damien Churchill 2010-02-17 01:50:05 +00:00
parent 7bd5ba3cdb
commit 1d2a6f7f0e
2 changed files with 38 additions and 16 deletions

View File

@ -60,9 +60,10 @@ Copyright:
Events.superclass.addListener.call(this, eventName, fn, scope, o); Events.superclass.addListener.call(this, eventName, fn, scope, o);
}, },
poll: function() { getEvents: function() {
Deluge.Client.web.get_events({ Deluge.Client.web.get_events({
success: this.onPollSuccess, success: this.onGetEventsSuccess,
failure: this.onGetEventsFailure,
scope: this scope: this
}); });
}, },
@ -74,18 +75,15 @@ Copyright:
Ext.each(this.toRegister, function(eventName) { Ext.each(this.toRegister, function(eventName) {
Deluge.Client.web.register_event_listener(eventName); Deluge.Client.web.register_event_listener(eventName);
}); });
this.poll = this.poll.createDelegate(this); this.running = true;
this.running = setInterval(this.poll, 2000); this.getEvents();
this.poll();
}, },
/** /**
* Stops the EventsManager checking for events. * Stops the EventsManager checking for events.
*/ */
stop: function() { stop: function() {
if (this.running) { this.running = false;
clearInterval(this.running);
}
}, },
// private // private
@ -95,14 +93,21 @@ Copyright:
this.on('PluginDisabledEvent', this.onPluginDisabled, this); this.on('PluginDisabledEvent', this.onPluginDisabled, this);
}, },
// private onGetEventsSuccess: function(events) {
onPollSuccess: function(events) {
if (!events) return; if (!events) return;
Ext.each(events, function(event) { Ext.each(events, function(event) {
var name = event[0], args = event[1]; var name = event[0], args = event[1];
args.splice(0, 0, name); args.splice(0, 0, name);
this.fireEvent.apply(this, args); this.fireEvent.apply(this, args);
}, this); }, this);
if (this.running) this.getEvents();
},
// private
onGetEventsFailure: function(events) {
// the request timed out so we just want to open up another
// one.
if (this.running) this.getEvents();
} }
}); });
@ -113,7 +118,8 @@ Copyright:
Events.prototype.on = Events.prototype.addListener Events.prototype.on = Events.prototype.addListener
/** /**
* Fires the specified event with the passed parameters (minus the event name). * Fires the specified event with the passed parameters (minus the
* event name).
* @method * @method
*/ */
Events.prototype.fire = Events.prototype.fireEvent Events.prototype.fire = Events.prototype.fireEvent

View File

@ -42,6 +42,7 @@ import hashlib
import tempfile import tempfile
from types import FunctionType from types import FunctionType
from twisted.internet import reactor
from twisted.internet.defer import Deferred, DeferredList from twisted.internet.defer import Deferred, DeferredList
from twisted.web import http, resource, server from twisted.web import http, resource, server
@ -328,6 +329,7 @@ class EventQueue(object):
self.__events = {} self.__events = {}
self.__handlers = {} self.__handlers = {}
self.__queue = {} self.__queue = {}
self.__loopers = {}
def add_listener(self, listener_id, event): def add_listener(self, listener_id, event):
""" """
@ -359,11 +361,25 @@ class EventQueue(object):
:param listener_id: A unique id for the listener :param listener_id: A unique id for the listener
:type listener_id: string :type listener_id: string
""" """
# Check to see if we have anything to return immediately
if listener_id in self.__queue: if listener_id in self.__queue:
queue = self.__queue[listener_id] queue = self.__queue[listener_id]
del self.__queue[listener_id] del self.__queue[listener_id]
return queue return queue
return None
# Create a deferred to and check again in 100ms
d = Deferred()
reactor.callLater(0.5, self._get_events, listener_id, d)
return d
def _get_events(self, listener_id, d):
if listener_id in self.__queue:
queue = self.__queue[listener_id]
del self.__queue[listener_id]
d.callback(queue)
else:
reactor.callLater(0.1, self._get_events, listener_id, d)
def remove_listener(self, listener_id, event): def remove_listener(self, listener_id, event):
""" """