[Perf] Add cache for emoji rendering

This commit is contained in:
Alexander Zinchuk 2022-04-19 15:12:07 +02:00
parent 4d448e4d07
commit 7a324ec7ff
2 changed files with 14 additions and 4 deletions

View File

@ -1,12 +1,15 @@
import React from '../../../lib/teact/teact';
import EMOJI_REGEX, { removeVS16s } from '../../../lib/twemojiRegex';
import EMOJI_REGEX from '../../../lib/twemojiRegex';
import { TextPart } from '../../../types';
import { RE_LINK_TEMPLATE, RE_MENTION_TEMPLATE } from '../../../config';
import { IS_EMOJI_SUPPORTED } from '../../../util/environment';
import {
fixNonStandardEmoji, handleEmojiLoad, LOADED_EMOJIS, nativeToUnified,
fixNonStandardEmoji,
handleEmojiLoad,
LOADED_EMOJIS,
nativeToUnifiedExtendedWithCache,
} from '../../../util/emoji';
import buildClassName from '../../../util/buildClassName';
import { compact } from '../../../util/iteratees';
@ -103,7 +106,7 @@ function replaceEmojis(textParts: TextPart[], size: 'big' | 'small', type: 'jsx'
result.push(parts[0]);
return emojis.reduce((emojiResult: TextPart[], emoji, i) => {
const code = nativeToUnified(removeVS16s(emoji));
const code = nativeToUnifiedExtendedWithCache(emoji);
if (!code) return emojiResult;
const src = `./img-apple-${size === 'big' ? '160' : '64'}/${code}.png`;
const className = buildClassName(

View File

@ -1,4 +1,5 @@
import EMOJI_REGEX from '../lib/twemojiRegex';
import EMOJI_REGEX, { removeVS16s } from '../lib/twemojiRegex';
import withCache from './withCache';
// Due to the fact that emoji from Apple do not contain some characters, it is necessary to remove them from emoji-data
// https://github.com/iamcal/emoji-data/issues/136
@ -72,6 +73,12 @@ export function nativeToUnified(emoji: string) {
return code;
}
function nativeToUnifiedExtended(emoji: string) {
return nativeToUnified(removeVS16s(emoji));
}
export const nativeToUnifiedExtendedWithCache = withCache(nativeToUnifiedExtended);
export function uncompressEmoji(data: EmojiRawData): EmojiData {
const emojiData: EmojiData = { categories: [], emojis: {} };