Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncThrottleSequence/Fixed error propagation #309

Merged
merged 1 commit into from
May 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions Sources/Async/AsyncThrottleSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,35 +116,30 @@ extension AsyncThrottleSequence: AsyncSequence where C.Duration == Duration {
/// - Throws: An error if the underlying `AsyncSequence` throws an error.
/// - Returns: The next element in the sequence, or `nil` if there are no more elements.
public func next() async throws -> Element? {
var task: Task<Element?, Error>?

lock.withLock {
let task = lock.withLock {
taskInExecution?.cancel()
taskInExecution = nil
let taskA = Task {
try await Task.sleep(until: clock.now.advanced(by: delayBetweenTasks), clock: clock)
let task = Task {
try await Task.sleep(
until: clock.now.advanced(by: delayBetweenTasks),
clock: clock
)
let result = try await baseIterator.next()
do {
// If task was cancelled while a request was being awaited,
// return `nil`!
try Task.checkCancellation()
return result
} catch {
if error is CancellationError {
return nil
}
throw error
}
try Task.checkCancellation()
return result
}
task = taskA
taskInExecution = taskA
taskInExecution = task
return task
}
do {
return try await task?.value
return try await task.value
} catch {
return nil
if error is CancellationError {
return nil
}
throw error
}
}
}
}

/// Creates a new iterator for the `AsyncThrottleSequence`.
Expand Down