From bea1436711a64d18b2c45ebc4ef252f329ef98a8 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 11 Feb 2018 10:41:01 -0800 Subject: [PATCH] fix search results race conditions --- routes/_actions/search.js | 25 +++++++++++++++++++++++++ routes/_components/search/Search.html | 27 ++++----------------------- 2 files changed, 29 insertions(+), 23 deletions(-) create mode 100644 routes/_actions/search.js diff --git a/routes/_actions/search.js b/routes/_actions/search.js new file mode 100644 index 0000000..990c0d2 --- /dev/null +++ b/routes/_actions/search.js @@ -0,0 +1,25 @@ +import { store } from '../_store/store' +import { toast } from '../_utils/toast' +import { search } from '../_api/search' + +export async function doSearch() { + let instanceName = store.get('currentInstance') + let accessToken = store.get('accessToken') + let queryInSearch = store.get('queryInSearch') + store.set({searchLoading: true}) + try { + let results = await search(instanceName, accessToken, queryInSearch) + let currentQueryInSearch = store.get('queryInSearch') // avoid race conditions + if (currentQueryInSearch === queryInSearch) { + store.set({ + searchResultsForQuery: queryInSearch, + searchResults: results + }) + } + } catch (e) { + toast.say('Error during search: ' + (e.name || '') + ' ' + (e.message || '')) + console.error(e) + } finally { + store.set({searchLoading: false}) + } +} \ No newline at end of file diff --git a/routes/_components/search/Search.html b/routes/_components/search/Search.html index 48d6330..06653ec 100644 --- a/routes/_components/search/Search.html +++ b/routes/_components/search/Search.html @@ -7,13 +7,13 @@ required bind:value="$queryInSearch"> - -{{#if loading}} +{{#if $searchLoading}}
@@ -58,8 +58,7 @@