Unidux icon indicating copy to clipboard operation
Unidux copied to clipboard

Confusion over StateElement

Open jordan-econify opened this issue 6 years ago • 1 comments

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!

jordan-econify avatar Nov 22 '19 22:11 jordan-econify

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 StateElement prevent 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.

mattak avatar Dec 04 '19 15:12 mattak