Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indexddb-local-backend - return the current sync to database promise … #3222

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 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 @@ -396,26 +396,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