package com.bitmovin.player.reactnative import com.bitmovin.player.api.event.Event import com.bitmovin.player.api.event.EventEmitter import kotlin.reflect.KClass private data class Subscription( val eventClass: KClass, val action: (E) -> Unit, ) /** * Attaches and detaches listener for the provided forwarding events on the current [EventEmitter] instance and * relays the received events together with their associated name to the provided event output. */ class EventRelay, T : Event>( /** * List of events that should be relayed and their associated name. */ forwardingEventClassesAndNameMapping: Map, String>, /** * Is called for every relayed event together with its associated name. */ private val eventOutput: (String, Event) -> Unit ) { private val eventListeners = forwardingEventClassesAndNameMapping.map { Subscription(it.key) { event -> eventOutput(it.value, event) } } /** * The [EventEmitter] for which the events are relayed. */ var eventEmitter: E? = null set(value) { field?.detachListeners(eventListeners) field = value value?.attachListeners(eventListeners) } } private fun EventEmitter.attachListeners(eventListener: List>) { eventListener.forEach { on(it.eventClass, it.action) } } private fun EventEmitter.detachListeners(eventListener: List>) { eventListener.forEach { off(it.eventClass, it.action) } }