Documentation
¶
Overview ¶
Package events 提供了简单的事件发布订阅功能
e := events.New[string]()
// 订阅事件
e.Attach(func(data string){
fmt.Println("subscriber 1:", data)
})
// 订阅事件
e.Attach(func(data string){
fmt.Println("subscriber 2:", data)
})
e.Publish(true, "test") // 发布事件
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrStopped = errors.New("该事件已经停止发布新内容")
ErrStopped 表示发布都已经调用 [Publisher.Destroy] 销毁了事件处理器
Functions ¶
This section is empty.
Types ¶
type Eventer ¶ added in v0.2.0
type Eventer[T any] interface { Publisher[T] Subscriber[T] }
type Publisher ¶
type Publisher[T any] interface { // Publish 触发事件 // // sync 表示订阅者是否以异步的方式执行; // data 传递给订阅者的数据; Publish(sync bool, data T) error // Destroy 销毁当前事件处理程序 Destroy() }
Publisher 事件的发布者
type SubscribeFunc ¶ added in v0.6.0
type SubscribeFunc[T any] func(data T)
SubscribeFunc 订阅者函数
每个订阅函数都是通过 go 异步执行。
data 为事件传递过来的数据,可能存在多个订阅者, 用户不应该直接修改 data 数据,否则结果是未知的。
type Subscriber ¶
type Subscriber[T any] interface { // Attach 注册订阅者 // // 返回唯一 ID,用户可以使用此 ID 取消订阅。 Attach(SubscribeFunc[T]) (int, error) // Detach 取消指定事件的订阅 Detach(int) }
Subscriber 供用户订阅事件的对象接口
Click to show internal directories.
Click to hide internal directories.