在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:asaskevich/EventBus开源软件地址:https://github.com/asaskevich/EventBus开源编程语言:Go 100.0%开源软件介绍:EventBusPackage EventBus is the little and lightweight eventbus with async compatibility for GoLang. InstallationMake sure that Go is installed on your computer. Type the following command in your terminal:
After it the package is ready to use. Import package in your projectAdd following line in your import "github.com/asaskevich/EventBus" If you unhappy to use long import (
evbus "github.com/asaskevich/EventBus"
) Examplefunc calculator(a int, b int) {
fmt.Printf("%d\n", a + b)
}
func main() {
bus := EventBus.New();
bus.Subscribe("main:calculator", calculator);
bus.Publish("main:calculator", 20, 40);
bus.Unsubscribe("main:calculator", calculator);
} Implemented methods
New()New returns new EventBus with empty handlers. bus := EventBus.New(); Subscribe(topic string, fn interface{}) errorSubscribe to a topic. Returns error if func Handler() { ... }
...
bus.Subscribe("topic:handler", Handler) SubscribeOnce(topic string, fn interface{}) errorSubscribe to a topic once. Handler will be removed after executing. Returns error if func HelloWorld() { ... }
...
bus.SubscribeOnce("topic:handler", HelloWorld) Unsubscribe(topic string, fn interface{}) errorRemove callback defined for a topic. Returns error if there are no callbacks subscribed to the topic. bus.Unsubscribe("topic:handler", HelloWord); HasCallback(topic string) boolReturns true if exists any callback subscribed to the topic. Publish(topic string, args ...interface{})Publish executes callback defined for a topic. Any additional argument will be transferred to the callback. func Handler(str string) { ... }
...
bus.Subscribe("topic:handler", Handler)
...
bus.Publish("topic:handler", "Hello, World!"); SubscribeAsync(topic string, fn interface{}, transactional bool)Subscribe to a topic with an asynchronous callback. Returns error if func slowCalculator(a, b int) {
time.Sleep(3 * time.Second)
fmt.Printf("%d\n", a + b)
}
bus := EventBus.New()
bus.SubscribeAsync("main:slow_calculator", slowCalculator, false)
bus.Publish("main:slow_calculator", 20, 60)
fmt.Println("start: do some stuff while waiting for a result")
fmt.Println("end: do some stuff while waiting for a result")
bus.WaitAsync() // wait for all async callbacks to complete
fmt.Println("do some stuff after waiting for result") Transactional determines whether subsequent callbacks for a topic are run serially (true) or concurrently(false) SubscribeOnceAsync(topic string, args ...interface{})SubscribeOnceAsync works like SubscribeOnce except the callback to executed asynchronously WaitAsync()WaitAsync waits for all async callbacks to complete. Cross Process EventsWorks with two rpc services:
server.go func main() {
server := NewServer(":2010", "/_server_bus_", New())
server.Start()
// ...
server.EventBus().Publish("main:calculator", 4, 6)
// ...
server.Stop()
} client.go func main() {
client := NewClient(":2015", "/_client_bus_", New())
client.Start()
client.Subscribe("main:calculator", calculator, ":2010", "/_server_bus_")
// ...
client.Stop()
} NotesDocumentation is available here: godoc.org. Full information about code coverage is also available here: EventBus on gocover.io. SupportIf you do have a contribution for the package feel free to put up a Pull Request or open Issue. contributorsSpecial thanks to
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论