@@ -29,7 +29,7 @@ class YRoom:
2929 room_id := "{file_type}:{file_format}:{file_id}"
3030 """
3131
32- _jupyter_ydoc : YBaseDoc
32+ _jupyter_ydoc : YBaseDoc | None
3333 """JupyterYDoc"""
3434 _ydoc : pycrdt .Doc
3535 """Ydoc"""
@@ -53,8 +53,8 @@ def __init__(
5353 room_id : str ,
5454 log : Logger ,
5555 loop : asyncio .AbstractEventLoop ,
56- fileid_manager : BaseFileIdManager ,
57- contents_manager : AsyncContentsManager | ContentsManager ,
56+ fileid_manager : BaseFileIdManager | None = None ,
57+ contents_manager : AsyncContentsManager | ContentsManager | None = None ,
5858 ):
5959 # Bind instance attributes
6060 self .log = log
@@ -65,23 +65,29 @@ def __init__(
6565 self ._client_group = YjsClientGroup (room_id = room_id , log = self .log , loop = self ._loop )
6666 self ._ydoc = pycrdt .Doc ()
6767 self ._awareness = pycrdt .Awareness (ydoc = self ._ydoc )
68- _ , file_type , _ = self .room_id .split (":" )
69- JupyterYDocClass = cast (
70- type [YBaseDoc ],
71- jupyter_ydoc_classes .get (file_type , jupyter_ydoc_classes ["file" ])
72- )
73- self ._jupyter_ydoc = JupyterYDocClass (ydoc = self ._ydoc , awareness = self ._awareness )
74-
75- # Initialize YRoomFileAPI and begin loading content
76- self .file_api = YRoomFileAPI (
77- room_id = self .room_id ,
78- jupyter_ydoc = self ._jupyter_ydoc ,
79- log = self .log ,
80- loop = self ._loop ,
81- fileid_manager = fileid_manager ,
82- contents_manager = contents_manager
83- )
84- self .file_api .load_ydoc_content ()
68+
69+ self .file_api = None
70+ self ._jupyter_ydoc = None
71+ if fileid_manager and contents_manager :
72+ _ , file_type , _ = self .room_id .split (":" )
73+ JupyterYDocClass = cast (
74+ type [YBaseDoc ],
75+ jupyter_ydoc_classes .get (file_type , jupyter_ydoc_classes ["file" ])
76+ )
77+ self ._jupyter_ydoc = JupyterYDocClass (ydoc = self ._ydoc , awareness = self ._awareness )
78+
79+ # Initialize YRoomFileAPI and begin loading content
80+ self .file_api = YRoomFileAPI (
81+ room_id = self .room_id ,
82+ jupyter_ydoc = self ._jupyter_ydoc ,
83+ log = self .log ,
84+ loop = self ._loop ,
85+ fileid_manager = fileid_manager ,
86+ contents_manager = contents_manager
87+ )
88+ self .file_api .load_ydoc_content ()
89+ self ._jupyter_ydoc .observe (self ._on_jupyter_ydoc_update )
90+
8591
8692 # Start observers on `self.ydoc` and `self.awareness` to ensure new
8793 # updates are broadcast to all clients and saved to disk.
@@ -91,7 +97,6 @@ def __init__(
9197 self ._ydoc_subscription = self ._ydoc .observe (
9298 self ._on_ydoc_update
9399 )
94- self ._jupyter_ydoc .observe (self ._on_jupyter_ydoc_update )
95100
96101 # Initialize message queue and start background task that routes new
97102 # messages in the message queue to the appropriate handler method.
@@ -118,7 +123,8 @@ async def get_jupyter_ydoc(self):
118123 (`jupyter_ydoc.ybasedoc.YBaseDoc`) after waiting for its content to be
119124 loaded from the ContentsManager.
120125 """
121- await self .file_api .ydoc_content_loaded
126+ if self .file_api :
127+ await self .file_api .ydoc_content_loaded
122128 return self ._jupyter_ydoc
123129
124130
@@ -127,7 +133,8 @@ async def get_ydoc(self):
127133 Returns a reference to the room's YDoc (`pycrdt.Doc`) after
128134 waiting for its content to be loaded from the ContentsManager.
129135 """
130- await self .file_api .ydoc_content_loaded
136+ if self .file_api :
137+ await self .file_api .ydoc_content_loaded
131138 return self ._ydoc
132139
133140
@@ -155,7 +162,8 @@ async def _process_message_queue(self) -> None:
155162 """
156163 # Wait for content to be loaded before processing any messages in the
157164 # message queue
158- await self .file_api .ydoc_content_loaded
165+ if self .file_api :
166+ await self .file_api .ydoc_content_loaded
159167
160168 # Begin processing messages from the message queue
161169 while True :
@@ -448,7 +456,8 @@ async def stop(self) -> None:
448456 # Remove all observers, as updates no longer need to be broadcast
449457 self ._ydoc .unobserve (self ._ydoc_subscription )
450458 self ._awareness .unobserve (self ._awareness_subscription )
451- self ._jupyter_ydoc .unobserve ()
459+ if self ._jupyter_ydoc :
460+ self ._jupyter_ydoc .unobserve ()
452461
453462 # Finish processing all messages, then stop the queue to end the
454463 # `_process_message_queue()` background task.
@@ -457,4 +466,5 @@ async def stop(self) -> None:
457466
458467 # Finally, stop FileAPI and return. This saves the final content of the
459468 # JupyterYDoc in the process.
460- await self .file_api .stop ()
469+ if self .file_api :
470+ await self .file_api .stop ()
0 commit comments