FIX: Prevent duplicate search requests to backend (#14786)

When sending a full search request to backend (i.e. when hitting Enter),
the debouncer needs to be cancelled, otherwise it will get invoked and
trigger a second search request to the backend.
This commit is contained in:
Penar Musaraj 2021-11-01 13:48:42 -04:00 committed by GitHub
parent 116b93595a
commit 7dcecef4c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import { Promise } from "rsvp";
import { search as searchCategoryTag } from "discourse/lib/category-tag-search"; import { search as searchCategoryTag } from "discourse/lib/category-tag-search";
import userSearch from "discourse/lib/user-search"; import userSearch from "discourse/lib/user-search";
import { CANCELLED_STATUS } from "discourse/lib/autocomplete"; import { CANCELLED_STATUS } from "discourse/lib/autocomplete";
import { cancel } from "@ember/runloop";
const CATEGORY_SLUG_REGEXP = /(\#[a-zA-Z0-9\-:]*)$/gi; const CATEGORY_SLUG_REGEXP = /(\#[a-zA-Z0-9\-:]*)$/gi;
const USERNAME_REGEXP = /(\@[a-zA-Z0-9\-\_]*)$/gi; const USERNAME_REGEXP = /(\@[a-zA-Z0-9\-\_]*)$/gi;
@ -190,6 +191,7 @@ export default createWidget("search-menu", {
defaultState(attrs) { defaultState(attrs) {
return { return {
inTopicContext: attrs.inTopicContext, inTopicContext: attrs.inTopicContext,
_debouncer: null,
}; };
}, },
@ -434,11 +436,17 @@ export default createWidget("search-menu", {
} }
searchData.loading = true; searchData.loading = true;
cancel(this.state._debouncer);
SearchHelper.perform(this); SearchHelper.perform(this);
} else { } else {
searchData.loading = false; searchData.loading = false;
if (!this.state.inTopicContext) { if (!this.state.inTopicContext) {
discourseDebounce(SearchHelper, SearchHelper.perform, this, 400); this.state._debouncer = discourseDebounce(
SearchHelper,
SearchHelper.perform,
this,
400
);
} }
} }
}, },