mrpmorris / Fluxor

Fluxor is a zero boilerplate Flux/Redux library for Microsoft .NET and Blazor.
MIT License
1.24k stars 141 forks source link

Access previous state #294

Closed carlblanchard closed 2 years ago

carlblanchard commented 2 years ago

Hi Mr Morris, Question for you.

At runtime, I can see the state store IState contains a Previousvalue property. (FYI I'm hooked into StateStore.StateChanged event) I'm having difficulties in casting the state store to StateSelection in order to access the Previous value property.

Everything I am doing is feeling hacky, so was wondering if you know of an elegant way to access the previous state when a state change is detected.

Kind regards

mrpmorris commented 2 years ago

Can you show me some code? I can't think where you are getting previous state from.

carlblanchard commented 2 years ago

image

Line 16 of https://github.com/mrpmorris/Fluxor/blob/master/Source/Fluxor/StateSelection.cs

However, on further inspection, I am not convinced it's actually the previous state. I think it's the same as the action. I was kind of hoping there would be a stack of states (used in timewarp) when a new action was dispatched the value property would be the new state and the previousvalue property would be, well the previous state.

My use case is SingleR subscribing and unsubscribing from groups of course if someone changes their group you need to unsubscribe before subscribing hence the need for the previous value.

mrpmorris commented 2 years ago

Can you make the notification "GroupChanged" and have old + new?

Or GroupsChanged in which there is the complete list of active ones?

Or clear all of them, and just subscribe to the new one?

carlblanchard commented 2 years ago

Yeah, I've worked around my problem... was my thinking of the Previous value property correct? it would be good to know you can access previous data.

mrpmorris commented 2 years ago

There is no built in way for you to get access to previous state. I expect you could do it with middleware.