commonMain.net.folivo.trixnity.client.store.cache.ConcurrentObservableSet.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of trixnity-client-jvm Show documentation
Show all versions of trixnity-client-jvm Show documentation
Multiplatform Kotlin SDK for matrix-protocol
package net.folivo.trixnity.client.store.cache
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.map
import net.folivo.trixnity.utils.concurrentOf
class ConcurrentObservableSet(
initialValue: Set = setOf(),
) {
private val _values = concurrentOf, MutableSet> { initialValue.toMutableSet() }
private val changeSignal = MutableSharedFlow(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
init {
changeSignal.tryEmit(Unit)
}
val values = changeSignal
.conflate()
.map { _values.read { toSet() } }
suspend fun add(element: T): Boolean = _values.write {
add(element)
}.also { if (it) changeSignal.emit(Unit) }
suspend fun remove(element: T): Boolean = _values.write {
remove(element)
}.also { if (it) changeSignal.emit(Unit) }
suspend fun removeAll() = _values.write {
clear()
}.also { changeSignal.emit(Unit) }
suspend fun size(): Int = _values.read { size }
}