immerjs / immer

Create the next immutable state by mutating the current one

Home Page:https://immerjs.github.io/immer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

auto freeze should be false by default

tonyxiao opened this issue · comments

I just spent 2 hours chasing down a bug caused by autoFreeze being enabled by default.

Immer is all about immutability, and it is really unexpected to pass an object into immer and to have the original object be then modified by immer using freeze.

This is in particular a problem on nodejs, which is not only not strict by default, but also hard to make strict to due a bug present since node 10. @see nodejs/node#30039

The effect of this is that methods that modify object properties were silently failing it took forever to chase down the root cause to passing object into immer.

Come to think about it maybe this should actually be a bug report. Nothing passed into immer should never be modified in anyway. If immer really wants to freeze them it should return a copy of the object frozen. (which is probably not a good idea as it would require a deep clone + freeze)

Nothing passed into immer should never be modified in anyway.

That is precisely the thing that freezing guarantees :). If freezing is disabled, Immer cannot know anymore for sure that an object isn't modified, and has more diffing to do (it will still be correct, just slower).

The effect of this is that methods that modify object properties were silently failing

That sounds like the actual problem? Writing to an immutable object will normally throw an exception and leave a clear message about what the problem is as an early heads up to the dev.

The problem is that immer is modifying a parameter passed to it.

And the problem with no exception being thrown when modifying frozen object is a bug in node not respecting useStrict flag when running script.

I remember immer only frozen the output data, not the input data. you can check this demo