-
-
Notifications
You must be signed in to change notification settings - Fork 856
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
Added support for frozen trees, and automatically freeze generated trees #15
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! I have added 4 small comments.
* This comes with a performance impact, so it is recommended to disable this option in production. | ||
* It is by default enabled. | ||
*/ | ||
export function setAutoFreeze(autoFreeze: boolean) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type should be void instead of implicit any
export function setAutoFreeze(autoFreeze: boolean):void
This is to prevent the use of setAutoFreeze return value in an unchecked way. Here is an example:
const a = setAutoFreeze(false)
a.test(); // error if the return type of setAutoFreeze is void, but will be ok if return type is any
immer.js
Outdated
let proxyTarget = base | ||
// special case, if the base tree is frozen, we cannot modify it's proxy it in strict mode, clone first. | ||
if (Object.isFrozen(proxyTarget)) { | ||
proxyTarget = Array.isArray(proxyTarget) ? proxyTarget.slice() : { ...proxyTarget } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since spread operator is used, babel-plugin-transform-object-rest-spread
should be added as a dev dependecny. Also, .babelrc should be updated with "plugins": ["transform-object-rest-spread"]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, actually I did not put the config there on purpose, to make sure the js files works on modern environments without compilation. I am a bit suprised Jest is fine with this (maybe it is because I switched to node 8). Probably I should add transpilation step soonish...
draft.object.c = 2 | ||
}) | ||
expect(Object.isFrozen(next.object)).toBe(true) | ||
expect(Object.isFrozen(next)).toBe(true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also safely assert that next.array
is not forzen in the process ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
__tests__/frozen.js
Outdated
const n = immer(b, draft => { | ||
draft.c = true | ||
draft.a.push(3) | ||
debugger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should debugger statement be removed ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@benbraou thanks for the review! Interested in becoming a maintainer for this package? |
@mweststrate thank you. Yes, I am interested in becoming a maintainer! |
Implemens #2