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

Failure to apply inverse patchset #59

Closed
michaelhabibi opened this issue Sep 17, 2024 · 3 comments
Closed

Failure to apply inverse patchset #59

michaelhabibi opened this issue Sep 17, 2024 · 3 comments

Comments

@michaelhabibi
Copy link

michaelhabibi commented Sep 17, 2024

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
Copy link
Owner

unadlib commented Sep 18, 2024

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

@unadlib
Copy link
Owner

unadlib commented Sep 18, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants