go-eventbus event bus
Address: github.com/lockp111/go-eventbus
Refer to go-observable, but there is a problem with the code, and it will crash because the map is read and written at the same time. After I repaired it and submitted it to the author, I still ignored me. , And if you don’t pay attention to using func, there may be two identical func, so change to interface, Benchmark shows a very big improvement
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) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。