From 83dab24fb910977bfe743ba4a5d178505c22ad08 Mon Sep 17 00:00:00 2001 From: Kode Date: Mon, 10 Apr 2023 23:37:24 +0100 Subject: [PATCH] PronounDB: Fix pronouns flickering (#862) Co-authored-by: V --- src/plugins/pronoundb/pronoundbUtils.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/plugins/pronoundb/pronoundbUtils.ts b/src/plugins/pronoundb/pronoundbUtils.ts index 74664315..0884261f 100644 --- a/src/plugins/pronoundb/pronoundbUtils.ts +++ b/src/plugins/pronoundb/pronoundbUtils.ts @@ -42,22 +42,27 @@ const bulkFetch = debounce(async () => { export function awaitAndFormatPronouns(id: string): string | null { const [result, , isPending] = useAwaiter(() => fetchPronouns(id), { - fallbackValue: null, + fallbackValue: getCachedPronouns(id), onError: e => console.error("Fetching pronouns failed: ", e) }); - // If the promise completed, the result was not "unspecified", and there is a mapping for the code, then return the mappings - if (!isPending && result && result !== "unspecified" && PronounMapping[result]) + // If the result is present and not "unspecified", and there is a mapping for the code, then return the mappings + if (result && result !== "unspecified" && PronounMapping[result]) return formatPronouns(result); return null; } +// Gets the cached pronouns, if you're too impatient for a promise! +export function getCachedPronouns(id: string): PronounCode | null { + return cache[id] ?? null; +} + // Fetches the pronouns for one id, returning a promise that resolves if it was cached, or once the request is completed export function fetchPronouns(id: string): Promise { return new Promise(res => { // If cached, return the cached pronouns - if (id in cache) res(cache[id]); + if (id in cache) res(getCachedPronouns(id)!); // If there is already a request added, then just add this callback to it else if (id in requestQueue) requestQueue[id].push(res); // If not already added, then add it and call the debounced function to make sure the request gets executed