Point-Free swift-composable-architecture 的一个配套库。
在当前的 TCA 中,TaskResult 不符合 Equatable 协议,所以必须这样写:
public struct HogeFeature: Reducer {
public struct State: Equatable {}
public enum Action: Equatable {
case onAppear
case response(TaskResult<EquatableVoid>)
}
public func reduce(into state: inout State, action: Action) -> Effect<Action> {
switch action {
case .onAppear:
return .run { send in
await send(
.response(
TaskResult {
try await asyncThrowsVoidFunc()
return VoidSuccess()
}
)
)
}
case .response(.success):
// do something
return .none
case .response(.failure(let error)):
// handle error
return .none
}
}
}
即使 Swift 中已经提供了 Void
,为了让 TaskResult
遵循 Equatable
协议而专门创建 VoidSuccess
对用户并不友好。因此,我们扩展了 TaskResult,使其可以这样写:
public struct HogeFeature: Reducer {
public struct State: Equatable {}
public enum Action: Equatable {
case onAppear
case response(TaskResult<VoidSuccess>)
}
public func reduce(into state: inout State, action: Action) -> Effect<Action> {
switch action {
case .onAppear:
return .run { send in
await send(
.response(
TaskResult {
try await asyncThrowsVoidFunc()
}
)
)
}
case .response(.success):
// do something
return .none
case .response(.failure(let error)):
// handle error
return .none
}
}
}
在依赖项部分,添加
.package(url: "https://github.com/Ryu0118/swift-composable-architecture-extras", from: "1.0.0")
在每个模块中,添加
.target(
name: "MyModule",
dependencies: [
.product(name: "ComposableArchitectureExtras", package: "swift-composable-architecture-extras")
]
),