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

Eventbus需求 #158

Open
zhangfisher opened this issue Sep 17, 2022 · 1 comment
Open

Eventbus需求 #158

zhangfisher opened this issue Sep 17, 2022 · 1 comment

Comments

@zhangfisher
Copy link

zhangfisher commented Sep 17, 2022

malagu应该内置一个Eventbus作为基础能力,用来为模块与模块之间的提供一种高效、易用的通讯机制,帮助模块与模块之间实现松耦合。

@malgu@eventbus应该具备以下能力:

  • 发布订阅功能
    模块可以按需要订阅框架或者其他模块的发布的消息

  • 全局消息广播

  • 点对点通讯

主要特性应该包括:

  • 模块寻址

在总线内部,每一个模块均需要具备一个地址标识,可以是模块id之类,只需要全局唯一即可。
模块地址应作为模块通讯的

  • 具备一定描述能力的消息格式
interface Message{
   id:number;                    // 可以是自增值,由框架产生
   from:string;                  // 消息发送者
   to?:string;                     // 消息接收者,仅在点对点时有用
  sid?:number
   meta:{              
        forwards:number
        [key:string]: string | number | booean
    }
    payload:any             // 消息负载
}

meta供应用层扩展提供一些额外的数据,特别有用。比如MQTT模块可以在meta中提供一些topic数据;TCP模块可以提供ip地址之类的。

forwards用来记录消息在总线内部流转的次数,当经总线转发一次就+1,总线可以设定一个最大forwards值,用来用来防止消息回路时使用。

sid用来供模块之间进行会话时标识消息使用,当模块与模块之间需要进行多次通讯时有用

  • 层次主题

总线发布的消息主题应支持层次结构,例a/b/c之类的,最好支持通配符。

  • 前端订阅

前端也可以向后端订阅?

  • 框架生命周期事件
    框架、组件、模块在其生命周期内也发布一些事件供应用层订阅
    一些重要的事件甚至可以广播
@muxiangqiu
Copy link
Member

这里有个 nestjs 的实现,我们可以参考:https://github.com/nestjs/cqrs/blob/master/src/event-bus.ts

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

No branches or pull requests

2 participants