-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nbstore): add awareness storage
- Loading branch information
Showing
4 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters