ReSwiftThunk 2.0.1

ReSwiftThunk 2.0.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2020年11月
SPM支持 SPM

Mike ColeBenjamin EnczMalcolm JarvisKarl BowdenChristian Tietze 维护。



  • ReSwift

ReSwift-Thunk

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

支持的 Swift 版本: Swift 4.2, Swift 5.0, Swift 5.3

当 ReSwift 是一个 Swift 中单向数据流架构的实现,类似于 Redux 时,ReSwift-Thunk 是类似于 redux-thunk。

为什么使用 ReSwift-Thunk?

示例

// First, you create the middleware, which needs to know the type of your `State`.
let thunkMiddleware: Middleware<MyState> = createThunkMiddleware()

// Note that it can perfectly live with other middleware in the chain.
let store = Store<MyState>(reducer: reducer, state: nil, middleware: [thunkMiddleware])

// 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.
let thunk = Thunk<MyState> { dispatch, getState in 
    if getState!.loading {
        return
    }
    dispatch(RequestStart())
    api.getSomething() { something in
        if something != nil {
            dispatch(RequestSuccess(something))
        } else {
            dispatch(RequestError())
        }
    }
}

// A thunk can also be a function if you want to pass on parameters
func thunkWithParams(_ identifier: Int) -> Thunk<MyState> {
    return Thunk<MyState> { dispatch, getState in
        guard let state = getState() else { return }
        
        if state.loading {
            return
        }
        
        api.getSomethingWithId(identifier) { something in
            if something != nil {
                dispatch(RequestSuccess(something))
            } else {
                dispatch(RequestError())
            }
        }
    }
}

// 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.
store.dispatch(thunk)

// You can do the same with the Thunk that requires parameters, like so
store.dispatch(thunkWithParams(10))

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

测试

作为 CocoaPods subspec 提供的 ExpectThunk 辅助程序,允许测试 dispatch 的顺序和操作,以及 getState 的依赖。

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

安装

ReSwift-Thunk 需要 ReSwift 基础模块。

CocoaPods

您可以通过将 ReSwift-Thunk 添加到您的 Podfile 来使用 CocoaPods 进行安装。

target 'TARGET' do
    pod 'ReSwiftThunk'
end

target 'TARGET-TESTS' do
    pod 'ReSwiftThunk/ExpectThunk'
end

然后运行 pod install

关于包含 ExpectThunk 的注意事项

如果使用 ExpectThunk 子规格,由于当前的限制,测试目标不能嵌套在其他目标中。测试目标必须像上面的代码片段中所示的那样是单独的目标。

Carthage

您可以通过将以下行添加到您的 Cartfile 来使用 Carthage 安装 ReSwift-Thunk:[Carthage](https://github.com/Carthage/Carthage)

github "ReSwift/ReSwift-Thunk"

Swift 包管理器

您可以通过将以下行添加到您的 Package.swift 来使用 Swift 包管理器安装 ReSwift-Thunk:[Swift 包管理器](https://swift.org/package-manager/)

import PackageDescription

let package = Package(
    [...]
    dependencies: [
        .Package(url: "https://github.com/ReSwift/ReSwift-Thunk.git", majorVersion: XYZ)
    ]
)

检出源代码

检出项目后,运行 pod install 以获取支持的最新版本的 SwiftLint,我们使用它来确保代码库中的风格一致。

示例项目

  • ReduxMovieDB:是一个简单应用,用于查询tmdb.org API以显示最新电影。允许搜索和查看详情。相关文件

贡献

有关如何开始的详细信息,请参阅贡献指南

致谢

许可

ReSwift-Thunk 版权 (c) 2018 ReSwift 贡献者。根据 MIT 许可证 (MIT) 分发。见 LICENSE.md