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

new objects added via produce are not frozen #363

Closed
vytautas-pranskunas- opened this issue May 11, 2019 · 8 comments
Closed

new objects added via produce are not frozen #363

vytautas-pranskunas- opened this issue May 11, 2019 · 8 comments

Comments

@vytautas-pranskunas-
Copy link

vytautas-pranskunas- commented May 11, 2019

I am playing around with immer.js. I have added new object to draft in produce function. After receiving new state whole object is frozen except newly added child.

const nextState = produce({items: {}}, draftState => {
    draftState.items['test'] = { id: t, name: 'ITEM-NEW', value: 0 };
  });

Object.isFrozen(nextState.items.test) // false

Or

const nextState = produce({items: [] }, draftState => {
    draftState.items.push({ id: t, name: 'ITEM-NEW', value: 0 });
  });

Object.isFrozen(nextState.items[0]) // false

But

Object.isFrozen(nextState.items) // true

I think it should be frozen or?

@cyrus-za
Copy link

@vytautas-pranskunas- it does not seem like @mweststrate intends for this. See test at

it("a new object is added to a nested draft", () => {

I am still very new to Immer, so not sure as to why this is intended (likely performance issues, or the fact that the produce function might reference a closure when you add the new item to the draft and you might not want that referenced object to suddenly become frozen. Either way, this seems to be intended.

@vytautas-pranskunas-
Copy link
Author

vytautas-pranskunas- commented May 11, 2019 via email

@cyrus-za
Copy link

I understand, but remember there is no guarantee that it is a new object. It could be referencing something from the parent scope. I suggest you call Object.freeze(newObj) before adding it to the draft. At least until @mweststrate or @aleclarson had time to respond

@vytautas-pranskunas-
Copy link
Author

vytautas-pranskunas- commented May 11, 2019 via email

@aleclarson
Copy link
Member

Duplicate of #260

For a workaround, see here: #260 (comment)

@vytautas-pranskunas-
Copy link
Author

vytautas-pranskunas- commented May 11, 2019 via email

@mweststrate
Copy link
Collaborator

mweststrate commented May 13, 2019

The point of this library is to make writing "updates" to immutable data structures easy. The point is not to make the data itself immutable (although it does make stuff mutated by producers immutable, in dev mode, as convenience for devs). if your goal is to make the data structures themselves immutable, you can use a library like deep-freeze, either on the entire tree, or just on the data passed in to producers, or create your own produce that calls immer produce first, and then applies deepFreeze to the result.

@vytautas-pranskunas-
Copy link
Author

vytautas-pranskunas- commented May 13, 2019 via email

mweststrate added a commit that referenced this issue Sep 11, 2019
…ply frozen.

BREAKING CHANGE: any value returned from produce will be deeply frozen in DEV, even when it wasn't frozen before the producer started. Fixes #412 #363 #260 #230 #313 #229 through #417
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

4 participants