pref: 事件流扩展方法
This commit is contained in:
parent
239e6109df
commit
5ff4c37a09
|
|
@ -56,6 +56,29 @@ fun <T : UiState, A> Flow<T>.collectPartialAsState(
|
|||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
inline fun <reified Event : UiEvent> Flow<UiEvent>.onEvent(
|
||||
noinline listener: suspend (Event) -> Unit
|
||||
) {
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
DisposableEffect(key1 = listener, key2 = this) {
|
||||
with(coroutineScope) {
|
||||
val job = launch {
|
||||
this@onEvent
|
||||
.filterIsInstance<Event>()
|
||||
.cancellable()
|
||||
.collect {
|
||||
launch {
|
||||
listener(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onDispose { job.cancel() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
inline fun <reified Event : UiEvent> BaseViewModel<*, *, *, *>.onEvent(
|
||||
noinline listener: suspend (Event) -> Unit
|
||||
|
|
|
|||
Loading…
Reference in New Issue