Add some caching to the wikidata search box, partial fix to #551 and #530

This commit is contained in:
pietervdvn 2021-11-07 02:23:28 +01:00
parent 027bf29ab5
commit 6b864e6fef
3 changed files with 24 additions and 16 deletions

View file

@ -172,7 +172,7 @@ export default class Wikidata {
lang +
"&type=item&origin=*" +
"&props=";// props= removes some unused values in the result
const response = await Utils.downloadJson(url)
const response = await Utils.downloadJsonCached(url, 10000)
const result: any[] = response.search
@ -192,6 +192,7 @@ export default class Wikidata {
return result;
}
public static async searchAndFetch(
search: string,
options?: WikidataSearchoptions
@ -287,7 +288,7 @@ export default class Wikidata {
}
const url = "https://www.wikidata.org/wiki/Special:EntityData/" + id + ".json";
const entities = (await Utils.downloadJson(url)).entities
const entities = (await Utils.downloadJsonCached(url, 10000)).entities
const firstKey = <string> Array.from(Object.keys(entities))[0] // Roundabout way to fetch the entity; it might have been a redirect
const response = entities[firstKey]

View file

@ -2,7 +2,7 @@ import {Utils} from "../Utils";
export default class Constants {
public static vNumber = "0.11.3";
public static vNumber = "0.11.4";
public static ImgurApiKey = '7070e7167f0a25a'
public static readonly mapillary_client_token_v3 = 'TXhLaWthQ1d4RUg0czVxaTVoRjFJZzowNDczNjUzNmIyNTQyYzI2'
public static readonly mapillary_client_token_v4 = "MLY|4441509239301885|b40ad2d3ea105435bd40c7e76993ae85"

View file

@ -13,6 +13,8 @@ import Svg from "../../Svg";
export default class WikidataSearchBox extends InputElement<string> {
private static readonly _searchCache = new Map<string, Promise<WikidataResponse[]>>()
IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false);
private readonly wikidataId: UIEventSource<string>
private readonly searchText: UIEventSource<string>
@ -29,6 +31,10 @@ export default class WikidataSearchBox extends InputElement<string> {
return this.wikidataId;
}
IsValid(t: string): boolean {
return t.startsWith("Q") && !isNaN(Number(t.substring(1)));
}
protected InnerConstructElement(): HTMLElement {
const searchField = new TextField({
@ -46,12 +52,20 @@ export default class WikidataSearchBox extends InputElement<string> {
return;
}
searchFailMessage.setData(undefined)
lastSearchResults.WaitForPromise(
Wikidata.searchAndFetch(searchText, {
lang: Locale.language.data,
const lang = Locale.language.data
const key = lang + ":" + searchText
let promise = WikidataSearchBox._searchCache.get(key)
if (promise === undefined) {
promise = Wikidata.searchAndFetch(searchText, {
lang,
maxCount: 5
}
), err => searchFailMessage.setData(err))
)
WikidataSearchBox._searchCache.set(key, promise)
}
lastSearchResults.WaitForPromise(promise, err => searchFailMessage.setData(err))
})
@ -61,10 +75,10 @@ export default class WikidataSearchBox extends InputElement<string> {
return new Combine([Translations.t.general.wikipedia.failed.Clone().SetClass("alert"), searchFailMessage.data])
}
if(searchResults.length === 0){
if (searchResults.length === 0) {
return Translations.t.general.wikipedia.noResults.Subs({search: searchField.GetValue().data ?? ""})
}
if (searchResults.length === 0) {
return Translations.t.general.wikipedia.doSearch
}
@ -88,7 +102,6 @@ export default class WikidataSearchBox extends InputElement<string> {
}, [searchFailMessage]))
//
const full = new Combine([
new Title(Translations.t.general.wikipedia.searchWikidata, 3).SetClass("m-2"),
new Combine([
@ -108,10 +121,4 @@ export default class WikidataSearchBox extends InputElement<string> {
]).ConstructElement();
}
IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false);
IsValid(t: string): boolean {
return t.startsWith("Q") && !isNaN(Number(t.substring(1)));
}
}