Fix search ANR (#1634)

Co-authored-by: fanchao <git@fanchao.dev>
This commit is contained in:
Fanchao Liu 2024-08-21 16:08:36 +10:00 committed by GitHub
parent d5ff61f021
commit 245241e614
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,21 +10,19 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.buffer import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.merge import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.plus import kotlinx.coroutines.plus
import org.session.libsignal.utilities.SettableFuture
import org.thoughtcrime.securesms.search.SearchRepository import org.thoughtcrime.securesms.search.SearchRepository
import org.thoughtcrime.securesms.search.model.SearchResult import org.thoughtcrime.securesms.search.model.SearchResult
import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
@HiltViewModel @HiltViewModel
@ -47,11 +45,8 @@ class GlobalSearchViewModel @Inject constructor(
// User input delay in case we get a new query within a few hundred ms this // User input delay in case we get a new query within a few hundred ms this
// coroutine will be cancelled and the expensive query will not be run. // coroutine will be cancelled and the expensive query will not be run.
delay(300) delay(300)
val settableFuture = SettableFuture<SearchResult>()
searchRepository.query(query.toString(), settableFuture::set)
try { try {
// search repository doesn't play nicely with suspend functions (yet) searchRepository.suspendQuery(query.toString()).toGlobalSearchResult()
settableFuture.get(10_000, TimeUnit.MILLISECONDS).toGlobalSearchResult()
} catch (e: Exception) { } catch (e: Exception) {
GlobalSearchResult(query.toString()) GlobalSearchResult(query.toString())
} }
@ -69,6 +64,12 @@ class GlobalSearchViewModel @Inject constructor(
} }
} }
private suspend fun SearchRepository.suspendQuery(query: String): SearchResult {
return suspendCoroutine { cont ->
query(query, cont::resume)
}
}
/** /**
* Re-emit whenever refreshes emits. * Re-emit whenever refreshes emits.
* */ * */