From d5db717b5466ff50d792a30891b047ce86f832b4 Mon Sep 17 00:00:00 2001 From: 777genius Date: Sun, 31 May 2026 06:54:46 +0300 Subject: [PATCH] perf(renderer): cache stable team message keys --- src/renderer/utils/teamMessageKey.ts | 40 +++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/renderer/utils/teamMessageKey.ts b/src/renderer/utils/teamMessageKey.ts index b1b98499..e73a831d 100644 --- a/src/renderer/utils/teamMessageKey.ts +++ b/src/renderer/utils/teamMessageKey.ts @@ -2,13 +2,45 @@ import type { InboxMessage } from '@shared/types'; const FALLBACK_SLICE = 80; +interface CachedMessageKey { + readonly messageId: InboxMessage['messageId']; + readonly timestamp: InboxMessage['timestamp']; + readonly from: InboxMessage['from']; + readonly text: InboxMessage['text']; + readonly key: string; +} + +const messageKeyCache = new WeakMap(); + /** * Stable key for a team message. Prefer messageId; otherwise build from timestamp, from, and text. */ export function toMessageKey(message: InboxMessage): string { - if (typeof message.messageId === 'string' && message.messageId.trim().length > 0) { - return message.messageId; + const cached = messageKeyCache.get(message); + if ( + cached && + cached.messageId === message.messageId && + cached.timestamp === message.timestamp && + cached.from === message.from && + cached.text === message.text + ) { + return cached.key; } - const text = (message.text ?? '').slice(0, FALLBACK_SLICE); - return `${message.timestamp}-${message.from}-${text}`; + + const rawMessageId = typeof message.messageId === 'string' ? message.messageId : ''; + const trimmedMessageId = rawMessageId.trim(); + const key = + trimmedMessageId.length > 0 + ? rawMessageId + : `${message.timestamp}-${message.from}-${(message.text ?? '').slice(0, FALLBACK_SLICE)}`; + + messageKeyCache.set(message, { + messageId: message.messageId, + timestamp: message.timestamp, + from: message.from, + text: message.text, + key, + }); + + return key; }