解决事件泄漏问题

This commit is contained in:
zqm
2025-12-26 13:09:35 +08:00
parent efa9fd64c7
commit 7f2f31156f
13 changed files with 924 additions and 1098 deletions

View File

@@ -202,6 +202,8 @@ const onToggleCollapse = () => {
panelId: props.id,
areaId: getCurrentAreaId(),
currentState: props.collapsed
}, {
source: { component: 'Panel', panelId: props.id }
})
};
@@ -211,6 +213,8 @@ const onMaximize = () => {
panelId: props.id,
areaId: getCurrentAreaId(),
currentState: props.maximized
}, {
source: { component: 'Panel', panelId: props.id }
})
};
@@ -222,6 +226,8 @@ const onClose = () => {
areaId: getCurrentAreaId(),
panelTitle: props.title,
requestTime: Date.now()
}, {
source: { component: 'Panel', panelId: props.id }
})
};
@@ -231,11 +237,14 @@ const onToggleToolbar = () => {
panelId: props.id,
areaId: getCurrentAreaId(),
currentState: props.toolbarExpanded
}, {
source: { component: 'Panel', panelId: props.id }
})
};
// 拖拽相关状态
let isDragging = false
let currentDragId = null
// 全局内存泄漏保护机制
if (!window.__panelMemoryProtection) {
@@ -378,19 +387,25 @@ const onDragStart = (e) => {
if (!e.target.closest('.title-bar-buttons') && !e.target.closest('button')) {
// 1. 立即重置之前的拖拽状态
isDragging = false
currentDragId = null
cleanupDragEventListeners()
isDragging = true
console.log(`[Panel:${props.id}] 开始拖拽`)
// 2. 使用事件总线触发拖拽开始事件
// 生成统一的 dragId
currentDragId = `panel_${props.id}_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`
console.log(`[Panel:${props.id}] 开始拖拽, dragId: ${currentDragId}`)
// 2. 使用事件总线触发拖拽开始事件,包含统一的 dragId 和标准化数据格式
emitEvent(EVENT_TYPES.PANEL_DRAG_START, {
dragId: currentDragId,
panelId: props.id,
areaId: getCurrentAreaId(),
position: { x: e.clientX, y: e.clientY },
timestamp: Date.now()
}, {
source: { component: 'Panel', panelId: props.id }
source: { component: 'Panel', panelId: props.id, dragId: currentDragId }
})
// 3. 防止文本选择和默认行为
@@ -404,40 +419,46 @@ const onDragStart = (e) => {
// 拖拽移动
const onDragMove = (e) => {
if (isDragging) {
if (isDragging && currentDragId) {
// 防止文本选择和默认行为
e.preventDefault();
e.stopPropagation();
// 使用事件总线触发拖拽移动事件
// 使用事件总线触发拖拽移动事件,包含 dragId
emitEvent(EVENT_TYPES.PANEL_DRAG_MOVE, {
dragId: currentDragId,
panelId: props.id,
areaId: getCurrentAreaId(),
position: { x: e.clientX, y: e.clientY },
timestamp: Date.now()
}, {
source: { component: 'Panel', panelId: props.id }
source: { component: 'Panel', panelId: props.id, dragId: currentDragId }
})
}
};
// 拖拽结束
const onDragEnd = () => {
if (isDragging) {
if (isDragging && currentDragId) {
isDragging = false;
console.log(`[Panel:${props.id}] 结束拖拽`)
// 使用事件总线触发拖拽结束事件
console.log(`[Panel:${props.id}] 结束拖拽, dragId: ${currentDragId}`)
// 使用事件总线触发拖拽结束事件,包含 dragId
emitEvent(EVENT_TYPES.PANEL_DRAG_END, {
dragId: currentDragId,
panelId: props.id,
areaId: getCurrentAreaId(),
timestamp: Date.now()
}, {
source: { component: 'Panel', panelId: props.id }
source: { component: 'Panel', panelId: props.id, dragId: currentDragId }
})
// 使用统一的清理方法,确保一致性和完整性
cleanupDragEventListeners()
// 重置 dragId
currentDragId = null
}
};
@@ -449,10 +470,9 @@ const setupEventListeners = () => {
// 监听面板最大化同步事件
const unsubscribeMaximizeSync = onEvent(EVENT_TYPES.PANEL_MAXIMIZE_SYNC, (data) => {
if (data.panelId === props.id) {
// 这里可以添加最大化状态同步的逻辑
console.log(`[Panel:${props.id}] 收到最大化同步事件`)
}
})
}, { componentId: `panel-${props.id}` })
const subscriptionId = `maximizeSync_${props.id}_${Date.now()}`
subscriptions.add(unsubscribeMaximizeSync)