feat(nbstore): add awareness storage&sync&frontend (#9016)

This commit is contained in:
EYHN
2024-12-17 04:37:15 +00:00
parent 36ac79351f
commit ffa0231cf5
21 changed files with 572 additions and 26 deletions

View File

@@ -0,0 +1,30 @@
import type {
AwarenessRecord,
AwarenessStorage,
} from '../../storage/awareness';
export class AwarenessSync {
constructor(
readonly local: AwarenessStorage,
readonly remotes: AwarenessStorage[]
) {}
async update(record: AwarenessRecord, origin?: string) {
await Promise.all(
[this.local, ...this.remotes].map(peer => peer.update(record, origin))
);
}
subscribeUpdate(
id: string,
onUpdate: (update: AwarenessRecord, origin?: string) => void,
onCollect: () => AwarenessRecord
): () => void {
const unsubscribes = [this.local, ...this.remotes].map(peer =>
peer.subscribeUpdate(id, onUpdate, onCollect)
);
return () => {
unsubscribes.forEach(unsubscribe => unsubscribe());
};
}
}

View File

@@ -3,7 +3,7 @@ import { difference } from 'lodash-es';
import type { BlobRecord, BlobStorage } from '../../storage';
import { MANUALLY_STOP, throwIfAborted } from '../../utils/throw-if-aborted';
export class BlobSyncEngine {
export class BlobSync {
private abort: AbortController | null = null;
constructor(

View File

@@ -1,7 +1,7 @@
import type { DocStorage, SyncStorage } from '../../storage';
import { DocSyncPeer } from './peer';
export class DocSyncEngine {
export class DocSync {
private readonly peers: DocSyncPeer[];
private abort: AbortController | null = null;

View File

@@ -1,10 +1,10 @@
import type { BlobStorage, DocStorage, SpaceStorage } from '../storage';
import { BlobSyncEngine } from './blob';
import { DocSyncEngine } from './doc';
import { BlobSync } from './blob';
import { DocSync } from './doc';
export class SyncEngine {
private readonly doc: DocSyncEngine | null;
private readonly blob: BlobSyncEngine | null;
export class Sync {
private readonly doc: DocSync | null;
private readonly blob: BlobSync | null;
constructor(
readonly local: SpaceStorage,
@@ -16,7 +16,7 @@ export class SyncEngine {
this.doc =
doc && sync
? new DocSyncEngine(
? new DocSync(
doc,
sync,
peers
@@ -25,7 +25,7 @@ export class SyncEngine {
)
: null;
this.blob = blob
? new BlobSyncEngine(
? new BlobSync(
blob,
peers
.map(peer => peer.tryGet('blob'))