add origin to rdt's 'delta' event

This commit is contained in:
Kevin Jahns
2026-07-02 00:59:03 +02:00
parent b6a1cbca78
commit d55f7f91ad
3 changed files with 34 additions and 5 deletions

View File

@@ -256,6 +256,28 @@ export const testRdtDeltaEvent = () => {
t.compare(captured, delta.create().insert('hello').done())
}
/**
* The `'delta'` event carries the transaction origin as its second argument.
*/
export const testRdtDeltaEventOrigin = () => {
const ydoc = new Y.Doc()
const ytext = ydoc.get()
/**
* @type {any}
*/
let capturedOrigin = null
ytext.on('delta', (_d, origin) => { capturedOrigin = origin })
const myOrigin = {}
ydoc.transact(() => {
ytext.insert(0, 'hello')
}, myOrigin)
t.assert(capturedOrigin === myOrigin)
// without an explicit origin, `null` is emitted
capturedOrigin = myOrigin
ytext.insert(5, ' world')
t.assert(capturedOrigin === null)
}
/**
* `useRenderer` changes the default renderer used by `toDelta` (and friends). Calling `toDelta()`
* with no argument afterwards is equivalent to passing the renderer explicitly.