Handle task cancellation in MastodonController.run

This commit is contained in:
Shadowfacts 2022-11-02 23:00:29 -04:00
parent b47876dc3d
commit 7bdbd9f71a
2 changed files with 9 additions and 1 deletions

View File

@ -68,7 +68,7 @@ class MastodonController: ObservableObject {
}
func run<Result>(_ request: Request<Result>) async throws -> (Result, Pagination?) {
return try await withCheckedThrowingContinuation({ continuation in
let result: (Result, Pagination?) = try await withCheckedThrowingContinuation({ continuation in
client.run(request) { response in
switch response {
case .failure(let error):
@ -78,6 +78,8 @@ class MastodonController: ObservableObject {
}
}
})
try Task.checkCancellation()
return result
}
/// - Returns: A tuple of client ID and client secret.

View File

@ -65,6 +65,8 @@ actor TimelineLikeController<Item> {
await loadingIndicator.end()
await emit(event: .replaceAllItems(items, token))
state = .idle
} catch is CancellationError {
return
} catch {
await loadingIndicator.end()
await emit(event: .loadAllError(error, token))
@ -85,6 +87,8 @@ actor TimelineLikeController<Item> {
}
await emit(event: .prependItems(items, token))
state = .idle
} catch is CancellationError {
return
} catch {
await emit(event: .loadNewerError(error, token))
state = .idle
@ -113,6 +117,8 @@ actor TimelineLikeController<Item> {
await loadingIndicator.end()
await emit(event: .appendItems(items, token))
state = .idle
} catch is CancellationError {
return
} catch {
await loadingIndicator.end()
await emit(event: .loadOlderError(error, token))