38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
// In-memory cache of the most-recently-rendered messages for each
|
|
// conversation. Survives React component unmount/remount (used by
|
|
// `useConversationMessages` to initialize state synchronously when
|
|
// ConversationPage is remounted on chat switch). Session-scoped — lost
|
|
// on full app reload. The SQLite cache (`messageCache.ts`) is still the
|
|
// source of truth for cross-session persistence; this layer just shaves
|
|
// off the round-trip-to-disk spinner flash.
|
|
//
|
|
// Two-tier semantics:
|
|
// * `hasCachedMessages(id)` returns true even for a known-empty chat
|
|
// so the hook can suppress the loading spinner on re-entry.
|
|
// * `getCachedMessages(id)` returns a defensive copy so callers can't
|
|
// mutate the cached array.
|
|
|
|
import type { DecryptedMessage } from '@chat-app/shared/chat';
|
|
|
|
const cache = new Map<string, DecryptedMessage[]>();
|
|
|
|
export function getCachedMessages(conversationId: string): DecryptedMessage[] {
|
|
const stored = cache.get(conversationId);
|
|
return stored ? stored.slice() : [];
|
|
}
|
|
|
|
export function hasCachedMessages(conversationId: string): boolean {
|
|
return cache.has(conversationId);
|
|
}
|
|
|
|
export function setCachedMessages(
|
|
conversationId: string,
|
|
messages: DecryptedMessage[],
|
|
): void {
|
|
cache.set(conversationId, messages.slice());
|
|
}
|
|
|
|
export function __resetForTests(): void {
|
|
cache.clear();
|
|
}
|