代码拉取完成,页面将自动刷新
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
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。