@@ -175,9 +175,8 @@ async def serve(self, websocket: YpyWebsocket, permissions) -> None:
175175 logger .info (f"Opening collaboration room: { websocket .path } ({ file_path } )" )
176176 document = YDOCS .get (file_type , YFILE )(room .ydoc )
177177 self .documents [websocket .path ] = document
178- is_notebook = file_type == "notebook"
179178 async with self .lock :
180- model = await self .contents .read_content (file_path , True , as_json = is_notebook )
179+ model = await self .contents .read_content (file_path , True , file_format )
181180 assert model .last_modified is not None
182181 self .last_modified [file_id ] = to_datetime (model .last_modified )
183182 if not room .ready :
@@ -201,11 +200,13 @@ async def serve(self, websocket: YpyWebsocket, permissions) -> None:
201200 document .dirty = False
202201 room .ready = True
203202 # save the document to file when changed
204- document .observe (partial (self .on_document_change , file_id , file_type , document ))
203+ document .observe (
204+ partial (self .on_document_change , file_id , file_type , file_format , document )
205+ )
205206 # update the document when file changes
206207 if file_id not in self .watchers :
207208 self .watchers [file_id ] = asyncio .create_task (
208- self .watch_file (file_type , file_id , document )
209+ self .watch_file (file_format , file_id , document )
209210 )
210211
211212 await self .websocket_server .serve (websocket )
@@ -245,7 +246,7 @@ async def get_file_path(self, file_id: str, document) -> str | None:
245246 document .path = file_path
246247 return file_path
247248
248- async def watch_file (self , file_type : str , file_id : str , document : YBaseDoc ) -> None :
249+ async def watch_file (self , file_format : str , file_id : str , document : YBaseDoc ) -> None :
249250 file_path = await self .get_file_path (file_id , document )
250251 assert file_path is not None
251252 logger .debug (f"Watching file: { file_path } " )
@@ -260,26 +261,25 @@ async def watch_file(self, file_type: str, file_id: str, document: YBaseDoc) ->
260261 self .contents .file_id_manager .unwatch (file_path , watcher )
261262 file_path = new_file_path
262263 # break
263- await self .maybe_load_file (file_type , file_path , file_id )
264+ await self .maybe_load_file (file_format , file_path , file_id )
264265
265- async def maybe_load_file (self , file_type : str , file_path : str , file_id : str ) -> None :
266+ async def maybe_load_file (self , file_format : str , file_path : str , file_id : str ) -> None :
266267 async with self .lock :
267268 model = await self .contents .read_content (file_path , False )
268269 # do nothing if the file was saved by us
269270 assert model .last_modified is not None
270271 if self .last_modified [file_id ] < to_datetime (model .last_modified ):
271272 # the file was not saved by us, update the shared document(s)
272- is_notebook = file_type == "notebook"
273273 async with self .lock :
274- model = await self .contents .read_content (file_path , True , as_json = is_notebook )
274+ model = await self .contents .read_content (file_path , True , file_format )
275275 assert model .last_modified is not None
276276 documents = [v for k , v in self .documents .items () if k .split (":" , 2 )[2 ] == file_id ]
277277 for document in documents :
278278 document .source = model .content
279279 self .last_modified [file_id ] = to_datetime (model .last_modified )
280280
281281 def on_document_change (
282- self , file_id : str , file_type : str , document : YBaseDoc , target , event
282+ self , file_id : str , file_type : str , file_format : str , document : YBaseDoc , target , event
283283 ) -> None :
284284 if target == "state" and "dirty" in event .keys :
285285 dirty = event .keys ["dirty" ]["newValue" ]
@@ -289,14 +289,18 @@ def on_document_change(
289289 # unobserve and observe again because the structure of the document may have changed
290290 # e.g. a new cell added to a notebook
291291 document .unobserve ()
292- document .observe (partial (self .on_document_change , file_id , file_type , document ))
292+ document .observe (
293+ partial (self .on_document_change , file_id , file_type , file_format , document )
294+ )
293295 if file_id in self .savers :
294296 self .savers [file_id ].cancel ()
295297 self .savers [file_id ] = asyncio .create_task (
296- self .maybe_save_document (file_id , file_type , document )
298+ self .maybe_save_document (file_id , file_type , file_format , document )
297299 )
298300
299- async def maybe_save_document (self , file_id : str , file_type : str , document : YBaseDoc ) -> None :
301+ async def maybe_save_document (
302+ self , file_id : str , file_type : str , file_format : str , document : YBaseDoc
303+ ) -> None :
300304 # save after 1 second of inactivity to prevent too frequent saving
301305 await asyncio .sleep (1 ) # FIXME: pass in config
302306 # if the room cannot be found, don't save
@@ -305,9 +309,8 @@ async def maybe_save_document(self, file_id: str, file_type: str, document: YBas
305309 except BaseException :
306310 return
307311 assert file_path is not None
308- is_notebook = file_type == "notebook"
309312 async with self .lock :
310- model = await self .contents .read_content (file_path , True , as_json = is_notebook )
313+ model = await self .contents .read_content (file_path , True , file_format )
311314 assert model .last_modified is not None
312315 if self .last_modified [file_id ] < to_datetime (model .last_modified ):
313316 # file changed on disk, let's revert
@@ -318,10 +321,9 @@ async def maybe_save_document(self, file_id: str, file_type: str, document: YBas
318321 # don't save if not needed
319322 # this also prevents the dirty flag from bouncing between windows of
320323 # the same document opened as different types (e.g. notebook/text editor)
321- format = "json" if file_type == "notebook" else "text"
322324 content = {
323325 "content" : document .source ,
324- "format" : format ,
326+ "format" : file_format ,
325327 "path" : file_path ,
326328 "type" : file_type ,
327329 }
0 commit comments