mrpmorris / Fluxor

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

Blazor Maui Hybrid -- Scoped States #280

Closed bradtwurst closed 2 years ago

bradtwurst commented 2 years ago

I'm testing out a Blazor Maui Hybrid application and I've hit a bit of a snag.

I'd like to be able to use the same Fluxor state both within the Maui app and also the Blazor Web View.

After reading through https://github.com/dotnet/maui/issues/5237 , it appears that a scoped injection will create separate instances of the Fluxor state for the two parts of the app.

I've tested this out by injecting a simple scoped service and different instances were created.

This becomes more complicated if I end up with multiple Blazor Web Views for different parts of the layout.

Right now, my thought is to duplicate the 'AddFluxor' method and change the AddScopes to AddSingletons. I'm not sure if that idea makes sense.

Are there any other suggestions on how to proceed?

mrpmorris commented 2 years ago

I could add a property to FluxorOptions

bradtwurst commented 2 years ago

I'll try it out locally first and will let you know if changing the scope has the intended result for me.

Thanks for the quick response

bradtwurst commented 2 years ago

Testing out using AddSingleton instead of AddScoped appears to be successful for State, Reducers, and Effects.

I have not attempted any sort of Fluxor middleware so I can't comment on how well that will work.

Adding a prop to FluxorOptions seems to be a valid way to go.

Would you like me to attempt to develop the change?

mrpmorris commented 2 years ago

What property name + type do you find most intuitive?

bradtwurst commented 2 years ago

gotta be honest and say my intuition has been really bad this week...

I think the question is whether the option should signal purpose or is it more literal?

purpose: Option(RegisterForBlazorHybrid=true) literal: Option(SetServiceLifetime=Scoped/Singleton)

I might lean more towards the first so that I don't have to guess

the second option would cause me to question which lifetime should be used in which situation.

mrpmorris commented 2 years ago

It shouldn't mention Blazor, because Fluxor's core library is UI agnostic.

bradtwurst commented 2 years ago

As I said, my intuition hasn't been the best lately.... completely forgot UI-agnostic

I'm ok with the second option, or I'm also open to suggestions.

Would you like for me to generate the initial pull request?

mrpmorris commented 2 years ago

Yes, please do.

Add it as a property, so that people won't think you can set it differently for each call to ScanAssemblies.

If you create your own enum we can limit it to Singleton / Scoped and worry about the naming later :)

It should default to Scoped.

mrpmorris commented 2 years ago

Thanks for your contribution!

hypdeb commented 7 months ago

@bradtwurst have you managed to get this to work when using routing / NavigationManager? As far as I understand, the NavigationManager is scoped to the BlazorWebView, meaning that you should not be able to inject / use it into a singleton effects / store.