package expo.modules.kotlin.viewevent import android.view.View import java.lang.ref.WeakReference import kotlin.reflect.KProperty typealias CoalescingKey = (event: T) -> Short class ViewEventDelegate( view: View, private val coalescingKey: CoalescingKey? ) { private val viewHolder = WeakReference(view) fun getValue(property: KProperty<*>): ViewEventCallback { val view = viewHolder.get() ?: throw IllegalStateException("Can't send the '${property.name}' event from the view that is deallocated") return ViewEvent(property.name, view, coalescingKey) } operator fun getValue(thisRef: View, property: KProperty<*>): ViewEventCallback { return getValue(property) } } @Suppress("NOTHING_TO_INLINE") inline operator fun ViewEventDelegate.getValue(thisObj: Any?, property: KProperty<*>): ViewEventCallback = getValue(property) /** * Creates a reference to the js callback. * @param coalescingKey a key generator used to determine which other events of this type this event can be coalesced with. * For example, touch move events should only be coalesced within a single gesture so a coalescing key there would be the unique gesture id. */ @Suppress("FunctionName") inline fun View.EventDispatcher(noinline coalescingKey: CoalescingKey? = null): ViewEventDelegate { return ViewEventDelegate(this, coalescingKey) } @JvmName("MapEventDispatcher") @Suppress("FunctionName") fun View.EventDispatcher(coalescingKey: CoalescingKey>? = null): ViewEventDelegate> { return ViewEventDelegate(this, coalescingKey) }