FIX: race conditions in search menu (#9262)
Race conditions could lead the previous query search term to be used in the next query. This commit also attempts to simplify code.
This commit is contained in:
parent
61c1af0124
commit
58ced428ee
|
@ -35,7 +35,8 @@ createWidget("search-term", {
|
||||||
}
|
}
|
||||||
|
|
||||||
const val = this.attrs.value;
|
const val = this.attrs.value;
|
||||||
const newVal = $(`#${this.buildId()}`).val();
|
// remove zero-width chars
|
||||||
|
const newVal = e.target.value.replace(/[\u200B-\u200D\uFEFF]/, "");
|
||||||
|
|
||||||
if (newVal !== val) {
|
if (newVal !== val) {
|
||||||
this.sendWidgetAction("searchTermChanged", newVal);
|
this.sendWidgetAction("searchTermChanged", newVal);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { get } from "@ember/object";
|
import { get } from "@ember/object";
|
||||||
import { debounce, later } from "@ember/runloop";
|
import { debounce, cancel } from "@ember/runloop";
|
||||||
import { popupAjaxError } from "discourse/lib/ajax-error";
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
||||||
import { searchForTerm, isValidSearchTerm } from "discourse/lib/search";
|
import { searchForTerm, isValidSearchTerm } from "discourse/lib/search";
|
||||||
import { createWidget } from "discourse/widgets/widget";
|
import { createWidget } from "discourse/widgets/widget";
|
||||||
|
@ -23,28 +23,17 @@ initSearchData();
|
||||||
// Helps with debouncing and cancelling promises
|
// Helps with debouncing and cancelling promises
|
||||||
const SearchHelper = {
|
const SearchHelper = {
|
||||||
_activeSearch: null,
|
_activeSearch: null,
|
||||||
_cancelSearch: null,
|
|
||||||
|
|
||||||
// for cancelling debounced search
|
// for cancelling debounced search
|
||||||
cancel() {
|
cancel() {
|
||||||
if (this._activeSearch) {
|
if (this._activeSearch) {
|
||||||
this._activeSearch.abort();
|
cancel(this._activeSearch);
|
||||||
|
this._activeSearch = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._cancelSearch = true;
|
|
||||||
later(() => (this._cancelSearch = false), 400);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
perform(widget) {
|
perform(widget) {
|
||||||
if (this._cancelSearch) {
|
this.cancel();
|
||||||
this._cancelSearch = null;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this._activeSearch) {
|
|
||||||
this._activeSearch.abort();
|
|
||||||
this._activeSearch = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { term, typeFilter, contextEnabled } = searchData;
|
const { term, typeFilter, contextEnabled } = searchData;
|
||||||
const searchContext = contextEnabled ? widget.searchContext() : null;
|
const searchContext = contextEnabled ? widget.searchContext() : null;
|
||||||
|
@ -67,11 +56,6 @@ const SearchHelper = {
|
||||||
this._activeSearch
|
this._activeSearch
|
||||||
.then(content => {
|
.then(content => {
|
||||||
searchData.noResults = content.resultTypes.length === 0;
|
searchData.noResults = content.resultTypes.length === 0;
|
||||||
|
|
||||||
if (content.grouped_search_result) {
|
|
||||||
searchData.term = content.grouped_search_result.term;
|
|
||||||
}
|
|
||||||
|
|
||||||
searchData.results = content;
|
searchData.results = content;
|
||||||
|
|
||||||
if (searchContext && searchContext.type === "topic") {
|
if (searchContext && searchContext.type === "topic") {
|
||||||
|
|
Loading…
Reference in New Issue