@@ -77,7 +77,7 @@ async def initialize(self) -> None:
77
77
this setter will subscribe for updates on the shared document.
78
78
"""
79
79
async with self ._initialization_lock :
80
- if self .ready : # type: ignore[has-type]
80
+ if self .ready :
81
81
return
82
82
83
83
self .log .info ("Initializing room %s" , self ._room_id )
@@ -88,7 +88,9 @@ async def initialize(self) -> None:
88
88
if self .ystore is not None and await self .ystore .exists (self ._room_id ):
89
89
# Load the content from the store
90
90
doc = await self .ystore .get (self ._room_id )
91
+ assert doc
91
92
self ._session_id = doc ["session_id" ]
93
+
92
94
await self .ystore .apply_updates (self ._room_id , self .ydoc )
93
95
self ._emit (
94
96
LogLevel .INFO ,
@@ -207,18 +209,7 @@ async def _on_content_change(self, event: str, args: dict[str, Any]) -> None:
207
209
if event == "metadata" and (
208
210
self ._last_modified is None or self ._last_modified < args ["last_modified" ]
209
211
):
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 ()
222
213
223
214
def _on_document_change (self , target : str , event : Any ) -> None :
224
215
"""
@@ -247,34 +238,35 @@ def _on_document_change(self, target: str, event: Any) -> None:
247
238
248
239
async def _load_document (self ) -> None :
249
240
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
+
251
247
except Exception as e :
252
248
msg = f"Error loading content from file: { self ._file .path } \n { e !r} "
253
249
self .log .error (msg , exc_info = e )
254
250
self ._emit (LogLevel .ERROR , None , msg )
255
251
return None
256
252
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
-
262
253
async def _save_document (self ) -> None :
263
254
"""
264
255
Saves the content of the document to disk.
265
256
"""
266
257
try :
267
258
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
+
277
260
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" ]
278
270
self ._document .dirty = False
279
271
280
272
self ._emit (LogLevel .INFO , "save" , "Content saved." )
@@ -299,40 +291,41 @@ async def _maybe_save_document(self) -> None:
299
291
# save after X seconds of inactivity
300
292
await asyncio .sleep (self ._save_delay )
301
293
294
+ if self ._outofband_lock .locked ():
295
+ return
296
+
302
297
try :
303
298
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" ]
313
299
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" ]
314
309
self ._document .dirty = False
315
310
316
311
self ._emit (LogLevel .INFO , "save" , "Content saved." )
317
312
318
313
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 ()
334
315
335
316
except Exception as e :
336
317
msg = f"Error saving file: { self ._file .path } \n { e !r} "
337
318
self .log .error (msg , exc_info = e )
338
319
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