feat(crdt): Create AwarenessManager for multi-user awareness
This commit is contained in:
@@ -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<string, any>) {
|
||||
this.awareness.setLocalState(state);
|
||||
}
|
||||
|
||||
getStates(): Map<number, Record<string, any>> {
|
||||
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[];
|
||||
};
|
||||
Reference in New Issue
Block a user