suspend fun <T> BroadcastChannel<T>.waitUntil(value: T): T {
consume {
for (element in this)
if (element == value)
return value
}
throw CancellationException("channel closed before item received")
}
bj0
02/27/2018, 5:56 PM
actually yours will spinlock, you probably want to do something about that
j
Joe
02/27/2018, 6:28 PM
But
consume
will pop the element out of the queue, right?
Joe
02/27/2018, 6:28 PM
I’m aware that it could spin forever, but my view was that it was the consumer’s responsibility to timeout or throw it away or whatever.
b
bj0
02/27/2018, 6:33 PM
it's a broadcast channel, there's no problem with popping it out of (your subscription). It wont effect anyone else getting elements.
bj0
02/27/2018, 6:33 PM
spinning forever can cause really bad performance or app lockups, you should really avoid it
j
Joe
02/27/2018, 6:51 PM
Both good points, I'll take another look at it. Thanks!