Emitting An Entity From Different Source Of Truth Using Mutablesharedflow
Published:
yeah, for the requirement, it should be in different sub-classes
Calling Function in a class --> the entity in a calling function class -> **the entity that is placed to emit source of truth**
|
|
------------------------> the other entity in a calling function class -> **the entity that collects the source **
If it’s in the same structure of calling class, then, there’s no need, the ability will be wasted anyway. Just handle it in the calling function class.
If it’s similar to that case up there, then proceed to make a cleaner, lazier way like as follows
- Make an Interface that contains a desired MutableSharedFlow
- Implements both the entity that has source to emit and the entity that collects it with that Interface
Using
yourMutableSharedFlow.emit(YourDesiredEntityToEmit)
wrap it inside acoroutineScope
inside a class that will emit the sourcesample
fun onSourceOfTruthCaptured(sourceOfTruth: T) { coroutineScope.launch { yourMutableSharedFlow.emit(sourceOfTruth) } }
Using
yourMutableSharedFlow.collect{ yourEntity -> }
call it in the class that will collect the emitted source, after you initialize the class, and wrap it inside acoroutineScope
sample
fun init(){ coroutineScope.launch { yourMutableSharedFlow.collect { it -> handleIt(it) } } }