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
@@ -0,0 +1,27 @@
import { Storage, type StorageOptions } from './storage';
export interface AwarenessStorageOptions extends StorageOptions {}
export type AwarenessRecord = {
docId: string;
bin: Uint8Array;
};
export abstract class AwarenessStorage<
Options extends AwarenessStorageOptions = AwarenessStorageOptions,
> extends Storage<Options> {
override readonly storageType = 'awareness';
/**
* Update the awareness record.
*
* @param origin - Internal identifier to recognize the source in the "update" event. Will not be stored or transferred.
*/
abstract update(record: AwarenessRecord, origin?: string): Promise<void>;
abstract subscribeUpdate(
id: string,
onUpdate: (update: AwarenessRecord, origin?: string) => void,
onCollect: () => AwarenessRecord
): () => void;
}
+5 -2
View File
@@ -3,7 +3,7 @@ import EventEmitter2 from 'eventemitter2';
import type { ConnectionStatus } from '../connection';
import type { BlobStorage } from './blob';
import type { DocStorage } from './doc';
import { type Storage, type StorageType } from './storage';
import type { Storage, StorageType } from './storage';
import type { SyncStorage } from './sync';
type Storages = DocStorage | BlobStorage | SyncStorage;
@@ -22,7 +22,10 @@ export class SpaceStorage {
tryGet<T extends StorageType>(
type: T
): Extract<Storages, { storageType: T }> | undefined {
return this.storages.get(type) as Extract<Storages, { storageType: T }>;
return this.storages.get(type) as unknown as Extract<
Storages,
{ storageType: T }
>;
}
get<T extends StorageType>(type: T): Extract<Storages, { storageType: T }> {
@@ -1,7 +1,7 @@
import type { Connection } from '../connection';
export type SpaceType = 'workspace' | 'userspace';
export type StorageType = 'blob' | 'doc' | 'sync';
export type StorageType = 'blob' | 'doc' | 'sync' | 'awareness';
export interface StorageOptions {
peer: string;