mrpmorris / Fluxor

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

F# example #343

Closed sandeep-eroad closed 2 years ago

sandeep-eroad commented 2 years ago

Can we add the following code as a F# example ?

open System.Reflection
open Fluxor
open Microsoft.Extensions.DependencyInjection

[<FeatureState>]
[<CLIMutable>]
type CounterState = {
    ClickCount: int
}

type CounterAction =
    | Increment
    | Decrement
    | IncrementBy of int
    | DecrementBy of int

module Reducer =
    [<ReducerMethod>]
    let incrementCounter (state: CounterState, action: CounterAction) =
        match action with
        | Increment -> { ClickCount = state.ClickCount + 1 }
        | Decrement -> { ClickCount = state.ClickCount - 1 }
        | IncrementBy x -> { ClickCount = state.ClickCount + x }
        | DecrementBy x -> { ClickCount = state.ClickCount - x }

type App(store: IStore, counterState: IState<CounterState>, dispatcher: IDispatcher) =
    do
        counterState.StateChanged.Add(fun x -> printfn $"-----> CounterState: {counterState.Value}")
    member this.Run ()=
        printfn "Initializing store..."
        store.InitializeAsync().Wait()
        dispatcher.Dispatch(Increment)
        dispatcher.Dispatch(Decrement)
        dispatcher.Dispatch(IncrementBy 5)
        dispatcher.Dispatch(DecrementBy 5)
        ()

let services = ServiceCollection()
services.AddScoped<App>() |> ignore
services.AddFluxor(fun x -> x.ScanAssemblies(Assembly.GetExecutingAssembly()) |> ignore) |> ignore
let serviceProvider = services.BuildServiceProvider()
let app = serviceProvider.GetRequiredService<App>()
app.Run()

printfn "Hello F#luxor"
mrpmorris commented 2 years ago

I don't know F#, so wouldn't feel comfortable supporting it, and providing demos implies support. Sorry :(

sandeep-eroad commented 2 years ago

Fair enough.