Skip to content

Instantly share code, notes, and snippets.

@etissieres
Created August 17, 2017 08:10
Show Gist options
  • Save etissieres/2eebd0d4650493f9cc13058b37f4247c to your computer and use it in GitHub Desktop.
Save etissieres/2eebd0d4650493f9cc13058b37f4247c to your computer and use it in GitHub Desktop.
Kotlin utilities
inline fun <reified T : Any> getLogger() = LoggerFactory.getLogger(T::class.java)!!
fun runInGui(runnable: () -> Unit) = SwingUtilities.invokeLater(Runnable(runnable))
class EventBus {
private val publisher = PublishSubject.create<Any>()
private val busExecutor = Executors.newSingleThreadExecutor()
fun emit(event: Any) {
log.trace("Event emitted [$event]")
busExecutor.submit {
publisher.onNext(event)
}
}
fun <T : Any> on(eventType: KClass<T>, listener: (T) -> Unit) {
publisher.ofType(eventType.java).subscribe(listener)
}
fun <T : Any> onGui(eventType: KClass<T>, listener: (T) -> Unit) {
publisher.ofType(eventType.java).subscribe { runInGui { listener(it) } }
}
companion object {
val log = getLogger<EventBus>()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment