https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

Joe

02/27/2018, 5:09 PM
Thanks for your help!
b

bj0

02/27/2018, 5:55 PM
looks similar to an extension function i use:
Copy code
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")
}
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?
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.
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!
4 Views