-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseMatrixSyncPrepared.ts
More file actions
45 lines (38 loc) · 1.18 KB
/
Copy pathuseMatrixSyncPrepared.ts
File metadata and controls
45 lines (38 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { ClientEvent, SyncState } from 'matrix-js-sdk'
import type { MatrixClient } from 'matrix-js-sdk'
function readSyncPrepared(matrixClient: MatrixClient): boolean {
if (matrixClient.isInitialSyncComplete?.() === true) {
return true
}
return matrixClient.getSyncState?.() === SyncState.Prepared
}
/**
* Tracks whether the Matrix client finished its initial sync. Persists across
* route changes so chat unread UI does not reset when leaving /chat briefly.
*/
export function useMatrixSyncPrepared(
client: Ref<MatrixClient | null>,
) {
const syncPrepared = useState('matrix-sync-prepared', () => false)
watch(
() => client.value,
(matrixClient, _previousClient, onCleanup) => {
if (!matrixClient) {
syncPrepared.value = false
return
}
syncPrepared.value = readSyncPrepared(matrixClient)
const onSyncState = (state: string): void => {
if (state === SyncState.Prepared) {
syncPrepared.value = true
}
}
matrixClient.on(ClientEvent.Sync, onSyncState)
onCleanup(() => {
matrixClient.off(ClientEvent.Sync, onSyncState)
})
},
{ immediate: true },
)
return syncPrepared
}