From 487cde9176a6285edbeb5c716f0c1d061b8b42ef Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sun, 19 Mar 2023 22:31:13 -0500 Subject: [PATCH] Custom Emoji: Fix loading large quantities (#2820) --- src/hooks/useEnsureCustomEmoji.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/hooks/useEnsureCustomEmoji.ts b/src/hooks/useEnsureCustomEmoji.ts index 814f7c21f..7fff8295d 100644 --- a/src/hooks/useEnsureCustomEmoji.ts +++ b/src/hooks/useEnsureCustomEmoji.ts @@ -5,16 +5,27 @@ import { throttle } from '../util/schedulers'; import useLastSyncTime from './useLastSyncTime'; -const LOAD_QUEUE = new Set(); +let LOAD_QUEUE = new Set(); const RENDER_HISTORY = new Set(); const THROTTLE = 200; +const LIMIT_PER_REQUEST = 100; const loadFromQueue = throttle(() => { + const queue = [...LOAD_QUEUE]; + + const queueToLoad = queue.slice(0, LIMIT_PER_REQUEST); + const otherQueue = queue.slice(LIMIT_PER_REQUEST + 1); + getActions().loadCustomEmojis({ - ids: [...LOAD_QUEUE], + ids: queueToLoad, }); - LOAD_QUEUE.clear(); + LOAD_QUEUE = new Set(otherQueue); + + // Schedule next load + if (LOAD_QUEUE.size) { + loadFromQueue(); + } }, THROTTLE, false); const updateLastRendered = throttle(() => {