Fix race condition in search
This commit is contained in:
parent
c578846b98
commit
92aa58356b
1 changed files with 13 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue