Collaboration
xml-api is designed to support real-time collaboration using CRDTs (Conflict-free Replicated Data Types) like Yjs or Automerge.
CollabBridge
The CollabBridge interface allows you to hook into the synchronization pipeline.
typescript
import { CollabBridge, Transaction } from '@miy2/xml-api';
class YjsBridge implements CollabBridge {
constructor(private yDoc: Y.Doc) {}
receiveLocalTransaction(tr: Transaction) {
// 1. Convert Transaction patches to Yjs operations
// 2. Apply to Y.Text or Y.XmlFragment
}
}
// Register the bridge
api.engine.setCollabBridge(new YjsBridge(yDoc));Remote Changes
When receiving changes from a remote peer (e.g. via Yjs provider):
- Apply the changes to your local CRDT store.
- Construct a
Transactionrepresenting the change in the source text. - Set
tr.isRemote = trueto prevent echo-back (infinite loops). - Dispatch the transaction.
typescript
yText.observe(event => {
if (event.transaction.local) return; // Ignore local changes
const tr = new Transaction(api.engine.state);
// ... map Yjs delta to tr.replace() ...
tr.isRemote = true;
api.engine.dispatch(tr);
});