Unidux
Unidux copied to clipboard
Confusion over StateElement
Hi There,
I have implemented this framework (thanks you for your work!), however I'm a bit confused. My StateTree inherits from StateBase. I don't use any StateElement and things seem to work fine. My reducer fires, updates my state, and my StateTree gets cloned.
What is the advantage to using StateElement? Does it prevent a full state tree clone or something? I'm just wondering what I'm missing here as things seem to work fine. Thank you!
Hi, @jordan-econify Thanks for your question and sorry for my late response.
background and description of StateElement
In Unidux, if the state tree is changed, Store clone the state and notify the cloned state to observers.
Store#ForceUpdate
It's important behaviour for receiving consistent state tree regardless the executing order.
Unidux notify whole state tree to receivers on everytime if the part of sub-state is changed.
Generally speaking, it's not need to receive all state changed event.
For handling specific sub-state changed event, there exists IStateChanged interface.
IStateChange has dirty flag, the flag will be reset always after state is notified.
StateUtil.ResetStateChanged
StateElement is designed for sub-state and it has default implementation of IStateChanged.
It's easy to implement sub-state by extending the StateElement class.
So if you don't need to handle state changed event, you don't need to use StateElement.
Is the
StateElementprevent to clone whole state? Does it prevent a full state tree clone or something?
No, it cannot prevent a full state tree clone by itself. In the default implementation of root state (generally StateBase), whole memory is cloned. StateBase#Clone
If you would like to prevent memory clone, you need to override IClonable interface on the root of state tree and customize it.