윤동환
10/15/2023, 11:41 PMmyanmarking
10/16/2023, 12:29 PM윤동환
10/16/2023, 1:01 PMclass EventBus {
private val scope = CoroutineScope(SuperviserJob))
private val _event = MutableSharedFlow<Int>()
val event = _event.asSharedFlow()
init {
scope.launch {
var no = 0
while (true) {
no++ // simulate io bound task. in real, it can throw a exception.
_event.emit(no)
delay(1000L)
}
}
}
}
suspend fun consume() {
val bus = EventBus()
bus.event
.onEach { no -> // do something IO bound task that can throw exception }
.launchIn(CoroutineScope(SupervisorJob()))
}
myanmarking
10/16/2023, 2:32 PM윤동환
10/16/2023, 10:41 PM