()
| 167 | // When initializing, you need to send the locally cached localPendingChangeset (if any) to the server |
| 168 | @errorCapture<RoomService>() |
| 169 | async sendLocalChangesetWithInit() { |
| 170 | const localPendingChangesetStorage = this.lsStore.namespace(BufferStorage.pendingChangesetsNamespace); |
| 171 | const opBufferStorage = this.lsStore.namespace(BufferStorage.bufferStorageNamespace); |
| 172 | |
| 173 | const changesetMap = localPendingChangesetStorage.getAll(); |
| 174 | const opBufferMap = opBufferStorage.getAll(); |
| 175 | |
| 176 | if (!Object.keys(changesetMap).length && !Object.keys(opBufferMap).length) { |
| 177 | return; |
| 178 | } |
| 179 | this.event.setRoomIOClear(false); |
| 180 | opBufferStorage.clearAll(); |
| 181 | localPendingChangesetStorage.clearAll(); |
| 182 | |
| 183 | this.clearAllStorage(); |
| 184 | try { |
| 185 | await this.applyLocalData(changesetMap, opBufferMap); |
| 186 | } catch (e) { |
| 187 | try { |
| 188 | await this.backupDB.setItem(String(Date.now()), { |
| 189 | changesetMap, |
| 190 | opBufferMap, |
| 191 | }); |
| 192 | } catch (e) { |
| 193 | if ((e as any).name === 'QuotaExceededError') { |
| 194 | await this.backupDB.clear(); |
| 195 | await this.backupDB.setItem(String(Date.now()), { changesetMap, opBufferMap }); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | const timestamps = await this.backupDB.keys(); |
| 200 | await Promise.all( |
| 201 | timestamps.map(async (timestamp) => { |
| 202 | // Clean up data whose backup time is greater than two weeks |
| 203 | if (dayjs().diff(Date.now(), 'day') > 14) { |
| 204 | await this.backupDB.removeItem(timestamp); |
| 205 | } |
| 206 | }) |
| 207 | ); |
| 208 | |
| 209 | Player.doTrigger(Events.app_error_logger, { |
| 210 | error: new Error(`Failed to initialize applyChangeset: ${(e as any).message}`), |
| 211 | metaData: { |
| 212 | roomId: this.roomId, |
| 213 | changesetMap: JSON.stringify(changesetMap), |
| 214 | opBufferMap: JSON.stringify(opBufferMap), |
| 215 | }, |
| 216 | }); |
| 217 | |
| 218 | throw new EnhanceError({ |
| 219 | code: ErrorCode.CollaModalError, |
| 220 | message: t(Strings.initialization_failed_message), |
| 221 | modalType: ModalType.Warning, |
| 222 | }); |
| 223 | } |
| 224 | |
| 225 | this.event.setRoomIOClear(true); |
| 226 | } |
no test coverage detected