Created
January 18, 2018 15:13
-
-
Save hector6872/b8f0f01d8205a7c4d3e7c7e8cf5d99f4 to your computer and use it in GitHub Desktop.
Simple Event Bus Kotlin
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
object Bus { | |
// val event = Event<Any>() | |
} | |
class Event<TYPE> { | |
private val handlers = arrayListOf<((TYPE) -> Unit)>() | |
operator fun plusAssign(handler: (TYPE) -> Unit) { | |
handlers.add(handler) | |
} | |
operator fun minusAssign(handler: (TYPE) -> Unit) { | |
handlers.remove(handler) | |
} | |
operator fun invoke(type: TYPE) = handlers.toList().forEach { it(type) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
Bus.event(Model())