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

commonMain.MergedEventSource.kt Maven / Gradle / Ivy

package kt.mobius

import kt.mobius.disposables.Disposable
import kt.mobius.functions.Consumer
import kotlin.js.JsName
import kotlin.jvm.JvmStatic

/**
 * An [EventSource] that merges multiple sources into one
 *
 * @param E The type of Events the sources will emit
 */
class MergedEventSource private constructor(
    private val eventSources: List>
) : EventSource {

    override fun subscribe(eventConsumer: Consumer): Disposable {
        val disposables = ArrayList(eventSources.size)
        for (eventSource in eventSources) {
            disposables.add(eventSource.subscribe(eventConsumer))
        }

        return Disposable {
            for (disposable in disposables) {
                disposable.dispose()
            }
        }
    }

    companion object {
        @JvmStatic
        @JsName("from")
        fun  from(vararg eventSources: EventSource): EventSource {
            val allSources = ArrayList>()
            allSources.addAll(eventSources)
            return MergedEventSource(allSources)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy