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

Freeze the state tree in dev? #135

Closed
gaearon opened this issue Jun 18, 2015 · 15 comments
Closed

Freeze the state tree in dev? #135

gaearon opened this issue Jun 18, 2015 · 15 comments

Comments

@gaearon
Copy link
Contributor

gaearon commented Jun 18, 2015

Should we deep-Object.freeze the state tree in development? I don't know how much of a perf hit that is.
It's hard for people to write mutation-free code from the first try so we might as well help them.

@emmenko
Copy link
Contributor

emmenko commented Jun 18, 2015

Is there a way maybe to validate that and log a warning?

Using Object.freeze you have to make sure every nested object gets also "frozen". Or maybe use immutables? :)

@gaearon
Copy link
Contributor Author

gaearon commented Jun 18, 2015

We could use something like https://github.com/substack/deep-freeze

@ooflorent
Copy link
Contributor

Sounds like a good idea.

@emmenko
Copy link
Contributor

emmenko commented Jun 18, 2015

Let's try it then 👍

@dariocravero
Copy link
Contributor

👍

@gaearon
Copy link
Contributor Author

gaearon commented Jun 18, 2015

If somebody wants to test this in a branch please do.

@leoasis
Copy link
Contributor

leoasis commented Jun 18, 2015

I think it would be better to do what @emmenko suggests. We could deep clone the state after each dispatch and before dispatching we can check if the state is deeply equal to that previously cloned state, and if not, throw a warning, or an error directly. We can also verify that the new state returned by each dispatch is a brand new object. Since it's only for dev it shouldn't matter that much with regards to performance. In any case, we can provide a way to disable that check.

@gaearon
Copy link
Contributor Author

gaearon commented Jun 18, 2015

Yeah, we could do that too. Would such cloning still work with ImmutableJS?

@leoasis
Copy link
Contributor

leoasis commented Jun 18, 2015

We can make it work with Immutable, I don't think any out of the box cloning library works with it though.

@gaearon
Copy link
Contributor Author

gaearon commented Jun 18, 2015

Perhaps we could bail out if we see Immutable (since it already guarantees immutability). Not sure.

@leoasis
Copy link
Contributor

leoasis commented Jun 18, 2015

If noone is already working on this I'm willing to give it a try

@johanneslumpe
Copy link
Contributor

Late to the party but this is a good idea to catch subtle bugs and late-night coding session issues.

@acdlite
Copy link
Collaborator

acdlite commented Jun 18, 2015

How about we only do this if the state is a plain object? If it's anything else (e.g. Immutable.js) we trust the user knows what they're doing.

@emmenko
Copy link
Contributor

emmenko commented Jun 18, 2015

Yes, we can try like this and see how it goes.

@gaearon
Copy link
Contributor Author

gaearon commented Jul 7, 2015

Let's continue the discussion in #138.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants