Skip to content

Commit

Permalink
Update to pycrdt 0.10 and observe the awareness changes to broadcast …
Browse files Browse the repository at this point in the history
…local changes
  • Loading branch information
brichet committed Oct 9, 2024
1 parent 52c80ed commit 1df8f38
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
19 changes: 15 additions & 4 deletions pycrdt_websocket/yroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import partial
from inspect import isawaitable
from logging import Logger, getLogger
from typing import Awaitable, Callable
from typing import Any, Awaitable, Callable

from anyio import (
TASK_STATUS_IGNORED,
Expand All @@ -24,6 +24,7 @@
create_sync_message,
create_update_message,
handle_sync_message,
create_awareness_message,
)

from .websocket import Websocket
Expand Down Expand Up @@ -81,7 +82,8 @@ def __init__(
self.ready = ready
self.ystore = ystore
self.log = log or getLogger(__name__)
self.awareness = Awareness(self.ydoc, self.local_update_awareness)
self.awareness = Awareness(self.ydoc)
self.awareness.observe(self.local_update_awareness)
self.clients = set()
self._on_message = None
self.exception_handler = exception_handler
Expand Down Expand Up @@ -305,12 +307,21 @@ async def serve(self, websocket: Websocket):
except Exception as exception:
self._handle_exception(exception)

def local_update_awareness(self, state: bytes) -> None:
def local_update_awareness(self, type: str, changes: dict[str, Any]) -> None:
"""
Callback to broadcast the server awareness to clients.
"""
if not changes[1] == "local":
return

if self._task_group is not None:
self._task_group.start_soon(self._local_update_awareness, state)
updated_clients = [
*changes[0].get("added", []),
*changes[0].get("filtered_updated", [])
]
state = self.awareness.encode_awareness_update(updated_clients)
message = create_awareness_message(state)
self._task_group.start_soon(self._local_update_awareness, message)
else:
self.log.error("Cannot broadcast server awareness: YRoom not started")

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ classifiers = [
dependencies = [
"anyio >=3.6.2,<5",
"sqlite-anyio >=0.2.3,<0.3.0",
"pycrdt >=0.9.16,<0.10.0",
"pycrdt >=0.10.0,<0.11.0",
]

[project.optional-dependencies]
Expand Down

0 comments on commit 1df8f38

Please sign in to comment.