Skip to content

Commit

Permalink
prevent overlapping sync accumulator persists
Browse files Browse the repository at this point in the history
add a flag to stop the sync worker trying to persist to indexeddb
if there are already persists in flight. accumulates user presence
updates in RAM to stop them being lost if the persist is skipped.

hopefully fixes https://github.com/vector-im/element-web/issues/21541
  • Loading branch information
ara4n committed May 21, 2022
1 parent a9516d0 commit 66adc2d
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/store/indexeddb-local-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend {
private db: IDBDatabase = null;
private disconnected = true;
private _isNewlyCreated = false;
private _isPersisting = false;
private _pendingUserPresenceData: UserTuple[] = [];

/**
* Does the actual reading from and writing to the indexeddb
Expand Down Expand Up @@ -401,11 +403,22 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend {
public async syncToDatabase(userTuples: UserTuple[]): Promise<void> {
const syncData = this.syncAccumulator.getJSON(true);

if (this._isPersisting) {
logger.warn("Skipping syncToDatabase() as persist already in flight");
this._pendingUserPresenceData.push(...userTuples);
return;
} else {
userTuples.unshift(...this._pendingUserPresenceData);
this._isPersisting = true;
}

await Promise.all([
this.persistUserPresenceEvents(userTuples),
this.persistAccountData(syncData.accountData),
this.persistSyncData(syncData.nextBatch, syncData.roomsData),
]);
]).finally(() => {
this._isPersisting = false;
});
}

/**
Expand All @@ -427,7 +440,9 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend {
nextBatch,
roomsData,
}); // put == UPSERT
return txnAsPromise(txn).then();
return txnAsPromise(txn).then(() => {
logger.log("Persisted sync data up to", nextBatch);
});
});
}

Expand Down

0 comments on commit 66adc2d

Please sign in to comment.