/redux-fsharp

⚛️ Predictable state container for F# apps

Primary LanguageF#MIT LicenseMIT

redux-fsharp

Build status NuGet

Predictable state container for F# applications.

redux-fsharp is a Redux-like implementation of the unidirectional data flow architecture in F#.

Add package

If you want to add this package to your project, execute the following command:

dotnet add package redux-fsharp

Build on your machine

If you want to build this library on your machine, execute the following commands:

git clone https://github.com/galassie/redux-fsharp.git
cd redux-fsharp
dotnet build

If you want to run the tests, execute the following command:

dotnet test

Build in Docker

Required:

  • Install Docker for your system

Build a Docker image called redux-fsharp. This will work without any local .NET Core installation.

docker build -t redux-fsharp .

Use the following to instantiate a Docker container from the redux-fsharp image and run the tests inside:

docker run --rm redux-fsharp dotnet test

Usage

State:

type State = { CurrentValue: int }

Actions:

type IncrementAction = { Amount: int }
type DecrementAction = { Amount: int }

type Actions =
    | Increment of IncrementAction
    | Decrement of DecrementAction

Reducer:

let incrementDecrementReducer state action =
    match action with
    | Increment { Amount = amount } -> { state with CurrentValue = state.CurrentValue + amount }
    | Decrement { Amount = amount } -> { state with CurrentValue = state.CurrentValue - amount }

Subscriber:

let consoleLogSubscriber state =
    printfn "Current value: %d" state.CurrentValue

Program:

[<EntryPoint>]
let main argv =
    let store = createStore incrementDecrementReducer { CurrentValue = 0 } id
    let unsubscribe = store.Subscribe(consoleLogSubscriber)

    store.Dispatch (Increment { Amount = 1 }) |> ignore
    store.Dispatch (Increment { Amount = 2 }) |> ignore
    store.Dispatch (Decrement { Amount = 1 }) |> ignore

    unsubscribe() |> ignore

    store.Dispatch (Decrement { Amount = 1 }) |> ignore

    let lastState = store.GetState()
    printfn "Expected Current value should be 1"
    printfn "Actual Current value is %d" lastState.CurrentValue
    0 // return an integer exit code

Examples

Contributing

Code contributions are more than welcome! 😻

Please commit any pull requests against the master branch.
If you find any issue, please report it!

License

This project is licensed under The MIT License (MIT).

Author: Enrico Galassi