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

androidMain.tech.skot.view.live.MediatorSKLiveData.kt Maven / Gradle / Ivy

There is a newer version: 1.2.9
Show newest version
package tech.skot.view.live

class MediatorSKLiveData(initialValue: D) : MutableSKLiveData(initialValue) {


    private val sources: MutableMap, SKLiveDataCommon<*>.AlwaysObserver> = mutableMapOf()


    fun  addSource(liveData: SKLiveData, onChanged: (s: S) -> Unit) {
        val observer = liveData.makeAlwaysObserver(onChanged)
        sources[liveData] = observer
        if (hasActiveObserver()) {
            observer.plug()
        }
    }

    fun  removeSource(liveData: SKLiveData) {
        sources.remove(liveData)?.unPug()
    }

    override fun onActive() {
        sources.values.forEach { it.plug() }
    }

    override fun onInactive() {
        sources.values.forEach { it.unPug() }
    }

}