feat(nbstore): improve nbstore (#9512)

This commit is contained in:
EYHN
2025-01-06 09:38:03 +00:00
parent a2563d2180
commit 46c8c4a408
103 changed files with 3337 additions and 3423 deletions

View File

@@ -2,6 +2,7 @@ import type {
AwarenessRecord,
AwarenessStorage,
} from '../../storage/awareness';
import type { PeerStorageOptions } from '../types';
export interface AwarenessSync {
update(record: AwarenessRecord, origin?: string): Promise<void>;
@@ -13,14 +14,13 @@ export interface AwarenessSync {
}
export class AwarenessSyncImpl implements AwarenessSync {
constructor(
readonly local: AwarenessStorage,
readonly remotes: AwarenessStorage[]
) {}
constructor(readonly storages: PeerStorageOptions<AwarenessStorage>) {}
async update(record: AwarenessRecord, origin?: string) {
await Promise.all(
[this.local, ...this.remotes].map(peer => peer.update(record, origin))
[this.storages.local, ...Object.values(this.storages.remotes)].map(peer =>
peer.update(record, origin)
)
);
}
@@ -29,9 +29,10 @@ export class AwarenessSyncImpl implements AwarenessSync {
onUpdate: (update: AwarenessRecord, origin?: string) => void,
onCollect: () => Promise<AwarenessRecord | null>
): () => void {
const unsubscribes = [this.local, ...this.remotes].map(peer =>
peer.subscribeUpdate(id, onUpdate, onCollect)
);
const unsubscribes = [
this.storages.local,
...Object.values(this.storages.remotes),
].map(peer => peer.subscribeUpdate(id, onUpdate, onCollect));
return () => {
unsubscribes.forEach(unsubscribe => unsubscribe());
};