我的目标是在 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 的 inline 和 reified 绕过它,但是这些不能在来自接口(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 Int
、 event is Long
)。
关于android - 在 Kotlin 中处理事件总线的通用监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58517770/