All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fireflysource.common.coroutine.ChannelExtension.kt Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
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)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy