So using an AsyncStream with continuations, by capturing the continuation (which is expressly allowed in this case) I have an easy means of pushing, over time, data into the sequence:
struct S {
static var cc: AsyncStream<Int>.Continuation!
static var stream1 = AsyncStream<Int> { continuation in
Self.cc = continuation
}
static func push(value: Int) {
Self.continuation.yield(value)
}
Now I can create one or more tasks that begin to read values from my stream, in a nicely async fashion. Super!
However, the assignment Self.cc = continuation
isn't going to be called until the first time something tries to await a value from the sequence. It's entirely possible someone will call push() before anyone else tries to read from the sequence. Is there some way of forcing that closure to be executed right away, and not waiting for the first pull from the sequence?
I've verified that multiple different tasks can be reading from the sequence at the same time; this is a super-easy way of making a "task-safe" producer/consumer queue that is easily awaitable. (If I could just get the continuation to be ready right away).