1 Star 2 Fork 0

werbenhu/eventbus

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
singleton.go 1.75 KB
一键复制 编辑 原始数据 按行查看 历史
package eventbus
var (
// singleton is a pointer to a unbuffered EventBus instance, which will be created when necessary.
singleton *EventBus
)
func init() {
ResetSingleton()
}
// ResetSingleton resets the singleton object. If the singleton object is not nil,
// it first closes the old singleton, and then creates a new singleton instance.
func ResetSingleton() {
if singleton != nil {
singleton.Close()
}
singleton = New()
}
// Unsubscribe removes handler defined for a topic.
// Returns error if there are no handlers subscribed to the topic.
func Unsubscribe(topic string, handler any) error {
return singleton.Unsubscribe(topic, handler)
}
// Subscribe subscribes to a topic, return an error if the handler is not a function.
// The handler must have two parameters: the first parameter must be a string,
// and the type of the handler's second parameter must be consistent with the type of the payload in `Publish()`
func Subscribe(topic string, handler any) error {
return singleton.Subscribe(topic, handler)
}
// Publish triggers the handlers defined for a topic. The `payload` argument will be passed to the handler.
// The type of the payload must correspond to the second parameter of the handler in `Subscribe()`.
func Publish(topic string, payload any) error {
return singleton.Publish(topic, payload)
}
// PublishSync is a synchronous version of Publish that triggers the handlers defined for a topic with the given payload.
// The type of the payload must correspond to the second parameter of the handler in `Subscribe()`.
func PublishSync(topic string, payload any) error {
return singleton.Publish(topic, payload)
}
// Close closes the singleton instance of EventBus.
func Close() {
if singleton != nil {
singleton.Close()
singleton = nil
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/werbenhu/eventbus.git
git@gitee.com:werbenhu/eventbus.git
werbenhu
eventbus
eventbus
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385