com.fireflysource.common.coroutine.ChannelExtension.kt Maven / Gradle / Ivy
package com.fireflysource.common.coroutine
import kotlinx.coroutines.channels.Channel
import java.util.concurrent.atomic.AtomicBoolean
inline fun Channel.pollAll(crossinline block: (T) -> Unit) {
while (true) {
val message = this.tryReceive().getOrNull()
if (message != null) block(message)
else break
}
}
fun Channel.clear() {
this.pollAll { }
}
class Signal {
private val channel: Channel = Channel(1)
private val notified = AtomicBoolean(false)
suspend fun wait(): T = channel.receive()
fun notify(e: T) {
if (notified.compareAndSet(false, true)) {
channel.trySend(e)
}
}
fun reset() {
notified.set(false)
}
}