Skip to content

Commit

Permalink
correct commit 'd1e28ef', set 'dirty' attribute while preventing save…
Browse files Browse the repository at this point in the history
… loop
  • Loading branch information
dlqqq committed Mar 20, 2024
1 parent 0a34513 commit 43df034
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions jupyter_collaboration/rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from jupyter_ydoc import ydocs as YDOCS
from pycrdt_websocket.websocket_server import YRoom
from pycrdt_websocket.ystore import BaseYStore, YDocNotFound
from pycrdt import MapEvent

from .loaders import FileLoader
from .utils import JUPYTER_COLLABORATION_EVENTS_URI, LogLevel, OutOfBandChanges
Expand Down Expand Up @@ -157,6 +158,7 @@ async def initialize(self) -> None:
if self.ystore:
await self.ystore.encode_state_as_update(self.ydoc)

self._document.dirty = False
self.ready = True
self._emit(LogLevel.INFO, "initialize", "Room initialized")

Expand Down Expand Up @@ -206,6 +208,7 @@ async def _on_outofband_change(self) -> None:
return

self._document.source = model["content"]
self._document.dirty = False

def _on_document_change(self, target: str, event: Any) -> None:
"""
Expand All @@ -222,6 +225,12 @@ def _on_document_change(self, target: str, event: Any) -> None:
document. This tasks are debounced (60 seconds by default) so we
need to cancel previous tasks before creating a new one.
"""
if target == "state" and isinstance(event, MapEvent) and list(event.keys.keys()) == ["dirty"]:
# do not write when we are just setting the `dirty` attribute to
# `False` for the JupyterLab UI. this prevents a save loop, as this
# is set when the Ydoc is saved.
return

if self._maybe_save_task and not self._maybe_save_task.done():
# only one `self._maybe_save_task` needs to be running.
# if this method is called after the save delay, then we need to set
Expand Down Expand Up @@ -266,6 +275,7 @@ async def _maybe_save_document(self) -> None:
}
))
await self._save_task
self._document.dirty = False
self._emit(LogLevel.INFO, "save", "Content saved.")

if self._should_resave:
Expand All @@ -286,6 +296,7 @@ async def _maybe_save_document(self) -> None:
return None

self._document.source = model["content"]
self._document.dirty = False
self._emit(LogLevel.INFO, "overwrite", "Out-of-band changes while saving.")

except Exception as e:
Expand Down

0 comments on commit 43df034

Please sign in to comment.