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

Conversation

paulgb
Copy link

@paulgb paulgb commented Sep 7, 2023

I wanted a clean way to wait for the initial data to be synchronized down to the client, similar to y-websocket's .on('sync') (docs), except that to be consistent with started(self), it uses an anyio.Event instead of a callback function.

Calling await provider.synced.wait() will resolve when the next synchronization message from the server is received and processed.

I'm not deep into async Python stuff -- I think this is a reasonable way to do this, but if there's another approach you'd prefer I'd be glad to update the PR.

Comment on lines 112 to 114
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?


@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."""

Copy link
Collaborator

@davidbrochart davidbrochart left a comment

Choose a reason for hiding this comment

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

Thanks @paulgb, I think this can be useful.
I left some comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants