|
1 | 1 | # kotlin-channel-event-bus 🔆
|
2 | 2 |
|
3 | 3 | [](https://github.com/hoc081098/kotlin-channel-event-bus/actions/workflows/build.yml)
|
| 4 | + |
| 5 | +[](http://kotlinlang.org) |
| 6 | +[](https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.7.3) |
| 7 | + |
| 8 | +[](https://hits.seeyoufarm.com) |
| 9 | + |
| 10 | +![badge][badge-jvm] |
| 11 | +![badge][badge-android] |
| 12 | +![badge][badge-js] |
| 13 | +![badge][badge-ios] |
| 14 | +![badge][badge-mac] |
| 15 | +![badge][badge-tvos] |
| 16 | +![badge][badge-watchos] |
| 17 | + |
| 18 | +<p align="center"> |
| 19 | + <img src="https://github.com/hoc081098/kmp-viewmodel/raw/master/logo.png" width="400"> |
| 20 | +</p> |
| 21 | + |
| 22 | +## Multi-keys, multi-producers, single-consumer event bus backed by `kotlinx.coroutines.channels.Channel`s. |
| 23 | + |
| 24 | +- A Kotlin Multiplatform library that provides a simple event bus implementation using |
| 25 | + `kotlinx.coroutines.channels.Channel`s. |
| 26 | + This is useful for UI applications where you want to send events to communicate between |
| 27 | + different parts / scope of your application. |
| 28 | + |
| 29 | +- This bus is thread-safe to be used by multiple threads. |
| 30 | + It is safe to send events from multiple threads without any synchronization. |
| 31 | + |
| 32 | +- `ChannelEvent.Key` will be used to identify a bus for a specific type of events. |
| 33 | + Each bus has a `Channel` to send events to and a `Flow` to receive events from. |
| 34 | + |
| 35 | +- The `Channel` is unbounded (`Channel.UNLIMITED` - default) or conflated `Channel.CONFLATED`. |
| 36 | + The `Flow` is cold and only one collector is allowed at a time. |
| 37 | + This make sure all events are consumed. |
| 38 | + |
| 39 | +## Author: [Petrus Nguyễn Thái Học](https://github.com/hoc081098) |
| 40 | + |
| 41 | +Liked some of my work? Buy me a coffee (or more likely a beer) |
| 42 | + |
| 43 | +<a href="https://www.buymeacoffee.com/hoc081098" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-blue.png" alt="Buy Me A Coffee" height=64></a> |
| 44 | + |
| 45 | +## Basic usage |
| 46 | + |
| 47 | +```kotlin |
| 48 | +// Create your event type |
| 49 | +data class AwesomeEvent(val payload: Int) : ChannelEvent<AwesomeEvent> { |
| 50 | + override val key get() = Key |
| 51 | + |
| 52 | + companion object Key : ChannelEventKey<AwesomeEvent>(AwesomeEvent::class) |
| 53 | +} |
| 54 | + |
| 55 | +// Create your bus instance |
| 56 | +val bus = ChannelEventBus() |
| 57 | + |
| 58 | +// Send events to the bus |
| 59 | +bus.send(AwesomeEvent(1)) |
| 60 | +bus.send(AwesomeEvent(2)) |
| 61 | +bus.send(AwesomeEvent(3)) |
| 62 | + |
| 63 | +// Receive events from the bus |
| 64 | +bus |
| 65 | + .receiveAsFlow(AwesomeEvent) // or bus.receiveAsFlow(AwesomeEvent.Key) if you want to be explicit |
| 66 | + .collect { e: AwesomeEvent -> println(e) } |
| 67 | +``` |
| 68 | + |
| 69 | +## Supported targets |
| 70 | + |
| 71 | +- `android`. |
| 72 | +- `jvm` (must add `kotlinx-coroutines-swing`/`kotlinx-coroutines-javafx` to your dependencies to |
| 73 | + make sure `Dispatchers.Main` available). |
| 74 | +- `js` (`IR`). |
| 75 | +- `Darwin` targets: |
| 76 | + - `iosArm64`, `iosArm32`, `iosX64`, `iosSimulatorArm64`. |
| 77 | + - `watchosArm32`, `watchosArm64`, `watchosX64`, `watchosX86`, `watchosSimulatorArm64`. |
| 78 | + - `tvosX64`, `tvosSimulatorArm64`, `tvosArm64`. |
| 79 | + - `macosX64`, `macosArm64`. |
| 80 | + |
| 81 | +## Docs |
| 82 | + |
| 83 | +TODO |
| 84 | + |
| 85 | +## License |
| 86 | + |
| 87 | +```license |
| 88 | +MIT License |
| 89 | +Copyright (c) 2023 Petrus Nguyễn Thái Học |
| 90 | +``` |
| 91 | + |
| 92 | +[badge-android]: http://img.shields.io/badge/android-6EDB8D.svg?style=flat |
| 93 | +[badge-ios]: http://img.shields.io/badge/ios-CDCDCD.svg?style=flat |
| 94 | +[badge-js]: http://img.shields.io/badge/js-F8DB5D.svg?style=flat |
| 95 | +[badge-jvm]: http://img.shields.io/badge/jvm-DB413D.svg?style=flat |
| 96 | +[badge-linux]: http://img.shields.io/badge/linux-2D3F6C.svg?style=flat |
| 97 | +[badge-windows]: http://img.shields.io/badge/windows-4D76CD.svg?style=flat |
| 98 | +[badge-mac]: http://img.shields.io/badge/macos-111111.svg?style=flat |
| 99 | +[badge-watchos]: http://img.shields.io/badge/watchos-C0C0C0.svg?style=flat |
| 100 | +[badge-tvos]: http://img.shields.io/badge/tvos-808080.svg?style=flat |
| 101 | +[badge-wasm]: https://img.shields.io/badge/wasm-624FE8.svg?style=flat |
| 102 | +[badge-nodejs]: https://img.shields.io/badge/nodejs-68a063.svg?style=flat |
0 commit comments