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

jsMain.dev.inmo.micro_utils.coroutines.ElementVisibilityFlow.kt Maven / Gradle / Ivy

package dev.inmo.micro_utils.coroutines

import dev.inmo.micro_utils.common.onRemoved
import dev.inmo.micro_utils.common.onVisibilityChanged
import kotlinx.coroutines.flow.*
import org.w3c.dom.Element

fun Element.visibilityFlow(): Flow = channelFlow {
    var previousData: Boolean? = null

    val observer = onVisibilityChanged { intersectionRatio, _ ->
        val currentData = intersectionRatio > 0
        if (currentData != previousData) {
            trySend(currentData)
        }
        previousData = currentData
    }

    val removeObserver = onRemoved {
        observer.disconnect()
        close()
    }

    invokeOnClose {
        observer.disconnect()
        removeObserver.disconnect()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy