event

package
v0.0.0-...-26e1b9e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

README

Event模块

Event模块提供了应用事件总线的发布和订阅功能。目前只实现了应用内的订阅发布功能,后续可以拓展rpc之间的订阅和发布。

使用示例:

func calculator(a int, b int) {
	fmt.Printf("%d\n", a+b)
}

func TestNew(t *testing.T) {
	bus := New()
	bus.Subscribe("main:calculator", calculator)
	bus.Publish("main:calculator", 20, 40)
	bus.Unsubscribe("main:calculator", calculator)
}

创建新的EventBus实例:

bus := EventBus.New();

订阅topic:

func Handler() { ... }
...
bus.Subscribe("topic:handler", Handler)

只订阅topic一次,回调函数处理完毕后会被移除。

func HelloWorld() { ... }
...
bus.SubscribeOnce("topic:handler", HelloWorld)

取消订阅topic:

bus.Unsubscribe("topic:handler", HelloWord);

判断事发后有回调函数:

HasCallback(topic string) bool

发布事件:

func Handler(str string) { ... }
...
bus.Subscribe("topic:handler", Handler)
...
bus.Publish("topic:handler", "Hello, World!")

异步订阅事件:

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("开始")
fmt.Println("结束")

bus.WaitAsync() 

fmt.Println("等待中")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

type Bus interface {
	BusController
	BusSubscriber
	BusPublisher
}

func New

func New() Bus

type BusController

type BusController interface {
	HasCallback(topic string) bool
	WaitAsync()
}

type BusPublisher

type BusPublisher interface {
	Publish(topic string, args ...interface{})
}

type BusSubscriber

type BusSubscriber interface {
	Subscribe(topic string, fn interface{}) error
	SubscribeAsync(topic string, fn interface{}, transactional bool) error
	SubscribeOnce(topic string, fn interface{}) error
	SubscribeOnceAsync(topic string, fn interface{}) error
	Unsubscribe(topic string, handler interface{}) error
}

type EventBus

type EventBus struct {
	// contains filtered or unexported fields
}

func (*EventBus) HasCallback

func (bus *EventBus) HasCallback(topic string) bool

func (*EventBus) Publish

func (bus *EventBus) Publish(topic string, args ...interface{})

func (*EventBus) Subscribe

func (bus *EventBus) Subscribe(topic string, fn interface{}) error

func (*EventBus) SubscribeAsync

func (bus *EventBus) SubscribeAsync(topic string, fn interface{}, transactional bool) error

func (*EventBus) SubscribeOnce

func (bus *EventBus) SubscribeOnce(topic string, fn interface{}) error

func (*EventBus) SubscribeOnceAsync

func (bus *EventBus) SubscribeOnceAsync(topic string, fn interface{}) error

func (*EventBus) Unsubscribe

func (bus *EventBus) Unsubscribe(topic string, handler interface{}) error

func (*EventBus) WaitAsync

func (bus *EventBus) WaitAsync()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL