diff --git a/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchViewModel.kt index 39c69f9e15..ab94a19876 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchViewModel.kt @@ -9,6 +9,7 @@ import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.buffer @@ -41,7 +42,7 @@ class GlobalSearchViewModel @Inject constructor( val result: StateFlow = _result - private val refreshes = Channel() + private val refreshes = MutableSharedFlow() private val _queryText: MutableStateFlow = MutableStateFlow("") @@ -50,7 +51,7 @@ class GlobalSearchViewModel @Inject constructor( } fun refresh() { - executor.launch { refreshes.send(Unit) } + refreshes.tryEmit(Unit) } init { @@ -86,4 +87,4 @@ class GlobalSearchViewModel @Inject constructor( * Re-emit whenever refreshes emits. * */ @OptIn(ExperimentalCoroutinesApi::class) -private fun Flow.reEmit(refreshes: Channel) = flatMapLatest { query -> merge(flowOf(query), refreshes.consumeAsFlow().map { query }) } +private fun Flow.reEmit(refreshes: Flow) = flatMapLatest { query -> merge(flowOf(query), refreshes.map { query }) }