mrpmorris / Fluxor

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

Blazor Hybrid MAUI app - ReducerMethods not being called #382

Closed lwinch2006 closed 1 year ago

lwinch2006 commented 1 year ago

I'm testing Fluxor with Blazor Hybrid app. And it looks like methods decorated with [ReducerMethod] attribute is not being called.

Setup in MauiProgram.cs

        builder.Services
            .AddFluxor(options =>
            {
                options.WithLifetime(StoreLifetime.Singleton);
                options.ScanAssemblies(typeof(AppStore).Assembly);
            })
            .AddScoped(sp => new HttpClient { BaseAddress = new Uri("https://localhost") })
            .AddBlazorWebViewDeveloperTools()
            .AddMauiBlazorWebView();

Defined action

public record ClearUserReadAction
{
    public ClearUserReadAction()
    {
        Console.WriteLine("ACTION CREATED");
    }
}

Defined reducer

    [ReducerMethod(typeof(ClearUserReadAction))]
    public static AppStore ReduceClearUserReadAction(AppStore appStore)
    {
        Console.WriteLine("ACTION PROCESSED");

        return appStore with {IsLoading = true, UserRead = null, Elapsed = TimeSpan.Zero, ErrorMessage = null };
    }

Calling from Razor component

    [Inject] private IState<AppStore> State { get; set; } = default!;

    [Inject] private Fluxor.IDispatcher Dispatcher { get; set; } = default!;

    private AppStore AppStore => State.Value;

    private async Task Refresh()
    {
        Console.WriteLine("BEFORE ACTION CALLED");

        Dispatcher.Dispatch(new ClearUserReadAction());

    }

So console prints out BEFORE ACTION CALLED AND ACTION CREATED, but not ACTION PROCESSED. So I'm concluding that ReducerMethod not being called.

Screenshot 2022-12-09 at 14 25 23

lwinch2006 commented 1 year ago

In usual Blazor WASM app all works OK Screenshot 2022-12-09 at 14 42 32

mrpmorris commented 1 year ago

Can you provide me with a repro?

lwinch2006 commented 1 year ago

Hello. I have created a sample MAUI + Blazor + Fluxor project. It is just a sample app generated from Microsoft template with Counter page. With Fluxor I created sample IncrementCountAction that should lead to calling ReduceIncrementCountAction, but it is not. Incrementing count with local function works normally. Tested for Mac Catalyst, iOS and Android platforms

Link to repo: https://github.com/lwinch2006/TestBlazorMauiWithFluxor

Interface:

Screenshot 2022-12-11 at 21 56 44
mrpmorris commented 1 year ago

Have you added a <StoreInitializer> component somewhere, such as Main.razor?

lwinch2006 commented 1 year ago

Oh my bad 😰, nope, the most embarrassing that I added it in WASM app but not added in MAUI app. After this was surprised that all looks good in one app and not working in another. Everything works great now, sorry for bothering.

mrpmorris commented 1 year ago

It's not a bother at all. This step is so infrequent it is easily forgotten :)