-
Notifications
You must be signed in to change notification settings - Fork 22
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
base: main
Are you sure you want to change the base?
Conversation
ypy_websocket/websocket_provider.py
Outdated
if self._synced is not None: | ||
self._synced.set() | ||
self._synced = None |
There was a problem hiding this comment.
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:
if self._synced is not None: | |
self._synced.set() | |
self._synced = None | |
if message[1] == YSyncMessageType.SYNC_STEP2: | |
self.synced.set() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
ypy_websocket/websocket_provider.py
Outdated
|
||
@property | ||
def synced(self) -> Event: | ||
"""An async event that is set when the WebSocket provider has initially synced with the server.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""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.""" |
There was a problem hiding this 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.
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 withstarted(self)
, it uses ananyio.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.