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

Add a way to listen on next synchronization #85

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
9 changes: 7 additions & 2 deletions docs/usage/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ async def client():
ydoc = Y.YDoc()
async with (
connect("ws://localhost:1234/my-roomname") as websocket,
WebsocketProvider(ydoc, websocket),
WebsocketProvider(ydoc, websocket) as provider,
):
ymap = ydoc.get_map("map")

# Wait until we've received the initial state from the server.
await provider.synced.wait()
print(ymap.to_json())

# Changes to remote ydoc are applied to local ydoc.
# Changes to local ydoc are sent over the WebSocket and
# broadcast to all clients.
ymap = ydoc.get_map("map")
with ydoc.begin_transaction() as t:
ymap.set(t, "key", "value")

Expand Down
12 changes: 12 additions & 0 deletions ypy_websocket/websocket_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class WebsocketProvider:
_update_send_stream: MemoryObjectSendStream
_update_receive_stream: MemoryObjectReceiveStream
_started: Event | None
_synced: Event | None
_starting: bool
_task_group: TaskGroup | None

Expand Down Expand Up @@ -63,6 +64,7 @@ def __init__(self, ydoc: Y.YDoc, websocket: Websocket, log: Logger | None = None
)
self._started = None
self._starting = False
self._synced = None
self._task_group = None
ydoc.observe_after_transaction(partial(put_updates, self._update_send_stream))

Expand All @@ -72,6 +74,13 @@ def started(self) -> Event:
if self._started is None:
self._started = Event()
return self._started

@property
def synced(self) -> Event:
"""An async event that is set when the WebSocket provider has initially synced with the server."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""An async event that is set when the WebSocket provider has initially synced with the server."""
"""An async event that is set when the WebSocket provider has synchronized with the server."""

if self._synced is None:
self._synced = Event()
return self._synced

async def __aenter__(self) -> WebsocketProvider:
if self._task_group is not None:
Expand Down Expand Up @@ -100,6 +109,9 @@ async def _run(self):
async for message in self._websocket:
if message[0] == YMessageType.SYNC:
await process_sync_message(message[1:], self._ydoc, self._websocket, self.log)
if self._synced is not None:
self._synced.set()
self._synced = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Synchronization is done after a SYNC_STEP2 message is processed.
Also, I don't understand why you set _synced back to None. Shouldn't it be just self.synced.set()? Something like:

Suggested change
if self._synced is not None:
self._synced.set()
self._synced = None
if message[1] == YSyncMessageType.SYNC_STEP2:
self.synced.set()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my intended use case, either will work, but I figured that to mirror the flexibility of the JS (which IIUC can be triggered on every sync), the semantics should be that the event fire on the next synchronization even if the initial sync has already happened. This way if you wanted to listen for each synchronization in a loop, you could, but it's still a useful interface if you only care about the first sync event.

I'm not committed to this, I would also be fine with the event firing only after the initial synchronization.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would there be multiple synchronizations?


async def _send(self):
async with self._update_receive_stream:
Expand Down
Loading