feat: Adds TCA Helpers, some are wip.

This commit is contained in:
2024-05-29 09:00:58 -04:00
parent 24211187bf
commit b7edf38e72
4 changed files with 24 additions and 68 deletions

View File

@@ -1,7 +1,7 @@
import ComposableArchitecture
public protocol ReceiveAction<ReceiveAction> {
associatedtype ReceiveAction
public protocol ReceiveAction<ReceiveAction>: CasePathable {
associatedtype ReceiveAction: CasePathable
static func receive(_ result: TaskResult<ReceiveAction>) -> Self
}

View File

@@ -1,51 +0,0 @@
import ComposableArchitecture
import OSLog
public struct _ReceiveFailureReducer<State, Action>: Reducer {
@usableFromInline
let toReceiveActionError: (Action) -> Error?
@usableFromInline
let logger: Logger
@inlinable
public init<ViewAction: ReceiveAction>(
action toReceiveAction: CaseKeyPath<Action, ViewAction>,
logger: Logger
) {
self.init(
internal: { action in
if case let .receive(.failure(error)) = AnyCasePath(toReceiveAction).extract(from: action) {
return error
}
return nil
},
logger: logger
)
}
@usableFromInline
init(internal toReceiveActionError: @escaping (Action) -> Error?, logger: Logger) {
self.toReceiveActionError = toReceiveActionError
self.logger = logger
}
@inlinable
public func reduce(
into state: inout State,
action: Action
) -> Effect<Action> {
guard let error = toReceiveActionError(action) else { return .none }
return .fail(error: error, logger: logger)
}
}
extension Reducer where Action: ReceiveAction, Action: CasePathable {
public func onFail(_ logger: Logger) -> _ReceiveFailureReducer<State, Action, Action> {
_ReceiveFailureReducer(
action: \.receive,
logger: logger
)
}
}