https://github.com/jeziellago/coroutinesflowext
Reactive components with Kotlin Flow ðŸ›
https://github.com/jeziellago/coroutinesflowext
coroutines kotlin kotlin-coroutines kotlin-flow reactive-components
Last synced: about 2 months ago
JSON representation
Reactive components with Kotlin Flow ðŸ›
- Host: GitHub
- URL: https://github.com/jeziellago/coroutinesflowext
- Owner: jeziellago
- License: apache-2.0
- Created: 2020-05-03T21:38:13.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-06-28T20:11:35.000Z (almost 5 years ago)
- Last Synced: 2025-05-07T07:09:45.617Z (about 2 months ago)
- Topics: coroutines, kotlin, kotlin-coroutines, kotlin-flow, reactive-components
- Language: Kotlin
- Homepage:
- Size: 67.4 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CoroutinesFlowExt
Reactive components with Kotlin Flow### FlowPublisher / FlowSubscriber
```kotlin
// Publisher
val publisher = flowPublisher {
emit(1)
emit(2)
emit(3)
}// or create publisher from source Flow
val myFlowEmitter = flow { emit(1) } // "source" Flow (emitter)val publisher = FlowPublisher(source = myFlowEmitter)
// or use Flow extension
val publisher = myFlowEmitter.asPublisher()
``````kotlin
// Create Subscribers// use `flowSubscriberOf`
val subscriberA = flowSubscriberOf(publisher)// or
val subscriberB = FlowSubscriber()
publisher.subscribe(subscriberB)
``````kotlin
// Receive values as Flow
val flowFromA = subscriberA.asFlow()
val flowFromB = subscriberB.asFlow()
``````kotlin
// Start emitter from publisher
publisher.open()
```
```kotlin
// Unsubscribe any receiver
publisher.unsubscribe(subscriberA)
```### FlowEmitter
```kotlin
val emitter = FlowEmitter()// use emitter as FlowPublisher
val flowPublisher = emitter.asPublisher()// emit values
emitter.emit(1)
emitter.emit(2)
emitter.emit(3)
...
```
### Easy collect using StateFlow
Use `observeState`:
```kotlin
coroutineScope.observeState(stateFlow) {
// collect value
}
```
Instead of:
```kotlin
coroutineScope.launch {
stateFlow.collect {
// collect value
}
}
```## Add dependencies
- Project `build.gradle`
```
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
- Module `build.gradle`
```
dependencies {
implementation 'com.github.jeziellago:CoroutinesFlowExt:0.1.2'
}
```