-
Notifications
You must be signed in to change notification settings - Fork 22
Add a way to listen on next synchronization #85
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||||
|
||||||||||||
|
@@ -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)) | ||||||||||||
|
||||||||||||
|
@@ -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.""" | ||||||||||||
if self._synced is None: | ||||||||||||
self._synced = Event() | ||||||||||||
return self._synced | ||||||||||||
|
||||||||||||
async def __aenter__(self) -> WebsocketProvider: | ||||||||||||
if self._task_group is not None: | ||||||||||||
|
@@ -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 | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Synchronization is done after a
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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: | ||||||||||||
|
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.