项目作者: galassie

项目描述 :
⚛️ Predictable state container for F# apps
高级语言: F#
项目地址: git://github.com/galassie/redux-fsharp.git
创建时间: 2019-11-09T23:50:51Z
项目社区:https://github.com/galassie/redux-fsharp

开源协议:MIT License

下载


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:

  1. dotnet add package redux-fsharp

Build on your machine

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

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

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

  1. 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.

  1. docker build -t redux-fsharp .

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

  1. docker run --rm redux-fsharp dotnet test

Usage

State:

  1. type State = { CurrentValue: int }

Actions:

  1. type IncrementAction = { Amount: int }
  2. type DecrementAction = { Amount: int }
  3. type Actions =
  4. | Increment of IncrementAction
  5. | Decrement of DecrementAction

Reducer:

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

Subscriber:

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

Program:

  1. [<EntryPoint>]
  2. let main argv =
  3. let store = createStore incrementDecrementReducer { CurrentValue = 0 } id
  4. let unsubscribe = store.Subscribe(consoleLogSubscriber)
  5. store.Dispatch (Increment { Amount = 1 }) |> ignore
  6. store.Dispatch (Increment { Amount = 2 }) |> ignore
  7. store.Dispatch (Decrement { Amount = 1 }) |> ignore
  8. unsubscribe() |> ignore
  9. store.Dispatch (Decrement { Amount = 1 }) |> ignore
  10. let lastState = store.GetState()
  11. printfn "Expected Current value should be 1"
  12. printfn "Actual Current value is %d" lastState.CurrentValue
  13. 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