Last active
June 8, 2024 14:09
-
-
Save takahirom/f2dbcc3053adfd87ac7e321d95a23021 to your computer and use it in GitHub Desktop.
EventBus by Kotlin coroutine
This file contains 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
import kotlinx.coroutines.experimental.channels.BroadcastChannel | |
import kotlinx.coroutines.experimental.channels.ConflatedBroadcastChannel | |
import kotlinx.coroutines.experimental.channels.ReceiveChannel | |
import kotlinx.coroutines.experimental.channels.filter | |
import kotlinx.coroutines.experimental.channels.map | |
import kotlinx.coroutines.experimental.launch | |
import javax.inject.Inject | |
import javax.inject.Singleton | |
/** | |
* You can use like this. | |
* val channel = EventBus().asChannel<ItemChangeAction>() | |
* launch (UI){ | |
* for(action in channel){ | |
* // You can use item | |
* action.item | |
* } | |
* } | |
*/ | |
@Singleton | |
class EventBus @Inject constructor() { | |
val bus: BroadcastChannel<Any> = ConflatedBroadcastChannel<Any>() | |
fun send(o: Any) { | |
launch { | |
bus.send(o) | |
} | |
} | |
inline fun <reified T> asChannel(): ReceiveChannel<T> { | |
return bus.openSubscription().filter { it is T }.map { it as T } | |
} | |
} |
this is for kotlin version ="1.4.20"
@ExperimentalCoroutinesApi
object CoroutinesEvent {
private val observerChanner = BroadcastChannel<Any>(Channel.BUFFERED)
suspend fun publish(Response: Any) {
observerChanner.send(Response)
}
fun <Any> listen(eventType: Class<Any>): ReceiveChannel<Any> =
observerChanner.openSubscription().filter { it is kotlin.Any }.map { it as Any }
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your idea, it's amazingly simple. I would suggest a slightly manipulated solution, because the documentation of ConflatedBroadcastChannel says:
This behaviour is (in my understanding) unwanted in case of the EventBus, because Subscribers would eventually receive an event, that was emitted before the Subscribtion. You can reproduce this with:
This test fails with:
Creating the Channel with an initial empty event and for every subscriber skipping the first event will avoid this beaviour: