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

View File

@@ -1,10 +1,21 @@
import { combineLatest, map, type Observable, of } from 'rxjs';
import type { BlobStorage, DocStorage, SpaceStorage } from '../storage';
import type { AwarenessStorage } from '../storage/awareness';
import { AwarenessSync } from './awareness';
import { BlobSync } from './blob';
import { DocSync } from './doc';
import { DocSync, type DocSyncState } from './doc';
export interface SyncState {
doc?: DocSyncState;
}
export class Sync {
private readonly doc: DocSync | null;
private readonly blob: BlobSync | null;
readonly doc: DocSync | null;
readonly blob: BlobSync | null;
readonly awareness: AwarenessSync | null;
readonly state$: Observable<SyncState>;
constructor(
readonly local: SpaceStorage,
@@ -13,6 +24,7 @@ export class Sync {
const doc = local.tryGet('doc');
const blob = local.tryGet('blob');
const sync = local.tryGet('sync');
const awareness = local.tryGet('awareness');
this.doc =
doc && sync
@@ -32,6 +44,18 @@ export class Sync {
.filter((v): v is BlobStorage => !!v)
)
: null;
this.awareness = awareness
? new AwarenessSync(
awareness,
peers
.map(peer => peer.tryGet('awareness'))
.filter((v): v is AwarenessStorage => !!v)
)
: null;
this.state$ = combineLatest([this.doc?.state$ ?? of(undefined)]).pipe(
map(([doc]) => ({ doc }))
);
}
start() {