Experimenting with Swift's concurrency, I would like to have a clean API for exposing an async sequence of a given element type and a throttled version of the same:
var intStream: AsyncStream<Int> {
AsyncStream<Int>(Int.self, bufferingPolicy: .bufferingNewest(5)) { continuation in
Task.detached {
for _ in 0..<100 {
try await Task.sleep(nanoseconds: 1 * 1_000_000_000)
continuation.yield(Int.random(in: 1...10))
}
continuation.finish()
}
}
}
var throttledIntStream: AsyncStream<Int> {
intStream.throttle(for: .seconds(2))
}
But this does not work as throttle returns its own type:
Error:
Cannot convert return expression of type 'AsyncThrottleSequence<AsyncStream<Int>, ContinuousClock, Int>' to return type 'AsyncStream<Int>'
To get type erasure I could do
var throttledIntStream: some AsyncSequence {
intStream.debounce(for: Duration.seconds(2))
}
but then I lose the element type information as well, which I would like to keep.
Any suggestions how to best solve that?
Edit: This is pointing to the solution I want, but I guess I will need to wait https://forums.swift.org/t/anyasyncsequence/50828/2