write sync token first

in case we get a TransactionInactiveError,
we have at least written the sync token
and won't repeat the same sync request
This commit is contained in:
Bruno Windels 2020-10-01 14:36:22 +02:00
parent 1117c77d05
commit 300529b7c5
2 changed files with 7 additions and 6 deletions

View file

@ -341,17 +341,18 @@ export class Session {
deviceMessageDecryptionPending: false deviceMessageDecryptionPending: false
}; };
const syncToken = syncResponse.next_batch; const syncToken = syncResponse.next_batch;
const deviceOneTimeKeysCount = syncResponse.device_one_time_keys_count;
if (this._e2eeAccount && deviceOneTimeKeysCount) {
changes.e2eeAccountChanges = this._e2eeAccount.writeSync(deviceOneTimeKeysCount, txn);
}
if (syncToken !== this.syncToken) { if (syncToken !== this.syncToken) {
const syncInfo = {token: syncToken, filterId: syncFilterId}; const syncInfo = {token: syncToken, filterId: syncFilterId};
// don't modify `this` because transaction might still fail // don't modify `this` because transaction might still fail
txn.session.set("sync", syncInfo); txn.session.set("sync", syncInfo);
changes.syncInfo = syncInfo; changes.syncInfo = syncInfo;
} }
const deviceOneTimeKeysCount = syncResponse.device_one_time_keys_count;
if (this._e2eeAccount && deviceOneTimeKeysCount) {
changes.e2eeAccountChanges = this._e2eeAccount.writeSync(deviceOneTimeKeysCount, txn);
}
if (this._deviceTracker) { if (this._deviceTracker) {
const deviceLists = syncResponse.device_lists; const deviceLists = syncResponse.device_lists;
if (deviceLists) { if (deviceLists) {

View file

@ -186,12 +186,12 @@ export class Sync {
let sessionChanges; let sessionChanges;
const syncTxn = this._openSyncTxn(); const syncTxn = this._openSyncTxn();
try { try {
sessionChanges = await this._session.writeSync(response, syncFilterId, syncTxn);
await Promise.all(roomStates.map(async rs => { await Promise.all(roomStates.map(async rs => {
console.log(` * applying sync response to room ${rs.room.id} ...`); console.log(` * applying sync response to room ${rs.room.id} ...`);
rs.changes = await rs.room.writeSync( rs.changes = await rs.room.writeSync(
rs.roomResponse, isInitialSync, rs.preparation, syncTxn); rs.roomResponse, isInitialSync, rs.preparation, syncTxn);
})); }));
sessionChanges = await this._session.writeSync(response, syncFilterId, syncTxn);
} catch(err) { } catch(err) {
// avoid corrupting state by only // avoid corrupting state by only
// storing the sync up till the point // storing the sync up till the point