Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Map enhancement (#220)
Browse files Browse the repository at this point in the history
## Description

<!--- 
Please include a summary of the change and which issue is fixed. Please
also include relevant motivation and context.
-->

Fixes #190 

## Type of change

<!--- Please delete options that are not relevant. -->

- [x] New feature (non-breaking change which adds functionality)

## Checklist

<!--- Please delete options that are not relevant. -->

- [x] My code follows the code style of this project.
- [x] All new and existing tests passed.
  • Loading branch information
o-nnerb authored Aug 22, 2023
1 parent 7d99c2d commit db55220
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension Internals {

enum Output {
case task(SessionTask)
case cache(((Internals.ResponseHead) -> Internals.AsyncStream<Internals.DataBuffer>?)?)
case cache((@Sendable (Internals.ResponseHead) -> Internals.AsyncStream<Internals.DataBuffer>?)?)
}

// MARK: - Internal properties
Expand Down Expand Up @@ -258,7 +258,7 @@ extension Internals {
private func cacheIfNeeded(
dataCache: DataCache,
request: Internals.Request
) async throws -> ((Internals.ResponseHead) -> Internals.AsyncStream<Internals.DataBuffer>?)? {
) async throws -> (@Sendable (Internals.ResponseHead) -> Internals.AsyncStream<Internals.DataBuffer>?)? {
guard request.isCacheEnabled else {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Modifiers {

// MARK: - Internal properties

let transform: @Sendable (Error) throws -> Void
let transform: @Sendable (Error) async throws -> Void

// MARK: - Public methods

Expand All @@ -34,7 +34,7 @@ extension Modifiers {
do {
return try await task.result()
} catch {
try transform(error)
try await transform(error)
throw error
}
}
Expand Down Expand Up @@ -65,7 +65,7 @@ extension RequestTask {
`transform` closure.
*/
public func flatMapError(
_ transform: @escaping @Sendable (Error) throws -> Void
_ transform: @escaping @Sendable (Error) async throws -> Void
) -> ModifiedRequestTask<Modifiers.FlatMapError<Element>> {
modifier(Modifiers.FlatMapError(transform: transform))
}
Expand Down Expand Up @@ -94,11 +94,11 @@ extension RequestTask {
*/
public func flatMapError<Failure: Error>(
_ type: Failure.Type,
_ transform: @escaping @Sendable (Failure) throws -> Void
_ transform: @escaping @Sendable (Failure) async throws -> Void
) -> ModifiedRequestTask<Modifiers.FlatMapError<Element>> {
flatMapError {
if let error = $0 as? Failure {
try transform(error)
try await transform(error)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension Modifiers {

// MARK: - Internal properties

let transform: @Sendable (Result<Input, Error>) throws -> Output
let transform: @Sendable (Result<Input, Error>) async throws -> Output

// MARK: - Public methods

Expand All @@ -48,7 +48,7 @@ extension Modifiers {
case .processed(let mapped):
return mapped
case .original(let original):
return try transform(.success(original))
return try await transform(.success(original))
}
}
}
Expand All @@ -59,13 +59,13 @@ extension Modifiers {
do {
return .success(.original(try await task.result()))
} catch {
return mapErrorIntoResult(error)
return await mapErrorIntoResult(error)
}
}

private func mapErrorIntoResult(_ error: Error) -> Result<Map, Error> {
private func mapErrorIntoResult(_ error: Error) async -> Result<Map, Error> {
do {
return .success(.processed(try transform(.failure(error))))
return try await .success(.processed(transform(.failure(error))))
} catch {
return .failure(error)
}
Expand All @@ -85,7 +85,7 @@ extension RequestTask {
- Returns: A new `RequestTask` that returns the flat-mapped element of type `NewElement`.
*/
public func flatMap<NewElement>(
_ transform: @escaping @Sendable (Result<Element, Error>) throws -> NewElement
_ transform: @escaping @Sendable (Result<Element, Error>) async throws -> NewElement
) -> ModifiedRequestTask<Modifiers.FlatMap<Element, NewElement>> {
modifier(Modifiers.FlatMap(transform: transform))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension Modifiers {

// MARK: - Internal properties

let transform: @Sendable (Error) throws -> Input
let transform: @Sendable (Error) async throws -> Input

// MARK: - Public methods

Expand All @@ -26,7 +26,7 @@ extension Modifiers {
do {
return try await task.result()
} catch {
return try transform(error)
return try await transform(error)
}
}
}
Expand All @@ -43,7 +43,7 @@ extension RequestTask {
- Returns: The modified task with the ``Modifiers/MapError`` modifier applied.
*/
public func mapError(
_ transform: @escaping @Sendable (Error) throws -> Element
_ transform: @escaping @Sendable (Error) async throws -> Element
) -> ModifiedRequestTask<Modifiers.MapError<Element>> {
modifier(Modifiers.MapError(transform: transform))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension Modifiers {

// MARK: - Internal properties

let transform: @Sendable (Input) throws -> Output
let transform: @Sendable (Input) async throws -> Output

// MARK: - Public methods

Expand All @@ -41,7 +41,7 @@ extension Modifiers {
- Throws: The error thrown by the closure, if any.
*/
public func body(_ task: Content) async throws -> Output {
try transform(await task.result())
try await transform(task.result())
}
}
}
Expand All @@ -57,7 +57,7 @@ extension RequestTask {
- Returns: A modified `RequestTask` with the transformed element.
*/
public func map<NewElement>(
_ transform: @escaping @Sendable (Element) throws -> NewElement
_ transform: @escaping @Sendable (Element) async throws -> NewElement
) -> ModifiedRequestTask<Modifiers.Map<Element, NewElement>> {
modifier(Modifiers.Map(transform: transform))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension Modifiers {
// MARK: - Internal properties

let contains: @Sendable (StatusCode) -> Bool
let transform: @Sendable (Input) throws -> Void
let transform: @Sendable (Input) async throws -> Void

// MARK: - Public methods

Expand All @@ -39,7 +39,7 @@ extension Modifiers {
return result
}

try transform(result)
try await transform(result)
return result
}
}
Expand All @@ -50,7 +50,7 @@ extension Modifiers {
extension RequestTask where Element: TaskResultPrimitive {

private func onStatusCode(
_ transform: @escaping @Sendable (Element) throws -> Void,
_ transform: @escaping @Sendable (Element) async throws -> Void,
contains: @escaping @Sendable (StatusCode) -> Bool
) -> ModifiedRequestTask<Modifiers.OnStatusCode<Element>> {
modifier(Modifiers.OnStatusCode(
Expand All @@ -72,7 +72,7 @@ extension RequestTask where Element: TaskResultPrimitive {
*/
public func onStatusCode(
_ statusCode: Range<StatusCode>,
_ transform: @escaping @Sendable (Element) throws -> Void
_ transform: @escaping @Sendable (Element) async throws -> Void
) -> ModifiedRequestTask<Modifiers.OnStatusCode<Element>> {
onStatusCode(transform) {
statusCode.contains($0)
Expand All @@ -92,7 +92,7 @@ extension RequestTask where Element: TaskResultPrimitive {
*/
public func onStatusCode(
_ statusCode: StatusCodeSet,
_ transform: @escaping @Sendable (Element) throws -> Void
_ transform: @escaping @Sendable (Element) async throws -> Void
) -> ModifiedRequestTask<Modifiers.OnStatusCode<Element>> {
onStatusCode(transform) {
statusCode.contains($0)
Expand All @@ -112,7 +112,7 @@ extension RequestTask where Element: TaskResultPrimitive {
*/
public func onStatusCode(
_ statusCode: StatusCode,
_ transform: @escaping @Sendable (Element) throws -> Void
_ transform: @escaping @Sendable (Element) async throws -> Void
) -> ModifiedRequestTask<Modifiers.OnStatusCode<Element>> {
onStatusCode(transform) {
statusCode == $0
Expand Down

0 comments on commit db55220

Please sign in to comment.