android - 在 Kotlin 中处理事件总线的通用监听器

标签 android generics events kotlin jvm

我的目标是在 Kotlin 中实现非常简单的事件总线,无需任何第 3 方库。我用下面的代码完成了这一点。

class EventListener<T>(
    val owner: Any,
    val event: Class<T>,
    val callback: (T) -> Unit
)

interface IEventBus {
    fun <T> subscribe(owner: Any, event: Class<T>, callback: (T) -> Unit)
    fun unsubscribe(owner: Any)
    fun <T> push(event: T)
}

class EventBus : IEventBus {
    private val _listeners = mutableListOf<EventListener<*>>()

    override fun <T> subscribe(owner: Any, event: Class<T>, callback: (T) -> Unit) {
        val listener = EventListener(owner, event, callback)
        _listeners.add(listener)
    }

    override fun unsubscribe(owner: Any) {
        _listeners.removeAll {
            it.owner == owner
        }
    }

    override fun <T> push(event: T) {
        _listeners.forEach { listener ->
            try {
                val l = listener as EventListener<T> // is always a success
                l.callback(event)                    // throws an exception if can't handle the event
            } catch (ex: Exception) { }
        }
    }
}

然后用法会是这样的:

// register listener
bus.subscribe(this, String::class.java) {
    print(it)
}
// push an event (from somewhere else in the project)
bus.push("Hello world!")

它可以工作并且完全可用,但是我对它不满意...将 listener 转换为 EventListener 将始终返回一些内容,然后 if l.callback(event)> 无法处理它将引发异常的事件类型。因此,如果有许多监听器订阅,那么它将生成许多不需要的异常,这些异常将被忽略。

我更愿意先进行某种检查,例如:

if (listener is EventListener<T>)
    listener.callback(event)

但我发现 JVM 在编译后丢失了有关泛型类型的信息。我还发现可以使用 kotlin 的 inlinereified 绕过它,但是这些不能在来自接口(interface)的方法上使用......

所以我的问题是你知道有什么更优雅的方法来处理这种通用问题吗?

最佳答案

由于您已经公开了事件的类 ( EventListener#event ),因此您可以使用 isInstance()检查该类是否与您的事件实例的分配兼容。

所以,而不是:

if (listener is EventListener<T>)
    listener.callback(event)

你可以这样做:

if (listener.event.isInstance(event)) {
    // The cast is safe since you checked if the event can be received by the listener.
    (listener as EventListener<T>).callback(event)
}

原语

如果您想支持T对于原始类型,您可以更改 Class<T>KClass<T>或手动检查每个基本类型的实例(例如 event is Intevent is Long )。

关于android - 在 Kotlin 中处理事件总线的通用监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58517770/

相关文章:

android - 禁用 PreferenceScreen 的滚动

c# - lambda 中的类型推断

javascript - 如何从 JavaScript 代码触发 keydown 事件?

javascript - Highcharts 自定义事件插件右键单击事件未触发

javascript - 覆盖网站上文本输入区域的默认移动设备键盘? (HTML/JS/jQuery)

android - 如何知道所有 firebase 数据消息都已收到?

c# - "default"通用约束有什么作用?

generics - 关于 Rust 中的泛型和特征的问题

时间:2018-01-08 标签:c#winforms: searching for datagridview event that will fire after changing cell value AND cell focus

android - 按产品口味配置 applicationId 后找不到 R 类