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
|
@Composable
|
||||||
inline fun <reified Event : UiEvent> BaseViewModel<*, *, *, *>.onEvent(
|
inline fun <reified Event : UiEvent> BaseViewModel<*, *, *, *>.onEvent(
|
||||||
noinline listener: suspend (Event) -> Unit
|
noinline listener: suspend (Event) -> Unit
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue