Skip to content

UndoManager Middleware withoutUndo() used inside an action affects all patches within that action #1195

@Sacharified

Description

@Sacharified

Bug report

  • I've checked documentation and searched for existing issues
  • I've made sure my project is based on the latest MST version
  • Fork this code sandbox or another minimal reproduction.

Version info:

mobx 5.9.0

mobx-state-tree 3.10.2

mst-middlewares 3.10.2

When using the UndoManager middleware's withoutUndo method, in an action which causes multiple patches, if any one of the patch-creating changes is made within a withoutUndo call, all of the patches from that action are excluded from the undo history.

Sandbox link or minimal reproduction code
https://codesandbox.io/s/7mxk54x7nx?fontsize=14

const { types } = require(`mobx-state-tree`);
const { UndoManager } = require(`mst-middlewares`);

let undoManager = {};
const setUndoManager = targetStore => {
    undoManager = UndoManager.create({}, { targetStore });
};

const store = types
    .model(`Person`, {
        name: types.string,
        age: types.number
    })
    .actions(self => {
        setUndoManager(self);

        return {
            setName(name) {
                undoManager.withoutUndo(() => {
                    self.name = name;
                });
            },

            setAge(age) {
                self.age = age;
            },

            setInfo({ name, age }) {
                self.setName(name);
                self.setAge(age);
            }
        };
    });


const person = store.create({ name: `Jon`, age: 20 });

person.setInfo({ name: `Bob`, age: 50 });
console.log(undoManager.history.length); // 0

Describe the expected behavior
I would expect this to generate 2 patches, 1 for each property changed. I would expect the patch to the name property not to be recorded in the undo history and the patch for the age property to be added to the undo history.

Describe the observed behavior
No patches are added to the undo history at all. If you remove the withoutUndo, 2 patches are created in the history.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementPossible enhancementhas PRA Pull Request to fix the issue is available

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions