From 7bdbd9f71a9d1e01aad7181b3f9bd0940d7003af Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 2 Nov 2022 23:00:29 -0400 Subject: [PATCH] Handle task cancellation in MastodonController.run --- Tusker/API/MastodonController.swift | 4 +++- Tusker/TimelineLikeController.swift | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Tusker/API/MastodonController.swift b/Tusker/API/MastodonController.swift index aec9107c..3b1c7584 100644 --- a/Tusker/API/MastodonController.swift +++ b/Tusker/API/MastodonController.swift @@ -68,7 +68,7 @@ class MastodonController: ObservableObject { } func run(_ request: Request) 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. diff --git a/Tusker/TimelineLikeController.swift b/Tusker/TimelineLikeController.swift index 591e4c99..525e5d87 100644 --- a/Tusker/TimelineLikeController.swift +++ b/Tusker/TimelineLikeController.swift @@ -65,6 +65,8 @@ actor TimelineLikeController { 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 { } 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 { 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))