Fix race condition in search

This commit is contained in:
itepechi 2023-11-16 06:19:26 +09:00
parent c578846b98
commit 92aa58356b
1 changed files with 13 additions and 0 deletions

View File

@ -29,6 +29,7 @@ const Search = {
], ],
data () { data () {
return { return {
queryCount: 0,
loadedInitially: false, loadedInitially: false,
loading: Object.fromEntries( loading: Object.fromEntries(
allSearchTypes.map((searchType) => [searchType, false]) allSearchTypes.map((searchType) => [searchType, false])
@ -105,6 +106,8 @@ const Search = {
return return
} }
const localQueryCount = ++this.queryCount
const isNewSearch = this.lastQuery !== query const isNewSearch = this.lastQuery !== query
this.$refs.searchInput.blur() this.$refs.searchInput.blur()
if (isNewSearch) { if (isNewSearch) {
@ -162,6 +165,11 @@ const Search = {
local: 'localOnly' in this.filter && this.filter.localOnly 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 // 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.userIds = this.userIds.concat(map(data.accounts, 'id'))
this.statuses = uniqBy(this.statuses.concat(data.statuses), 'id') this.statuses = uniqBy(this.statuses.concat(data.statuses), 'id')
@ -182,6 +190,11 @@ const Search = {
} catch (error) { } catch (error) {
console.error(error) console.error(error)
} finally { } finally {
if (localQueryCount !== this.queryCount) {
// Skip cleanups if there's a newer query
return
}
if (!this.loadedInitially && this.hasAtLeastOneResult) { if (!this.loadedInitially && this.hasAtLeastOneResult) {
// Show results on the first meaningful response // Show results on the first meaningful response
this.loadedInitially = true this.loadedInitially = true