Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Added timers cancellation (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
spietras authored May 20, 2022
1 parent d84f158 commit e81d29b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
13 changes: 7 additions & 6 deletions theatre/src/theatre/server/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,15 @@ def cleanup() -> None:
if len(self._users) == 0:
self.emit("empty")

async def timeout() -> None:
if user.id not in self._connected_users:
cleanup()

timer = Timer(60, timeout)

@connection.on("connected")
def on_connected() -> None:
timer.cancel()
self._connected_users.add(user.id)
self.handle_connect(user.id)

Expand All @@ -124,11 +131,5 @@ def on_disconnected() -> None:
def on_data(message: Union[bytes, str]) -> None:
self.handle_data(user.id, message)

async def timeout() -> None:
if user.id not in self._connected_users:
cleanup()

Timer(60, timeout)

self._users[user.id] = UserInfo(data=user, connection=connection)
return user
11 changes: 6 additions & 5 deletions theatre/src/theatre/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ async def close() -> None:
await room.close()
self.rooms.pop(code, None)

@room.on("empty")
async def on_empty() -> None:
await close()

async def timeout() -> None:
if len(room.users) == 0:
await close()

Timer(60, timeout)
timer = Timer(60, timeout)

@room.on("empty")
async def on_empty() -> None:
timer.cancel()
await close()

self.rooms[code] = room
return room
Expand Down

0 comments on commit e81d29b

Please sign in to comment.