refactor(LocalDataSource, PostRepository, ChatScreen): enhance database handling and message loading
Some checks failed
Frontend CI / build-and-push-web (push) Successful in 2m41s
Frontend CI / ota-android (push) Successful in 12m45s
Frontend CI / build-android-apk (push) Has been cancelled

- Introduced a promise-based initialization mechanism in LocalDataSource to prevent concurrent initialization issues.
- Updated PostRepository to utilize an enqueueWrite method for database operations, ensuring thread-safe writes.
- Refactored ChatScreen to improve message loading logic, including preloading history and managing scroll behavior more effectively.
- Enhanced message rendering logic to maintain correct indices in inverted lists and optimize user experience during scrolling.
This commit is contained in:
lafay
2026-03-23 23:06:19 +08:00
parent 7305254e11
commit c98f1917f7
9 changed files with 396 additions and 185 deletions

View File

@@ -287,6 +287,29 @@ class MessageManager {
return Array.from(merged.values()).sort((a, b) => a.seq - b.seq);
}
/**
* 历史消息合并优化:
* - 常见场景incoming 全部比 existing 更旧,直接前插,避免全量排序
* - 异常/重叠场景:回退到通用 mergeMessagesById
*/
private mergeOlderMessages(existing: MessageResponse[], incoming: MessageResponse[]): MessageResponse[] {
if (incoming.length === 0) return existing;
const incomingAsc = [...incoming].sort((a, b) => a.seq - b.seq);
if (existing.length === 0) return incomingAsc;
const existingMinSeq = existing[0]?.seq ?? Number.MAX_SAFE_INTEGER;
const incomingMaxSeq = incomingAsc[incomingAsc.length - 1]?.seq ?? Number.MIN_SAFE_INTEGER;
// 纯历史前插快路径:避免全量 merge + sort
if (incomingMaxSeq < existingMinSeq) {
return [...incomingAsc, ...existing];
}
// 兜底:存在重叠/乱序时走通用路径
return this.mergeMessagesById(existing, incomingAsc);
}
private updateConversationList() {
// 会话排序:置顶优先,再按最后消息时间排序
const list = Array.from(this.state.conversations.values()).sort((a, b) => {
@@ -1513,7 +1536,8 @@ class MessageManager {
if (localMessages.length >= limit) {
// 本地有足够数据
const formattedMessages: MessageResponse[] = localMessages.map(m => ({
// 本地查询是 seq DESC这里转成 ASC匹配渲染时间序
const formattedMessages: MessageResponse[] = [...localMessages].reverse().map(m => ({
id: m.id,
conversation_id: m.conversationId,
sender_id: m.senderId,
@@ -1525,7 +1549,7 @@ class MessageManager {
// 合并到现有消息
const existingMessages = this.state.messagesMap.get(conversationId) || [];
const mergedMessages = this.mergeMessagesById(existingMessages, formattedMessages);
const mergedMessages = this.mergeOlderMessages(existingMessages, formattedMessages);
this.state.messagesMap.set(conversationId, mergedMessages);
this.notifySubscribers({
@@ -1561,7 +1585,7 @@ class MessageManager {
// 合并消息
const existingMessages = this.state.messagesMap.get(conversationId) || [];
const mergedMessages = this.mergeMessagesById(existingMessages, serverMessages);
const mergedMessages = this.mergeOlderMessages(existingMessages, serverMessages);
this.state.messagesMap.set(conversationId, mergedMessages);
this.notifySubscribers({