refactor(message): simplify state management and remove redundant service methods
Refactor the message module to streamline state management by removing the `MessageStateManager` and `SubscriptionManager` layers, relying instead on Zustand's built-in selector mechanism for reactive updates. - Remove redundant `setConversations`, `addMessage`, and `updateMessage` actions from the Zustand store to favor more specialized update methods. - Clean up `MessageService` by removing deprecated conversation and message sending convenience methods. - Update service interfaces to support new synchronization capabilities including `syncBySeq` and `restartSources`. - Simplify documentation and comments across the message store and its associated services.
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
/**
|
||||
* MessageManager - 消息管理核心模块(重构版)
|
||||
*
|
||||
* MessageManager - 消息管理核心模块
|
||||
*
|
||||
* 作为轻量级协调者,整合各个专注的服务模块
|
||||
* 直接使用 zustand store 进行状态管理
|
||||
*
|
||||
* 重构说明:
|
||||
* - 移除了 MessageStateManager 包装层
|
||||
* - 直接使用 zustand store
|
||||
* - 服务层移至 services/ 目录
|
||||
* - 移除了 SubscriptionManager,改用 Zustand selector 进行响应式订阅
|
||||
*/
|
||||
|
||||
import type { ConversationResponse, MessageResponse, MessageSegment, UserDTO } from '../../types/dto';
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
/**
|
||||
* 消息模块 React Hooks
|
||||
*
|
||||
*
|
||||
* 提供React组件与消息状态管理的集成
|
||||
* 所有hooks都基于zustand store的selector机制
|
||||
* 确保组件能实时获取状态更新
|
||||
*
|
||||
* 重构说明:
|
||||
* - 移除了 SubscriptionManager,改用 Zustand selector
|
||||
* - Zustand 会自动处理依赖追踪和组件重渲染
|
||||
* - 不需要手动管理订阅/取消订阅
|
||||
*/
|
||||
|
||||
import { useState, useEffect, useCallback, useRef } from 'react';
|
||||
|
||||
@@ -1,12 +1,5 @@
|
||||
/**
|
||||
* 消息模块统一导出
|
||||
*
|
||||
* 重构说明:
|
||||
* - 移除了 MessageStateManager 包装层
|
||||
* - 直接使用 zustand store 进行状态管理
|
||||
* - 服务层移至 services/ 目录
|
||||
* - 新增 hooks.ts 封装常用逻辑
|
||||
* - 移除了 SubscriptionManager,改用 Zustand selector 进行响应式订阅
|
||||
*/
|
||||
|
||||
// ==================== 主管理器 ====================
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
/**
|
||||
* 会话操作服务
|
||||
* 处理会话的 CRUD 操作
|
||||
*
|
||||
* 重构说明:
|
||||
* - 直接使用 zustand store 替代 IMessageStateManager
|
||||
* - 移除了 SubscriptionManager 调用,Zustand 的 set() 会自动触发组件重渲染
|
||||
*/
|
||||
|
||||
import type { ConversationResponse } from '../../../types/dto';
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
/**
|
||||
* 消息发送服务
|
||||
* 处理消息发送相关逻辑
|
||||
*
|
||||
* 重构说明:
|
||||
* - 直接使用 zustand store 替代 IMessageStateManager
|
||||
* - 移除了 SubscriptionManager 调用,Zustand 的 set() 会自动触发组件重渲染
|
||||
*/
|
||||
|
||||
import type { MessageResponse, MessageSegment, ConversationResponse } from '../../../types/dto';
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
/**
|
||||
* 消息同步服务
|
||||
* 处理消息和会话的服务器同步逻辑
|
||||
*
|
||||
* 重构说明:
|
||||
* - 直接使用 zustand store 替代 IMessageStateManager
|
||||
* - 移除了 SubscriptionManager 调用,Zustand 的 set() 会自动触发组件重渲染
|
||||
*/
|
||||
|
||||
import type { ConversationResponse, MessageResponse } from '../../../types/dto';
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
/**
|
||||
* 已读回执管理器
|
||||
* 管理消息已读状态的同步和保护
|
||||
*
|
||||
* 重构说明:
|
||||
* - 直接使用 zustand store 替代 IMessageStateManager
|
||||
* - 移除了 SubscriptionManager 调用,Zustand 的 set() 会自动触发组件重渲染
|
||||
*/
|
||||
|
||||
import type { ConversationResponse } from '../../../types/dto';
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
/**
|
||||
* WebSocket 消息处理器
|
||||
* 处理所有来自 WebSocket 的消息事件
|
||||
*
|
||||
* 重构说明:
|
||||
* - 直接使用 zustand store 替代 IMessageStateManager
|
||||
* - 移除了 SubscriptionManager 调用,Zustand 的 set() 会自动触发组件重渲染
|
||||
*/
|
||||
|
||||
import type {
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/**
|
||||
* 消息服务层统一导出
|
||||
*
|
||||
* 重构说明:所有服务直接使用 zustand store,移除了 IMessageStateManager 接口依赖
|
||||
*/
|
||||
|
||||
// 消息去重服务
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
/**
|
||||
* 消息状态 Zustand Store
|
||||
* 使用 zustand 进行状态管理,提供响应式状态更新
|
||||
*
|
||||
* 重构说明:
|
||||
* - 移除了 MessageStateManager 包装层
|
||||
* - 直接使用 zustand store 进行状态管理
|
||||
* - 移除了 SubscriptionManager,改用 Zustand selector 进行响应式订阅
|
||||
*/
|
||||
|
||||
import { create } from 'zustand';
|
||||
@@ -78,7 +73,6 @@ export interface MessageActions {
|
||||
isNotificationMuted: (conversationId: string) => boolean;
|
||||
|
||||
// 设置状态
|
||||
setConversations: (conversations: Map<string, ConversationResponse>) => void;
|
||||
setConversationsWithUnread: (
|
||||
conversations: Map<string, ConversationResponse>,
|
||||
totalUnread: number,
|
||||
@@ -87,8 +81,6 @@ export interface MessageActions {
|
||||
updateConversation: (conversation: ConversationResponse) => void;
|
||||
removeConversation: (conversationId: string) => void;
|
||||
setMessages: (conversationId: string, messages: MessageResponse[]) => void;
|
||||
addMessage: (conversationId: string, message: MessageResponse) => void;
|
||||
updateMessage: (conversationId: string, messageId: string, updates: Partial<MessageResponse>) => void;
|
||||
setUnreadCount: (total: number, system: number) => void;
|
||||
setLastSystemMessageAt: (time: string | null) => void;
|
||||
setSSEConnected: (connected: boolean) => void;
|
||||
@@ -253,21 +245,6 @@ export const useMessageStore = create<MessageStore>((set, get) => ({
|
||||
|
||||
// ==================== 设置状态 ====================
|
||||
|
||||
setConversations: (conversations: Map<string, ConversationResponse>) => {
|
||||
const conversationList = sortConversationList(conversations);
|
||||
const notificationMutedMap = new Map<string, boolean>();
|
||||
conversations.forEach((conv, id) => {
|
||||
if (conv.notification_muted) {
|
||||
notificationMutedMap.set(id, true);
|
||||
}
|
||||
});
|
||||
set(state => ({
|
||||
conversations,
|
||||
conversationList,
|
||||
notificationMutedMap: new Map([...state.notificationMutedMap, ...notificationMutedMap]),
|
||||
}));
|
||||
},
|
||||
|
||||
setConversationsWithUnread: (
|
||||
conversations: Map<string, ConversationResponse>,
|
||||
totalUnread: number,
|
||||
@@ -348,39 +325,6 @@ export const useMessageStore = create<MessageStore>((set, get) => ({
|
||||
});
|
||||
},
|
||||
|
||||
addMessage: (conversationId: string, message: MessageResponse) => {
|
||||
const normalizedId = normalizeConversationId(conversationId);
|
||||
set(state => {
|
||||
const existing = state.messagesMap.get(normalizedId) || [];
|
||||
const exists = existing.some(m => String(m.id) === String(message.id));
|
||||
|
||||
if (exists) {
|
||||
return state;
|
||||
}
|
||||
|
||||
const updated = mergeMessagesById(existing, [message]);
|
||||
const newMessagesMap = new Map(state.messagesMap);
|
||||
newMessagesMap.set(normalizedId, updated);
|
||||
return { messagesMap: newMessagesMap };
|
||||
});
|
||||
},
|
||||
|
||||
updateMessage: (conversationId: string, messageId: string, updates: Partial<MessageResponse>) => {
|
||||
const normalizedId = normalizeConversationId(conversationId);
|
||||
set(state => {
|
||||
const messages = state.messagesMap.get(normalizedId);
|
||||
if (!messages) return state;
|
||||
|
||||
const updated = messages.map(m =>
|
||||
String(m.id) === String(messageId) ? { ...m, ...updates } : m
|
||||
);
|
||||
|
||||
const newMessagesMap = new Map(state.messagesMap);
|
||||
newMessagesMap.set(normalizedId, updated);
|
||||
return { messagesMap: newMessagesMap };
|
||||
});
|
||||
},
|
||||
|
||||
setUnreadCount: (total: number, system: number) => {
|
||||
set({ totalUnreadCount: total, systemUnreadCount: system });
|
||||
},
|
||||
|
||||
@@ -73,7 +73,9 @@ export interface IMessageSyncService {
|
||||
fetchMessages(conversationId: string, afterSeq?: number): Promise<void>;
|
||||
loadMoreMessages(conversationId: string, beforeSeq: number, limit?: number): Promise<MessageResponse[]>;
|
||||
fetchUnreadCount(): Promise<void>;
|
||||
syncBySeq(): Promise<boolean>;
|
||||
canLoadMoreConversations(): boolean;
|
||||
restartSources(): void;
|
||||
}
|
||||
|
||||
export interface IWSMessageHandler {
|
||||
|
||||
Reference in New Issue
Block a user