From 0b11f1082173ed6204d31f8efa16ad781cf22b30 Mon Sep 17 00:00:00 2001 From: Lars Benner Date: Sun, 5 May 2024 16:02:25 +0200 Subject: [PATCH 1/3] Bug fix: Connection has no attribute 'callbacks' but 'listeners' instead. --- realtime/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/realtime/connection.py b/realtime/connection.py index 5546aac..dc58a62 100644 --- a/realtime/connection.py +++ b/realtime/connection.py @@ -156,4 +156,4 @@ def summary(self) -> None: """ for topic, chans in self.channels.items(): for chan in chans: - print(f"Topic: {topic} | Events: {[e for e, _ in chan.callbacks]}]") + print(f"Topic: {topic} | Events: {[e for e, _ in chan.listeners]}]") From 482306cc4ef430dbcaaba39005fab03f27917ace Mon Sep 17 00:00:00 2001 From: Lars Benner Date: Sun, 5 May 2024 16:08:06 +0200 Subject: [PATCH 2/3] Implemented public async functions. --- realtime/channel.py | 7 +++++++ realtime/connection.py | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/realtime/channel.py b/realtime/channel.py index e4ac908..c45a0af 100644 --- a/realtime/channel.py +++ b/realtime/channel.py @@ -46,6 +46,13 @@ def join(self) -> Channel: loop.run_until_complete(self._join()) return self + async def join_async(self) -> None: + """ + Wrapper for async def _join(). + :return: None + """ + await self._join() + async def _join(self) -> None: """ Coroutine that attempts to join Phoenix Realtime server via a certain topic diff --git a/realtime/connection.py b/realtime/connection.py index dc58a62..1cfb450 100644 --- a/realtime/connection.py +++ b/realtime/connection.py @@ -65,6 +65,18 @@ def listen(self) -> None: loop = asyncio.get_event_loop() # TODO: replace with get_running_loop loop.run_until_complete(asyncio.gather(self._listen(), self._keep_alive())) + async def listen_async(self) -> None: + """ + Wrapper for async def _listen() and async def _keep_alive() to expose an async interface. + :return: None + """ + # @ensure_connection is definitely nicer, but I don't know if it is also + # working for asynchronous functions. + if not self.connected: + raise NotConnectedError(self.listen_async.__name__) + + await asyncio.gather(self._listen(), self._keep_alive()) + async def _listen(self) -> None: """ An infinite loop that keeps listening. @@ -103,6 +115,12 @@ def connect(self) -> None: loop.run_until_complete(self._connect()) self.connected = True + async def connect_async(self) -> None: + """ + Wrapper for async def _connect() to expose a async interface. + """ + await self._connect() + async def _connect(self) -> None: ws_connection = await websockets.connect(self.url) From c45ff4ebfc820ef2c35552e2b1152b6c0c5ca0c3 Mon Sep 17 00:00:00 2001 From: Lars Benner Date: Mon, 6 May 2024 10:07:52 +0200 Subject: [PATCH 3/3] Fixed flaw found by sourcery. --- realtime/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/realtime/connection.py b/realtime/connection.py index 1cfb450..01213a0 100644 --- a/realtime/connection.py +++ b/realtime/connection.py @@ -174,4 +174,4 @@ def summary(self) -> None: """ for topic, chans in self.channels.items(): for chan in chans: - print(f"Topic: {topic} | Events: {[e for e, _ in chan.listeners]}]") + print(f"Topic: {topic} | Events: {[e for e, _ in chan.listeners]}")