-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(session): Async/await support (#4)
- Loading branch information
Showing
12 changed files
with
150 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Foundation | ||
import Combine | ||
|
||
#if canImport(_Concurrency) | ||
|
||
extension Session { | ||
public func response<Output: Decodable>(for request: Request<Output>) async throws -> Output { | ||
try await response(publisher: publisher(for: request)) | ||
} | ||
|
||
public func response(for request: Request<Void>) async throws { | ||
try await response(publisher: publisher(for: request)) | ||
} | ||
|
||
private func response<Output>(publisher: AnyPublisher<Output, Error>) async throws -> Output { | ||
var cancellable: Set<AnyCancellable> = [] | ||
let onCancel = { cancellable.removeAll() } | ||
|
||
return try await withTaskCancellationHandler( | ||
handler: { onCancel() }, | ||
operation: { | ||
try await withCheckedThrowingContinuation { continuation in | ||
publisher | ||
.sink( | ||
receiveCompletion: { | ||
if case let .failure(error) = $0 { | ||
return continuation.resume(throwing: error) | ||
} | ||
}, | ||
receiveValue: { | ||
continuation.resume(returning: $0) | ||
}) | ||
.store(in: &cancellable) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.