@@ -175,9 +175,8 @@ async def serve(self, websocket: YpyWebsocket, permissions) -> None:
175
175
logger .info (f"Opening collaboration room: { websocket .path } ({ file_path } )" )
176
176
document = YDOCS .get (file_type , YFILE )(room .ydoc )
177
177
self .documents [websocket .path ] = document
178
- is_notebook = file_type == "notebook"
179
178
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 )
181
180
assert model .last_modified is not None
182
181
self .last_modified [file_id ] = to_datetime (model .last_modified )
183
182
if not room .ready :
@@ -201,11 +200,13 @@ async def serve(self, websocket: YpyWebsocket, permissions) -> None:
201
200
document .dirty = False
202
201
room .ready = True
203
202
# 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
+ )
205
206
# update the document when file changes
206
207
if file_id not in self .watchers :
207
208
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 )
209
210
)
210
211
211
212
await self .websocket_server .serve (websocket )
@@ -245,7 +246,7 @@ async def get_file_path(self, file_id: str, document) -> str | None:
245
246
document .path = file_path
246
247
return file_path
247
248
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 :
249
250
file_path = await self .get_file_path (file_id , document )
250
251
assert file_path is not None
251
252
logger .debug (f"Watching file: { file_path } " )
@@ -260,26 +261,25 @@ async def watch_file(self, file_type: str, file_id: str, document: YBaseDoc) ->
260
261
self .contents .file_id_manager .unwatch (file_path , watcher )
261
262
file_path = new_file_path
262
263
# 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 )
264
265
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 :
266
267
async with self .lock :
267
268
model = await self .contents .read_content (file_path , False )
268
269
# do nothing if the file was saved by us
269
270
assert model .last_modified is not None
270
271
if self .last_modified [file_id ] < to_datetime (model .last_modified ):
271
272
# the file was not saved by us, update the shared document(s)
272
- is_notebook = file_type == "notebook"
273
273
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 )
275
275
assert model .last_modified is not None
276
276
documents = [v for k , v in self .documents .items () if k .split (":" , 2 )[2 ] == file_id ]
277
277
for document in documents :
278
278
document .source = model .content
279
279
self .last_modified [file_id ] = to_datetime (model .last_modified )
280
280
281
281
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
283
283
) -> None :
284
284
if target == "state" and "dirty" in event .keys :
285
285
dirty = event .keys ["dirty" ]["newValue" ]
@@ -289,14 +289,18 @@ def on_document_change(
289
289
# unobserve and observe again because the structure of the document may have changed
290
290
# e.g. a new cell added to a notebook
291
291
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
+ )
293
295
if file_id in self .savers :
294
296
self .savers [file_id ].cancel ()
295
297
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 )
297
299
)
298
300
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 :
300
304
# save after 1 second of inactivity to prevent too frequent saving
301
305
await asyncio .sleep (1 ) # FIXME: pass in config
302
306
# 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
305
309
except BaseException :
306
310
return
307
311
assert file_path is not None
308
- is_notebook = file_type == "notebook"
309
312
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 )
311
314
assert model .last_modified is not None
312
315
if self .last_modified [file_id ] < to_datetime (model .last_modified ):
313
316
# 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
318
321
# don't save if not needed
319
322
# this also prevents the dirty flag from bouncing between windows of
320
323
# the same document opened as different types (e.g. notebook/text editor)
321
- format = "json" if file_type == "notebook" else "text"
322
324
content = {
323
325
"content" : document .source ,
324
- "format" : format ,
326
+ "format" : file_format ,
325
327
"path" : file_path ,
326
328
"type" : file_type ,
327
329
}
0 commit comments