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

Wait for a full sync on startup #2372

Merged
merged 2 commits into from
May 22, 2024
Merged
Changes from 1 commit
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
18 changes: 14 additions & 4 deletions src/matrix-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,23 @@ const SYNC_STORE_NAME = "element-call-sync";
// (It's a good opportunity to make the database names consistent.)
const CRYPTO_STORE_NAME = "element-call-crypto";

function waitForSync(client: MatrixClient): Promise<void> {
async function waitForSync(client: MatrixClient): Promise<void> {
// If there is a saved sync, wait for it to be restored before the first real sync,
// as the saved sync may be missing some state
let waitForSavedSync = !!(await client.store.getSavedSyncToken());
return new Promise<void>((resolve, reject) => {
const onSync = (
state: SyncState,
_old: SyncState | null,
data?: ISyncStateData,
): void => {
if (state === "PREPARED") {
client.removeListener(ClientEvent.Sync, onSync);
resolve();
if (waitForSavedSync) {
waitForSavedSync = false;
AndrewFerr marked this conversation as resolved.
Show resolved Hide resolved
} else {
client.removeListener(ClientEvent.Sync, onSync);
resolve();
}
} else if (state === "ERROR") {
client.removeListener(ClientEvent.Sync, onSync);
reject(data?.error);
Expand Down Expand Up @@ -190,8 +197,11 @@ export async function initClient(

await client.initCrypto();
client.setGlobalErrorOnUnknownDevices(false);
// Apply listener before starting the client.
// Otherwise, the first sync could resolve before the listener gets applied.
const syncPromise = waitForSync(client);
await client.startClient();
await waitForSync(client);
await syncPromise;
AndrewFerr marked this conversation as resolved.
Show resolved Hide resolved

return client;
}
Expand Down
Loading