Skip to content

Commit

Permalink
feat(nbstore): add awareness storage
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Dec 5, 2024
1 parent aa67016 commit 7a90114
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
42 changes: 42 additions & 0 deletions packages/common/nbstore/src/impls/idb/awareness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { DummyConnection } from '../../connection';
import {
type AwarenessRecord,
AwarenessStorage,
} from '../../storage/awareness';

export class IndexedDBAwarenessStorage extends AwarenessStorage {
override readonly storageType = 'awareness';
override readonly connection = new DummyConnection();

private readonly subscriptions = new Map<
string,
Set<(update: AwarenessRecord) => void>
>();

private readonly cached = new Map<string, AwarenessRecord>();

override update(record: AwarenessRecord): Promise<void> {
const subscribers = this.subscriptions.get(record.docId);
if (subscribers) {
subscribers.forEach(callback => callback(record));
}
this.cached.set(record.docId, record);
return Promise.resolve();
}

override subscribeUpdate(
id: string,
callback: (update: AwarenessRecord) => void
): () => void {
const subscribers = this.subscriptions.get(id) ?? new Set();
subscribers.add(callback);
this.subscriptions.set(id, subscribers);
const cached = this.cached.get(id);
if (cached) {
callback(cached);
}
return () => {
subscribers.delete(callback);
};
}
}
21 changes: 21 additions & 0 deletions packages/common/nbstore/src/storage/awareness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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';

abstract update(record: AwarenessRecord): Promise<void>;

abstract subscribeUpdate(
id: string,
callback: (update: AwarenessRecord) => void
): () => void;
}
2 changes: 2 additions & 0 deletions packages/common/nbstore/src/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import EventEmitter2 from 'eventemitter2';

import type { ConnectionStatus } from '../connection';
import { type Storage, type StorageType } from '../storage';
import type { AwarenessStorage } from './awareness';
import type { BlobStorage } from './blob';
import type { DocStorage } from './doc';
import type { SyncStorage } from './sync';
Expand All @@ -20,6 +21,7 @@ export class SpaceStorage {
tryGet(type: 'blob'): BlobStorage | undefined;
tryGet(type: 'sync'): SyncStorage | undefined;
tryGet(type: 'doc'): DocStorage | undefined;
tryGet(type: 'awareness'): AwarenessStorage | undefined;
tryGet(type: StorageType) {
return this.storages.get(type);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/common/nbstore/src/storage/storage.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 7a90114

Please sign in to comment.