abbic
09/26/2023, 12:03 PM@Singleton
class AppEventBus @Inject constructor() {
private val internalSharedFlow =
MutableSharedFlow<AppEvent>()
val allEvents: Flow<AppEvent> get() = internalSharedFlow
inline fun <reified T: AppEvent> observe(): Flow<T> =
allEvents.filterIsInstance()
fun tryPostEvent(event: AppEvent) {
internalSharedFlow.tryEmit(event)
}
suspend fun postEvent(event: AppEvent) {
internalSharedFlow.emit(event)
}
}
I threw it together in 5 minutes so im sure i can make many improvements, bust specifically i was wondering, given that this class will be a singleton, can I rely on the flow staying alive on its own for the lifecycle of the program? sorry if its a strange question, i am probably worrying about nothingFilippo Vigani
09/26/2023, 3:44 PMabbic
09/26/2023, 4:20 PMFilippo Vigani
09/27/2023, 11:15 AMSharedFlow
cannot be closed or cancelled by definition. It will be kept alive and the instance will be shared in your caseabbic
09/27/2023, 11:15 AM