diff --git a/frontend/src/crdt/AwarenessManager.ts b/frontend/src/crdt/AwarenessManager.ts new file mode 100644 index 0000000..6e8a7c0 --- /dev/null +++ b/frontend/src/crdt/AwarenessManager.ts @@ -0,0 +1,40 @@ +import { Awareness } from 'y-protocols/awareness'; +import * as Y from 'yjs'; + +export class AwarenessManager { + awareness: Awareness; + + constructor(doc: Y.Doc) { + this.awareness = new Awareness(doc); + } + + setLocalState(state: Record) { + this.awareness.setLocalState(state); + } + + getStates(): Map> { + return this.awareness.getStates(); + } + + on(event: 'change', callback: (updates: AwarenessUpdate) => void): void; + on(event: 'update', callback: (updates: AwarenessUpdate) => void): void; + on(event: string, callback: (updates: AwarenessUpdate) => void): void { + this.awareness.on(event as any, callback); + } + + off(event: 'change', callback: (updates: AwarenessUpdate) => void): void; + off(event: 'update', callback: (updates: AwarenessUpdate) => void): void; + off(event: string, callback: (updates: AwarenessUpdate) => void): void { + this.awareness.off(event as any, callback); + } + + destroy() { + this.awareness.destroy(); + } +} + +type AwarenessUpdate = { + added: number[]; + updated: number[]; + removed: number[]; +}; \ No newline at end of file