mrpmorris / Fluxor

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

Documentation for Maui setup #317

Closed dallas1287 closed 2 years ago

dallas1287 commented 2 years ago

Maybe I'm just dumb but I can't get this to work with my Maui app. I got it working on Blazor, but I try to initialize my Store in the App class constructor but then I noticed that every time I try to dispatch an action I'm queueing actions and when I inject the store into other places it reads as not initialized, but I saw it get initialized in the App constructor. The store initialization kinda gets hidden with a component in Blazor so I'm not really sure what to expect here.

mrpmorris commented 2 years ago

At the start of the app lifetime try resolving IDispatcher and IStore, then initialize the IStore.

Does that help?

dallas1287 commented 2 years ago

It only seems to work if I inject the dispatcher, the store, and the state together. Then before each call to dispatch an action, I initialize the store, then the state change event gets raised and surprisingly the counter increments correctly even though I reinitialize the store each time. But if I don't reinitialize it, the reducer never gets called.

dallas1287 commented 2 years ago

I take that back if i initialize it within the constructor that it gets injected then I don't have to call it each time, but it has to be within the class that it gets injected. So for example: I inject Store into App.xaml.cs constructor and initialize it there. The dispatcher that gets injected into a ContentPage viewmodel isn't valid and needs a store injected and that store has to be initialized in it's own constructor and then it works. I haven't gone deep enough to figure out what this means for something more complex than just the single counter.

mrpmorris commented 2 years ago

Are you setting the mode to Singleton in the AddFluxor options?

dallas1287 commented 2 years ago

yeah that totally did it. I tried that before and it didn't seem to have an effect but I must've had other variables affecting it. I broke it all back down to a minimal example and got it working with StoreLifetime.Singleton then was able to reinsert that into my larger project and get it working correctly. I don't remember what all I tinkered with that may have thrown it off, if I do I will update this thread for anyone else that runs across this.

mrpmorris commented 2 years ago

Glad you got it working!

I don't know MAUI at all, so if ever you'd like to help me to work out how to get a MAUI + Fluxor app running let me know, then I can write a demo app.

napalm684 commented 1 year ago

I think this would be incredibly useful as I attempt to figure this out myself. Especially since MAUI isn't really blazor server or wasm.

eharbitz commented 10 months ago

In MauiProgram.cs

builder.Services.AddFluxor(o => { o.ScanAssemblies(typeof(MauiProgram).Assembly); o.WithLifetime(StoreLifetime.Singleton); });