mobxjs / mobx-state-tree

Full-featured reactive state management without the boilerplate
https://mobx-state-tree.js.org/
MIT License
6.94k stars 640 forks source link

Error is thrown on undo: Not a child: 0 #2010

Closed stoefln closed 1 year ago

stoefln commented 1 year ago

Bug report

Describe the expected behavior No error

Describe the observed behavior

On undoing a certain operation, the data structure is rolled back, but the following error is thrown:

Error: [mobx-state-tree] Not a child: 0
    at fail$1 (mobx-state-tree.module.js?f7d3:3839)
    at ArrayType.value (mobx-state-tree.module.js?f7d3:5426)
    at ObjectNode.value (mobx-state-tree.module.js?f7d3:1858)
    at resolveNodeByPathParts (mobx-state-tree.module.js?f7d3:3759)
    at eval (mobx-state-tree.module.js?f7d3:1989)
    at Array.forEach (<anonymous>)
    at eval (mobx-state-tree.module.js?f7d3:1983)
    at executeAction (mobx.esm.js?685c:1136)
    at <unnamed action> (mobx.esm.js?685c:1120)
    at runMiddleWares (mobx-state-tree.module.js?f7d3:3246)
    at runWithActionContext (mobx-state-tree.module.js?f7d3:3089)
    at ObjectNode.res [as _applyPatches] (mobx-state-tree.module.js?f7d3:3116)
    at ObjectNode.value (mobx-state-tree.module.js?f7d3:1565)
    at applyPatch (mobx-state-tree.module.js?f7d3:240)
    at eval (mst-middlewares.module.js?e48f:612)
    at skipRecording (mst-middlewares.module.js?e48f:576)
eval    @   TestView.js?e4f0:331

I could catch the error, but the problem is that apparently the UndoManager kind of ignores that that undo has been applied, and so if the user hits UNDO AGAIN, the UndoManager tries to repeatedly apply the undo, which leads to really bad side effects (like duplicate ids in the state tree).

I guess you won't be able to help without repro steps, but is there maybe a way to debug/log what is going on inside of the UndoManager, so I get some idea of what is happening? For me it's quite a black box and I don't even know where to start digging.

coolsoftwaretyler commented 1 year ago

Hey @stoefln - sorry about the bug! Thank you for considering the repro steps. I have a similar situation sometimes with a large enough tree and errors that aren't super clear-cut.

I don't have great advice without the reproduction, as you said. But I can at least point you in the right direction for the UndoManager.

This is the source file for that: https://github.com/mobxjs/mobx-state-tree/blob/master/packages/mst-middlewares/src/undo-manager.ts

Locally, I think you'll probably find your copy of this file in node_modules/mobx-state-tree/dist/middlewares, or somewhere near that if you want to drop some debug statements around your local repo.

If you get more details, post them back here! I have notifications on so hopefully we can work together on this.

coolsoftwaretyler commented 1 year ago

Hey @stoefln - without a reproduction of the problem, I'd like to close out this issue.

I'm hoping the links to the source code were helpful for you.

Feel free to file another issue if you can get a repro together, or if you find the specific bug in the middleware.

Thanks!