-
-
Notifications
You must be signed in to change notification settings - Fork 850
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
Excluding a single property from Object freezing #928
Comments
Sorry that is a bit too much code to understand to see why or why not your project is working and the error thrown in the test is not the one you've posted above, but immutability is indeed on the object level, see structural sharing and all other benefits of immutable objects would be pointless / confusing if attributes where still allowed to be modified. |
I've modified the sandbox and removed everything not needed for the test.
I do understand your concern about making bad practices possible, but this simple requirement forces me to fall back to the standard shallow freeze and to develop my own methods for cloning and modifying. I need some way of notifying other parts of my program about an object change. In this case by calling all observers with the newly produced object. Those observers have to be stored somewhere and I don't think any other place than the object itself would make any sense. |
You could use a stable ref to make a part mutable again, by creating a small wrapper object and make sure to never reassign
WeakMap's might be a great option here. |
Thanks for the suggestions. WeakMaps sound like a good alternative. The wrapper method doesn't change anything because the object is frozen recursively regardless of [immerable] being true or false. I'll just stick with Object.freeze, it's the cleanest solution so far. |
Did you test that by any chance? |
It results in the same error with just the _observer property swapped out and all functions changed to use the inner array. |
🙋♂ Question
I am trying to buld a simple Subject/Observer pattern together with immerjs to ensure consistent update propagation in my project.
Right now immerjs is causing some trouble because I want to register an observer to an object that has run through immer and is now frozen.
Basically I have to exclude the array of observers from getting touched by immer. How could I achieve that?
If it's not possible, why?
Link to repro
The codesandbox tests don't trigger my observers for some reason.
Running the same code locally gives the correct result.
The first test passes and the second test fails with the above error.
https://codesandbox.io/s/xmzig2
Environment
setUseProxies(true)
setUseProxies(false)
(ES5 only)The text was updated successfully, but these errors were encountered: