Fix a bug in the memoization of resolve_room and use asyncronicity better in init

This commit is contained in:
histausse 2021-10-03 16:59:45 +02:00
parent e368d661ac
commit 0a96845fdf
Signed by: histausse
GPG key ID: 67486F107F62E9E9
2 changed files with 6 additions and 7 deletions

View file

@ -60,15 +60,13 @@ class Client(Aobject):
if isinstance(resp, nio.responses.LoginError): if isinstance(resp, nio.responses.LoginError):
raise RuntimeError(f"Fail to connect: {resp.message}") raise RuntimeError(f"Fail to connect: {resp.message}")
# TODO: Where is the async map when you need it?
self.allowed_rooms = None self.allowed_rooms = None
if allowed_rooms_names: if allowed_rooms_names:
self.allowed_rooms = {} self.allowed_rooms = {}
for room_name in allowed_rooms_names: rooms = await asyncio.gather(*(self.resolve_room(room_name) for room_name in allowed_rooms_names))
room = await self.resolve_room(room_name) for room in rooms:
self.allowed_rooms[room.id] = room # room uniqueness is handled by self.resolve_room self.allowed_rooms[room.id] = room # room uniqueness is handled by self.resolve_room
async def resolve_room( async def resolve_room(
self, self,
room_name: Union[RoomAlias, RoomId] room_name: Union[RoomAlias, RoomId]
@ -86,7 +84,9 @@ class Client(Aobject):
return self.__rooms_by_id[room_name] return self.__rooms_by_id[room_name]
# If it is a unknown room id: # If it is a unknown room id:
elif room_name[0] == '!': elif room_name[0] == '!':
return Room(id=room_name) room = Room(id=room_name)
self.__rooms_by_id[room_name] = room
return room
# If it is not a room id nor a room alias: # If it is not a room id nor a room alias:
elif room_name[0] != '#': elif room_name[0] != '#':
raise ValueError(f"Invalid room_name: {room_name}") raise ValueError(f"Invalid room_name: {room_name}")
@ -122,7 +122,6 @@ class Client(Aobject):
if isinstance(sync_resp, nio.responses.SyncError): if isinstance(sync_resp, nio.responses.SyncError):
print(f"Error while syncronizing: {sync_resp.message}") # TODO: use proper logging print(f"Error while syncronizing: {sync_resp.message}") # TODO: use proper logging
continue continue
print(sync_resp)
async def run( async def run(
self, self,

View file

@ -16,7 +16,7 @@ async def main():
os.environ["PASSWD"], os.environ["PASSWD"],
os.environ["ROOMS"].split(",") os.environ["ROOMS"].split(",")
) )
# print(client.allowed_rooms) print(client.allowed_rooms)
await client.run() await client.run()