feat(nbstore): add doc sync frontend (#9070)

This commit is contained in:
EYHN
2024-12-11 07:53:25 +00:00
parent eee0ed45ee
commit 331e674e8b
8 changed files with 499 additions and 58 deletions

View File

@@ -3,37 +3,44 @@ import { BlobSyncEngine } from './blob';
import { DocSyncEngine } from './doc';
export class SyncEngine {
private readonly doc: DocSyncEngine | null;
private readonly blob: BlobSyncEngine | null;
constructor(
readonly local: SpaceStorage,
readonly peers: SpaceStorage[]
) {}
) {
const doc = local.tryGet('doc');
const blob = local.tryGet('blob');
const sync = local.tryGet('sync');
async run(signal?: AbortSignal) {
const doc = this.local.tryGet('doc');
const blob = this.local.tryGet('blob');
const sync = this.local.tryGet('sync');
await Promise.allSettled([
(async () => {
if (doc && sync) {
const peerDocs = this.peers
.map(peer => peer.tryGet('doc'))
.filter((v): v is DocStorage => !!v);
const engine = new DocSyncEngine(doc, sync, peerDocs);
await engine.run(signal);
}
})(),
(async () => {
if (blob) {
const peerBlobs = this.peers
this.doc =
doc && sync
? new DocSyncEngine(
doc,
sync,
peers
.map(peer => peer.tryGet('doc'))
.filter((v): v is DocStorage => !!v)
)
: null;
this.blob = blob
? new BlobSyncEngine(
blob,
peers
.map(peer => peer.tryGet('blob'))
.filter((v): v is BlobStorage => !!v);
.filter((v): v is BlobStorage => !!v)
)
: null;
}
const engine = new BlobSyncEngine(blob, peerBlobs);
await engine.run(signal);
}
})(),
]);
start() {
this.doc?.start();
this.blob?.start();
}
stop() {
this.doc?.stop();
this.blob?.stop();
}
}