diff --git a/jupyter_collaboration/app.py b/jupyter_collaboration/app.py index 2cab64a8..62561201 100644 --- a/jupyter_collaboration/app.py +++ b/jupyter_collaboration/app.py @@ -3,8 +3,6 @@ from __future__ import annotations import asyncio -from anyio import create_task_group -from anyio.abc import TaskGroup from jupyter_server.extension.application import ExtensionApp from traitlets import Bool, Float, Type @@ -83,7 +81,7 @@ def initialize_handlers(self): # Set configurable parameters to YStore class for k, v in self.config.get(self.ystore_class.__name__, {}).items(): setattr(self.ystore_class, k, v) - + # Instantiate the store self._store = self.ystore_class(log=self.log) diff --git a/jupyter_collaboration/handlers.py b/jupyter_collaboration/handlers.py index fe45c24c..afd5466c 100644 --- a/jupyter_collaboration/handlers.py +++ b/jupyter_collaboration/handlers.py @@ -14,8 +14,8 @@ from jupyter_ydoc import ydocs as YDOCS from tornado import web from tornado.websocket import WebSocketHandler -from ypy_websocket.websocket_server import YRoom from ypy_websocket.stores import BaseYStore +from ypy_websocket.websocket_server import YRoom from ypy_websocket.yutils import YMessageType, write_var_uint from .loaders import FileLoaderMapping @@ -62,7 +62,7 @@ def create_task(self, aw): task.add_done_callback(self._background_tasks.discard) async def prepare(self): - # NOTE: Initialize in the ExtensionApp.start_extension once + # NOTE: Initialize in the ExtensionApp.start_extension once # https://github.com/jupyter-server/jupyter_server/issues/1329 # is done. # We are temporarily initializing the store here because `start`` diff --git a/jupyter_collaboration/rooms.py b/jupyter_collaboration/rooms.py index bf6662a3..2e755f9c 100644 --- a/jupyter_collaboration/rooms.py +++ b/jupyter_collaboration/rooms.py @@ -10,8 +10,8 @@ from jupyter_events import EventLogger from jupyter_ydoc import ydocs as YDOCS +from ypy_websocket.stores import BaseYStore from ypy_websocket.websocket_server import YRoom -from ypy_websocket.stores import BaseYStore, YDocNotFound from ypy_websocket.yutils import write_var_uint from .loaders import FileLoader @@ -111,14 +111,30 @@ async def initialize(self) -> None: if self.ystore is not None and await self.ystore.exists(self._room_id): # Load the content from the store await self.ystore.apply_updates(self._room_id, self.ydoc) - self._emit(LogLevel.INFO, "load", "Content loaded from the store {}".format(self.ystore.__class__.__qualname__)) - self.log.info("Content in room %s loaded from the ystore %s", self._room_id, self.ystore.__class__.__name__,) + self._emit( + LogLevel.INFO, + "load", + "Content loaded from the store {}".format( + self.ystore.__class__.__qualname__ + ), + ) + self.log.info( + "Content in room %s loaded from the ystore %s", + self._room_id, + self.ystore.__class__.__name__, + ) # if YStore updates and source file are out-of-sync, resync updates with source if self._document.source != model["content"]: - self._emit(LogLevel.INFO, "initialize", "The file is out-of-sync with the ystore.") - self.log.info("Content in file %s is out-of-sync with the ystore %s", self._file.path, self.ystore.__class__.__name__,) - + self._emit( + LogLevel.INFO, "initialize", "The file is out-of-sync with the ystore." + ) + self.log.info( + "Content in file %s is out-of-sync with the ystore %s", + self._file.path, + self.ystore.__class__.__name__, + ) + doc = await self.ystore.get(self._room_id) await self.ystore.remove(self._room_id) version = 0 @@ -130,14 +146,15 @@ async def initialize(self) -> None: else: self._emit(LogLevel.INFO, "load", "Content loaded from disk.") - self.log.info("Content in room %s loaded from file %s", self._room_id, self._file.path) + self.log.info( + "Content in room %s loaded from file %s", self._room_id, self._file.path + ) self._document.source = model["content"] if self.ystore is not None: await self.ystore.create(self._room_id, 0) await self.ystore.encode_state_as_update(self._room_id, self.ydoc) - self._last_modified = model["last_modified"] self._document.dirty = False self.ready = True diff --git a/jupyter_collaboration/stores.py b/jupyter_collaboration/stores.py index 4b8db21f..aa2de45f 100644 --- a/jupyter_collaboration/stores.py +++ b/jupyter_collaboration/stores.py @@ -4,11 +4,11 @@ from __future__ import annotations from logging import Logger + from traitlets import Int, Unicode from traitlets.config import LoggingConfigurable - -from ypy_websocket.stores import SQLiteYStore as _SQLiteYStore from ypy_websocket.stores import FileYStore +from ypy_websocket.stores import SQLiteYStore as _SQLiteYStore class TempFileYStore(FileYStore): diff --git a/jupyter_collaboration/websocketserver.py b/jupyter_collaboration/websocketserver.py index 51baa814..ec989db0 100644 --- a/jupyter_collaboration/websocketserver.py +++ b/jupyter_collaboration/websocketserver.py @@ -9,7 +9,6 @@ from tornado.websocket import WebSocketHandler from ypy_websocket.websocket_server import WebsocketServer, YRoom -from ypy_websocket.stores import BaseYStore class RoomNotFound(LookupError): diff --git a/tests/conftest.py b/tests/conftest.py index 234dbb3c..dd323d42 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -170,8 +170,10 @@ def rtc_create_SQLite_store(jp_serverapp): setattr(SQLiteYStore, k, v) async def _inner(type: str, path: str, content: str) -> DocumentRoom: - db = SQLiteYStore(path=f"{type}:{path}") + room_id = f"{type}:{path}" + db = SQLiteYStore() await db.start() + await db.initialize() if type == "notebook": doc = YNotebook() @@ -179,7 +181,9 @@ async def _inner(type: str, path: str, content: str) -> DocumentRoom: doc = YUnicode() doc.source = content - await db.encode_state_as_update(doc.ydoc) + + await db.create(room_id, 0) + await db.encode_state_as_update(room_id, doc.ydoc) return db