openpencil/tests/helpers/yjs.ts
rcoenen d2d816b688
fix(collab): replicate document content to joiners
Squashed PR #340 with maintainer follow-up fixes and local validation.
2026-06-30 11:56:58 +03:00

18 lines
539 B
TypeScript

import * as Y from 'yjs'
export function connectYDocs(left: Y.Doc, right: Y.Doc): () => void {
const syncLeftToRight = (update: Uint8Array) => Y.applyUpdate(right, update)
const syncRightToLeft = (update: Uint8Array) => Y.applyUpdate(left, update)
left.on('update', syncLeftToRight)
right.on('update', syncRightToLeft)
Y.applyUpdate(right, Y.encodeStateAsUpdate(left))
Y.applyUpdate(left, Y.encodeStateAsUpdate(right))
return () => {
left.off('update', syncLeftToRight)
right.off('update', syncRightToLeft)
}
}