Skip to content

Conversation

boomwwww
Copy link
Contributor

整理事件总线模块

  • mitt库进行封装,使其类型安全
  • 调整了消息频道名称和事件对象类型
  • 整理了部分组件的格式,清理了 部分组件的 和事件总线有关的 无用代码

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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库,并引入了详细的类型定义(如WebSocketEventIframeEventEditorEvent),极大地增强了事件的类型安全性。
  • 事件命名规范化: 所有事件名称都已标准化,采用了类型:事件名的命名约定(例如,iframe:refresh-gameeditor:update-sceneweb-socket:on-message),提高了事件的可读性和可维护性。
  • 事件载荷标准化为对象: 事件的有效载荷现在统一以对象形式传递(例如,{ scene: string }{ message: string }{ sentence: string }),而非原始值,这使得事件数据结构更加清晰,并进一步强化了类型检查。
  • 清理冗余代码与@ts-ignore注释: 移除了多个组件中与旧事件总线相关的冗余代码和// @ts-ignore注释,例如MainArea.tsxGraphicalEditor.tsxEditorDebugger.tsx,提升了整体代码质量和可读性。
  • 本地化文件行号更新: 由于源文件中的代码调整,所有相关的本地化文件(en.poja.pozhCn.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

  1. 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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 的实现上发现了一个可以简化的地方,具体请看文件中的评论。

Comment on lines 23 to 57
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 };
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

这个对 mitt 的封装很好,实现了类型安全。不过,handlers 这个 Map 以及对 on, off, emit 的封装似乎是多余的。mitt 实例 bus 内部已经管理了事件和处理函数的映射,并且其方法已经是类型安全的。

为了简化代码,可以直接导出 mitt 实例的方法,如下面的建议所示。这样做既能保持类型安全,又能让代码更简洁。

export const eventBus = {
  all: bus.all,
  on: bus.on,
  emit: bus.emit,
  off: bus.off,
};

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