Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for system event #264

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,11 @@ jobs:
with:
version: latest

- name: Setup Local Supabase
run: |
supabase start --workdir tests
supabase db reset --workdir tests
supabase status --workdir tests -o env > tests/.env \
--override-name auth.anon_key=SUPABASE_ANON_KEY \
--override-name api.url=SUPABASE_URL

- name: Wait for Supabase to be ready
run: |
echo "Waiting for 5 seconds to ensure Supabase is fully initialized..."
sleep 5

- name: Run Tests
run: make run_tests || make run_tests
run: make run_tests

- name: Upload coverage to Coveralls
if: ${{ matrix.python-version }} == "3.12"
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,12 @@ tests_pre_commit:

run_tests: tests

tests_only:
setup_test_infra:
supabase start --workdir tests
supabase db reset --workdir tests
supabase status --workdir tests -o env > tests/.env \
--override-name auth.anon_key=SUPABASE_ANON_KEY \
--override-name api.url=SUPABASE_URL

tests_only: setup_test_infra
poetry run pytest --cov=./ --cov-report=xml --cov-report=html -vv
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "2.1.0" # {x-release-please-version}
description = ""
authors = [
"Joel Lee <joel@joellee.org>",
"Andrew Smith <a.smith@silentworks.co.uk>"
"Andrew Smith <a.smith@silentworks.co.uk>",
]
license = "MIT"
readme = "README.md"
Expand Down Expand Up @@ -33,3 +33,7 @@ pytest-cov = "^5.0.0"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.pytest.ini_options]
asyncio_mode = "strict"
asyncio_default_fixture_loop_scope = "function"
11 changes: 11 additions & 0 deletions realtime/_async/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,17 @@ def on_postgres_changes(
callback=lambda payload, _: callback(payload),
)

def on_system(
self, callback: Callable[[Dict[str, Any], None]]
) -> AsyncRealtimeChannel:
"""
Set up a listener for system events.

:param callback: The callback function to execute when a system event is received.
:return: The Channel instance for method chaining.
"""
return self._on("system", callback=lambda payload, _: callback(payload))

# Presence methods
async def track(self, user_status: Dict[str, Any]) -> None:
"""
Expand Down
1 change: 0 additions & 1 deletion realtime/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ async def _flush_send_buffer(self):
await callback()
self.send_buffer = []

@ensure_connection
async def close(self) -> None:
"""
Close the WebSocket connection.
Expand Down
5 changes: 5 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,16 @@ def delete_callback(payload):
delete_event.set()

subscribed_event = asyncio.Event()
system_event = asyncio.Event()

await channel.on_postgres_changes(
"*", all_changes_callback, table="todos"
).on_postgres_changes("INSERT", insert_callback, table="todos").on_postgres_changes(
"UPDATE", update_callback, table="todos"
).on_postgres_changes(
"DELETE", delete_callback, table="todos"
).on_system(
lambda _: system_event.set()
).subscribe(
lambda state, error: (
subscribed_event.set()
Expand All @@ -150,6 +153,8 @@ def delete_callback(payload):
)
)

await asyncio.wait_for(system_event.wait(), 10)

# Wait for the channel to be subscribed
await asyncio.wait_for(subscribed_event.wait(), 10)

Expand Down
Loading