eventbus

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Min 最低优先级
	Min = -300
	// Low 低优先级
	Low = -200
	// BelowNormal 相对低优先级
	BelowNormal = -100
	// Normal 正常优先级
	Normal = 0
	// AboveNormal 稍高优先级
	AboveNormal = 100
	// High 高优先级
	High = 200
	// Max 最高优先级
	Max = 300
)
View Source
const Wildcard = "*"

Wildcard 事件名称,通配符,表示所有

Variables

This section is empty.

Functions

func AddEvent

func AddEvent(e IEvent) error

AddEvent 添加事件到管理器 e: 事件对象

func AsyncPublish

func AsyncPublish(e IEvent)

AsyncPublish 异步触发事件 e:事件对象

func HasEvent

func HasEvent(name string) bool

HasEvent 判断事件是否存在

func HasListeners

func HasListeners(name string) bool

HasListeners 判断是否存在指定名称的监听方法 name: 事件名

func Listen

func Listen(name string, listener BaseListener, priority ...int) (err error)

Listen 监听事件 name: 事件名 listener:事件监听器 priority:监听优先级

func ListenedNames

func ListenedNames() []string

ListenedNames 获取监听的事件名列表

func Listeners

func Listeners() map[string]*ListenerQueue

Listeners 获取所有监听列表

func ListenersCount

func ListenersCount(name string) int

ListenersCount 获取指定事件名下的监听方法数量

func Publish

func Publish(e IEvent) error

Publish 使用事件对象触发事件 e:事件对象

func PublishBatch

func PublishBatch(es ...interface{}) (ers []error)

PublishBatch 批量触发事件 Usage:

PublishBatch("name1", "name2", &MyEvent{})

func RemoveEvent

func RemoveEvent(name string)

RemoveEvent 移除自定义事件

func RemoveEvents

func RemoveEvents()

RemoveEvents 移除所有自定义事件

func RemoveListeners

func RemoveListeners(listener IListener)

RemoveListeners 移除监听方法

func RemoveListenersByName

func RemoveListenersByName(name string)

RemoveListenersByName 取消指定事件名的所有事件监听 name: 事件名

func Subscribe

func Subscribe(listener IListener, priority ...int) (err error)

Subscribe 订阅事件 listener: 事件监听器,支持通过Listen()方法获取监听事件的名称 priority:监听优先级

func UnListen

func UnListen(name string, listener BaseListener)

UnListen 取消事件的监听 name: 事件名 listener:事件监听器

func UnSubscribe

func UnSubscribe(listener IListener)

UnSubscribe 取消事件订阅 listener: 事件监听器,支持通过Listen()方法获取监听事件的名称

Types

type BaseListener

type BaseListener interface {
	Process(event IEvent) error
}

BaseListener 基础监听器

type Event

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

Event 事件的基础实现

func NewEvent

func NewEvent(name string, data map[interface{}]interface{}) *Event

NewEvent 创建事件

func (*Event) Abort

func (that *Event) Abort(abort bool)

Abort 设置事件是否终止

func (*Event) AttachTo

func (that *Event) AttachTo(m *EventBus) error

AttachTo 把事件加入到指定的管理器中

func (*Event) Data

func (that *Event) Data() map[interface{}]interface{}

Data 获取事件的全部参数

func (*Event) Get

func (that *Event) Get(key interface{}) interface{}

Get 获取元素

func (*Event) IsAborted

func (that *Event) IsAborted() bool

IsAborted 判断事件是否终止

func (*Event) Name

func (that *Event) Name() string

Name 获取事件名

func (*Event) Set

func (that *Event) Set(key interface{}, val interface{})

Set 设置元素

func (*Event) SetData

func (that *Event) SetData(data map[interface{}]interface{}) IEvent

SetData 设置事件的全部参数

func (*Event) SetName

func (that *Event) SetName(name string)

SetName 设置事件名

type EventBus added in v0.0.8

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

EventBus 事件管理器

func New

func New(name string) *EventBus

New 创建事件管理器对象

func (*EventBus) AddEvent added in v0.0.8

func (that *EventBus) AddEvent(e IEvent) error

AddEvent 添加自定义事件

func (*EventBus) AsyncPublish added in v0.0.8

func (that *EventBus) AsyncPublish(e IEvent)

AsyncPublish 异步触发事件

func (*EventBus) Clear added in v0.0.8

func (that *EventBus) Clear()

Clear 清空事件管理对象

func (*EventBus) Fire added in v0.0.8

func (that *EventBus) Fire(name string, params map[interface{}]interface{}) (e IEvent, err error)

Fire 使用事件名触发事件 name: 事件名 params:需要传递给事件的参数

func (*EventBus) GetEvent added in v0.0.8

func (that *EventBus) GetEvent(name string) (IEvent, bool)

GetEvent 根据name获取自定义事件

func (*EventBus) HasEvent added in v0.0.8

func (that *EventBus) HasEvent(name string) bool

HasEvent 判断自定义事件是否存在

func (*EventBus) HasListeners added in v0.0.8

