Skip to content

Commit

Permalink
Start ystore in a task
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed May 6, 2024
1 parent 7100c29 commit e9c572d
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions projects/jupyter-server-ydoc/jupyter_server_ydoc/rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
class DocumentRoom(YRoom):
"""A Y room for a possibly stored document (e.g. a notebook)."""

_background_tasks: set[asyncio.Task]

def __init__(
self,
room_id: str,
Expand Down Expand Up @@ -48,6 +50,7 @@ def __init__(
self._cleaner: asyncio.Task | None = None
self._saving_document: asyncio.Task | None = None
self._messages: dict[str, asyncio.Lock] = {}
self._background_tasks = set()

# Listen for document changes
self._document.observe(self._on_document_change)
Expand Down Expand Up @@ -100,6 +103,9 @@ async def initialize(self) -> None:
# try to apply Y updates from the YStore for this document
read_from_source = True
if self.ystore is not None:
if not self.ystore.started.is_set():
self.create_task(self.ystore.start())
await self.ystore.started.wait()
try:
await self.ystore.apply_updates(self.ydoc)
self._emit(
Expand Down Expand Up @@ -177,6 +183,11 @@ async def stop(self) -> None:
self._document.unobserve()
self._file.unobserve(self.room_id)

def create_task(self, aw):
task = asyncio.create_task(aw)
self._background_tasks.add(task)
task.add_done_callback(self._background_tasks.discard)

async def _broadcast_updates(self):
# FIXME should be upstreamed
try:
Expand Down

0 comments on commit e9c572d

Please sign in to comment.