Documentation ¶
Overview ¶
Package event is lightweight event manager and dispatcher implements by Go.
Index ¶
- Constants
- Variables
- func AddEvent(e Event)
- func AddSubscriber(sbr Subscriber)
- func AsyncFire(e Event)
- func FireBatch(es ...interface{}) []error
- func FireEvent(e Event) error
- func HasEvent(name string) bool
- func HasListeners(name string) bool
- func Listen(name string, listener Listener, priority ...int)
- func On(name string, listener Listener, priority ...int)
- func Reset()
- func Subscribe(sbr Subscriber)
- func TriggerEvent(e Event) error
- type BasicEvent
- func (e *BasicEvent) Abort(abort bool)
- func (e *BasicEvent) Add(key string, val interface{})
- func (e *BasicEvent) AttachTo(em ManagerFace)
- func (e *BasicEvent) Data() map[string]interface{}
- func (e *BasicEvent) Fill(target interface{}, data M) *BasicEvent
- func (e *BasicEvent) Get(key string) interface{}
- func (e *BasicEvent) IsAborted() bool
- func (e *BasicEvent) Name() string
- func (e *BasicEvent) Set(key string, val interface{})
- func (e *BasicEvent) SetData(data M) Event
- func (e *BasicEvent) SetName(name string) *BasicEvent
- func (e *BasicEvent) SetTarget(target interface{}) *BasicEvent
- func (e *BasicEvent) Target() interface{}
- type ByPriorityItems
- type Event
- type Listener
- type ListenerFunc
- type ListenerItem
- type ListenerQueue
- func (lq *ListenerQueue) Clear()
- func (lq *ListenerQueue) IsEmpty() bool
- func (lq *ListenerQueue) Items() []*ListenerItem
- func (lq *ListenerQueue) Len() int
- func (lq *ListenerQueue) Push(li *ListenerItem) *ListenerQueue
- func (lq *ListenerQueue) Remove(listener Listener)
- func (lq *ListenerQueue) Sort() *ListenerQueue
- type M
- type Manager
- func (em *Manager) AddEvent(e Event)
- func (em *Manager) AddListener(name string, listener Listener, priority ...int)
- func (em *Manager) AddSubscriber(sbr Subscriber)
- func (em *Manager) AsyncFire(e Event)
- func (em *Manager) AwaitFire(e Event) (err error)
- func (em *Manager) Clear()
- func (em *Manager) Fire(name string, params M) (err error, e Event)
- func (em *Manager) FireBatch(es ...interface{}) (ers []error)
- func (em *Manager) FireEvent(e Event) (err error)
- func (em *Manager) GetEvent(name string) (e Event, ok bool)
- func (em *Manager) HasEvent(name string) bool
- func (em *Manager) HasListeners(name string) bool
- func (em *Manager) Listen(name string, listener Listener, priority ...int)
- func (em *Manager) ListenedNames() map[string]int
- func (em *Manager) Listeners() map[string]*ListenerQueue
- func (em *Manager) ListenersByName(name string) *ListenerQueue
- func (em *Manager) ListenersCount(name string) int
- func (em *Manager) MustFire(name string, params M) Event
- func (em *Manager) MustTrigger(name string, params M) Event
- func (em *Manager) On(name string, listener Listener, priority ...int)
- func (em *Manager) RemoveEvent(name string)
- func (em *Manager) RemoveEvents()
- func (em *Manager) RemoveListener(name string, listener Listener)
- func (em *Manager) RemoveListeners(name string)
- func (em *Manager) Reset()
- func (em *Manager) Subscribe(sbr Subscriber)
- func (em *Manager) Trigger(name string, params M) (error, Event)
- type ManagerFace
- type Subscriber
Constants ¶
const ( Min = -300 Low = -200 BelowNormal = -100 Normal = 0 AboveNormal = 100 High = 200 Max = 300 )
There are some default priority constants
const Wildcard = "*"
Wildcard event name
Variables ¶
var DefaultEM = NewManager("default")
DefaultEM default event manager
Functions ¶
func AddSubscriber ¶
func AddSubscriber(sbr Subscriber)
AddSubscriber register a listener to the event
func HasListeners ¶
HasListeners has listeners for the event name.
Types ¶
type BasicEvent ¶
type BasicEvent struct {
// contains filtered or unexported fields
}
BasicEvent a basic event struct define.
func (*BasicEvent) AttachTo ¶
func (e *BasicEvent) AttachTo(em ManagerFace)
AttachTo add current event to the event manager.
func (*BasicEvent) Fill ¶
func (e *BasicEvent) Fill(target interface{}, data M) *BasicEvent
Fill event data
func (*BasicEvent) SetData ¶
func (e *BasicEvent) SetData(data M) Event
SetData set data to the event
func (*BasicEvent) SetName ¶
func (e *BasicEvent) SetName(name string) *BasicEvent
SetName set event name
func (*BasicEvent) SetTarget ¶
func (e *BasicEvent) SetTarget(target interface{}) *BasicEvent
SetTarget set event target
type ByPriorityItems ¶
type ByPriorityItems []*ListenerItem
ByPriorityItems type. implements the sort.Interface
func (ByPriorityItems) Less ¶
func (ls ByPriorityItems) Less(i, j int) bool
Less implements the sort.Interface.Less.
func (ByPriorityItems) Swap ¶
func (ls ByPriorityItems) Swap(i, j int)
Swap implements the sort.Interface.Swap.
type Event ¶
type Event interface { Name() string Get(key string) interface{} Set(key string, val interface{}) Add(key string, val interface{}) Data() map[string]interface{} SetData(M) Event Abort(bool) IsAborted() bool }
Event interface
type ListenerFunc ¶
ListenerFunc func definition.
func (ListenerFunc) Handle ¶
func (fn ListenerFunc) Handle(e Event) error
Handle event. implements the Listener interface
type ListenerItem ¶
ListenerItem storage a event listener and it's priority value.
type ListenerQueue ¶
type ListenerQueue struct {
// contains filtered or unexported fields
}
ListenerQueue storage sorted Listener instance.
func (*ListenerQueue) IsEmpty ¶
func (lq *ListenerQueue) IsEmpty() bool
IsEmpty get items length == 0
func (*ListenerQueue) Items ¶
func (lq *ListenerQueue) Items() []*ListenerItem
Items get all ListenerItem
func (*ListenerQueue) Push ¶
func (lq *ListenerQueue) Push(li *ListenerItem) *ListenerQueue
Push get items length
func (*ListenerQueue) Remove ¶
func (lq *ListenerQueue) Remove(listener Listener)
Remove a listener from the queue
func (*ListenerQueue) Sort ¶
func (lq *ListenerQueue) Sort() *ListenerQueue
Sort the queue items by ListenerItem's priority. Priority:
High > Low
type Manager ¶
type Manager struct { sync.Mutex // EnableLock enable lock on fire event. EnableLock bool // contains filtered or unexported fields }
Manager event manager definition. for manage events and listeners
func (*Manager) AddListener ¶
AddListener alias of the method On()
func (*Manager) AddSubscriber ¶
func (em *Manager) AddSubscriber(sbr Subscriber)
AddSubscriber add events by subscriber interface. you can register multi event listeners in a struct func. more usage please see README or test.
func (*Manager) AwaitFire ¶
AwaitFire async fire event by 'go' keywords, but will wait return result
func (*Manager) FireBatch ¶
FireBatch fire multi event at once. Usage:
FireBatch("name1", "name2", &MyEvent{})
func (*Manager) HasListeners ¶
HasListeners has listeners for the event name.
func (*Manager) ListenedNames ¶
ListenedNames get listened event names
func (*Manager) Listeners ¶
func (em *Manager) Listeners() map[string]*ListenerQueue
Listeners get all listeners
func (*Manager) ListenersByName ¶
func (em *Manager) ListenersByName(name string) *ListenerQueue
ListenersByName get listeners by given event name
func (*Manager) ListenersCount ¶
ListenersCount get listeners number for the event name.
func (*Manager) MustTrigger ¶
MustTrigger alias of the method MustFire()
func (*Manager) On ¶
On register a event handler/listener. can setting priority.
Usage:
On("evt0", listener) On("evt0", listener, High)
func (*Manager) RemoveEvent ¶
RemoveEvent delete Event by name
func (*Manager) RemoveEvents ¶
func (em *Manager) RemoveEvents()
RemoveEvents remove all registered events
func (*Manager) RemoveListener ¶
RemoveListener remove a given listener, you can limit event name.
Usage:
RemoveListener("", listener) RemoveListener("name", listener) // limit event name.
func (*Manager) RemoveListeners ¶
RemoveListeners remove listeners by given name
func (*Manager) Subscribe ¶
func (em *Manager) Subscribe(sbr Subscriber)
Subscribe alias of the AddSubscriber()
type ManagerFace ¶
type ManagerFace interface { // AddEvent events: add event AddEvent(Event) // On listeners: add listeners On(name string, listener Listener, priority ...int) // Fire event Fire(name string, params M) (error, Event) }
ManagerFace event manager interface
type Subscriber ¶
type Subscriber interface { // SubscribedEvents register event listeners // key: is event name // value: can be Listener or ListenerItem interface SubscribedEvents() map[string]interface{} }
Subscriber event subscriber interface. you can register multi event listeners in a struct func.