mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 12:55:00 +00:00
feat(nbstore): add awareness storage&sync&frontend (#9016)
This commit is contained in:
30
packages/common/nbstore/src/sync/awareness/index.ts
Normal file
30
packages/common/nbstore/src/sync/awareness/index.ts
Normal 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());
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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(
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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'))
|
||||
|
||||
Reference in New Issue
Block a user