import * as Y from 'yjs' import {writable} from 'svelte/store' import type { Readable, Writable, } from 'svelte/store' export const deriveUpdates = ( doc: Y.Doc, ): Readable => { const updates: Writable = writable( Y.encodeStateAsUpdate(doc) ) doc.on('update', (update: Uint8Array) => updates.set(update) ) return { subscribe: updates.subscribe, } } export interface Unsubscriber { unsubscribe: () => void; } export const applyUpdates = ( doc: Y.Doc, updates: Readable ): Unsubscriber => { const subscriber = (update: Uint8Array) => Y.applyUpdate(doc, update) return { unsubscribe: updates.subscribe(subscriber), } }