An open API service indexing awesome lists of open source software.

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 🛠

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'
}
```