Add basic update handler

This commit is contained in:
Dan 2017-12-08 23:40:29 +01:00
parent 315563ede0
commit fe9b968ad8
2 changed files with 15 additions and 3 deletions

View File

@ -81,6 +81,12 @@ class Client:
self.config = None
self.session = None
self.update_handler = None
# TODO: Better update handler
def set_update_handler(self, callback):
self.update_handler = callback
def send(self, data: Object):
return self.session.send(data)
@ -249,6 +255,7 @@ class Client:
self.load_session(self.session_name)
self.session = Session(self.dc_id, self.test_mode, self.auth_key, self.config.api_id)
self.session.update_handler = self.update_handler
terms = self.session.start()

View File

@ -96,6 +96,8 @@ class Session:
self.is_connected = Event()
self.update_handler = None
self.total_connections = 0
self.total_messages = 0
self.total_bytes = 0
@ -246,19 +248,22 @@ class Session:
self.pending_acks.add(i.body.answer_msg_id)
continue
if isinstance(i.body, types.NewSessionCreated):
continue
msg_id = None
if isinstance(i.body, (types.BadMsgNotification, types.BadServerSalt)):
msg_id = i.body.bad_msg_id
elif isinstance(i.body, types.RpcResult):
msg_id = i.body.req_msg_id
elif isinstance(i.body, types.Pong):
msg_id = i.body.msg_id
elif isinstance(i.body, core.FutureSalts):
msg_id = i.body.req_msg_id
else:
if self.update_handler:
self.update_handler(i.body)
if msg_id in self.results:
self.results[msg_id].value = getattr(i.body, "result", i.body)