-
-
Notifications
You must be signed in to change notification settings - Fork 70
refactor: eventBus #465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
refactor: eventBus #465
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @boomwwww, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
此拉取请求主要对项目的事件总线模块进行了全面的重构和优化。核心目标是提高事件通信的类型安全性、标准化事件命名,并清理项目中与事件总线相关的冗余代码。通过这些改进,系统内部的事件流将更加清晰、可预测,并减少潜在的运行时错误,从而提升整体代码质量和开发体验。
Highlights
- 事件总线核心重构与类型安全增强: 核心的事件总线工具(
eventBus.ts
)经过重构,现在封装了mitt
库,并引入了详细的类型定义(如WebSocketEvent
、IframeEvent
、EditorEvent
),极大地增强了事件的类型安全性。 - 事件命名规范化: 所有事件名称都已标准化,采用了
类型:事件名
的命名约定(例如,iframe:refresh-game
、editor:update-scene
、web-socket:on-message
),提高了事件的可读性和可维护性。 - 事件载荷标准化为对象: 事件的有效载荷现在统一以对象形式传递(例如,
{ scene: string }
、{ message: string }
、{ sentence: string }
),而非原始值,这使得事件数据结构更加清晰,并进一步强化了类型检查。 - 清理冗余代码与
@ts-ignore
注释: 移除了多个组件中与旧事件总线相关的冗余代码和// @ts-ignore
注释,例如MainArea.tsx
、GraphicalEditor.tsx
和EditorDebugger.tsx
,提升了整体代码质量和可读性。 - 本地化文件行号更新: 由于源文件中的代码调整,所有相关的本地化文件(
en.po
、ja.po
、zhCn.po
)中的msgid
注释行号已同步更新。
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
本次 PR 对事件总线(eventBus)进行了重构,通过封装 mitt
库实现了类型安全,这是一个非常好的改进。同时,对事件名称和事件对象类型进行了统一调整,使得事件系统更加清晰和规范。代码中移除了相关的 @ts-ignore
注释,并清理了部分无用代码,提升了代码质量。
整体来看,这次重构非常成功。我只在 eventBus.ts
的实现上发现了一个可以简化的地方,具体请看文件中的评论。
const handlers = new Map<keyof Events, Set<Handler<Events[keyof Events]>>>(); | ||
|
||
const all = bus.all; | ||
|
||
/** 订阅事件 */ | ||
const on = <K extends keyof Events>(type: K, handler: Handler<Events[K]>): void => { | ||
const handlerSet = handlers.get(type); | ||
if (!handlerSet) { | ||
const newHandlerSet = new Set<Handler<Events[keyof Events]>>(); | ||
newHandlerSet.add(handler as Handler<Events[keyof Events]>); | ||
handlers.set(type, newHandlerSet); | ||
} else { | ||
handlerSet.add(handler as Handler<Events[keyof Events]>); | ||
} | ||
bus.on(type, handler); | ||
}; | ||
|
||
/** 发布事件 */ | ||
const emit = <K extends keyof Events>(type: K, event: Events[K]): void => { | ||
bus.emit(type, event); | ||
}; | ||
|
||
/** 取消订阅事件 */ | ||
const off = <K extends keyof Events>(type: K, handler: Handler<Events[K]>): void => { | ||
const handlerSet = handlers.get(type); | ||
if (handlerSet) { | ||
handlerSet.delete(handler as Handler<Events[keyof Events]>); | ||
if (handlerSet.size === 0) { | ||
handlers.delete(type); | ||
} | ||
} | ||
bus.off(type, handler); | ||
}; | ||
|
||
export const eventBus = { all, on, emit, off }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
整理事件总线模块
mitt
库进行封装,使其类型安全