unadlib / mutative

Efficient immutable updates, 2-6x faster than naive handcrafted reducer, and more than 10x faster than Immer.
http://mutative.js.org/
MIT License
1.57k stars 18 forks source link

Failure to apply inverse patchset #59

Closed michaelhabibi closed 21 hours ago

michaelhabibi commented 1 day ago

I've run into a case where apply fails when applying the inverse patchset generated by create. Take the following code example

const parentId = "parent";
const childId = "child";

const myObj = {
  [parentId]: { name: "parent", children: [childId] },
  [childId]: { name: "child" },
};

const [newState, patchset, inverse] = create(
  myObj,
  (draft) => {
    // delete children
    while (draft[parentId].children.length) {
      const id = draft[parentId].children[0];
      draft[parentId].children.splice(0, 1);
      delete draft[id]; // delete child object
    }

    // delete parent
    delete draft[parentId];
  },
  { enablePatches: { arrayLengthAssignment: false } }
);

const reverted = apply(newState, inverse); // throws an error

When trying to apply inverse to newState, apply throws the following error:

Cannot apply patch at 'parent/children/0'

Expected The inverse patchset is correctly generated and applied.

Note Reversing the inverse patchset in this case correctly applies it.

unadlib commented 1 day ago

hi @michaelhabibi , thanks for reporting the issue. I will fix it later.

unadlib commented 21 hours ago

Thank you again for reporting this issue.

We're pleased to inform you that the fix for this issue has been implemented and is now available in version v1.0.10, which has been released.

michaelhabibi commented 21 hours ago

Thank you for such a quick turnaround! Confirming the fix works 👍