feat: Adds onFailure reducer.

This commit is contained in:
2024-05-31 17:40:03 -04:00
parent ab915f88db
commit 36fe70c688
6 changed files with 153 additions and 142 deletions

View File

@@ -24,7 +24,7 @@ import Foundation
/// @Dependency(\.logger) var logger
///
/// public var body: some ReducerOf<Self> {
/// ReceiveReducer(onFail: .fail(logger: logger)) { state, action in
/// ReceiveReducer { state, action in
/// // Handle the success cases by switching on the receive action.
/// switch action {
/// case let .numberFact(fact):
@@ -32,6 +32,8 @@ import Foundation
/// return .none
/// }
/// }
/// .onFail(.log(logger: logger))
///
/// ...
/// }
///
@@ -40,33 +42,26 @@ public struct ReceiveReducer<State, Action: ReceiveAction>: Reducer {
@usableFromInline
let toResult: (Action) -> TaskResult<Action.ReceiveAction>?
@usableFromInline
let onFail: OnFailAction<State, Action>
@usableFromInline
let onSuccess: (inout State, Action.ReceiveAction) -> Effect<Action>
@usableFromInline
init(
internal toResult: @escaping (Action) -> TaskResult<Action.ReceiveAction>?,
onFail: OnFailAction<State, Action>,
onSuccess: @escaping(inout State, Action.ReceiveAction) -> Effect<Action>
) {
self.toResult = toResult
self.onFail = onFail
self.onSuccess = onSuccess
}
@inlinable
public init(
onFail: OnFailAction<State, Action> = .ignore,
onSuccess: @escaping (inout State, Action.ReceiveAction) -> Effect<Action>
) {
self.init(
internal: {
AnyCasePath(unsafe: Action.receive).extract(from: $0)
},
onFail: onFail,
onSuccess: onSuccess
)
}
@@ -75,8 +70,8 @@ public struct ReceiveReducer<State, Action: ReceiveAction>: Reducer {
public func reduce(into state: inout State, action: Action) -> Effect<Action> {
guard let result = toResult(action) else { return .none }
switch result {
case let .failure(error):
return onFail(state: &state, error: error)
case .failure:
return .none
case let .success(value):
return onSuccess(&state, value)
}