Created
August 17, 2017 08:10
-
-
Save etissieres/2eebd0d4650493f9cc13058b37f4247c to your computer and use it in GitHub Desktop.
Kotlin utilities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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