|
11 | 11 | from jupyter_events import EventLogger |
12 | 12 | from jupyter_ydoc import ydocs as YDOCS |
13 | 13 | from ypy_websocket.websocket_server import YRoom |
14 | | -from ypy_websocket.ystore import BaseYStore, YDocNotFound |
| 14 | +from ypy_websocket.stores import BaseYStore, YDocNotFound |
15 | 15 | from ypy_websocket.yutils import write_var_uint |
16 | 16 |
|
17 | 17 | from .loaders import FileLoader |
@@ -104,56 +104,40 @@ 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 |
132 | | - |
133 | | - if not read_from_source: |
| 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(LogLevel.INFO, "load", "Content loaded from the store {}".format(self.ystore.__class__.__qualname__)) |
| 115 | + self.log.info("Content in room %s loaded from the ystore %s", self._room_id, self.ystore.__class__.__name__,) |
| 116 | + |
134 | 117 | # if YStore updates and source file are out-of-sync, resync updates with source |
135 | 118 | if self._document.source != model["content"]: |
136 | | - # TODO: Delete document from the store. |
137 | | - self._emit( |
138 | | - LogLevel.INFO, "initialize", "The file is out-of-sync with the ystore." |
139 | | - ) |
140 | | - self.log.info( |
141 | | - "Content in file %s is out-of-sync with the ystore %s", |
142 | | - self._file.path, |
143 | | - self.ystore.__class__.__name__, |
144 | | - ) |
145 | | - read_from_source = True |
146 | | - |
147 | | - if read_from_source: |
| 119 | + self._emit(LogLevel.INFO, "initialize", "The file is out-of-sync with the ystore.") |
| 120 | + self.log.info("Content in file %s is out-of-sync with the ystore %s", self._file.path, self.ystore.__class__.__name__,) |
| 121 | + |
| 122 | + doc = await self.ystore.get(self._room_id) |
| 123 | + await self.ystore.remove(self._room_id) |
| 124 | + version = 0 |
| 125 | + if "version" in doc: |
| 126 | + version = doc["version"] + 1 |
| 127 | + |
| 128 | + await self.ystore.create(self._room_id, version) |
| 129 | + await self.ystore.encode_state_as_update(self._room_id, self.ydoc) |
| 130 | + |
| 131 | + else: |
148 | 132 | self._emit(LogLevel.INFO, "load", "Content loaded from disk.") |
149 | | - self.log.info( |
150 | | - "Content in room %s loaded from file %s", self._room_id, self._file.path |
151 | | - ) |
| 133 | + self.log.info("Content in room %s loaded from file %s", self._room_id, self._file.path) |
152 | 134 | self._document.source = model["content"] |
153 | 135 |
|
154 | | - if self.ystore: |
155 | | - await self.ystore.encode_state_as_update(self.ydoc) |
| 136 | + if self.ystore is not None: |
| 137 | + await self.ystore.create(self._room_id, 0) |
| 138 | + await self.ystore.encode_state_as_update(self._room_id, self.ydoc) |
156 | 139 |
|
| 140 | + |
157 | 141 | self._last_modified = model["last_modified"] |
158 | 142 | self._document.dirty = False |
159 | 143 | self.ready = True |
|
0 commit comments