Giter Site home page Giter Site logo

hoc081098 / kotlin-channel-event-bus Goto Github PK

View Code? Open in Web Editor NEW
24.0 2.0 1.0 1.76 MB

A Kotlin Multiplatform library that provides a simple event bus implementation using KotlinX Coroutines Channels. Multi-keys, multi-producers, single-consumer and thread-safe event bus backed by kotlinx.coroutines.channels.Channels

Home Page: https://hoc081098.github.io/kotlin-channel-event-bus/docs/0.x

License: Apache License 2.0

Kotlin 99.12% Shell 0.88%
kotlin-coroutines kotlin-multiplatform kmm kmm-jetpack-compose kmm-library kmm-mvvm kmm-sample kmm-viewmodel kmp kmp-library

kotlin-channel-event-bus's Introduction

kotlin-channel-event-bus 🔆

maven-central codecov Build and publish snapshot Build sample Publish Release Kotlin version KotlinX Coroutines version Hits GitHub license badge badge badge badge badge badge badge badge badge badge badge badge

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 (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 a Channel to send events to and a Flow to receive events from.

  • The Channel is unbounded (Channel.UNLIMITED - default) or conflated Channel.CONFLATED. The Flow is cold and only one collector is allowed at a time. This make sure all events are consumed.

Liked some of my work? Buy me a coffee (or more likely a beer)

Buy Me A Coffee

Docs

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 and Home.

    • In Register, we have 3 steps (3 screens) to allow user to input their information, step by step.

      • A RegisterSharedViewModel (bound to Register navigation graph scope) is used to hold the whole state of the registration process. It observes events from the ChannelEventBus and update the state accordingly.

      • Each step screen has a ViewModel to hold the state of the screen, and will send events to the ChannelEventBus, then the RegisterSharedViewModel will receive those events and update the state.

    • In Home nav graph, we have 2 screens: Home and Detail.

      • Home screen has a HomeViewModel to hold the results received from the Detail screen. Those result events are sent from the Detail screen to the ChannelEventBus, and the HomeViewModel will receive those events and update the state.

      • Detail screen will send events to the ChannelEventBus when user clicks on the button. The HomeViewModel will receive those events and update the state.

Screen_recording_20231203_155312.mp4

Roadmap

  • Support more targets: wasm (depends on supported targets by kotlinx.coroutines).
  • More samples.
  • More docs.
  • More tests.

License

                                 Apache License
                           Version 2.0, January 2004
                        http://www.apache.org/licenses/

kotlin-channel-event-bus's People

Contributors

dependabot[bot] avatar hoc081098 avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

kotlin-channel-event-bus's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: Error updating branch: update failure

Errored

These updates encountered an error and will be retried. Click on a checkbox below to force a retry now.

  • chore(deps): update all dependencies (major) (actions/cache, actions/checkout, actions/setup-java, actions/setup-python, actions/upload-artifact, codecov/codecov-action, ffurrer2/extract-release-notes, softprops/action-gh-release, androidx.lifecycle:lifecycle-viewmodel-compose, androidx.lifecycle:lifecycle-runtime-compose)

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/build.yml
  • actions/checkout v3
  • actions/setup-java v3
  • actions/cache v3
  • actions/cache v3
  • codecov/codecov-action v3.1.6
  • actions/upload-artifact v3
  • actions/checkout v3
  • actions/setup-java v3
  • actions/cache v3
  • actions/cache v3
  • actions/setup-python v4
  • JamesIves/github-pages-deploy-action v4.6.0
.github/workflows/publish-release.yml
  • actions/checkout v3
  • ffurrer2/extract-release-notes v1
  • softprops/action-gh-release v1
  • actions/checkout v3
  • actions/setup-java v3
  • actions/cache v3
  • actions/cache v3
  • actions/setup-python v4
  • JamesIves/github-pages-deploy-action v4.6.0
.github/workflows/sample.yml
  • actions/checkout v3
  • actions/setup-java v3
  • actions/cache v3
  • actions/cache v3
gradle
gradle.properties
settings.gradle.kts
  • org.gradle.toolchains.foojay-resolver-convention 0.8.0
build.gradle.kts
channel-event-bus/gradle.properties
channel-event-bus/build.gradle.kts
gradle/libs.versions.toml
  • org.jetbrains.kotlinx:kotlinx-coroutines-core 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-coroutines-android 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-coroutines-swing 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-coroutines-test 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-coroutines-jdk8 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-serialization-json 1.6.3
  • org.jetbrains.kotlinx:kotlinx-collections-immutable 0.3.7
  • org.jetbrains.compose.runtime:runtime 1.6.2
  • co.touchlab:stately-concurrency 2.0.7
  • io.github.aakira:napier 2.7.1
  • io.github.hoc081098:FlowExt 0.8.1-Beta
  • io.insert-koin:koin-android 3.5.6
  • io.insert-koin:koin-androidx-compose 3.5.6
  • io.insert-koin:koin-core 3.5.6
  • io.insert-koin:koin-compose 1.1.5
  • io.coil-kt:coil-compose 2.6.0
  • io.nlopez.compose.rules:detekt 0.3.19
  • androidx.lifecycle:lifecycle-viewmodel-ktx 2.7.0
  • androidx.lifecycle:lifecycle-viewmodel-savedstate 2.7.0
  • androidx.lifecycle:lifecycle-runtime-compose androidx-lifecycle
  • androidx.lifecycle:lifecycle-viewmodel-compose androidx-lifecycle
  • androidx.annotation:annotation 1.7.1
  • androidx.activity:activity-compose 1.9.0
  • androidx.compose:compose-bom 2024.05.00
  • androidx.navigation:navigation-compose 2.7.7
  • io.github.hoc081098:kmp-viewmodel 0.7.1
  • io.github.hoc081098:kmp-viewmodel-savedstate 0.7.1
  • io.github.hoc081098:kmp-viewmodel-compose 0.7.1
  • io.github.hoc081098:kmp-viewmodel-koin-compose 0.7.1
  • io.github.hoc081098:solivagant-navigation 0.3.0
  • com.jakewharton.timber:timber 5.0.1
  • org.jetbrains.kotlin.multiplatform 1.9.23
  • org.jetbrains.kotlin.android 1.9.23
  • org.jetbrains.kotlin.native.cocoapods 1.9.23
  • org.jetbrains.kotlin.plugin.serialization 1.9.23
  • org.jetbrains.kotlin.plugin.parcelize 1.9.23
  • org.jetbrains.compose 1.6.2
  • com.android.application 8.4.0
  • com.android.library 8.4.0
  • com.diffplug.gradle.spotless 6.25.0
  • io.gitlab.arturbosch.detekt 1.23.6
  • org.jetbrains.kotlinx.kover 0.7.6
  • org.jetbrains.dokka 1.9.20
  • org.jetbrains.kotlinx.binary-compatibility-validator 0.14.0
  • com.vanniktech.maven.publish 0.28.0
sample/standalone-androidApp/build.gradle.kts
sample/standalone-composeMultiplatform/composeApp/build.gradle.kts
sample/standalone-composeMultiplatform/desktopApp/build.gradle.kts
gradle-wrapper
gradle/wrapper/gradle-wrapper.properties
  • gradle 8.7

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.