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

Data integrity issue in produceWithPatches #468

Closed
2 tasks done
james-pellow opened this issue Nov 22, 2019 · 2 comments · Fixed by boardgameio/boardgame.io#796
Closed
2 tasks done

Data integrity issue in produceWithPatches #468

james-pellow opened this issue Nov 22, 2019 · 2 comments · Fixed by boardgameio/boardgame.io#796

Comments

@james-pellow
Copy link

james-pellow commented Nov 22, 2019

🐛 Bug Report

A case where calling produceWithPatches produces a correct next state, but incorrect patches.

To Reproduce

import { produceWithPatches, applyPatches } from "immer";

const item = { id: 1 };

const state = [item];

const [nextState, patches] = produceWithPatches(state, draft => {
  draft[0].id = 2;
  draft[1] = item;
});

const final = applyPatches(state, patches);

console.log("Next state:", nextState);
console.log("Patches:", patches);
console.log("Final:", final);

Expected behavior

I would expect nextState to deep equal final. nextState is correct while the result from applying patches is incorrect. The second operation in the patch is incorrect:

[ 
  { op: 'replace', path: [ 0, 'id' ], value: 2 },
  { op: 'add', path: [ 0 ], value: { id: 2 } } 
]

Notice how the value of patch 2 is the new value of draft[0], and it is applying it at draft[0].

Link to repro

Code Sandbox

Environment

  • Immer v5.0.0
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)
@darthzeran
Copy link

D o you need to call apply patches inside of a produceWithPatches?

mweststrate added a commit that referenced this issue Jun 10, 2020
* Introduced `current`, which takes a snapshot of the current state of a draft and finalizes it (but without freezing). Current is a great utility to print the current state during debugging (no Proxies in the way), and the output of current can also be safely leaked outside the producer. Implements #441, #591
* [BREAKING CHANGE] getters and setters are now handled consistently: own getters and setters will always by copied into fields (like Object.assign does), inherited getters and setters will be left as-is. This should allow using Immer directly on objects that trap their fields, like down in Vue or MobX. Fixes #584, #439, #593, #558
* [BREAKING CHANGE] produce no longer accepts  non-draftable objects as first argument
* [BREAKING CHANGE] original can only be called on drafts and will throw otherwise (fixes #605)
* [BREAKING CHANGE] non-enumerable and symbolic fields will never be frozen
* [BREAKING CHANGE] the patches for arrays are now computed differently to fix some scenarios in which they were incorrect. In some cases they will be more optimal now, in other cases less. Especially splicing / unshifting items into an existing array might result in a lot of patches. Fixes #468
* Improved documentation in several areas, there is now a page for typical update patterns and a separate page on how to work with classes. And additional performance tips have been included. Fixes #457, #115, #462
* Fixed #462: All branches of the produced state should be frozen
* Fixed #588: Inconsistent behavior with nested produce
* Fixed #577: Immer might not work with polyfilled symbols
* Fixed #514, #609: Explicitly calling `useProxies(false)` shouldn’t check for the presence of Proxy.
@aleclarson
Copy link
Member

🎉 This issue has been resolved in version 7.0.0 🎉

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants