mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-25 14:28:51 +08:00
feat(nbstore): add awareness storage&sync&frontend (#9016)
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import { nanoid } from 'nanoid';
|
||||
import {
|
||||
applyAwarenessUpdate,
|
||||
type Awareness,
|
||||
encodeAwarenessUpdate,
|
||||
} from 'y-protocols/awareness.js';
|
||||
|
||||
import type { AwarenessRecord } from '../storage/awareness';
|
||||
import type { AwarenessSync } from '../sync/awareness';
|
||||
|
||||
type AwarenessChanges = Record<'added' | 'updated' | 'removed', number[]>;
|
||||
|
||||
export class AwarenessFrontend {
|
||||
constructor(private readonly sync: AwarenessSync) {}
|
||||
|
||||
connect(awareness: Awareness) {
|
||||
const uniqueId = nanoid();
|
||||
const handleAwarenessUpdate = (
|
||||
changes: AwarenessChanges,
|
||||
origin: string
|
||||
) => {
|
||||
if (origin === uniqueId) {
|
||||
return;
|
||||
}
|
||||
const changedClients = Object.values(changes).reduce((res, cur) =>
|
||||
res.concat(cur)
|
||||
);
|
||||
|
||||
const update = encodeAwarenessUpdate(awareness, changedClients);
|
||||
|
||||
this.sync
|
||||
.update(
|
||||
{
|
||||
docId: awareness.doc.guid,
|
||||
bin: update,
|
||||
},
|
||||
uniqueId
|
||||
)
|
||||
.catch(error => {
|
||||
console.error('update awareness error', error);
|
||||
});
|
||||
};
|
||||
|
||||
awareness.on('update', handleAwarenessUpdate);
|
||||
const handleSyncUpdate = (update: AwarenessRecord, origin?: string) => {
|
||||
if (origin === uniqueId) {
|
||||
// skip self update
|
||||
return;
|
||||
}
|
||||
|
||||
applyAwarenessUpdate(awareness, update.bin, origin);
|
||||
};
|
||||
const handleSyncCollect = () => {
|
||||
return {
|
||||
docId: awareness.doc.guid,
|
||||
bin: encodeAwarenessUpdate(awareness, [awareness.clientID]),
|
||||
};
|
||||
};
|
||||
const unsubscribe = this.sync.subscribeUpdate(
|
||||
awareness.doc.guid,
|
||||
handleSyncUpdate,
|
||||
handleSyncCollect
|
||||
);
|
||||
|
||||
return () => {
|
||||
awareness.off('update', handleAwarenessUpdate);
|
||||
unsubscribe();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
import type { BlobRecord, BlobStorage } from '../storage';
|
||||
import type { BlobSyncEngine } from '../sync/blob';
|
||||
import type { BlobSync } from '../sync/blob';
|
||||
|
||||
export class BlobFrontend {
|
||||
constructor(
|
||||
readonly storage: BlobStorage,
|
||||
readonly sync?: BlobSyncEngine
|
||||
readonly sync?: BlobSync
|
||||
) {}
|
||||
|
||||
get(blobId: string) {
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
} from 'yjs';
|
||||
|
||||
import type { DocRecord, DocStorage } from '../storage';
|
||||
import type { DocSyncEngine } from '../sync/doc';
|
||||
import type { DocSync } from '../sync/doc';
|
||||
import { AsyncPriorityQueue } from '../utils/async-priority-queue';
|
||||
import { isEmptyUpdate } from '../utils/is-empty-update';
|
||||
import { throwIfAborted } from '../utils/throw-if-aborted';
|
||||
@@ -56,7 +56,7 @@ export class DocFrontend {
|
||||
|
||||
constructor(
|
||||
private readonly storage: DocStorage,
|
||||
private readonly sync: DocSyncEngine | null,
|
||||
private readonly sync: DocSync | null,
|
||||
readonly options: DocFrontendOptions = {}
|
||||
) {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user