feat(nbstore): add doc sync state (#9131)

This commit is contained in:
EYHN
2024-12-18 03:59:46 +00:00
parent 8374346b2e
commit 3fddf050a4
4 changed files with 129 additions and 8 deletions
+42 -3
View File
@@ -1,16 +1,55 @@
import type { Observable } from 'rxjs';
import { combineLatest, map } from 'rxjs';
import type { DocStorage, SyncStorage } from '../../storage';
import { DocSyncPeer } from './peer';
export interface DocSyncState {
total: number;
syncing: number;
retrying: boolean;
errorMessage: string | null;
}
export interface DocSyncDocState {
syncing: boolean;
retrying: boolean;
errorMessage: string | null;
}
export class DocSync {
private readonly peers: DocSyncPeer[];
private readonly peers: DocSyncPeer[] = this.remotes.map(
remote => new DocSyncPeer(this.local, this.sync, remote)
);
private abort: AbortController | null = null;
readonly state$: Observable<DocSyncState> = combineLatest(
this.peers.map(peer => peer.peerState$)
).pipe(
map(allPeers => ({
total: allPeers.reduce((acc, peer) => acc + peer.total, 0),
syncing: allPeers.reduce((acc, peer) => acc + peer.syncing, 0),
retrying: allPeers.some(peer => peer.retrying),
errorMessage:
allPeers.find(peer => peer.errorMessage)?.errorMessage ?? null,
}))
);
constructor(
readonly local: DocStorage,
readonly sync: SyncStorage,
readonly remotes: DocStorage[]
) {
this.peers = remotes.map(remote => new DocSyncPeer(local, sync, remote));
) {}
docState$(docId: string): Observable<DocSyncDocState> {
return combineLatest(this.peers.map(peer => peer.docState$(docId))).pipe(
map(allPeers => ({
errorMessage:
allPeers.find(peer => peer.errorMessage)?.errorMessage ?? null,
retrying: allPeers.some(peer => peer.retrying),
syncing: allPeers.some(peer => peer.syncing),
}))
);
}
start() {