Skip to content

Commit

Permalink
[components] For mozilla-mobile/android-components#12361 - Changed `d…
Browse files Browse the repository at this point in the history
…istinctUntilChangedBy` to `ifChanged` in SearchFeature
  • Loading branch information
Alexandru2909 authored and mergify[bot] committed Jun 28, 2022
1 parent 806dd24 commit 8f5e85d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
Expand Up @@ -6,8 +6,6 @@ package mozilla.components.feature.search

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.distinctUntilChangedBy
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import mozilla.components.browser.state.action.ContentAction
Expand All @@ -16,6 +14,7 @@ import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.search.SearchRequest
import mozilla.components.lib.state.ext.flowScoped
import mozilla.components.support.base.feature.LifecycleAwareFeature
import mozilla.components.support.ktx.kotlinx.coroutines.flow.ifChanged
import mozilla.components.support.utils.ext.toNullablePair

/**
Expand All @@ -36,16 +35,10 @@ class SearchFeature(

override fun start() {
scope = store.flowScoped { flow ->
flow
.map { state ->
val tab = state.findTabOrCustomTabOrSelectedTab(tabId)
tab?.content?.searchRequest to tab?.id
}
flow.map { state -> state.findTabOrCustomTabOrSelectedTab(tabId) }
.ifChanged { it?.content?.searchRequest }
// Do nothing if searchRequest or sessionId is null
.mapNotNull { pair -> pair.toNullablePair() }
// We may see repeat values if other state changes before we handle the request.
// Filter these out.
.distinctUntilChangedBy { (searchRequest, _) -> searchRequest }
.mapNotNull { tab -> Pair(tab?.content?.searchRequest, tab?.id).toNullablePair() }
.collect { (searchRequest, sessionId) ->
performSearch(searchRequest, sessionId)
store.dispatch(ContentAction.ConsumeSearchRequestAction(sessionId))
Expand Down
Expand Up @@ -109,4 +109,22 @@ class SearchFeatureTest {

assertNull(store.state.selectedTab!!.content.searchRequest)
}

@Test
fun `WHEN the same search is requested two times THEN both search requests are preformed and consumed`() {
val searchRequest = SearchRequest(isPrivate = false, query = "query")
verify(performSearch, times(0)).invoke(searchRequest, SELECTED_TAB_ID)

store.dispatch(ContentAction.UpdateSearchRequestAction(SELECTED_TAB_ID, searchRequest)).joinBlocking()
store.waitUntilIdle()

verify(performSearch, times(1)).invoke(searchRequest, SELECTED_TAB_ID)
assertNull(store.state.selectedTab!!.content.searchRequest)

store.dispatch(ContentAction.UpdateSearchRequestAction(SELECTED_TAB_ID, searchRequest)).joinBlocking()
store.waitUntilIdle()

verify(performSearch, times(2)).invoke(searchRequest, SELECTED_TAB_ID)
assertNull(store.state.selectedTab!!.content.searchRequest)
}
}

0 comments on commit 8f5e85d

Please sign in to comment.