mrpmorris / Fluxor

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

[Question] - Recomendations for synchronizing server data state #331

Closed gtejeda closed 2 years ago

gtejeda commented 2 years ago

Greetings guys, new here, but in love with your work so far!

This is more a question, maybe even a philosophical question on how you'd approach this.

I implemented a Blazor server livechat solution for my company; I've reached a point that the UI has many components and I have to keep track of their state independently, often calling my APIs and coming up with "workarounds" to call StateHasChanged() on parents far more than I need to.

I read on a few of the questions here about singletons and global states, which I think is what I need, but allow me to be as detail as possible, so hopefully anybody has done a similar task and can throw a few pointers.

I use MemoryCache with a List (including messages), and when someone sends a message, this is broadcasted to all SignalR clients connected to simply "refresh" the UI at that moment.

I will implement Fluxor and do the exact same thing, simply update the UI on any message received, but instead of my callbacks and forcing state changes manually, I will grab state from the server and update everything with actions at this moment, it just sounds a bit inefficient (in my head)

What I was leaning towards was, some way I could update the "Conversation List" from the background/server, thus having a single source of truth on the UI (conversations should in fact be the same for all users at all times); but at the same time, user A, maybe looking at his conversation with user B, and user B with D and E, so evidently, I need to keep tabs on a per user state (which is the most obvious choice for fluxor of course).

Sorry for thinking out loud, but if you throw me a bone here, your help would be invaluable.

Thanks guys, Geo

mrpmorris commented 2 years ago

If you descend your components from FluxorComponent / FluxorLayout then you don't need to worry about calling StateHasChanged.

You can register your store as singleton, so that would give you what you need, it should also be thread safe.

However, the singleton approach was really meant for single-user apps that create a new scope per page (like MAUI). You'll have to keep in mind that only a single user can dispatch an action at a given moment. This might not scale well, I've not tried it but it wasn't designed for this kind of thing.

So, my advice would be to test it under extreme load and see if the performance is acceptable.

gtejeda commented 2 years ago

Thanks @mrpmorris, It was not needed in the end, In case anybody else is wondering, I'm using Fluxor as intended, scoped and for user state.

For global state, I'm simply using a singleton class with anything global and I'm able to interact with both global and user state beautifully.

Great job on the library guys! Geo

mrpmorris commented 2 years ago

Thanks for posting your solution!