oftentimes, i find myself trying to “wrap” an AsyncSequence
(usually, an AsyncStream
) with another AsyncStream
that i want to inject some additional events into. the outer stream should never outlive the inner stream.
var c:AsyncThrowingStream<Event, any Error>.Continuation? = nil
let s:AsyncThrowingStream<Event, any Error> = .init { c = $0 }
guard
let c:AsyncThrowingStream<Event, any Error>.Continuation
else
{
fatalError("unreachable")
}
i end up writing something like:
async
let _:Void =
{
do
{
for try await message:Message in $1
{
c.yield(.message(message))
}
$0.finish()
}
catch let error
{
$0.finish(throwing: error)
}
} (c, messagesInbound)
but i could not find any examples of this pattern in the swift-async-algorithms
package. is this a good idea, and if not, what should i be doing instead?