-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Default async/await implementation (#37)
* Add default implementation async/await methods in Client. AsyncAwaitHelper added. * fix intents. used resume with Result<>. self use removed
- Loading branch information
Showing
4 changed files
with
54 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// AsyncAwaitHelper.swift | ||
// | ||
// | ||
// Created by Aleksei Tiurnin on 31.01.2022. | ||
// | ||
|
||
import Foundation | ||
|
||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) | ||
public enum AsyncAwaitHelper { | ||
public typealias ContentContinuation<T> = CheckedContinuation<T, Error> | ||
|
||
public static func adaptToAsync<T>(dataTaskClosure: (ContentContinuation<T>) -> Progress) async throws -> T { | ||
let progressWrapper = ProgressWrapper() | ||
return try await withTaskCancellationHandler(handler: { | ||
progressWrapper.cancel() | ||
}, operation: { | ||
try await withCheckedThrowingContinuation { (continuation: ContentContinuation<T>) in | ||
let progress = dataTaskClosure(continuation) | ||
progressWrapper.progress = progress | ||
} | ||
}) | ||
} | ||
} |
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