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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reassigning to the same Date object generates new copy with empty patches #858

Open
3 tasks done
somebody32 opened this issue Oct 15, 2021 · 2 comments
Open
3 tasks done

Comments

@somebody32
Copy link

馃悰 Bug Report

If you mutate Date property inside produce but then revert it, immer still assumes changes while returning an empty set of patches.

Link to repro

https://codesandbox.io/s/immer-reassigning-breaks-immutabillity-2u7ob

import {enablePatches, produceWithPatches} from "immer"

enablePatches();

type DataObject = { 
  date: Date;
}

let initialDate = new Date();
let data: DataObject[] = [{date: initialDate}];

let [newData, patches] = produceWithPatches(data, draft => {
  let element = draft[0];
  let dateBefore = element.date;
 
  element.date = new Date()
  element.date = dateBefore;
});

console.log("Patches:", patches) // []
console.log("New Date equal to Initial", newData[0].date === initialDate) // true
console.log("Array equal", newData === data); // expect true but getting false

Environment

Immer version: 9.0.6

  • I filed this report against the latest version of Immer
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)
@somebody32
Copy link
Author

I looked more deeply into the issue, and in reality, it is not a bug, but more a current limitation of immer. And it happens with any primitive, not only objects. So I think it can be treated more like a feature request.

Also, is there anything architecturally limiting from achieving this? As far as I can tell, setter that now sets _modified can also reset that flag if the original value was passed again, or am I wrong?

@mweststrate
Copy link
Collaborator

I think that analysis is correct, it could detect the same value is assigned and keep the result. Feel free to attempt a PR

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

No branches or pull requests

2 participants