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

org.jitsi.utils.event.EventEmitter.kt Maven / Gradle / Ivy

There is a newer version: 1.0-133-g6af1020
Show newest version
/*
 * Copyright @ 2020 - present 8x8, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jitsi.utils.event

import org.jitsi.utils.logging2.createLogger
import java.lang.Exception
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.Executor

interface EventEmitter {
    /** Fire an event (it may be fired synchronously or asynchronously depending on the implementation). */
    fun fireEvent(event: EventHandlerType.() -> Unit)
    fun addHandler(handler: EventHandlerType)
    fun removeHandler(handler: EventHandlerType)
    val eventHandlers: List
}

sealed class BaseEventEmitter(
    initialHandlers: List
) : EventEmitter {
    private val logger = createLogger()

    override val eventHandlers: MutableList = CopyOnWriteArrayList(initialHandlers)

    override fun addHandler(handler: EventHandlerType) {
        eventHandlers += handler
    }

    override fun removeHandler(handler: EventHandlerType) {
        eventHandlers -= handler
    }

    protected fun wrap(block: () -> Unit) {
        try {
            block()
        } catch (e: Exception) {
            logger.error("Exception from event handler: ${e.message}", e)
        }
    }
}

/** An [EventEmitter] which fires events synchronously. */

class SyncEventEmitter(initialHandlers: List) :
    BaseEventEmitter(initialHandlers) {

    constructor() : this(emptyList())

    override fun fireEvent(event: EventHandlerType.() -> Unit) {
        eventHandlers.forEach { wrap { it.apply(event) } }
    }
}

/** An [EventEmitter] which fires events asynchronously. */
class AsyncEventEmitter(
    private val executor: Executor,
    initialHandlers: List = emptyList()
) : BaseEventEmitter(initialHandlers) {

    constructor(executor: Executor) : this(executor, emptyList())

    override fun fireEvent(event: EventHandlerType.() -> Unit) {
        eventHandlers.forEach {
            executor.execute { wrap { it.apply(event) } }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy