Skip to content

fix #4 #18

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion __tests__/undo-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UndoManager } from "../src"
import { types, clone, getSnapshot, flow, Instance } from "mobx-state-tree"
import { types, clone, getSnapshot, flow, Instance, destroy } from "mobx-state-tree"
import { configure } from "mobx"

let undoManager: any = {}
Expand Down Expand Up @@ -596,3 +596,89 @@ test("#15 - rollback by recorder.undo() should not be an UndoState", () => {
expect(model.value).toBe(1)
expect(_undoManager.history).toHaveLength(0)
})

test('#4 - should not saves patches from non-targeted store', () => {
let UID = 1;
const Box = types
.model("Box", {
id: types.optional(types.identifier, ''),
})

const BoxStore = types
.model("BoxStore", {
boxes: types.array(Box)
})
.actions((self) => ({
afterCreate() {
setUndoManager(self);
},
addRandomBox() {
const randomBox = Box.create({ id: `box-${UID++}` })
self.boxes.push(randomBox)
return randomBox;
},
remove(box: any) {
destroy(box)
}
}))

const Focus = types
.model("Focus", {
focusedBoxes: types.array(types.safeReference(Box))
})
.actions(self => ({
toggleBoxFocus(box: any) {
if (self.focusedBoxes.includes(box)) {
self.focusedBoxes.remove(box)
} else {
self.focusedBoxes.push(box)
}
}
}))

const RootStore = types
.model("RootStore", {
boxStore: BoxStore,
focus: Focus,
})

const store = RootStore.create({
boxStore: { boxes: [] },
focus: { focusedBoxes: [] }
});

let _undoManager: any = null
const setUndoManager = (targetStore: any) => {
_undoManager = UndoManager.create({}, { targetStore })
};

const box = store.boxStore.addRandomBox()
store.focus.toggleBoxFocus(box)

// don't record `focusedBoxes.push(box)`
expect(getSnapshot(_undoManager)).toEqual({
history: [
{
patches: [{ op: "add", path: "/boxStore/boxes/0", value: { id: "box-1" } }],
inversePatches: [{ op: "remove", path: "/boxStore/boxes/0" }]
}
],
undoIdx: 1
});

store.boxStore.remove(box)
// don't record safeReference's side effect : `focusedBoxes.remove(box)`
expect(getSnapshot(_undoManager)).toEqual({
history: [
{
patches: [{ op: "add", path: "/boxStore/boxes/0", value: { id: "box-1" } }],
inversePatches: [{ op: "remove", path: "/boxStore/boxes/0" }]
},
{
patches: [{ op: "remove", path: "/boxStore/boxes/0" }],
inversePatches: [{ op: "add", path: "/boxStore/boxes/0", value: { id: "box-1" } }]
}
],
undoIdx: 2
});
})
7 changes: 6 additions & 1 deletion src/undo-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
addMiddleware,
addDisposer,
decorate,
isActionContextThisOrChildOf
isActionContextThisOrChildOf,
getPath
} from "mobx-state-tree"
import { atomic } from "."

Expand Down Expand Up @@ -72,6 +73,10 @@ const UndoManager = types
if (recordingDisabled) {
return false
}
// only record patches that were generated by targetStore
if (!_patch.path.startsWith(getPath(targetStore))) {
return false
}
// only record patches that were generated by this action or children of this action
return (
!!actionContext && isActionContextThisOrChildOf(actionContext, call.id)
Expand Down