From 300529b7c571d430a3903a10b71c820de0e29734 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 1 Oct 2020 14:36:22 +0200 Subject: [PATCH] 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 --- src/matrix/Session.js | 11 ++++++----- src/matrix/Sync.js | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/matrix/Session.js b/src/matrix/Session.js index dbb95c24..3c2f0e8c 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -341,17 +341,18 @@ export class Session { deviceMessageDecryptionPending: false }; 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) { const syncInfo = {token: syncToken, filterId: syncFilterId}; // don't modify `this` because transaction might still fail txn.session.set("sync", 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) { const deviceLists = syncResponse.device_lists; if (deviceLists) { diff --git a/src/matrix/Sync.js b/src/matrix/Sync.js index ff41f9c1..889be6ad 100644 --- a/src/matrix/Sync.js +++ b/src/matrix/Sync.js @@ -186,12 +186,12 @@ export class Sync { let sessionChanges; const syncTxn = this._openSyncTxn(); try { + sessionChanges = await this._session.writeSync(response, syncFilterId, syncTxn); await Promise.all(roomStates.map(async rs => { console.log(` * applying sync response to room ${rs.room.id} ...`); rs.changes = await rs.room.writeSync( rs.roomResponse, isInitialSync, rs.preparation, syncTxn); })); - sessionChanges = await this._session.writeSync(response, syncFilterId, syncTxn); } catch(err) { // avoid corrupting state by only // storing the sync up till the point