diff --git a/plugins/contents/fps_contents/__init__.py b/plugins/contents/fps_contents/__init__.py index b01223b7..fa571767 100644 --- a/plugins/contents/fps_contents/__init__.py +++ b/plugins/contents/fps_contents/__init__.py @@ -1,3 +1 @@ -from .fileid import get_watch # noqa - __version__ = "0.0.44" diff --git a/plugins/contents/fps_contents/fileid.py b/plugins/contents/fps_contents/fileid.py index e58468cf..7f730752 100644 --- a/plugins/contents/fps_contents/fileid.py +++ b/plugins/contents/fps_contents/fileid.py @@ -1,6 +1,6 @@ import asyncio import logging -from typing import Callable, Dict, List, Optional +from typing import Dict, List, Optional from uuid import uuid4 import aiosqlite @@ -151,10 +151,13 @@ async def watch_files(self): def watch(self, path: str) -> Watcher: watcher = Watcher(path) if path not in self.watchers: - self.watchers[path] = watchers = [] # type: ignore - watchers.append(watcher) + self.watchers[path] = [] + self.watchers[path].append(watcher) return watcher + def unwatch(self, path: str, watcher: Watcher): + self.watchers[path].remove(watcher) + async def get_mtime(path, db) -> Optional[float]: if db: @@ -192,7 +195,3 @@ async def maybe_rename( other_paths.remove(other_path) return changed_paths.append(changed_path) - - -def get_watch() -> Callable[[str], Watcher]: - return FileIdManager().watch diff --git a/plugins/yjs/fps_yjs/routes.py b/plugins/yjs/fps_yjs/routes.py index cd59319f..30d45db5 100644 --- a/plugins/yjs/fps_yjs/routes.py +++ b/plugins/yjs/fps_yjs/routes.py @@ -18,13 +18,6 @@ from fps_contents.fileid import FileIdManager from fps_contents.routes import read_content, write_content # type: ignore -try: - from fps_contents import get_watch - - has_watch = True -except ImportError: - has_watch = False - from fps_auth_base import websocket_auth # type: ignore from jupyter_ydoc import ydocs as YDOCS # type: ignore from ypy_websocket.websocket_server import WebsocketServer, YRoom # type: ignore @@ -237,24 +230,16 @@ async def on_message(self, message: bytes) -> bool: return skip async def watch_file(self): - if has_watch: - file_format, file_type, file_path = await self.get_file_info() - while True: - async for changes in get_watch()(file_path): - file_format, file_type, new_file_path = await self.get_file_info() - if new_file_path != file_path: - # file was renamed - file_path = new_file_path - break - await self.maybe_load_document() - else: - # contents plugin doesn't provide watcher, fall back to polling - poll_interval = 1 # FIXME: pass in config - if not poll_interval: - self.room.watcher = None - return - while True: - await asyncio.sleep(poll_interval) + file_format, file_type, file_path = await self.get_file_info() + while True: + watcher = FileIdManager().watch(file_path) + async for changes in watcher: + file_format, file_type, new_file_path = await self.get_file_info() + if new_file_path != file_path: + # file was renamed + FileIdManager().unwatch(file_path, watcher) + file_path = new_file_path + break await self.maybe_load_document() async def maybe_load_document(self):