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

jvmMain.io.mths.swing.flow.binding.FlowBindings.kt Maven / Gradle / Ivy

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)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy