1010
1111from jupyter_events import EventLogger
1212from jupyter_ydoc import ydocs as YDOCS
13+ from ypy_websocket .stores import BaseYStore
1314from ypy_websocket .websocket_server import YRoom
14- from ypy_websocket .ystore import BaseYStore , YDocNotFound
1515from ypy_websocket .yutils import write_var_uint
1616
1717from .loaders import FileLoader
@@ -104,36 +104,28 @@ async def initialize(self) -> None:
104104 return
105105
106106 self .log .info ("Initializing room %s" , self ._room_id )
107-
108107 model = await self ._file .load_content (self ._file_format , self ._file_type , True )
109108
110109 async with self ._update_lock :
111110 # try to apply Y updates from the YStore for this document
112- read_from_source = True
113- if self .ystore is not None :
114- try :
115- await self .ystore .apply_updates (self .ydoc )
116- self ._emit (
117- LogLevel .INFO ,
118- "load" ,
119- "Content loaded from the store {}" .format (
120- self .ystore .__class__ .__qualname__
121- ),
122- )
123- self .log .info (
124- "Content in room %s loaded from the ystore %s" ,
125- self ._room_id ,
126- self .ystore .__class__ .__name__ ,
127- )
128- read_from_source = False
129- except YDocNotFound :
130- # YDoc not found in the YStore, create the document from the source file (no change history)
131- pass
111+ if self .ystore is not None and await self .ystore .exists (self ._room_id ):
112+ # Load the content from the store
113+ await self .ystore .apply_updates (self ._room_id , self .ydoc )
114+ self ._emit (
115+ LogLevel .INFO ,
116+ "load" ,
117+ "Content loaded from the store {}" .format (
118+ self .ystore .__class__ .__qualname__
119+ ),
120+ )
121+ self .log .info (
122+ "Content in room %s loaded from the ystore %s" ,
123+ self ._room_id ,
124+ self .ystore .__class__ .__name__ ,
125+ )
132126
133- if not read_from_source :
134127 # if YStore updates and source file are out-of-sync, resync updates with source
135128 if self ._document .source != model ["content" ]:
136- # TODO: Delete document from the store.
137129 self ._emit (
138130 LogLevel .INFO , "initialize" , "The file is out-of-sync with the ystore."
139131 )
@@ -142,17 +134,26 @@ async def initialize(self) -> None:
142134 self ._file .path ,
143135 self .ystore .__class__ .__name__ ,
144136 )
145- read_from_source = True
146137
147- if read_from_source :
138+ doc = await self .ystore .get (self ._room_id )
139+ await self .ystore .remove (self ._room_id )
140+ version = 0
141+ if "version" in doc :
142+ version = doc ["version" ] + 1
143+
144+ await self .ystore .create (self ._room_id , version )
145+ await self .ystore .encode_state_as_update (self ._room_id , self .ydoc )
146+
147+ else :
148148 self ._emit (LogLevel .INFO , "load" , "Content loaded from disk." )
149149 self .log .info (
150150 "Content in room %s loaded from file %s" , self ._room_id , self ._file .path
151151 )
152152 self ._document .source = model ["content" ]
153153
154- if self .ystore :
155- await self .ystore .encode_state_as_update (self .ydoc )
154+ if self .ystore is not None :
155+ await self .ystore .create (self ._room_id , 0 )
156+ await self .ystore .encode_state_as_update (self ._room_id , self .ydoc )
156157
157158 self ._last_modified = model ["last_modified" ]
158159 self ._document .dirty = False
0 commit comments