Skip to content
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

feat(EventEmitter): 新增事件链式注册,统一清除功能 #1968

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

admin1949
Copy link
Contributor

增强现有的事件系统的注册,清除功能

Copy link

changeset-bot bot commented Nov 21, 2024

⚠️ No Changeset found

Latest commit: 59950d3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@admin1949
Copy link
Contributor Author

@ChangeSuger 👋,这里是事件系统的调整代码

@boyongjiong
Copy link
Collaborator

嗨~ 你能补充一下这个功能的使用场景吗?然后最好在 feature-example 项目中增加一个使用 demo。

我希望能解释一下增加这个功能的必要性

因为现在在我看来,我们支持一次注册多个时间 eventCenter.on('x1,x2,x3', () => { // callback } );也应该支持一次性 eventCenter.off('x1,x2,x3', () => {}) 这样。链式调用的好处和必要性是什么呢❓(因为我目前看注释,定义 off 方法,是链式调用的返回结果,后面调用 off 的地方,可能还不如我上面这个写法直观,需要回溯去看 off 的定义,再看是否能直接复用)

@boyongjiong
Copy link
Collaborator

我刚在路上突然想到,你这个设计好像跟我们之前的用法也不冲突,好像也没有啥问题

@admin1949
Copy link
Contributor Author

嗨~ 你能补充一下这个功能的使用场景吗?然后最好在 feature-example 项目中增加一个使用 demo。

我希望能解释一下增加这个功能的必要性

因为现在在我看来,我们支持一次注册多个时间 eventCenter.on('x1,x2,x3', () => { // callback } );也应该支持一次性 eventCenter.off('x1,x2,x3', () => {}) 这样。链式调用的好处和必要性是什么呢❓(因为我目前看注释,定义 off 方法,是链式调用的返回结果,后面调用 off 的地方,可能还不如我上面这个写法直观,需要回溯去看 off 的定义,再看是否能直接复用)

我的想法是这样的,对于多个不同的事件,应该更友好的支持用户使用不同的回调函数。并且修改之后的代码也兼容以前的写法

Copy link
Collaborator

@boyongjiong boyongjiong left a comment

Choose a reason for hiding this comment

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

辛苦看一下我的注释,欢迎讨论~

on<T extends string>(evt: T, callback: EventCallback<T>): void
on(evt: string, callback: EventCallback) {
this.graphModel.eventCenter.on(evt, callback)
on: OnMethod = (evt: string, callback: EventCallback, once?: boolean) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

我感觉类型命名 OnEvent 会比 OnMethod 好一些

Copy link
Contributor Author

Choose a reason for hiding this comment

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

确实是这样 OnEvent 是要更好一点

once<T extends string>(evt: T, callback: EventCallback<T>): void
once(evt: string, callback: EventCallback) {
this.graphModel.eventCenter.once(evt, callback)
once: OnceMethod = (evt: string, callback: EventCallback) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

同上,OnceMethod -> OnceEvent

Copy link
Contributor Author

Choose a reason for hiding this comment

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

+1

callback: EventCallback<T>,
once?: boolean,
): ClearMethod
(evt: string, callback: EventCallback, once?: boolean): ClearMethod
Copy link
Collaborator

Choose a reason for hiding this comment

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

类型定义这儿不需要 35 这一行的类型定义了感觉,因为这一行会导致使用时 ts 类型提示失效
类型重载,上面两个是类型,下面是方法定义。

image

image 现在这种写法会导致 上图上中类型提示失效,你可以再确认下

以下为参考:

type OnEvent = {
  <T extends keyof EventArgs>(evt: T, callback: EventCallback<T>): void;
  <T extends string>(evt: T, callback: EventCallback<T>): void;
};

once: OnceEvent = (evt: string, callback: EventCallback) => {
  this.graphModel.eventCenter.once(evt, callback);
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

我是了一下,并不会导致类型失效。但其实按照我们现有的代码逻辑,永远不会走到最后一个函数重载,删掉也是对的

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

Copy link
Collaborator

Choose a reason for hiding this comment

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

我是了一下,并不会导致类型失效。但其实按照我们现有的代码逻辑,永远不会走到最后一个函数重载,删掉也是对的

@admin1949 补充一下类型提示的区别你看下
当前 PR:
image

之前的类型提示:
image

你看下提示的区别。 @ChangeSuger 你有空可以看下这个问题怎么解?这个 PR 别的应该都 OK,就现在这个有点问题,哈哈哈

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@boyongjiong 我将代码切换到最新的master分支测试了一下出来的效果和我提交PR之后是一样的,我认为这只是编辑器对于类型提示的风格不一样,并不会影响实际的功能
image

Copy link
Collaborator

Choose a reason for hiding this comment

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

@boyongjiong 我将代码切换到最新的master分支测试了一下出来的效果和我提交PR之后是一样的,我认为这只是编辑器对于类型提示的风格不一样,并不会影响实际的功能 image

这个是 vscode 吗?你鼠标放到 data 上会出现 data 的类型提示吗?像下面这样
我纠结的点是因为如果提示的是 NodeEventArgsPick<> 这个,对用户是无帮助的,像下面这样才是符合预期的

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

有的呀,我用 vscode 和 WebStorm 都检验过了
这是vscode的截图
image
这里是 WebStorm 的截图
image

(evt: string, callback: EventCallback): ClearMethod
}

interface ClearMethod {
Copy link
Collaborator

Choose a reason for hiding this comment

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

建议:ClearMethod -> ClearCallback

Copy link
Contributor Author

Choose a reason for hiding this comment

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

同意

evt: T,
callback: EventCallback<T>,
on: OnMethod = (
evt: string,
Copy link
Collaborator

Choose a reason for hiding this comment

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

这块儿有点问题,evt 不能直接写成 string,上面说的类型提示失效好像是这块儿的问题,得再确认一下

@@ -149,4 +198,29 @@ export default class EventEmitter {
}
}

const createClearMethod = (
Copy link
Collaborator

Choose a reason for hiding this comment

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

createClearCallback

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

OnMethod -> OnEvent
删除无用的类型注释
@boyongjiong
Copy link
Collaborator

对了,我还忘了一件事儿,咱这个功能,应该在文档里面也体现一下,你看下官网项目,找到对应的事件,加一块儿进去写一下?

@admin1949
Copy link
Contributor Author

对了,我还忘了一件事儿,咱这个功能,应该在文档里面也体现一下,你看下官网项目,找到对应的事件,加一块儿进去写一下?

嗯嗯,我去弄下

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.

2 participants