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

Added support for frozen trees, and automatically freeze generated trees #15

Merged
merged 6 commits into from
Jan 1, 2018

Conversation

mweststrate
Copy link
Collaborator

Implemens #2

Copy link
Collaborator

@benbraou benbraou left a 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)
Copy link
Collaborator

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 }
Copy link
Collaborator

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"]

Copy link
Collaborator Author

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)
Copy link
Collaborator

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 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const n = immer(b, draft => {
draft.c = true
draft.a.push(3)
debugger
Copy link
Collaborator

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 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@mweststrate
Copy link
Collaborator Author

mweststrate commented Jan 1, 2018

@benbraou thanks for the review! Interested in becoming a maintainer for this package?

@mweststrate mweststrate merged commit 0d24494 into master Jan 1, 2018
@benbraou
Copy link
Collaborator

benbraou commented Jan 1, 2018

@mweststrate thank you. Yes, I am interested in becoming a maintainer!

@aleclarson aleclarson deleted the feature/freeze branch November 30, 2018 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants