diff --git a/lib/shared/search-utils.js b/lib/shared/search-utils.js --- a/lib/shared/search-utils.js +++ b/lib/shared/search-utils.js @@ -230,17 +230,19 @@ messages: $ReadOnlyArray, endReached: boolean, threadID: string, + queryID?: number, ) => mixed, cursor?: string, + queryID?: number, ) => void { const callSearchMessages = useServerCall(searchMessages); const dispatchActionPromise = useDispatchActionPromise(); return React.useCallback( - (query, threadID, onResultsReceived, cursor) => { + (query, threadID, onResultsReceived, cursor, queryID) => { const searchMessagesPromise = (async () => { if (query === '') { - onResultsReceived([], true, threadID); + onResultsReceived([], true, threadID, queryID); return; } const { messages, endReached } = await callSearchMessages({ @@ -248,7 +250,7 @@ threadID, cursor, }); - onResultsReceived(messages, endReached, threadID); + onResultsReceived(messages, endReached, threadID, queryID); })(); dispatchActionPromise(searchMessagesActionTypes, searchMessagesPromise); diff --git a/web/search/message-search-state-provider.react.js b/web/search/message-search-state-provider.react.js --- a/web/search/message-search-state-provider.react.js +++ b/web/search/message-search-state-provider.react.js @@ -114,13 +114,20 @@ const searchMessagesCall = useSearchMessages(); const loading = React.useRef(false); + const queryIDref = React.useRef(0); const appendResults = React.useCallback( ( newMessages: $ReadOnlyArray, end: boolean, threadID: string, + queryID?: number, ) => { + if (queryID !== queryIDref.current) { + loading.current = false; + return; + } + appendResult(newMessages, threadID); if (end) { setEndReached(threadID); @@ -135,12 +142,14 @@ if (loading.current || endsReached.current.has(threadID)) { return; } + queryIDref.current += 1; loading.current = true; searchMessagesCall( queries.current[threadID], threadID, appendResults, lastIDs.current[threadID], + queryIDref.current, ); }, [appendResults, endsReached, searchMessagesCall],