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

When producing the next state, all branches of the draft should be frozen #462

Closed
zhangenming opened this issue Nov 14, 2019 · 6 comments
Closed

Comments

@zhangenming
Copy link

🐛 Bug Report

modifice orgin will affect im
modifice im will affect orgin

To Reproduce

      var origin = {
        a: {
          value: 'no',
        },
        b: {
          value: 'no',
        },
      }
      var im = produce(origin, origin=> {
        origin.a.value = 'im'
      })
      // origin.a.value = 'yes'
      // origin.a = 'yes'
      origin.b.value = 'yes' //modifice orgin will affect im
      // origin.b = 'yes'
      // origin = {}

      // im.a.value = 'yes'
      // im.a = 'yes'
      im.b.value = 'yes' //modifice im will affect orgin
      // im.b = 'yes'
      // im = {}

Expected behavior

I don't know if this is as expected
or it shuold not affect each other?

@mweststrate
Copy link
Collaborator

are you on the last immer version? If so, please provide a reproduction in a codesandbox.

@zhangenming
Copy link
Author

yes, Didn't you succeed in reproducing it?
https://codesandbox.io/s/bold-greider-7f629

@mweststrate
Copy link
Collaborator

Thanks for the repro.

Note that this in principle not an Immer bug, as you are modifying an object that was created outside of immer, and which is never touched by immer, outside of immer again.

That being said, I think we should try to freeze the entire tree to avoid confusion in cases like these.

@zhangenming
Copy link
Author

i have a immature idea
if we have a large obj , such as

var obj = {
a1:1,
a2:2,
a3:3,
...
a1000000:1000000
}

now, immer looks like it's going to copy the object all key again, like{...obj}, it's will slow
how about just like this:
var objProto = {_proto_:obj}
then, we can access property like objProto.a1,
and we can modify property like objProto.a1 = 2,( and we Not modify origin data obj )

@mweststrate mweststrate changed the title if this is as expected? When producing the next state, all branches of the draft should be frozen Jan 9, 2020
mweststrate added a commit that referenced this issue Jan 9, 2020
@mweststrate
Copy link
Collaborator

This has been fixed in Immer 6 (?)

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

No branches or pull requests

3 participants