go-eventbus事件总线
地址: github.com/lockp111/go-eventbus
参考go-observable, 但是那个代码有问题, 会因为map同时读写出现crash, 我修复之后提交给作者也一直不理我, 于是就自己fork一份自用, 后来觉得反射调用的损耗还是比较大的, 而且使用func如果不注意, 有可能出现两个相同的func, 于是改成interface, Benchmark显示提升非常大
Usage
go get -u github.com/lockp111/go-eventbus
New()
Create a new bus struct reference
bus := eventbus.New()
On(topic string, e ...Event)
Subscribe event
type ready struct{
}
func (e ready) Dispatch(msg interface{}){
fmt.Println("I am ready!")
}
bus.On("ready", &ready{})
You can also subscribe multiple events for example:
type run struct{
}
func (e run) Dispatch(msg interface{}){
fmt.Println("I am run!")
}
bus.On("ready", &ready{}, &ready{}).On("run", &run{})
Off(topic string, e ...Event)
Unsubscribe event
e := &ready{}
bus.On("ready", e)
bus.Off("ready", e)
You can also unsubscribe multiple events for example:
e1 := &ready{}
e2 := &ready{}
bus.On("ready", e1, e2)
bus.Off("ready", e1, e2)
You can unsubscribe all events for example:
bus.On("ready", &ready{}, &ready{})
bus.Off("ready")
You can unsubscribe all topics for example:
bus.On("ready", &ready{}, &ready{})
bus.On("run", &run{})
bus.Off(ALL)
Trigger(topic string, msg ...interface{})
Dispatch events
bus.Trigger("ready")
You can also dispatch multiple events for example:
bus.Trigger("ready", &struct{"1"}, &struct{"2"})
You can also dispatch all events for example:
bus.Trigger(ALL, &struct{"1"})
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。