10
10
11
11
from jupyter_events import EventLogger
12
12
from jupyter_ydoc import ydocs as YDOCS
13
+ from ypy_websocket .stores import BaseYStore
13
14
from ypy_websocket .websocket_server import YRoom
14
- from ypy_websocket .ystore import BaseYStore , YDocNotFound
15
15
from ypy_websocket .yutils import write_var_uint
16
16
17
17
from .loaders import FileLoader
@@ -104,36 +104,28 @@ async def initialize(self) -> None:
104
104
return
105
105
106
106
self .log .info ("Initializing room %s" , self ._room_id )
107
-
108
107
model = await self ._file .load_content (self ._file_format , self ._file_type , True )
109
108
110
109
async with self ._update_lock :
111
110
# 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
+ )
132
126
133
- if not read_from_source :
134
127
# if YStore updates and source file are out-of-sync, resync updates with source
135
128
if self ._document .source != model ["content" ]:
136
- # TODO: Delete document from the store.
137
129
self ._emit (
138
130
LogLevel .INFO , "initialize" , "The file is out-of-sync with the ystore."
139
131
)
@@ -142,17 +134,26 @@ async def initialize(self) -> None:
142
134
self ._file .path ,
143
135
self .ystore .__class__ .__name__ ,
144
136
)
145
- read_from_source = True
146
137
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 :
148
148
self ._emit (LogLevel .INFO , "load" , "Content loaded from disk." )
149
149
self .log .info (
150
150
"Content in room %s loaded from file %s" , self ._room_id , self ._file .path
151
151
)
152
152
self ._document .source = model ["content" ]
153
153
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 )
156
157
157
158
self ._last_modified = model ["last_modified" ]
158
159
self ._document .dirty = False
0 commit comments