Browse Source

make callback dict in EventEmitter private

pull/18/merge
Rossen Georgiev 9 years ago
parent
commit
4e886fa0d8
  1. 39
      steam/util/events.py

39
steam/util/events.py

@ -25,20 +25,21 @@ class EventEmitter(object):
gevent.idle() gevent.idle()
if hasattr(self, '_event_callbacks'): if hasattr(self, '_EventEmitter__callbacks'):
for callback in list(self._event_callbacks[event]): if event in self.__callbacks:
if isinstance(callback, AsyncResult): for callback in list(self.__callbacks[event]):
self.remove_listener(event, callback) if isinstance(callback, AsyncResult):
self.remove_listener(event, callback)
result = args result = args
if len(args) == 1: if len(args) == 1:
result = args[0] result = args[0]
callback.set(result) callback.set(result)
else: else:
gevent.spawn(callback, *args) gevent.spawn(callback, *args)
gevent.idle() gevent.idle()
# every event is also emitted as None # every event is also emitted as None
if event is not None: if event is not None:
@ -53,10 +54,14 @@ class EventEmitter(object):
:type callback: function, method or :py:class:`gevent.event.AsyncResult` :type callback: function, method or :py:class:`gevent.event.AsyncResult`
""" """
if not hasattr(self, '_event_callbacks'): if not hasattr(self, '_EventEmitter__callbacks'):
return return
self._event_callbacks[event].pop(callback, None) if event in self.__callbacks:
del self.__callbacks[event][callback]
if not self.__callbacks[event]:
del self.__callbacks[event]
def wait_event(self, event, timeout=None): def wait_event(self, event, timeout=None):
""" """
@ -93,16 +98,16 @@ class EventEmitter(object):
To listen for any event, use :py:class:`None` as event identifier. To listen for any event, use :py:class:`None` as event identifier.
""" """
if not hasattr(self, '_event_callbacks'): if not hasattr(self, '_EventEmitter__callbacks'):
self._event_callbacks = defaultdict(dict) self.__callbacks = defaultdict(dict)
# when used function # when used function
if callback: if callback:
self._event_callbacks[event][callback] = None self.__callbacks[event][callback] = None
return return
# as decorator # as decorator
def wrapper(callback): def wrapper(callback):
self._event_callbacks[event][callback] = None self.__callbacks[event][callback] = None
return callback return callback
return wrapper return wrapper

Loading…
Cancel
Save