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

'Cannot apply patch, path doesn't resolve' when modifying property of an object in Map #952

Closed
3 tasks done
AhsanSarwar45 opened this issue Jun 13, 2022 · 2 comments · Fixed by #1025
Closed
3 tasks done
Labels

Comments

@AhsanSarwar45
Copy link

AhsanSarwar45 commented Jun 13, 2022

🐛 Bug Report

applyPatches throws error Cannot apply patch, path doesn't resolve: map/1/b when applying inversePatches generated by the following produce call:

interface Test {
  a: number;
  b: string;
}

let state = {
  map: new Map<number, Test>([])
};

state.map.set(1, { a: 5, b: "hello" }); // Setting initial state

let inversePatches: Patch[] = [];

state = produce(
    state,
    (draftState) => {
      const test = draftState.map.get(1);
      if (test) test.b = "world";
    },
    (_patches, _inversePatches) => {
      inversePatches = _inversePatches;
    }
);

It works fine when the patch op is add instead of replace:

state = produce(
    state,
    (draftState) => {
      const test = draftState.map.get(1);
      if (test) draftState.map.set(1, {...test, b: "world"}); // works
    },
    (_patches, _inversePatches) => {
      inversePatches = _inversePatches;
    }
);

Link to repro

https://codesandbox.io/s/awesome-stitch-xxtxq3?file=/src/index.ts

  • Click Set Map Property button to set map property
  • Click Undo button to call applyPatches with inversePatches

To Reproduce

Steps to reproduce the behavior:

  • Call enableMapSet and enablePatches
  • Create a Map with an object as the value type and add some key-value pairs to it
  • Use produce to mutate the Map
  • Inside produce, retrieve an element using Map.get() and modify a property on the element
  • Save the inversePatches generated by produce
  • Call applyPatches(map, inversePatches)

Observed behavior

applyPatches throws error Cannot apply patch, path doesn't resolve: map/1/b.

Expected behavior

The inversePatches should be applied successfully and state should revert back to the state it was in before the produce call

Environment

  • Immer version: 9.0.14
  • I filed this report against the latest version of Immer
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)
@azeem-io
Copy link

Getting the same error. Any updates on this?

@github-actions
Copy link
Contributor

🎉 This issue has been resolved in version 9.0.20 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

2 participants