diff --git a/assets/js/autocomplete.ts b/assets/js/autocomplete.ts index 4a769ef3..8fac48ea 100644 --- a/assets/js/autocomplete.ts +++ b/assets/js/autocomplete.ts @@ -8,7 +8,7 @@ import { getTermContexts } from './match_query'; import store from './utils/store'; import { TermContext } from './query/lex'; import { $$ } from './utils/dom'; -import { fetchSuggestions, SuggestionsPopup, TermSuggestion } from './utils/suggestions'; +import { fetchLocalAutocomplete, fetchSuggestions, SuggestionsPopup, TermSuggestion } from './utils/suggestions'; let inputField: HTMLInputElement | null = null, originalTerm: string | undefined, @@ -118,13 +118,13 @@ function listenAutocomplete() { let serverSideSuggestionsTimeout: number | undefined; let localAc: LocalAutocompleter | null = null; - let localFetched = false; + let isLocalLoading = false; - document.addEventListener('focusin', fetchLocalAutocomplete); + document.addEventListener('focusin', loadAutocompleteFromEvent); document.addEventListener('input', event => { popup.hide(); - fetchLocalAutocomplete(event); + loadAutocompleteFromEvent(event); window.clearTimeout(serverSideSuggestionsTimeout); if (!(event.target instanceof HTMLInputElement)) return; @@ -195,21 +195,15 @@ function listenAutocomplete() { } }); - function fetchLocalAutocomplete(event: Event) { + function loadAutocompleteFromEvent(event: Event) { if (!(event.target instanceof HTMLInputElement)) return; - if (!localFetched && event.target.dataset && 'ac' in event.target.dataset) { - const now = new Date(); - const cacheKey = `${now.getUTCFullYear()}-${now.getUTCMonth()}-${now.getUTCDate()}`; + if (!isLocalLoading && event.target.dataset.ac) { + isLocalLoading = true; - localFetched = true; - - fetch(`/autocomplete/compiled?vsn=2&key=${cacheKey}`, { credentials: 'omit', cache: 'force-cache' }) - .then(handleError) - .then(resp => resp.arrayBuffer()) - .then(buf => { - localAc = new LocalAutocompleter(buf); - }); + fetchLocalAutocomplete().then(autocomplete => { + localAc = autocomplete; + }); } } diff --git a/assets/js/utils/suggestions.ts b/assets/js/utils/suggestions.ts index 7f4e9637..e9071c34 100644 --- a/assets/js/utils/suggestions.ts +++ b/assets/js/utils/suggestions.ts @@ -1,6 +1,7 @@ import { makeEl } from './dom.ts'; import { mouseMoveThenOver } from './events.ts'; import { handleError } from './requests.ts'; +import { LocalAutocompleter } from './local-autocompleter.ts'; export interface TermSuggestion { label: string; @@ -163,3 +164,20 @@ export async function fetchSuggestions(endpoint: string, targetTerm: string) { return promisedSuggestions; } + +export function purgeSuggestionsCache() { + cachedSuggestions.clear(); +} + +export async function fetchLocalAutocomplete(): Promise { + const now = new Date(); + const cacheKey = `${now.getUTCFullYear()}-${now.getUTCMonth()}-${now.getUTCDate()}`; + + return await fetch(`/autocomplete/compiled?vsn=2&key=${cacheKey}`, { + credentials: 'omit', + cache: 'force-cache', + }) + .then(handleError) + .then(resp => resp.arrayBuffer()) + .then(buf => new LocalAutocompleter(buf)); +}