项目作者: ReSwift

项目描述 :
Thunk middleware for ReSwift.
高级语言: Swift
项目地址: git://github.com/ReSwift/ReSwift-Thunk.git
创建时间: 2018-10-25T15:25:07Z
项目社区:https://github.com/ReSwift/ReSwift-Thunk

开源协议:MIT License

下载


ReSwift-Thunk

Build Status
Code coverage status
CocoaPods Compatible
Platform support
License MIT Reviewed by Hound

Supported Swift Versions: Swift 4.2, Swift 5.0, Swift 5.3

When ReSwift is a Redux-like implementation of the unidirectional data flow architecture in Swift, ReSwift-Thunk is like redux-thunk.

Why Use ReSwift-Thunk?

Example

  1. // First, you create the middleware, which needs to know the type of your `State`.
  2. let thunkMiddleware: Middleware<MyState> = createThunkMiddleware()
  3. // Note that it can perfectly live with other middleware in the chain.
  4. let store = Store<MyState>(reducer: reducer, state: nil, middleware: [thunkMiddleware])
  5. // A thunk represents an action that can perform side effects, access the current state of the store, and dispatch new actions, as if it were a ReSwift middleware.
  6. let thunk = Thunk<MyState> { dispatch, getState in
  7. if getState!.loading {
  8. return
  9. }
  10. dispatch(RequestStart())
  11. api.getSomething() { something in
  12. if something != nil {
  13. dispatch(RequestSuccess(something))
  14. } else {
  15. dispatch(RequestError())
  16. }
  17. }
  18. }
  19. // A thunk can also be a function if you want to pass on parameters
  20. func thunkWithParams(_ identifier: Int) -> Thunk<MyState> {
  21. return Thunk<MyState> { dispatch, getState in
  22. guard let state = getState() else { return }
  23. if state.loading {
  24. return
  25. }
  26. api.getSomethingWithId(identifier) { something in
  27. if something != nil {
  28. dispatch(RequestSuccess(something))
  29. } else {
  30. dispatch(RequestError())
  31. }
  32. }
  33. }
  34. }
  35. // As the thunk type conforms to the `Action` protocol, you can dispatch it as usual, without having to implement an overload of the `dispatch` function inside the ReSwift library.
  36. store.dispatch(thunk)
  37. // You can do the same with the Thunk that requires parameters, like so
  38. store.dispatch(thunkWithParams(10))
  39. // Note that these actions won't reach the reducers, instead, the thunks middleware will catch it and execute its body, producing the desired side effects.

Testing

The ExpectThunk helper, available as a CocoaPods subspec, allows for testing the order and actions of dispatch as well as the
dependencies on getState.

  1. ExpectThunk(thunk)
  2. .getsState(RequestState(loading: false))
  3. // If the action is Equatable it will be asserted for equality with `dispatches`.
  4. .dispatches(RequestStart())
  5. .dispatches { action in
  6. XCTAssert(action.something == expectedSomething)
  7. }
  8. .wait() // or simply run() for synchronous flows

Installation

ReSwift-Thunk requires the ReSwift base module.

CocoaPods

You can install ReSwift-Thunk via CocoaPods by adding it to your Podfile:

  1. target 'TARGET' do
  2. pod 'ReSwiftThunk'
  3. end
  4. target 'TARGET-TESTS' do
  5. pod 'ReSwiftThunk/ExpectThunk'
  6. end

And run pod install.

A Note on Including ExpectThunk

If the ExpectThunk subspec is used, the tests target cannot be nested in another target due to current limitations. The tests target must
be a standalone target as shown in the snippet above.

Carthage

You can install ReSwift-Thunk via Carthage by adding the following line to your Cartfile:

  1. github "ReSwift/ReSwift-Thunk"

Swift Package Manager

You can install ReSwift-Thunk via Swift Package Manager by adding the following line to your Package.swift:

  1. import PackageDescription
  2. let package = Package(
  3. [...]
  4. dependencies: [
  5. .Package(url: "https://github.com/ReSwift/ReSwift-Thunk.git", majorVersion: XYZ)
  6. ]
  7. )

Import testing support by importing ReSwiftThunkTesting

  1. import ReSwiftThunkTesting

Checking out Source Code

After checking out the project run pod install to get the latest supported version of SwiftLint, which we use to ensure a consistent style in the codebase.

Example Projects

  • ReduxMovieDB: A simple App that queries the tmdb.org API to display the latest movies. Allows searching and viewing details. Relevant file.

Contributing

You can find all the details on how to get started in the Contributing Guide.

Credits

License

ReSwift-Thunk Copyright (c) 2018 ReSwift Contributors. Distributed under the MIT License (MIT). See LICENSE.md.