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

commonMain.com.arkivanov.mvikotlin.rx.internal.Serializer.kt Maven / Gradle / Ivy

package com.arkivanov.mvikotlin.rx.internal

internal class Serializer(
    private val onValue: (T) -> Unit
) {

    private val lock = Lock()
    private val queue = ArrayDeque()
    private var isDraining = false

    fun onNext(value: T) {
        lock.synchronized {
            queue.addLast(value)

            if (isDraining) {
                return
            }

            isDraining = true
        }

        drain()
    }

    private fun drain() {
        while (true) {
            val value =
                lock.synchronized {
                    if (queue.isEmpty()) {
                        isDraining = false
                        return
                    }

                    queue.removeFirst()
                }

            onValue(value)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy