diff --git a/src/app/pages/client/ClientRoot.tsx b/src/app/pages/client/ClientRoot.tsx
index fc4b4ba0b..e7fe4f8af 100644
--- a/src/app/pages/client/ClientRoot.tsx
+++ b/src/app/pages/client/ClientRoot.tsx
@@ -424,39 +424,33 @@ export function ClientRoot({ children }: ClientRootProps) {
return (
-
- {mx && }
- {(!mx || isError) && }
- {isError && (
-
-
-
-
-
- )}
- {!mx || (!syncReadyClient && !isError) ? (
-
- ) : isError ? null : (
+ {mx && }
+ {(!mx || isError) && }
+ {isError && (
+
+
+
+
+
+ )}
+ {!mx || (!syncReadyClient && !isError) ? (
+
+ ) : isError ? null : (
+
{(serverConfigs) => (
@@ -470,8 +464,8 @@ export function ClientRoot({ children }: ClientRootProps) {
)}
- )}
-
+
+ )}
);
}
diff --git a/src/client/initMatrix.ts b/src/client/initMatrix.ts
index e8d3c4ad3..330891d4b 100644
--- a/src/client/initMatrix.ts
+++ b/src/client/initMatrix.ts
@@ -4,7 +4,13 @@ import type {
MSC3575SlidingSyncRequest,
MSC3575SlidingSyncResponse,
} from '$types/matrix-sdk';
-import { createClient, IndexedDBStore, IndexedDBCryptoStore } from '$types/matrix-sdk';
+import {
+ ClientEvent,
+ createClient,
+ IndexedDBStore,
+ IndexedDBCryptoStore,
+ SyncState,
+} from '$types/matrix-sdk';
import { clearNavToActivePathStore } from '$state/navToActivePath';
import type { Session, Sessions, SessionStoreName } from '$state/sessions';
@@ -23,8 +29,51 @@ const log = createLogger('initMatrix');
const debugLog = createDebugLogger('initMatrix');
const slidingSyncByClient = new WeakMap();
const presenceSyncByClient = new WeakMap();
+const presenceStartCleanupByClient = new WeakMap void>();
const SLIDING_SYNC_POLL_TIMEOUT_MS = 20000;
+const isInitialSyncReady = (state: string | null): boolean =>
+ state === SyncState.Prepared || state === SyncState.Syncing || state === SyncState.Catchup;
+
+const startPresenceAfterInitialSync = (
+ mx: MatrixClient,
+ manager: PresenceSyncManager
+): (() => void) => {
+ let started = false;
+ let startTimer: ReturnType | undefined;
+
+ const start = () => {
+ if (started) return;
+ started = true;
+ presenceStartCleanupByClient.delete(mx);
+ mx.removeListener(ClientEvent.Sync, onSync);
+ manager.start();
+ };
+
+ const scheduleStart = () => {
+ if (startTimer !== undefined) return;
+ startTimer = globalThis.setTimeout(() => {
+ startTimer = undefined;
+ start();
+ }, 0);
+ };
+
+ const onSync = (state: SyncState) => {
+ if (isInitialSyncReady(state)) scheduleStart();
+ };
+
+ if (isInitialSyncReady(mx.getSyncState())) scheduleStart();
+ else mx.on(ClientEvent.Sync, onSync);
+
+ const cleanup = () => {
+ if (startTimer !== undefined) globalThis.clearTimeout(startTimer);
+ mx.removeListener(ClientEvent.Sync, onSync);
+ presenceStartCleanupByClient.delete(mx);
+ };
+ presenceStartCleanupByClient.set(mx, cleanup);
+ return cleanup;
+};
+
type StartupPhase = 'sync_store' | 'rust_crypto' | 'client_init' | 'client_start';
const measureStartupPhase = async (
@@ -321,6 +370,8 @@ const initializeClient = async (
}
const { mx, indexedDBStore } = builtClient;
+ void mx.getVersions().catch(() => undefined);
+
const syncStorePromise = measureStartupPhase('sync_store', () => indexedDBStore.startup());
const cryptoPromise = measureStartupPhase('rust_crypto', () =>
mx.initRustCrypto({ cryptoDatabasePrefix })
@@ -438,6 +489,7 @@ const disposeSlidingSync = (mx: MatrixClient): void => {
};
const disposePresenceSync = (mx: MatrixClient): void => {
+ presenceStartCleanupByClient.get(mx)?.();
const manager = presenceSyncByClient.get(mx);
if (!manager) return;
manager.dispose();
@@ -461,8 +513,7 @@ export const startClient = async (mx: MatrixClient, config?: StartClientConfig):
const presenceManager = new PresenceSyncManager(mx);
presenceSyncByClient.set(mx, presenceManager);
-
- presenceManager.start();
+ startPresenceAfterInitialSync(mx, presenceManager);
let manager: SlidingSyncManager | undefined;