feat: Adding more helpers, wip

This commit is contained in:
2024-05-30 10:00:04 -04:00
parent 4a85915ebe
commit ee983f7337
4 changed files with 345 additions and 31 deletions

View File

@@ -0,0 +1,51 @@
import ComposableArchitecture
import OSLog
public protocol ReceiveAction<ReceiveAction> {
associatedtype ReceiveAction
static func receive(_ result: TaskResult<ReceiveAction>) -> Self
var result: TaskResult<ReceiveAction>? { get }
}
extension ReceiveAction {
public var result: TaskResult<ReceiveAction>? {
AnyCasePath(unsafe: Self.receive).extract(from: self)
}
}
extension Effect where Action: ReceiveAction {
public static func receive(
_ operation: @escaping () async throws -> Action.ReceiveAction
) -> Self {
.run { send in
await send(.receive(
TaskResult { try await operation() }
))
}
}
public static func receive<T>(
_ operation: @escaping () async throws -> T,
transform: @escaping (T) -> Action.ReceiveAction
) -> Self {
.run { send in
await send(.receive(
TaskResult { try await operation() }
.map(transform)
))
}
}
public static func receive<T>(
_ toReceiveAction: CaseKeyPath<Action.ReceiveAction, T>,
_ operation: @escaping () async throws -> T
) -> Self {
return .receive(operation) {
AnyCasePath(toReceiveAction).embed($0)
}
}
}