Use Promise.allSettle when fetching emojis (#6921)

This commit is contained in:
Elias Nahum 2023-01-03 23:45:50 +02:00 committed by GitHub
parent 411a7e22a2
commit 0c4e554534
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 4 deletions

View file

@ -87,10 +87,17 @@ const debouncedFetchEmojiByNames = debounce(async (serverUrl: string) => {
promises.push(client.getCustomEmojiByName(name));
}
const emojis = await Promise.all(promises);
try {
await operator.handleCustomEmojis({emojis, prepareRecordsOnly: false});
const emojisResult = await Promise.allSettled(promises);
const emojis = emojisResult.reduce<CustomEmoji[]>((result, e) => {
if (e.status === 'fulfilled') {
result.push(e.value);
}
return result;
}, []);
if (emojis.length) {
await operator.handleCustomEmojis({emojis, prepareRecordsOnly: false});
}
return {error: undefined};
} catch (error) {
return {error};

View file

@ -57,7 +57,7 @@ const Emoji = (props: EmojiProps) => {
} catch {
// do nothing
}
} else if (name && !isUnicodeEmoji(name)) {
} else if (name && (name.length > 1 || !isUnicodeEmoji(name))) {
fetchCustomEmojiInBatch(serverUrl, name);
}
}

View file

@ -16,6 +16,20 @@ import {registerNavigationListeners} from '@screens/navigation';
let alreadyInitialized = false;
let serverCredentials: ServerCredential[];
// Fallback Polyfill for Promise.allSettle
Promise.allSettled = Promise.allSettled || (<T>(promises: Array<Promise<T>>) => Promise.all(
promises.map((p) => p.
then((value) => ({
status: 'fulfilled',
value,
})).
catch((reason) => ({
status: 'rejected',
reason,
})),
),
));
export async function initialize() {
if (!alreadyInitialized) {
alreadyInitialized = true;