Skip to content

Commit

Permalink
Check cancellation for task
Browse files Browse the repository at this point in the history
  • Loading branch information
junjielu committed Jan 16, 2025
1 parent 48795d9 commit 23eb7bc
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Sources/ComposableArchitecture/Effects/Cancellation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ extension Effect {
if cancelInFlight {
$0.cancel(id: id, path: navigationIDPath)
}
let task = Task { try await operation() }
let task = Task {
let result = try await operation()
try Task.checkCancellation()
return result
}
let cancellable = AnyCancellable { task.cancel() }
$0.insert(cancellable, at: id, path: navigationIDPath)
return (cancellable, task)
Expand Down Expand Up @@ -209,7 +213,11 @@ extension Effect {
if cancelInFlight {
$0.cancel(id: id, path: navigationIDPath)
}
let task = Task { try await operation() }
let task = Task {
let result = try await operation()
try Task.checkCancellation()
return result
}
let cancellable = AnyCancellable { task.cancel() }
$0.insert(cancellable, at: id, path: navigationIDPath)
return (cancellable, task)
Expand Down

0 comments on commit 23eb7bc

Please sign in to comment.