fix: 添加 mockSocketService 缺失的事件订阅方法
修复控制台警告 "[useEventNotifications] socket.subscribeToEvents 方法不存在" **问题原因** - mockSocketService.js 中缺少 `subscribeToEvents`、`unsubscribeFromEvents` 等方法 - socketService.js 有这些方法,但 mock 版本没有实现 - 导致 useEventNotifications Hook 无法正常工作 **修复内容** 在 mockSocketService.js 中添加以下方法(lines 688-793): 1. **subscribeToEvents(options)** - 订阅事件推送 - 参数:eventType, importance, onNewEvent, onSubscribed - Mock 实现:立即触发 onSubscribed 回调(100ms 延迟) - 注册 onNewEvent 监听器到 'new_event' 事件 2. **unsubscribeFromEvents(options)** - 取消订阅 - 参数:eventType, onUnsubscribed - Mock 实现:移除 'new_event' 监听器 - 立即触发 onUnsubscribed 回调(100ms 延迟) 3. **subscribeToAllEvents(onNewEvent)** - 快捷方法:订阅所有事件 - 调用 subscribeToEvents,eventType='all', importance='all' 4. **subscribeToImportantEvents(importance, onNewEvent)** - 快捷方法:按重要性订阅 - 调用 subscribeToEvents,eventType='all' 5. **subscribeToEventType(eventType, onNewEvent)** - 快捷方法:按类型订阅 - 调用 subscribeToEvents,importance='all' **实现方式** - Mock 实现使用 setTimeout 模拟异步回调 - 使用现有的 EventEmitter 机制(on/off) - 与 socketService.js 保持 API 一致 **测试结果** - ✅ 编译成功 - ✅ 不再显示 "socket.subscribeToEvents 方法不存在" 警告 - ✅ useEventNotifications Hook 可以正常调用订阅方法 - ✅ Mock 模式下事件订阅功能可用 **文件修改** - src/services/mockSocketService.js (+108 lines) - 新增 subscribeToEvents 方法 - 新增 unsubscribeFromEvents 方法 - 新增 3 个快捷订阅方法
This commit is contained in:
@@ -684,6 +684,113 @@ class MockSocketService {
|
||||
getMaxReconnectAttempts() {
|
||||
return Infinity;
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅事件推送(Mock 实现)
|
||||
* @param {object} options - 订阅选项
|
||||
* @param {string} options.eventType - 事件类型 ('all' | 'policy' | 'market' | 'tech' | ...)
|
||||
* @param {string} options.importance - 重要性 ('all' | 'S' | 'A' | 'B' | 'C')
|
||||
* @param {Function} options.onNewEvent - 收到新事件时的回调函数
|
||||
* @param {Function} options.onSubscribed - 订阅成功的回调函数(可选)
|
||||
*/
|
||||
subscribeToEvents(options = {}) {
|
||||
const {
|
||||
eventType = 'all',
|
||||
importance = 'all',
|
||||
onNewEvent,
|
||||
onSubscribed,
|
||||
} = options;
|
||||
|
||||
logger.info('mockSocketService', 'Subscribing to events', { eventType, importance });
|
||||
|
||||
// Mock: 立即触发订阅成功回调
|
||||
if (onSubscribed) {
|
||||
setTimeout(() => {
|
||||
onSubscribed({
|
||||
success: true,
|
||||
event_type: eventType,
|
||||
importance: importance,
|
||||
message: 'Mock subscription confirmed'
|
||||
});
|
||||
}, 100);
|
||||
}
|
||||
|
||||
// Mock: 如果提供了 onNewEvent 回调,监听 'new_event' 事件
|
||||
if (onNewEvent) {
|
||||
// 先移除之前的监听器(避免重复)
|
||||
this.off('new_event', onNewEvent);
|
||||
// 添加新的监听器
|
||||
this.on('new_event', onNewEvent);
|
||||
logger.info('mockSocketService', 'Event listener registered for new_event');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消订阅事件推送(Mock 实现)
|
||||
* @param {object} options - 取消订阅选项
|
||||
* @param {string} options.eventType - 事件类型
|
||||
* @param {Function} options.onUnsubscribed - 取消订阅成功的回调函数(可选)
|
||||
*/
|
||||
unsubscribeFromEvents(options = {}) {
|
||||
const {
|
||||
eventType = 'all',
|
||||
onUnsubscribed,
|
||||
} = options;
|
||||
|
||||
logger.info('mockSocketService', 'Unsubscribing from events', { eventType });
|
||||
|
||||
// Mock: 移除 new_event 监听器
|
||||
this.off('new_event');
|
||||
|
||||
// Mock: 立即触发取消订阅成功回调
|
||||
if (onUnsubscribed) {
|
||||
setTimeout(() => {
|
||||
onUnsubscribed({
|
||||
success: true,
|
||||
event_type: eventType,
|
||||
message: 'Mock unsubscription confirmed'
|
||||
});
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 快捷方法:订阅所有类型的事件(Mock 实现)
|
||||
* @param {Function} onNewEvent - 收到新事件时的回调函数
|
||||
*/
|
||||
subscribeToAllEvents(onNewEvent) {
|
||||
this.subscribeToEvents({
|
||||
eventType: 'all',
|
||||
importance: 'all',
|
||||
onNewEvent,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 快捷方法:订阅指定重要性的事件(Mock 实现)
|
||||
* @param {string} importance - 重要性级别 ('S' | 'A' | 'B' | 'C')
|
||||
* @param {Function} onNewEvent - 收到新事件时的回调函数
|
||||
*/
|
||||
subscribeToImportantEvents(importance, onNewEvent) {
|
||||
this.subscribeToEvents({
|
||||
eventType: 'all',
|
||||
importance,
|
||||
onNewEvent,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 快捷方法:订阅指定类型的事件(Mock 实现)
|
||||
* @param {string} eventType - 事件类型
|
||||
* @param {Function} onNewEvent - 收到新事件时的回调函数
|
||||
*/
|
||||
subscribeToEventType(eventType, onNewEvent) {
|
||||
this.subscribeToEvents({
|
||||
eventType,
|
||||
importance: 'all',
|
||||
onNewEvent,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 导出单例
|
||||
|
||||
Reference in New Issue
Block a user