func (that *EventBus) HasListeners(name string) bool

HasListeners 判断是否存在指定名称的监听

func (*EventBus) Listen added in v0.0.8

func (that *EventBus) Listen(name string, listener BaseListener, priority ...int) (err error)

Listen 监听事件

func (*EventBus) ListenedNames added in v0.0.8

func (that *EventBus) ListenedNames() []string

ListenedNames 获取监听的事件名列表

func (*EventBus) Listeners added in v0.0.8

func (that *EventBus) Listeners() map[string]*ListenerQueue

Listeners 获取监听列表

func (*EventBus) ListenersByName added in v0.0.8

func (that *EventBus) ListenersByName(name string) *ListenerQueue

ListenersByName 获取指定事件名称的监听列表

func (*EventBus) ListenersCount added in v0.0.8

func (that *EventBus) ListenersCount(name string) int

ListenersCount 获取指定名称的监听列表数量

func (*EventBus) Publish added in v0.0.8

func (that *EventBus) Publish(e IEvent) error

Publish 触发事件

func (*EventBus) PublishBatch added in v0.0.8

func (that *EventBus) PublishBatch(es ...interface{}) (ers []error)

PublishBatch 批量触发事件 Usage:

PublishBatch("name1", "name2", &MyEvent{})

func (*EventBus) RemoveEvent added in v0.0.8

func (that *EventBus) RemoveEvent(name string)

RemoveEvent 移除自定义事件

func (*EventBus) RemoveEvents added in v0.0.8

func (that *EventBus) RemoveEvents()

RemoveEvents 移除所有自定义事件

func (*EventBus) RemoveListeners added in v0.0.8

func (that *EventBus) RemoveListeners(listener BaseListener)

RemoveListeners 移除监听方法

func (*EventBus) RemoveListenersByName added in v0.0.8

func (that *EventBus) RemoveListenersByName(name string)

RemoveListenersByName 清除所有指定事件名的事件监听器

func (*EventBus) Reset added in v0.0.8

func (that *EventBus) Reset()

Reset 重置事件管理对象

func (*EventBus) Subscribe added in v0.0.8

func (that *EventBus) Subscribe(listener IListener, priority ...int) (err error)

Subscribe 注册监听事件

func (*EventBus) UnListen added in v0.0.8

func (that *EventBus) UnListen(name string, listener BaseListener)

UnListen 移除事件监听

func (*EventBus) UnSubscribe added in v0.0.8

func (that *EventBus) UnSubscribe(listener IListener)

UnSubscribe 取消监听事件

type IEvent

type IEvent interface {
	// Name 获取事件名
	Name() string
	// Get 获取事件携带的参数
	Get(key interface{}) interface{}
	// Set 设置事件参数
	Set(key interface{}, val interface{})
	// Data 获取事件的全部参数
	Data() map[interface{}]interface{}
	// SetData 批量设置事件参数
	SetData(map[interface{}]interface{}) IEvent
	// Abort 设置是否中途停止,设置为true,则表示执行到该监听器为止,后续监听器不在执行
	Abort(bool)
	// IsAborted 判断是否要继续触发后续监听器
	IsAborted() bool
}

IEvent 事件的接口

func Fire

func Fire(name string, params map[interface{}]interface{}) (e IEvent, err error)

Fire 使用事件名触发事件 name: 事件名 params:需要传递给事件的参数

func GetEvent

func GetEvent(name string) (IEvent, bool)

GetEvent 获取指定事件名的事件对象

type IListener

type IListener interface {
	Listen() []interface{}
	Process(event IEvent) error
}

IListener 完全体监听器

type ListenerFunc

type ListenerFunc func(e IEvent) error

ListenerFunc 强制把监听的方法转换成对象

func (ListenerFunc) Process

func (fn ListenerFunc) Process(e IEvent) error

type ListenerItem

type ListenerItem struct {
	Priority int
	Listener BaseListener
}

ListenerItem 事件监听的数据结构

func NewListenerItem

func NewListenerItem(listener BaseListener, priority int) *ListenerItem

NewListenerItem 新建事件监听条目

type ListenerQueue

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

ListenerQueue 监听队列,以优先级排序

func ListenersByName

func ListenersByName(name string) *ListenerQueue

ListenersByName 获取指定事件名下的监听列表

func (*ListenerQueue) Add

func (that *ListenerQueue) Add(item *ListenerItem)

Add 存入条目到优先队列

func (*ListenerQueue) Clear

func (that *ListenerQueue) Clear()

Clear 清除事件

func (*ListenerQueue) IsEmpty

func (that *ListenerQueue) IsEmpty() bool

IsEmpty 判断监听队列是否为空

func (*ListenerQueue) Items

func (that *ListenerQueue) Items() []*ListenerItem

Items 获取所有监听器条目

func (*ListenerQueue) Len

func (that *ListenerQueue) Len() int

Len 获取优先队列的长度

func (*ListenerQueue) Remove

func (that *ListenerQueue) Remove(listener BaseListener)

Remove 移除指定的监听

Jump to

Keyboard shortcuts

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