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

Clearing ES6 Stores doesn't restore the initial state #24

Open
moretti opened this issue Jun 26, 2015 · 9 comments
Open

Clearing ES6 Stores doesn't restore the initial state #24

moretti opened this issue Jun 26, 2015 · 9 comments

Comments

@moretti
Copy link
Contributor

moretti commented Jun 26, 2015

Hi, according to the docs, Store.clear should replace the state with the initial state when called.

This works fine in classic mode because Store.clear internally calls Store.getInitialState.
However in ES6 mode Store.getInitialState is not defined because the state is typically initialised in the class constructor and calling Store.clear simply sets the state to an empty object.

I ended up using getInitialState in my ES6 stores as temporary workaround, but it seems an anti-pattern.

Any thoughts on this?

@taion
Copy link
Member

taion commented Jun 26, 2015

I'm actually using getInitialState in my ES6 stores as well, for exactly this reason. It doesn't seem awful - maybe we should capture the initial state for stores after registration, though.

@moretti moretti changed the title Clearing ES6 Stores doens't restore the initial state Clearing ES6 Stores doesn't restore the initial state Jun 26, 2015
@jhollingworth
Copy link
Contributor

This is an annoying side effect of ES6 and something I'm not overly happy with. The issue with capturing the initial state is, if its mutable, you're just re-assigning the initial state to itself. Also, cloning the object has lots of issues (e.g. what happens if its a class?).

I concluded that Store#clear wasn't used that much (I mainly used it in tests which is no longer needed now we have no singleton) and it was better to have a simpler API that reflects React's. I'm happy to make getInitialState required in v0.11 if you think this is better?

@taion
Copy link
Member

taion commented Jun 28, 2015

I would just _.cloneDeep(store.state) on registration and assign it somewhere. I think that might be a nice convenience.

@moretti
Copy link
Contributor Author

moretti commented Jun 28, 2015

I agree that Store.clear is not really useful in ES6 mode, I think it would be probably simpler to just
reinstantiate the class. The reason why I have this problem is because I'm invoking app.clearState to wipe out every store when users sign out.

@taion
Copy link
Member

taion commented Jul 2, 2015

I thought Lodash's cloning function handled most cases with objects with non-trivial prototypes. Might be worth investigating - probably we need getInitialState to handle all edge cases, but if cloning the initial state is sufficient for most use cases, then we should do so.

@jhollingworth
Copy link
Contributor

What about

this.state = {
  user: new User(123)
}

Which results in _.clone(this.state).user === this.state.user. Do you think this correct behaviour?

@taion
Copy link
Member

taion commented Jul 2, 2015

Hmm, and cloneDeep doesn't preserve the constructor (i.e. you just get an object).

That said, isn't this going to generally break dehydration/rehydration anyway? I still think this should do the right thing in most cases - anybody who needs something special can implement getInitialState, just like they do with custom dehydration and rehydration methods.

@jimmed
Copy link

jimmed commented Jul 2, 2015

I'm with @moretti on this one - admittedly I'm heavily biased as I work with him.

Naturally, capturing the initial state and re-assigning it on clear isn't suitable due to the state possibly being mutable (or uncloneable). Unless I'm missing something, simply destroying and re-instantiating the store object is the only clear solution to this problem.

This would support both ES6 and ES5 implementations (class-based or using Marty.createStore) without suffering any state mutation issues, or serious performance hits.

I'm open to suggestions!

@taion
Copy link
Member

taion commented Jul 2, 2015

@jimmed I see what you mean now. That makes a lot of sense.

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

No branches or pull requests

4 participants