mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-08 21:10:29 +00:00
Add basic updates processing to ignore updates with lower .pts
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
from threading import Lock, Event
|
||||
from collections import deque
|
||||
from datetime import datetime
|
||||
from threading import RLock, Event
|
||||
|
||||
from .tl import types as tl
|
||||
|
||||
|
||||
class UpdateState:
|
||||
@@ -9,10 +12,13 @@ class UpdateState:
|
||||
def __init__(self, enabled):
|
||||
self.enabled = enabled
|
||||
self.handlers = []
|
||||
self._updates_lock = Lock()
|
||||
self._updates_lock = RLock()
|
||||
self._updates_available = Event()
|
||||
self._updates = deque()
|
||||
|
||||
# https://core.telegram.org/api/updates
|
||||
self._state = tl.updates.State(0, 0, datetime.now(), 0, 0)
|
||||
|
||||
def has_any(self):
|
||||
"""Returns True if a call to .pop_update() won't lock"""
|
||||
return self._updates_available.is_set()
|
||||
@@ -31,11 +37,16 @@ class UpdateState:
|
||||
"""Processes an update object. This method is normally called by
|
||||
the library itself.
|
||||
"""
|
||||
for handler in self.handlers:
|
||||
handler(update)
|
||||
if not self.enabled:
|
||||
return
|
||||
|
||||
with self._updates_lock:
|
||||
if isinstance(update, tl.updates.State):
|
||||
self._state = update
|
||||
elif not hasattr(update, 'pts') or update.pts > self._state.pts:
|
||||
self._state.pts = getattr(update, 'pts', self._state.pts)
|
||||
for handler in self.handlers:
|
||||
handler(update)
|
||||
|
||||
if self.enabled:
|
||||
with self._updates_lock:
|
||||
self._updates.append(update)
|
||||
self._updates_available.set()
|
||||
|
||||
|
Reference in New Issue
Block a user