Skip to content

Commit

Permalink
Fix file watchers
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Nov 23, 2022
1 parent 558a6d5 commit 7d87e5c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 34 deletions.
2 changes: 0 additions & 2 deletions plugins/contents/fps_contents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
from .fileid import get_watch # noqa

__version__ = "0.0.44"
13 changes: 6 additions & 7 deletions plugins/contents/fps_contents/fileid.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
35 changes: 10 additions & 25 deletions plugins/yjs/fps_yjs/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 7d87e5c

Please sign in to comment.