From cddba50570f373e206b743d437a40e07cd1c7a21 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 7 Sep 2021 14:44:06 -0400 Subject: [PATCH] FIX: Error in Ember CLI environment There are a few fixes at play here: 1) We were still not initializing objects to the correct types. 2) If a debounce timed out, it was returning a string instead of an array which was not appropriately handled. 3) In testing mode we never cancel the search promise for stability. --- .../discourse/app/lib/category-tag-search.js | 11 +++++------ .../javascripts/discourse/app/widgets/search-menu.js | 12 ++++++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/discourse/app/lib/category-tag-search.js b/app/assets/javascripts/discourse/app/lib/category-tag-search.js index eba355ad616..370fff4efbe 100644 --- a/app/assets/javascripts/discourse/app/lib/category-tag-search.js +++ b/app/assets/javascripts/discourse/app/lib/category-tag-search.js @@ -20,12 +20,11 @@ function updateCache(term, results) { function searchTags(term, categories, limit) { return new Promise((resolve) => { - const clearPromise = later( - () => { - resolve(CANCELLED_STATUS); - }, - isTesting() ? 50 : 5000 - ); + let clearPromise = isTesting() + ? null + : later(() => { + resolve(CANCELLED_STATUS); + }, 5000); const debouncedSearch = (q, cats, resultFunc) => { discourseDebounce( diff --git a/app/assets/javascripts/discourse/app/widgets/search-menu.js b/app/assets/javascripts/discourse/app/widgets/search-menu.js index 8e953ab297c..3463070b575 100644 --- a/app/assets/javascripts/discourse/app/widgets/search-menu.js +++ b/app/assets/javascripts/discourse/app/widgets/search-menu.js @@ -9,6 +9,7 @@ import { popupAjaxError } from "discourse/lib/ajax-error"; import { Promise } from "rsvp"; import { search as searchCategoryTag } from "discourse/lib/category-tag-search"; import userSearch from "discourse/lib/user-search"; +import { CANCELLED_STATUS } from "discourse/lib/autocomplete"; const CATEGORY_SLUG_REGEXP = /(\#[a-zA-Z0-9\-:]*)$/gi; const USERNAME_REGEXP = /(\@[a-zA-Z0-9\-\_]*)$/gi; @@ -18,7 +19,7 @@ const searchData = {}; export function initSearchData() { searchData.loading = false; - searchData.results = []; + searchData.results = {}; searchData.noResults = false; searchData.term = undefined; searchData.typeFilter = null; @@ -52,8 +53,9 @@ const SearchHelper = { if (matchSuggestions) { searchData.noResults = true; - searchData.results = []; + searchData.results = {}; searchData.loading = false; + searchData.suggestionResults = []; if (matchSuggestions.type === "category") { const categorySearchTerm = matchSuggestions.categoriesMatch[0].replace( @@ -66,8 +68,10 @@ const SearchHelper = { widget.siteSettings ); Promise.resolve(categoryTagSearch).then((results) => { - searchData.suggestionResults = results; - searchData.suggestionKeyword = "#"; + if (results !== CANCELLED_STATUS) { + searchData.suggestionResults = results; + searchData.suggestionKeyword = "#"; + } widget.scheduleRerender(); }); } else if (matchSuggestions.type === "username") {