feat: Adds TCA Helpers, some are wip.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user