@@ -77,7 +77,7 @@ async def initialize(self) -> None:
7777 this setter will subscribe for updates on the shared document.
7878 """
7979 async with self ._initialization_lock :
80- if self .ready : # type: ignore[has-type]
80+ if self .ready :
8181 return
8282
8383 self .log .info ("Initializing room %s" , self ._room_id )
@@ -88,7 +88,9 @@ async def initialize(self) -> None:
8888 if self .ystore is not None and await self .ystore .exists (self ._room_id ):
8989 # Load the content from the store
9090 doc = await self .ystore .get (self ._room_id )
91+ assert doc
9192 self ._session_id = doc ["session_id" ]
93+
9294 await self .ystore .apply_updates (self ._room_id , self .ydoc )
9395 self ._emit (
9496 LogLevel .INFO ,
@@ -207,18 +209,7 @@ async def _on_content_change(self, event: str, args: dict[str, Any]) -> None:
207209 if event == "metadata" and (
208210 self ._last_modified is None or self ._last_modified < args ["last_modified" ]
209211 ):
210- self .log .info ("Out-of-band changes. Overwriting the content in room %s" , self ._room_id )
211- self ._emit (LogLevel .INFO , "overwrite" , "Out-of-band changes. Overwriting the room." )
212-
213- msg_id = str (uuid .uuid4 ())
214- self ._messages [msg_id ] = asyncio .Lock ()
215- await self ._outofband_lock .acquire ()
216- data = msg_id .encode ()
217- self .broadcast_msg (
218- bytes ([MessageType .ROOM , RoomMessages .FILE_CHANGED ])
219- + write_var_uint (len (data ))
220- + data
221- )
212+ await self ._send_confict_msg ()
222213
223214 def _on_document_change (self , target : str , event : Any ) -> None :
224215 """
@@ -247,34 +238,35 @@ def _on_document_change(self, target: str, event: Any) -> None:
247238
248239 async def _load_document (self ) -> None :
249240 try :
250- model = await self ._file .load_content (self ._file_format , self ._file_type , True )
241+ async with self ._update_lock :
242+ model = await self ._file .load_content (self ._file_format , self ._file_type , True )
243+ self ._document .source = model ["content" ]
244+ self ._last_modified = model ["last_modified" ]
245+ self ._document .dirty = False
246+
251247 except Exception as e :
252248 msg = f"Error loading content from file: { self ._file .path } \n { e !r} "
253249 self .log .error (msg , exc_info = e )
254250 self ._emit (LogLevel .ERROR , None , msg )
255251 return None
256252
257- async with self ._update_lock :
258- self ._document .source = model ["content" ]
259- self ._last_modified = model ["last_modified" ]
260- self ._document .dirty = False
261-
262253 async def _save_document (self ) -> None :
263254 """
264255 Saves the content of the document to disk.
265256 """
266257 try :
267258 self .log .info ("Saving the content from room %s" , self ._room_id )
268- model = await self ._file .save_content (
269- {
270- "format" : self ._file_format ,
271- "type" : self ._file_type ,
272- "last_modified" : self ._last_modified ,
273- "content" : self ._document .source ,
274- }
275- )
276- self ._last_modified = model ["last_modified" ]
259+
277260 async with self ._update_lock :
261+ model = await self ._file .save_content (
262+ {
263+ "format" : self ._file_format ,
264+ "type" : self ._file_type ,
265+ "last_modified" : self ._last_modified ,
266+ "content" : self ._document .source ,
267+ }
268+ )
269+ self ._last_modified = model ["last_modified" ]
278270 self ._document .dirty = False
279271
280272 self ._emit (LogLevel .INFO , "save" , "Content saved." )
@@ -299,40 +291,41 @@ async def _maybe_save_document(self) -> None:
299291 # save after X seconds of inactivity
300292 await asyncio .sleep (self ._save_delay )
301293
294+ if self ._outofband_lock .locked ():
295+ return
296+
302297 try :
303298 self .log .info ("Saving the content from room %s" , self ._room_id )
304- model = await self ._file .maybe_save_content (
305- {
306- "format" : self ._file_format ,
307- "type" : self ._file_type ,
308- "last_modified" : self ._last_modified ,
309- "content" : self ._document .source ,
310- }
311- )
312- self ._last_modified = model ["last_modified" ]
313299 async with self ._update_lock :
300+ model = await self ._file .maybe_save_content (
301+ {
302+ "format" : self ._file_format ,
303+ "type" : self ._file_type ,
304+ "last_modified" : self ._last_modified ,
305+ "content" : self ._document .source ,
306+ }
307+ )
308+ self ._last_modified = model ["last_modified" ]
314309 self ._document .dirty = False
315310
316311 self ._emit (LogLevel .INFO , "save" , "Content saved." )
317312
318313 except OutOfBandChanges :
319- self .log .info ("Out-of-band changes. Overwriting the content in room %s" , self ._room_id )
320- try :
321- model = await self ._file .load_content (self ._file_format , self ._file_type , True )
322- except Exception as e :
323- msg = f"Error loading content from file: { self ._file .path } \n { e !r} "
324- self .log .error (msg , exc_info = e )
325- self ._emit (LogLevel .ERROR , None , msg )
326- return None
327-
328- async with self ._update_lock :
329- self ._document .source = model ["content" ]
330- self ._last_modified = model ["last_modified" ]
331- self ._document .dirty = False
332-
333- self ._emit (LogLevel .INFO , "overwrite" , "Out-of-band changes while saving." )
314+ await self ._send_confict_msg ()
334315
335316 except Exception as e :
336317 msg = f"Error saving file: { self ._file .path } \n { e !r} "
337318 self .log .error (msg , exc_info = e )
338319 self ._emit (LogLevel .ERROR , None , msg )
320+
321+ async def _send_confict_msg (self ) -> None :
322+ self .log .info ("Out-of-band changes in room %s" , self ._room_id )
323+ self ._emit (LogLevel .INFO , "overwrite" , f"Out-of-band changes in room { self ._room_id } " )
324+
325+ msg_id = str (uuid .uuid4 ())
326+ self ._messages [msg_id ] = asyncio .Lock ()
327+ await self ._outofband_lock .acquire ()
328+ data = msg_id .encode ()
329+ self .broadcast_msg (
330+ bytes ([MessageType .ROOM , RoomMessages .FILE_CHANGED ]) + write_var_uint (len (data )) + data
331+ )
0 commit comments