diff options
author | Kode <TheKodeToad@proton.me> | 2023-04-10 23:37:24 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-11 00:37:24 +0200 |
commit | 83dab24fb910977bfe743ba4a5d178505c22ad08 (patch) | |
tree | d914f59f1a21c5a81f9b1eb623444c4f3aefae96 /src/plugins/pronoundb/pronoundbUtils.ts | |
parent | 8a305d2d1112f800c09b01ed59131945f03a8e01 (diff) | |
download | Vencord-83dab24fb910977bfe743ba4a5d178505c22ad08.tar.gz Vencord-83dab24fb910977bfe743ba4a5d178505c22ad08.tar.bz2 Vencord-83dab24fb910977bfe743ba4a5d178505c22ad08.zip |
PronounDB: Fix pronouns flickering (#862)
Co-authored-by: V <vendicated@riseup.net>
Diffstat (limited to 'src/plugins/pronoundb/pronoundbUtils.ts')
-rw-r--r-- | src/plugins/pronoundb/pronoundbUtils.ts | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/plugins/pronoundb/pronoundbUtils.ts b/src/plugins/pronoundb/pronoundbUtils.ts index 7466431..0884261 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<PronounCode> { 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 |