mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-03 02:20:19 +08:00
feat(nbstore): add awareness storage&sync&frontend (#9016)
This commit is contained in:
@@ -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());
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user