PronounDB: Fix pronouns flickering (#862)
Co-authored-by: V <vendicated@riseup.net>
This commit is contained in:
parent
8a305d2d11
commit
83dab24fb9
1 changed files with 9 additions and 4 deletions
|
@ -42,22 +42,27 @@ const bulkFetch = debounce(async () => {
|
||||||
|
|
||||||
export function awaitAndFormatPronouns(id: string): string | null {
|
export function awaitAndFormatPronouns(id: string): string | null {
|
||||||
const [result, , isPending] = useAwaiter(() => fetchPronouns(id), {
|
const [result, , isPending] = useAwaiter(() => fetchPronouns(id), {
|
||||||
fallbackValue: null,
|
fallbackValue: getCachedPronouns(id),
|
||||||
onError: e => console.error("Fetching pronouns failed: ", e)
|
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 the result is present and not "unspecified", and there is a mapping for the code, then return the mappings
|
||||||
if (!isPending && result && result !== "unspecified" && PronounMapping[result])
|
if (result && result !== "unspecified" && PronounMapping[result])
|
||||||
return formatPronouns(result);
|
return formatPronouns(result);
|
||||||
|
|
||||||
return null;
|
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
|
// 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> {
|
export function fetchPronouns(id: string): Promise<PronounCode> {
|
||||||
return new Promise(res => {
|
return new Promise(res => {
|
||||||
// If cached, return the cached pronouns
|
// 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
|
// If there is already a request added, then just add this callback to it
|
||||||
else if (id in requestQueue) requestQueue[id].push(res);
|
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
|
// If not already added, then add it and call the debounced function to make sure the request gets executed
|
||||||
|
|
Loading…
Reference in a new issue