Skip to content

Commit

Permalink
Start ystore in a task (#303)
Browse files Browse the repository at this point in the history
* Start ystore in a task

* Use YStore's start_lock

* Bump pycrdt-websocket>=0.13.4
  • Loading branch information
davidbrochart authored May 7, 2024
1 parent 0cc728a commit 5b166c4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 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,10 @@ 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:
async with self.ystore.start_lock:
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 +184,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
2 changes: 1 addition & 1 deletion projects/jupyter-server-ydoc/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ authors = [
dependencies = [
"jupyter_server>=2.4.0,<3.0.0",
"jupyter_ydoc>=2.0.0,<3.0.0",
"pycrdt-websocket>=0.13.1,<0.14.0",
"pycrdt-websocket>=0.13.4,<0.14.0",
"jupyter_events>=0.10.0",
"jupyter_server_fileid>=0.7.0,<1",
"jsonschema>=4.18.0"
Expand Down

0 comments on commit 5b166c4

Please sign in to comment.