Files
alan 0a19ef229f Show a reopened chat's last transcript while it refreshes
ChatScreen keeps a bounded in-memory cache of recently left transcripts
(16 chats / 4000 messages, LRU). A reopened chat is seeded from it with no
loading state, still re-subscribes and re-reads, and the newest page replaces
the cached copy while live frames received during the read are kept. /close
removes the entry; only transcripts backed by a successful read on the
current connection are written.

Adds tool/chat_reopen_probe.mjs and an optional history delay in the loopback
fixture. Measurement shows a second cost the cache does not remove: opening at
the end of the top-anchored transcript lays out every row (notes).
2026-10-24 18:14:27 +01:00

72 lines
2.3 KiB
Dart

import 'dart:collection';
import '../../../core/api_gateway.dart';
/// The newest transcript one conversation view held when it was left.
///
/// It is a starting point for the next visit, never a final state: a reopened
/// conversation still subscribes and reads its newest page, which replaces it.
class ChatTranscriptSnapshot {
ChatTranscriptSnapshot({
required List<JsonMap> messages,
this.cursor = '',
this.hasMore = false,
}) : messages = List.unmodifiable(messages);
final List<JsonMap> messages;
/// The server's opaque older-page cursor that matched [messages].
final String cursor;
final bool hasMore;
}
/// Transcripts of recently left conversations, kept in memory by the chat
/// destination for the rest of the app session, like its scroll positions.
///
/// Bounded twice: at most [maxSessions] conversations and [maxMessages] held
/// messages in total, evicting the least recently used first. A transcript
/// larger than [maxMessages] on its own is not kept. Nothing is written to
/// disk, so a restarted app opens every conversation from the server.
class ChatTranscriptCache {
ChatTranscriptCache({this.maxSessions = 16, this.maxMessages = 4000});
final int maxSessions;
final int maxMessages;
/// Least recently used first.
final LinkedHashMap<String, ChatTranscriptSnapshot> _entries =
LinkedHashMap();
int _messageCount = 0;
/// The cached sessions, least recently used first.
List<String> get sessionIds => List.unmodifiable(_entries.keys);
int get messageCount => _messageCount;
/// The transcript [sessionId] was left with, marking it recently used.
ChatTranscriptSnapshot? read(String sessionId) {
final snapshot = _entries.remove(sessionId);
if (snapshot != null) _entries[sessionId] = snapshot;
return snapshot;
}
void write(String sessionId, ChatTranscriptSnapshot snapshot) {
remove(sessionId);
if (snapshot.messages.length > maxMessages) return;
_entries[sessionId] = snapshot;
_messageCount += snapshot.messages.length;
while (_entries.length > maxSessions || _messageCount > maxMessages) {
remove(_entries.keys.first);
}
}
void remove(String sessionId) {
final removed = _entries.remove(sessionId);
if (removed != null) _messageCount -= removed.messages.length;
}
void clear() {
_entries.clear();
_messageCount = 0;
}
}