diff options
author | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-05-27 20:50:16 +0200 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-05-28 01:00:57 +0200 |
commit | 05235c8df61b0cd6bf588ee15a924576cb10aa0a (patch) | |
tree | 820706b25287b1c877d47fb3e7dad18731d988c9 /swiftstory/Player.py | |
parent | 40d54a78a6cfb510bfca30d9ab5cdb01f7956548 (diff) |
Don't use coroutines for functions sending notifications
Functions which generate notifications for clients were all making use
of await and async. This is not great because if we add a notification
somewhere, we need to change the function to a coroutine and update all
invocations (recursively changing all functions to coroutines).
Instead, we now add a task to the event loop whenever a notification
needs to be generated. This allows to drop the await and async
specifiers from mostly everywhere.
On the downside, it means that if we send a notification to n clients,
we have to register n tasks.
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'swiftstory/Player.py')
-rw-r--r-- | swiftstory/Player.py | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/swiftstory/Player.py b/swiftstory/Player.py index 786569c..7193b64 100644 --- a/swiftstory/Player.py +++ b/swiftstory/Player.py @@ -17,9 +17,9 @@ class Player: def pop_card(self, card_id): return self.cards.pop(card_id) - async def inc_score(self): + def inc_score(self): self.score += 1 - await self.send_notification({ + self.register_notification({ 'op': 'updated_score', 'content': self.score, }) @@ -29,10 +29,10 @@ class Player: self.next_idx += 1 return self.next_idx - 1 - async def send_notification(self, obj): + def register_notification(self, obj): if self.client is None: return message = json.dumps({'type': 'notification', 'content': obj}) - await self.client.send_notification(message) + self.client.register_notification(message) |