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

commonMain.jetbrains.datalore.base.observable.event.CompositeEventSource.kt Maven / Gradle / Ivy

There is a newer version: 4.5.3-alpha1
Show newest version
/*
 * Copyright (c) 2019. JetBrains s.r.o.
 * Use of this source code is governed by the MIT license that can be found in the LICENSE file.
 */

package jetbrains.datalore.base.observable.event

import jetbrains.datalore.base.registration.Registration

internal class CompositeEventSource : EventSource {
    private var myHandlers: Listeners>? = null
    private val myEventSources = ArrayList>()
    private val myRegistrations = ArrayList()

    constructor(vararg sources: EventSource) {
        for (s in sources) {
            add(s)
        }
    }

    constructor(sources: Iterable>) {
        for (s in sources) {
            add(s)
        }
    }

    fun add(source: EventSource) {
        myEventSources.add(source)
    }

    fun remove(source: EventSource) {
        myEventSources.remove(source)
    }

    override fun addHandler(handler: EventHandler): Registration {
        if (myHandlers == null) {
            myHandlers = object : Listeners>() {
                override fun beforeFirstAdded() {
                    for (src in myEventSources) {
                        addHandlerTo(src)
                    }
                }

                override fun afterLastRemoved() {
                    for (hr in myRegistrations) {
                        hr.remove()
                    }
                    myRegistrations.clear()
                    myHandlers = null
                }
            }
        }
        return myHandlers!!.add(handler)
    }

    private fun  addHandlerTo(src: EventSource) {
        myRegistrations.add(src.addHandler(object : EventHandler {
            override fun onEvent(event: PartEventT) {
                myHandlers!!.fire(object : ListenerCaller> {
                    override fun call(l: EventHandler) {
                        l.onEvent(event)
                    }
                })
            }
        }))
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy