Skip to content

Commit

Permalink
indexddb-local-backend - return the current sync to database promise …
Browse files Browse the repository at this point in the history
…if a sync is in flight

I’m trying to shutdown my matrix clients while using an indexdb, but awaiting the save() function has no effect because a previous sync was in flight. I ended up deleting the matrix client while the save was in flight and I saw a crash.

signed-off-by Austin Ellis <austin@hntlabs.com>

fix linter
  • Loading branch information
texuf committed Mar 30, 2023
1 parent bc76532 commit c31cf63
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/store/indexeddb-local-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend {
private db?: IDBDatabase;
private disconnected = true;
private _isNewlyCreated = false;
private isPersisting = false;
private syncToDatabasePromise?: Promise<void>;
private pendingUserPresenceData: UserTuple[] = [];

/**
Expand Down Expand Up @@ -388,26 +388,34 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend {
});
}

/**
* Sync users and all accumulated sync data to the database.
* If a previous sync is in flight, the new data will be added to the
* next sync and the current sync's promise will be returned.
* @param userTuples - The user tuples
* @returns Promise which resolves if the data was persisted.
*/
public async syncToDatabase(userTuples: UserTuple[]): Promise<void> {
if (this.isPersisting) {
if (this.syncToDatabasePromise) {
logger.warn("Skipping syncToDatabase() as persist already in flight");
this.pendingUserPresenceData.push(...userTuples);
return;
} else {
userTuples.unshift(...this.pendingUserPresenceData);
this.isPersisting = true;
}
return this.syncToDatabasePromise;
}
userTuples.unshift(...this.pendingUserPresenceData);
this.syncToDatabasePromise = this.doSyncToDatabase(userTuples);
return this.syncToDatabasePromise;
}

private async doSyncToDatabase(userTuples: UserTuple[]): Promise<void> {
try {
const syncData = this.syncAccumulator.getJSON(true);

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

Expand Down

0 comments on commit c31cf63

Please sign in to comment.