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

EventHub 粗糙实现笔记 #25

Open
Pomelo1213 opened this issue Oct 18, 2019 · 0 comments
Open

EventHub 粗糙实现笔记 #25

Pomelo1213 opened this issue Oct 18, 2019 · 0 comments

Comments

@Pomelo1213
Copy link
Owner

EventHub 基本实现

  • 首先 eventHub 是模块之间进行传递的一种方式,采用发布订阅模式。关于概念好想也没啥说的。下一步就是找到 eventHub 需要实现的方法,分别是 emitonoff
  • 基本思路是,我们有个缓存能存储监听器,当时对应的事件触发(emit)时,调用监听器就好,当想要注销事件时,off 一下就好了。
    carbon

上述就是简单的实现啦,实际上我们可以再抽象一下监听器的概念,将所有的 handler 都抽象成一个 listener ,类似于 addEventListener 一样。
carbon (1)
实际上到这一步,我们很粗糙的 EventHub 算是完成了,但是我们还可以尽量的优化一下。

下面就是优化后的版本。
carbon (2)

如果,觉得图太长了,可以来我的 github 瞧瞧。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant