remove cache
Some checks failed
Chromatic / Run Chromatic (push) Has been cancelled

This commit is contained in:
ChaosExAnima 2025-09-23 11:52:05 +02:00
parent 1d9a5e4abd
commit df52e59a5c
No known key found for this signature in database
GPG Key ID: 8F2B333100FB6117

View File

@ -41,22 +41,8 @@ export function emojifyElement<Element extends HTMLElement>(
appState: EmojiAppState, appState: EmojiAppState,
extraEmojis: ExtraCustomEmojiMap = {}, extraEmojis: ExtraCustomEmojiMap = {},
): Element | null { ): Element | null {
const finish = timingMeasurementHelper('emojifyElement');
// Check the cache and return it if we get a hit.
const cacheKey = createTextCacheKey(element, appState, extraEmojis);
const cached = textCache.get(cacheKey);
if (cached !== undefined) {
log('Cache hit on %s', element.outerHTML);
if (cached === null) {
// return null;
}
// element.innerHTML = cached;
// return element;
}
// Exit if there are no emoji in the string. // Exit if there are no emoji in the string.
if (!stringHasAnyEmoji(element.innerHTML)) { if (!stringHasAnyEmoji(element.innerHTML)) {
textCache.set(cacheKey, null);
return null; return null;
} }
@ -96,8 +82,6 @@ export function emojifyElement<Element extends HTMLElement>(
} }
} }
} }
textCache.set(cacheKey, element.innerHTML);
finish();
return element; return element;
} }
@ -106,46 +90,20 @@ export function emojifyText(
appState: EmojiAppState, appState: EmojiAppState,
extraEmojis: ExtraCustomEmojiMap = {}, extraEmojis: ExtraCustomEmojiMap = {},
): string | null { ): string | null {
const cacheKey = createTextCacheKey(text, appState, extraEmojis);
const cached = textCache.get(cacheKey);
if (cached !== undefined) {
log('Cache hit on %s', text);
return cached ?? text;
}
if (!stringHasAnyEmoji(text)) { if (!stringHasAnyEmoji(text)) {
textCache.set(cacheKey, null);
return text; return text;
} }
const tokens = tokenizeText(text, appState.mode); const tokens = tokenizeText(text, appState.mode);
const eleArray = tokensToElementArray(tokens, appState, extraEmojis); const eleArray = tokensToElementArray(tokens, appState, extraEmojis);
if (!eleArray) { if (!eleArray) {
textCache.set(cacheKey, null);
return text; return text;
} }
const rendered = renderedToHTML(eleArray, document.createElement('div')); const rendered = renderedToHTML(eleArray, document.createElement('div'));
textCache.set(cacheKey, rendered.innerHTML);
return rendered.innerHTML; return rendered.innerHTML;
} }
// Private functions // Private functions
// This is the text cache. It contains full HTML strings or null to indicate there is no emoji here.
const textCache = createLimitedCache<string | null>({
log: log.extend('text'),
});
function createTextCacheKey(
input: HTMLElement | string,
appState: EmojiAppState,
extraEmojis: ExtraCustomEmojiMap,
) {
return JSON.stringify([
input instanceof HTMLElement ? input.outerHTML : input,
appState,
extraEmojis,
]);
}
// These are the unicode/custom emoji data caches. // These are the unicode/custom emoji data caches.
const unicodeEmojiCache = createLimitedCache< const unicodeEmojiCache = createLimitedCache<
Required<EmojiStateUnicode> | EmojiStateMissing Required<EmojiStateUnicode> | EmojiStateMissing
@ -230,7 +188,10 @@ export function tokenizeText(text: string, mode: EmojiMode): TokenizedText {
return tokens; return tokens;
} }
function shouldRenderUnicodeImage(code: string, mode: EmojiMode): boolean { export function shouldRenderUnicodeImage(
code: string,
mode: EmojiMode,
): boolean {
// If the mode is native or native with flags for non-flag emoji // If the mode is native or native with flags for non-flag emoji
// we can just append the text node directly. // we can just append the text node directly.
if (mode === EMOJI_MODE_NATIVE) { if (mode === EMOJI_MODE_NATIVE) {
@ -424,7 +385,6 @@ function timingMeasurementHelper(name: string) {
// Testing helpers // Testing helpers
export const testCacheClear = () => { export const testCacheClear = () => {
textCache.clear();
unicodeEmojiCache.clear(); unicodeEmojiCache.clear();
customEmojiCache.clear(); customEmojiCache.clear();
}; };