Skip to content

feat: 支持按通道名注册事件监听器和全局事件分发 - #3

Merged
wszxdhr merged 4 commits into
mainfrom
001-manager-dispatch
Jan 20, 2026
Merged

wszxdhr merged 4 commits into
mainfrom
001-manager-dispatch

Conversation

@wszxdhr

@wszxdhr wszxdhr commented Jan 20, 2026

Copy link
Copy Markdown
Contributor

概述

  • 添加全局事件监听器注册机制,支持按通道名过滤
  • MagicDrag 子类可通过静态 channelName 指定跨标签页通信通道
  • Manager 统一分发所有拖拽信号,包括系统消息(HEARTBEAT 等)
  • 无监听器时记录可观测日志,便于调试和监控

主要改动

新增功能

  1. 事件监听器管理

    • addEventListener(type, listener, options) - 注册事件监听器,支持按 channelName 过滤
    • removeEventListener(type, listener) - 移除事件监听器
  2. 通道名支持

    • MagicDrag 子类可通过静态 channelName 属性指定使用的通信通道
    • Manager 自动管理通道的创建和销毁(引用计数)
  3. 全局事件分发

    • 所有拖拽信号(包括 DRAG_START, DRAG_MOVE, DRAG_END 等)都会通过事件系统分发
    • 系统消息(HEARTBEAT, HEARTBEAT_ACK, TAB_ACTIVATED)也会被分发
  4. 可观测性增强

    • 当某个事件类型没有监听器时,定期记录警告日志
    • 默认间隔 30 秒,避免日志刷屏

行为变更

  • activeTabId 在拖拽开始和进入目标标签页时更新,确保跨标签页拖拽状态正确
  • Manager 内部使用多通道机制,每个通道独立管理生命周期

文档和配置

  • 添加 .npmignore,优化发布的包大小
  • 新增 specs/ 目录,包含 001-manager-dispatch 的完整设计文档
  • 更新项目配置文件(.gitignore, .prettierignore, eslint.config.js)

测试

  • 单元测试覆盖新增功能
  • 手动测试跨标签页拖拽场景
  • 验证事件监听器过滤逻辑

Breaking Changes

无

相关 Issue

Closes #?

- 添加 addEventListener/removeEventListener 方法,支持按通道名过滤事件
- MagicDrag 子类可通过静态 channelName 指定跨标签页通信通道
- Manager 统一分发所有拖拽信号(包括 HEARTBEAT 等系统消息)
- 无监听器时记录可观测日志,便于调试
- 修复跨标签页拖拽时 activeTabId 更新逻辑
- 新增 .npmignore 和 specs/ 目录
- 更新项目文档和配置文件
@coderabbitai

coderabbitai Bot commented Jan 20, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

- 将 MagicDrag.channelName 改为 readonly
- 移除冗余的类型别名 MagicDragAnyEventListenerStore
- 更新 MagicDragManager 使用正确的类型 MagicDragEventListenerStore

修复了以下 ESLint 错误:
- sonarjs/public-static-readonly
- sonarjs/redundant-type-aliases

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1685ca4b7f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/magic-drag/MagicDragManager.ts
- 添加 messageListeners Set 来存储通过 addEventListener 注册的监听器
- 实现 addEventListener 方法
- 实现 removeEventListener 方法
- 修改 postMessage 方法以同时触发 onmessage 和 addEventListener 监听器

修复了测试环境中的 TypeError: channel.addEventListener is not a function 错误
问题:在 handleExternalDragMove 中,只有当鼠标进入某个 Tab 时才更新 activeTabId,但鼠标离开 Tab 时不会清除它。这导致当用户拖出所有 Tab 后,activeTabId 仍然保持为最后一个进入的 Tab ID,使得 MagicDrag.handleDragEnd 中的 abort 条件 (!dragState.activeTabId) 永远无法满足。

修复:在 handleExternalDragMove 中添加逻辑,当鼠标不在当前 Tab 内且 activeTabId 等于当前 Tab ID 时,将 activeTabId 设为 null。这样当用户拖出所有 Tab 时,activeTabId 会变为 null,从而正确触发 DRAG_ABORT。
@wszxdhr
wszxdhr merged commit ae2d1dd into main Jan 20, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant