Peekaboo/peekaboo-cli/Sources/peekaboo/AsyncUtils.swift
Peter Steinberger 50984f8dc2 Fix async concurrency issues without semaphores
- Replace problematic DispatchSemaphore usage with NSCondition-based async bridge
- Revert to ParsableCommand for compatibility while maintaining async operations
- Use CGWindowListCopyWindowInfo for sync permission checking instead of async ScreenCaptureKit
- Remove all RunLoop workarounds in favor of proper Task.runBlocking pattern
- Eliminate all deadlock sources while preserving async capture functionality
2025-06-08 10:10:04 +01:00

33 lines
No EOL
1,009 B
Swift

import Foundation
extension Task where Success == Void, Failure == Never {
/// Runs an async operation synchronously by blocking the current thread.
/// This is a safer alternative to using DispatchSemaphore with Swift concurrency.
static func runBlocking<T>(operation: @escaping () async throws -> T) throws -> T {
var result: Result<T, Error>?
let condition = NSCondition()
Task {
do {
let value = try await operation()
condition.lock()
result = .success(value)
condition.signal()
condition.unlock()
} catch {
condition.lock()
result = .failure(error)
condition.signal()
condition.unlock()
}
}
condition.lock()
while result == nil {
condition.wait()
}
condition.unlock()
return try result!.get()
}
}