From 92aa58356b80318d27272b2a5453907960475602 Mon Sep 17 00:00:00 2001 From: itepechi <72330683+itepechi@users.noreply.github.com> Date: Thu, 16 Nov 2023 06:19:26 +0900 Subject: [PATCH] Fix race condition in search --- src/components/search/search.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/components/search/search.js b/src/components/search/search.js index a5ee1daf..84f808c0 100644 --- a/src/components/search/search.js +++ b/src/components/search/search.js @@ -29,6 +29,7 @@ const Search = { ], data () { return { + queryCount: 0, loadedInitially: false, loading: Object.fromEntries( allSearchTypes.map((searchType) => [searchType, false]) @@ -105,6 +106,8 @@ const Search = { return } + const localQueryCount = ++this.queryCount + const isNewSearch = this.lastQuery !== query this.$refs.searchInput.blur() if (isNewSearch) { @@ -162,6 +165,11 @@ const Search = { local: 'localOnly' in this.filter && this.filter.localOnly }) + if (localQueryCount !== this.queryCount) { + // Query count differs, there should be a newer query + return + } + // Always append to old results. If new results are empty, this doesn't change anything this.userIds = this.userIds.concat(map(data.accounts, 'id')) this.statuses = uniqBy(this.statuses.concat(data.statuses), 'id') @@ -182,6 +190,11 @@ const Search = { } catch (error) { console.error(error) } finally { + if (localQueryCount !== this.queryCount) { + // Skip cleanups if there's a newer query + return + } + if (!this.loadedInitially && this.hasAtLeastOneResult) { // Show results on the first meaningful response this.loadedInitially = true