Hey there, can anybody help me understand this from the Awareness docs please?
Awareness CRDT updates work similarly to Yjs updates. First, you sync with a client. Then you exchange incremental updates with that client using the
update
event
What does “First, you sync with client” actually look like?
Context: I’m trying to create a custom provider with Firestore, lifting somebody else’s solution from a Github issue comment. The YDoc is syncing but I can’t get any Awareness information to propagate, code below - please excuse any stupid mistakes - I have trawled through the docs but I’m coming up with nothing.
Thanks!
@Injectable({ providedIn: 'root' })
export class FirebaseProvider extends Observable<string> {
public awareness: Awareness;
public doc: Y.Doc;
constructor(private db: AngularFireDatabase) {
super();
}
public configure(doc: FirestoreDocument, ydoc: Y.Doc) {
this.doc = ydoc;
this.awareness = new Awareness(ydoc);
this.connectAwareness();
this.db.database.ref(doc.id).on('child_added', (a, b) => {
try {
applyUpdate(ydoc, a.val());
} catch (e) {}
});
ydoc.on('update', (update) => {
this.db.database.ref(doc.id).push(update);
});
ydoc.on('destroy', this.destroy);
}
connectAwareness() {
// Encode the complete Awareness state
const encodedAwState = encodeAwarenessUpdate(
this.awareness,
Array.from(this.awareness.getStates().keys()),
);
// Make sure to share the complete awareness state whenever you
// connect to a new client. Similarly to the Yjs CRDT, it doesn't
// matter if the remote client receives the same state-updates several
// times. What is important is that the state is distributed.
applyAwarenessUpdate(this.awareness, encodedAwState, this);
// Whenever the local state changes, communicate that change to all connected clients
this.awareness.on('update', ({ added, updated, removed }) => {
const changedClients = added.concat(updated).concat(removed);
const update = encodeAwarenessUpdate(this.awareness, changedClients);
applyAwarenessUpdate(this.awareness, update, this);
});
}
disconnect() {}
destroy() {
this.doc.off('destroy', this.destroy);
// TODO
// 1 - If only one user present, update the Firestore document
// and then remove Yjs data from Firebase
// 2 - removeAwarenessStates
}
}