TODO - TBD - WIP
Multi-keys, multi-producers, single-consumer event bus backed by kotlinx.coroutines.channels.Channels.
-
A Kotlin Multiplatform library that provides a simple event bus implementation using
kotlinx.coroutines.channels.Channels. This is useful for UI applications where you want to send events to communicate between different parts / scope of your application. -
This bus is thread-safe to be used by multiple threads. It is safe to send events from multiple threads without any synchronization.
-
ChannelEvent.Keywill be used to identify a bus for a specific type of events. Each bus has aChannelto send events to and aFlowto receive events from. -
The
Channelis unbounded (Channel.UNLIMITED- default) or conflatedChannel.CONFLATED. TheFlowis cold and only one collector is allowed at a time. This make sure all events are consumed.
Author: Petrus Nguyễn Thái Học
Liked some of my work? Buy me a coffee (or more likely a beer)
// Create your event type data class AwesomeEvent(val payload: Int) : ChannelEvent<AwesomeEvent> { override val key get() = Key companion object Key : ChannelEventKey<AwesomeEvent>(AwesomeEvent::class) } // Create your bus instance val bus = ChannelEventBus() // Send events to the bus bus.send(AwesomeEvent(1)) bus.send(AwesomeEvent(2)) bus.send(AwesomeEvent(3)) // Receive events from the bus bus .receiveAsFlow(AwesomeEvent) // or bus.receiveAsFlow(AwesomeEvent.Key) if you want to be explicit .collect { e: AwesomeEvent -> println(e) }jvm/android.js(IR).Darwintargets:iosArm64,iosArm32,iosX64,iosSimulatorArm64.watchosArm32,watchosArm64,watchosX64,watchosX86,watchosSimulatorArm64.tvosX64,tvosSimulatorArm64,tvosArm64.macosX64,macosArm64.
TODO - TBD - WIP
-
Android Compose sample: an Android app using Compose UI to show how to use the library. It has two nested navigation graphs:
RegisterScreenandHomeScreen.In
RegisterScreen, we have 3 steps (3 screens) to allow user to input their information, step by step.-
A
RegisterSharedViewModel(bound toRegisterScreennavigation graph scope) is used to hold the whole state of the registration process. It observes events from theChannelEventBusand update the state accordingly. -
Each step screen has a
ViewModelto hold the state of the screen, and will send events to theChannelEventBus, then theRegisterSharedViewModelwill receive those events and update the state.
-
TODO - TBD - WIP
TODO - TBD - WIP 
