kotlin-channel-event-bus 🔆
kotlinx.coroutines.channels.Channel
s.
Multi-keys, multi-producers, single-consumer event bus backed by -
A Kotlin Multiplatform library that provides a simple event bus implementation using
kotlinx.coroutines.channels.Channel
s. This is useful for UI applications where you want to send events to communicate between different parts / scope of your application (e.g. send results from a screen to another screen). -
This bus is thread-safe to be used by multiple threads. It is safe to send events from multiple threads without any synchronization.
-
ChannelEvent.Key
will be used to identify a bus for a specific type of events. Each bus has aChannel
to send events to and aFlow
to receive events from. -
The
Channel
is unbounded (Channel.UNLIMITED
- default) or conflatedChannel.CONFLATED
. TheFlow
is cold and only one collector is allowed at a time. This make sure all events are consumed.
Petrus Nguyễn Thái Học
Author:Liked some of my work? Buy me a coffee (or more likely a beer)
Docs
0.x release
docs: https://hoc081098.github.io/kotlin-channel-event-bus/docs/0.x- Snapshot docs: https://hoc081098.github.io/kotlin-channel-event-bus/docs/latest/
Installation
allprojects {
repositories {
[...]
mavenCentral()
}
}
implementation("io.github.hoc081098:channel-event-bus:0.0.2")
Snapshot
Snapshots of the development version are available in Sonatype's snapshots repository.
- Kotlin
allprojects {
repositories {
[...]
maven(url = "https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
}
dependencies {
implementation("io.github.hoc081098:channel-event-bus:0.0.3-SNAPSHOT")
}
- Groovy
allprojects {
repositories {
[...]
maven { url "https://s01.oss.sonatype.org/content/repositories/snapshots/" }
}
}
dependencies {
implementation 'io.github.hoc081098:channel-event-bus:0.0.3-SNAPSHOT'
}
Basic usage
// 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) }
Supported targets
jvm
/android
.js
(IR
).Darwin
targets:iosArm64
,iosX64
,iosSimulatorArm64
.watchosArm32
,watchosArm64
,watchosX64
,watchosSimulatorArm64
,watchosDeviceArm64
.tvosX64
,tvosSimulatorArm64
,tvosArm64
.macosX64
,macosArm64
.
mingwX64
linuxX64
,linuxArm64
.androidNativeArm32
,androidNativeArm64
,androidNativeX86
,androidNativeX64
.
Sample
-
Android Compose sample: an Android app using Compose UI to show how to use the library. It has two nested navigation graphs:
Register
andHome
.-
In
Register
, we have 3 steps (3 screens) to allow user to input their information, step by step.-
A
RegisterSharedViewModel
(bound toRegister
navigation graph scope) is used to hold the whole state of the registration process. It observes events from theChannelEventBus
and update the state accordingly. -
Each step screen has a
ViewModel
to hold the state of the screen, and will send events to theChannelEventBus
, then theRegisterSharedViewModel
will receive those events and update the state.
-
-
In
Home
nav graph, we have 2 screens:Home
andDetail
.-
Home
screen has aHomeViewModel
to hold the results received from theDetail
screen. Those result events are sent from theDetail
screen to theChannelEventBus
, and theHomeViewModel
will receive those events and update the state. -
Detail
screen will send events to theChannelEventBus
when user clicks on the button. TheHomeViewModel
will receive those events and update the state.
-
-
Screen_recording_20231203_155312.mp4
Roadmap
- Support more targets:
wasm
(depends on supported targets bykotlinx.coroutines
). - More samples.
- More docs.
- More tests.
License
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/