jvmMain.io.mths.swing.flow.binding.FlowBindings.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swing-flow Show documentation
Show all versions of swing-flow Show documentation
Kotlin Flow integration for Swing.
The newest version!
package io.mths.swing.flow.binding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlin.reflect.KFunction1
import kotlin.reflect.KMutableProperty
context (CoroutineScope)
operator fun KFunction1.invoke(
supplyObservable: () -> Flow
) {
bindTo(supplyObservable())
}
context (CoroutineScope)
operator fun KMutableProperty.invoke(
supplyObservable: () -> Flow
) {
/**
* Since a call to KMutableProperty.Setter.call takes a vararg Any,
* a local function serves as a bridge.
*/
fun onChange(newValue: Type) {
setter.call(newValue)
}
::onChange bindTo supplyObservable()
}
context (CoroutineScope)
internal infix fun KFunction1.bindTo(
observable: Flow
) {
observable
.onEach(this)
.flowOn(Dispatchers.Default)
.launchIn(this@CoroutineScope)
}