mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-23 01:46:35 +03:00
Allow adding callback methods to UpdateState
This commit is contained in:
parent
d4f36162cd
commit
d237375208
|
@ -907,16 +907,13 @@ class TelegramClient(TelegramBareClient):
|
||||||
def add_update_handler(self, handler):
|
def add_update_handler(self, handler):
|
||||||
"""Adds an update handler (a function which takes a TLObject,
|
"""Adds an update handler (a function which takes a TLObject,
|
||||||
an update, as its parameter) and listens for updates"""
|
an update, as its parameter) and listens for updates"""
|
||||||
return # TODO Implement
|
self.updates.handlers.append(handler)
|
||||||
self._update_callbacks.append(handler)
|
|
||||||
|
|
||||||
def remove_update_handler(self, handler):
|
def remove_update_handler(self, handler):
|
||||||
return # TODO Implement
|
self.updates.handlers.remove(handler)
|
||||||
self._update_callbacks.remove(handler)
|
|
||||||
|
|
||||||
def list_update_handlers(self):
|
def list_update_handlers(self):
|
||||||
return # TODO Implement
|
return self.updates.handlers[:]
|
||||||
return self._update_callbacks[:]
|
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ class UpdateState:
|
||||||
"""
|
"""
|
||||||
def __init__(self, enabled):
|
def __init__(self, enabled):
|
||||||
self.enabled = enabled
|
self.enabled = enabled
|
||||||
|
self.handlers = []
|
||||||
self._updates_lock = Lock()
|
self._updates_lock = Lock()
|
||||||
self._updates_available = Event()
|
self._updates_available = Event()
|
||||||
self._updates = deque()
|
self._updates = deque()
|
||||||
|
@ -30,9 +31,11 @@ class UpdateState:
|
||||||
"""Processes an update object. This method is normally called by
|
"""Processes an update object. This method is normally called by
|
||||||
the library itself.
|
the library itself.
|
||||||
"""
|
"""
|
||||||
if not self.enabled:
|
for handler in self.handlers:
|
||||||
return
|
handler(update)
|
||||||
|
|
||||||
|
if self.enabled:
|
||||||
|
with self._updates_lock:
|
||||||
|
self._updates.append(update)
|
||||||
|
self._updates_available.set()
|
||||||
|
|
||||||
with self._updates_lock:
|
|
||||||
self._updates.append(update)
|
|
||||||
self._updates_available.set()
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user