0.1.4 • Published 1 day ago

y-utility v0.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 day ago

Utilities for Yjs

YMultiDocUndoManager

The Y.UndoManager only tracks operations on a single Y.Doc. Previously, you needed to maintain several undo managers if you had several documents. With the introduction of subdocuments, we now have documents nested inside each other and it often makes sense to have only a single undo/redo history for the document collection.

YMultiDocUndoManager is an UndoManager that enables you to track operations on a collection of documents. It implements the same API as Y.UndoManager and can safely be used in editor bindings that accept an existing Y.UndoManager as a parameter.

import { YMultiDocUndoManager } from 'y-utility/y-multidoc-undomanager'

const ydoc1 = new Y.Doc()
const ymap1 = ydoc1.getMap('my-map')
const ydoc2 = new Y.Doc()
const yarray2 = ydoc1.getArray('my-array')

// either specify tracked types at the beginning
const um = new YMultiDocUndoManager([ymap])
// or add them dynamically
um.addToScope([yarray2])

ymap1.set('a', 1)
yarray2.insert(0, ['a'])

um.undo() // yarray.toArray() ⇒ []
um.undo() // ymap.toJSON() ⇒ {}

YKeyValue

Y.Map doesn't make use of Yjs' optimizations if you write key-value entries in alternating order. Always writing the same entry does't significantly increase the size of the document. But writing key1, then key2, then key1, then key2 (alternating order) breaks Yjs' optimization. YKeyValue implements a more efficient key-value store that allows frequently updating alternating entries. Y.Map needs to retain all key values that were created in history to resolve potential conflicts. This makes Y.Map unsuitable as a key-value store. Using this implementation, the size of your document will shrink significantly when deleting keys.

import * as Y from 'yjs'
import { YKeyValue } from 'y-utility/y-keyvalue'

const ydoc = new Y.Doc()
const yarr = ydoc.getArray()
const ykv = new YKeyValue(yarr)

// Fires events similarly to Y.Map when content changes
ykv.on('change', changes => {
  console.log(changes) // => Map<string, { action: 'delete', oldValue: T } | { action: 'update', oldValue: T, newValue: T } | { action: 'add', newValue: T }>
})

ykv.set('key1', 'val1')
ykv.set('key1', 'updated')
ykv.delete('key1')
ykv.set('key1', 'new val')
ykv.get('key1') // => 'new val'

Benchmarks

npm test

The benchmarking suite operates on N different keys. We generate X set operations on the keys.

The benchmarks show that Y.Map creates documents that depend on the number of operations created. While YKeyValue's size only depends on the size of the map.

We measure the size of the Y.Doc using the different approaches (Y.Map vs YKeyValue).

operationskeysYKeyValue doc size (bytes)Y.Map doc size (bytes)JSON size (bytes)
100k10271524985121
100k10028175782311291
100k10003001759383413891
500k103292684482131
500k100301329542491391
500k100031005299224414891

Potential optimization

We call yarray.toArray() every time something changes. This does make key-value store unsuitable for huge collections (>1 million objects). Furthermore, the benchmarks take quite some time. However, each operation individually still takes less than 1 millisecond when operating on datasets with less than 1 million objects.

We can work directly with Yjs' Item objects without calling yarray.toArray() every time something changes. This requires us to expose some internal features of Yjs which I don't want to do until the Move feature lands in Yjs.

0.1.4

1 day ago

0.1.3

1 year ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago