Add shared async timeout helper for AX operations

This commit is contained in:
Peter Steinberger 2025-11-19 02:53:24 +01:00
parent 89cbe7a274
commit 39e300e216

View File

@ -94,3 +94,42 @@ public struct AXTimeoutWrapper {
return nil
}
}
public enum AXTimeoutHelper {
/// Async timeout helper reused by downstreams (e.g., ScreenCaptureService)
/// to keep timeout logic in one place.
@MainActor
public static func withTimeout<T: Sendable>(
seconds: TimeInterval,
operation: @escaping @Sendable () async throws -> T) async throws -> T
{
try await withThrowingTaskGroup(of: T.self) { group in
group.addTask {
try await operation()
}
group.addTask {
try await Task.sleep(nanoseconds: UInt64(seconds * 1_000_000_000))
throw AXTimeoutError.operationTimedOut(duration: seconds)
}
guard let result = try await group.next() else {
throw AXTimeoutError.operationTimedOut(duration: seconds)
}
group.cancelAll()
return result
}
}
}
public enum AXTimeoutError: Error, Sendable, CustomStringConvertible {
case operationTimedOut(duration: TimeInterval)
public var description: String {
switch self {
case let .operationTimedOut(duration):
return "Operation timed out after \(duration)s"
}
}
}