Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Y.UndoManager fails silently when YTypes from different YDocs are used #509

Closed
acnebs opened this issue Mar 3, 2023 — with Huly GitHub · 2 comments
Closed

Y.UndoManager fails silently when YTypes from different YDocs are used #509

acnebs opened this issue Mar 3, 2023 — with Huly GitHub · 2 comments
Assignees
Labels

Comments

Copy link

acnebs commented Mar 3, 2023

Describe the bug Just spent a while debugging an issue that was caused by a lack of loud warnings when I do the (obvious in retrospect) wrong thing. Namely, the first argument of a Y.UndoManager can be either a single Y.Type or an array of Y.Types, but all the Y.Types must be from the same document. This is not mentioned in the docs but is true if you actually dig into the source code, or if you stumble across a utility like this: https://github.com/yjs/y-utility/#ymultidocundomanager. I think that at the very least the Y.UndoManager should throw an error if you try to initialize it with types from different docs.

However, due to the existence of YMultiDocUndoManager, I think an even better solution would be to have two classes under-the-hood, YMultiDocUndoManager and YSingleDocUndoManager, and a unifying class Y.UndoManager, and when someone instantiates a Y.UndoManager you can just check if all the Y.Types are from the same doc. If they are, you use YSingleDocUndoManager and if they're not you use the multi-doc manager.

Or if there is no downside to using YMultiDocUndoManager for everything (even when all in the same doc), you just use that code as the Y.UndoManager code.

To Reproduce

import * as Y from 'yjs';
import { YMultiDocUndoManager } from 'y-utility/y-multidoc-undomanager'

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

const um = new Y.UndoManager([y1map, y2array])

y1map.set('a', 1)
y2array.insert(0, ['a'])

console.log(y1map.toJSON())
console.log(y2array.toJSON())
console.log("[1st undo]")
um.undo()
console.log(y1map.toJSON())
console.log(y2array.toJSON())
console.log("[2nd undo]")
um.undo()
console.log(y1map.toJSON())
console.log(y2array.toJSON())

Huly®: YJS-380

@acnebs acnebs added the bug label Mar 3, 2023
@miyanokomiya
Copy link

if you try to initialize it with types from different docs

I just came across this situation and wondered why. Thank this issue for saving my time

@dmonad
Copy link
Member

dmonad commented Aug 24, 2023

Thanks for the suggestions. I don't want to make MultiDocUndoManager the default because some projects have good reasons to fork it and make it their own.

Checking that all observed types are part of the same YDoc is a good idea. That is not completely obvious from the API. I will do that.

@dmonad dmonad closed this as completed in 97c09a6 Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants