From d66b2e10f229b8e501cb13b65be6db447c841734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sun, 12 Dec 2021 20:34:29 +0100 Subject: [PATCH] Fix: Race condition in search MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- app/soapbox/actions/search.js | 5 ++-- app/soapbox/reducers/search.js | 42 +++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/app/soapbox/actions/search.js b/app/soapbox/actions/search.js index ca515d06c..d14d13152 100644 --- a/app/soapbox/actions/search.js +++ b/app/soapbox/actions/search.js @@ -63,7 +63,7 @@ export function submitSearch(filter) { dispatch(importFetchedStatuses(response.data.statuses)); } - dispatch(fetchSearchSuccess(response.data)); + dispatch(fetchSearchSuccess(response.data, value)); dispatch(fetchRelationships(response.data.accounts.map(item => item.id))); }).catch(error => { dispatch(fetchSearchFail(error)); @@ -78,10 +78,11 @@ export function fetchSearchRequest(value) { }; } -export function fetchSearchSuccess(results) { +export function fetchSearchSuccess(results, searchTerm) { return { type: SEARCH_FETCH_SUCCESS, results, + searchTerm, }; } diff --git a/app/soapbox/reducers/search.js b/app/soapbox/reducers/search.js index 8a4ec4c5b..b950e5f58 100644 --- a/app/soapbox/reducers/search.js +++ b/app/soapbox/reducers/search.js @@ -28,29 +28,33 @@ const toIds = items => { return ImmutableOrderedSet(items.map(item => item.id)); }; -const importResults = (state, results) => { +const importResults = (state, results, searchTerm) => { return state.withMutations(state => { - state.set('results', ImmutableMap({ - accounts: toIds(results.accounts), - statuses: toIds(results.statuses), - hashtags: fromJS(results.hashtags), // it's a list of maps - accountsHasMore: results.accounts.length >= 20, - statusesHasMore: results.statuses.length >= 20, - hashtagsHasMore: results.hashtags.length >= 20, - accountsLoaded: true, - statusesLoaded: true, - hashtagsLoaded: true, - })); + if (state.get('value') === searchTerm) { + state.set('results', ImmutableMap({ + accounts: toIds(results.accounts), + statuses: toIds(results.statuses), + hashtags: fromJS(results.hashtags), // it's a list of maps + accountsHasMore: results.accounts.length >= 20, + statusesHasMore: results.statuses.length >= 20, + hashtagsHasMore: results.hashtags.length >= 20, + accountsLoaded: true, + statusesLoaded: true, + hashtagsLoaded: true, + })); - state.set('submitted', true); + state.set('submitted', true); + } }); }; -const paginateResults = (state, searchType, results) => { +const paginateResults = (state, searchType, results, searchTerm) => { return state.withMutations(state => { - state.setIn(['results', `${searchType}HasMore`], results[searchType].length >= 20); - state.setIn(['results', `${searchType}Loaded`], true); - state.updateIn(['results', searchType], items => items.concat(results[searchType].map(item => item.id))); + if (state.get('value') === searchTerm) { + state.setIn(['results', `${searchType}HasMore`], results[searchType].length >= 20); + state.setIn(['results', `${searchType}Loaded`], true); + state.updateIn(['results', searchType], items => items.concat(results[searchType].map(item => item.id))); + } }); }; @@ -77,13 +81,13 @@ export default function search(state = initialState, action) { case SEARCH_FETCH_REQUEST: return handleSubmitted(state, action.value); case SEARCH_FETCH_SUCCESS: - return importResults(state, action.results); + return importResults(state, action.results, action.searchTerm); case SEARCH_FILTER_SET: return state.set('filter', action.value); case SEARCH_EXPAND_REQUEST: return state.setIn(['results', `${action.searchType}Loaded`], false); case SEARCH_EXPAND_SUCCESS: - return paginateResults(state, action.searchType, action.results); + return paginateResults(state, action.searchType, action.results, action.searchTerm); default: return state; }