Skip to content

Commit

Permalink
fix: remove listen
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Feb 6, 2025
1 parent 8aa1ec2 commit d941250
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
12 changes: 10 additions & 2 deletions realtime/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def __init__(
self.max_retries = max_retries
self.initial_backoff = initial_backoff
self.timeout = DEFAULT_TIMEOUT
self._listen_task: Optional[asyncio.Task] = None
self._heartbeat_task: Optional[asyncio.Task] = None

async def _listen(self) -> None:
"""
Expand Down Expand Up @@ -154,11 +156,15 @@ async def connect(self) -> None:
f"Failed to establish WebSocket connection after {self.max_retries} attempts"
)

async def listen(self):
await asyncio.gather(self._listen(), self._heartbeat())
# async def listen(self):
# await asyncio.gather(self._listen(), self._heartbeat())

async def _on_connect(self):
self.is_connected = True

self._listen_task = asyncio.create_task(self._listen())
self._heartbeat_task = asyncio.create_task(self._heartbeat())

await self._flush_send_buffer()

async def _flush_send_buffer(self):
Expand All @@ -180,6 +186,8 @@ async def close(self) -> None:

await self.ws_connection.close()
self.is_connected = False
self._listen_task.cancel()
self._heartbeat_task.cancel()

async def _heartbeat(self) -> None:
while self.is_connected:
Expand Down
4 changes: 0 additions & 4 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ async def test_set_auth(socket: AsyncRealtimeClient):
@pytest.mark.asyncio
async def test_broadcast_events(socket: AsyncRealtimeClient):
await socket.connect()
listen_task = asyncio.create_task(socket.listen())

channel = socket.channel(
"test-broadcast", params={"config": {"broadcast": {"self": True}}}
Expand Down Expand Up @@ -94,15 +93,13 @@ def broadcast_callback(payload):
assert received_events[2]["payload"]["message"] == "Event 3"

await socket.close()
listen_task.cancel()


@pytest.mark.asyncio
async def test_postgrest_changes(socket: AsyncRealtimeClient):
token = await access_token()

await socket.connect()
listen_task = asyncio.create_task(socket.listen())

await socket.set_auth(token)

Expand Down Expand Up @@ -192,7 +189,6 @@ def delete_callback(payload):
assert received_events["delete"] == [delete]

await socket.close()
listen_task.cancel()


async def create_todo(access_token: str, todo: dict) -> str:
Expand Down
3 changes: 0 additions & 3 deletions tests/test_presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ def socket() -> AsyncRealtimeClient:
async def test_presence(socket: AsyncRealtimeClient):
await socket.connect()

listen_task = asyncio.create_task(socket.listen())

channel: AsyncRealtimeChannel = socket.channel("room")

join_events: List[Tuple[str, List[Dict], List[Dict]]] = []
Expand Down Expand Up @@ -106,7 +104,6 @@ def on_leave(key: str, current_presences: List[Dict], left_presences: List[Dict]
assert leave_events[0] != leave_events[1]

await socket.close()
listen_task.cancel()


def test_transform_state_raw_presence_state():
Expand Down

0 comments on commit d941250

Please sign in to comment.