Skip to content

Repository files navigation

events

A lightweight, coroutine-native in-process event bus for Kotlin Multiplatform.

Quick Start

Add the dependency to your project:

dependencies {
    implementation("io.github.briangits.events:events:<version>")
}

Then create an event bus, publish events, and subscribe to them:

val bus = Events()

val subscription = bus.subscribe<UserLoggedIn> { event ->
    println("User logged in: ${event.username}")
}

bus.publish { UserLoggedIn("janedoe") }

subscription.cancel()

Usage

Define an Event

data class UserLoggedIn(val username: String)

Subscribe to an Event

val bus = Events()
val subscription = bus.subscribe<UserLoggedIn> { event ->
    println("User logged in: ${event.username}")
}

A subscribe() returns a Job that can be canceled when no longer needed.

subscription.cancel()

Publish an Event

bus.publish { UserLoggedIn("janedoe") }

Structured Concurrency

Subscriptions can be attached to an existing CoroutineScope and are canceled when the scope is canceled

val scope = CoroutineScope(Dispatchers.Default)

val subscription = bus.subscribe<UserLoggedIn>(scope) { event ->
    println("User logged in: ${event.username}")
}

// Cancelling the scope cancels the subscription
scope.cancel()

License

Apache-2.0

About

A lightweight, coroutine-native in-process event bus for KMP

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages