libMsgBus

package
v0.0.0-...-5961e9c Latest Latest
Warning

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

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

Documentation

Overview

Package event is lightweight event manager and dispatcher implements by Go.

Index

Constants

View Source
const (
	Min         = -300
	Low         = -200
	BelowNormal = -100
	Normal      = 0
	AboveNormal = 100
	High        = 200
	Max         = 300
)

There are some default priority constants

View Source
const Wildcard = "*"

Wildcard event name

Variables

View Source
var DefaultEM = NewManager("default")

DefaultEM default event manager

Functions

func AddEvent

func AddEvent(e Event)

AddEvent has event check.

func AddSubscriber

func AddSubscriber(sbr Subscriber)

AddSubscriber register a listener to the event

func AsyncFire

func AsyncFire(e Event)

AsyncFire async fire event by 'go' keywords

func FireBatch

func FireBatch(es ...interface{}) []error

FireBatch fire multi event at once.

func FireEvent

func FireEvent(e Event) error

FireEvent fire listeners by Event instance.

func HasEvent

func HasEvent(name string) bool

HasEvent has event check.

func HasListeners

func HasListeners(name string) bool

HasListeners has listeners for the event name.

func Listen

func Listen(name string, listener Listener, priority ...int)

Listen register a listener to the event

func On

func On(name string, listener Listener, priority ...int)

On register a listener to the event

func Reset

func Reset()

Reset the default event manager

func Subscribe

func Subscribe(sbr Subscriber)

Subscribe register a listener to the event

func TriggerEvent

func TriggerEvent(e Event) error

TriggerEvent alias of FireEvent

Types

type BasicEvent

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

BasicEvent a basic event struct define.

func NewBasic

func NewBasic(name string, data M) *BasicEvent

NewBasic new a basic event instance

func (*BasicEvent) Abort

func (e *BasicEvent) Abort(abort bool)

Abort event loop exec

func (*BasicEvent) Add

func (e *BasicEvent) Add(key string, val interface{})

Add value by key

func (*BasicEvent) AttachTo

func (e *BasicEvent) AttachTo(em ManagerFace)

AttachTo add current event to the event manager.

func (*BasicEvent) Data

func (e *BasicEvent) Data() map[string]interface{}

Data get all data

func (*BasicEvent) Fill

func (e *BasicEvent) Fill(target interface{}, data M) *BasicEvent

Fill event data

func (*BasicEvent) Get

func (e *BasicEvent) Get(key string) interface{}

Get data by index

func (*BasicEvent) IsAborted

func (e *BasicEvent) IsAborted() bool

IsAborted check.

func (*BasicEvent) Name

func (e *BasicEvent) Name() string

Name get event name

func (*BasicEvent) Set

func (e *BasicEvent) Set(key string, val interface{})

Set value by key

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

func (*BasicEvent) Target

func (e *BasicEvent) Target() interface{}

Target get target

type ByPriorityItems

type ByPriorityItems []*ListenerItem

ByPriorityItems type. implements the sort.Interface

func (ByPriorityItems) Len

func (ls ByPriorityItems) Len() int

Len get items length

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

func Fire

func Fire(name string, params M) (error, Event)

Fire listeners by name.

func GetEvent

func GetEvent(name string) (Event, bool)

GetEvent get event by name.

func MustFire

func MustFire(name string, params M) Event

MustFire fire event by name. will panic on error

func MustTrigger

func MustTrigger(name string, params M) Event

MustTrigger alias of MustFire

func Trigger

func Trigger(name string, params M) (error, Event)

Trigger alias of Fire

type Listener

type Listener interface {
	Handle(e Event) error
}

Listener interface

type ListenerFunc

type ListenerFunc func(e Event) error

ListenerFunc func definition.

func (ListenerFunc) Handle

func (fn ListenerFunc) Handle(e Event) error

Handle event. implements the Listener interface

type ListenerItem

type ListenerItem struct {
	Priority int
	Listener Listener
}

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) Clear

func (lq *ListenerQueue) Clear()

Clear clear all listeners

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) Len

func (lq *ListenerQueue) Len() int

Len get items length

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 M

type M = map[string]interface{}

M is short name for map[string]...

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 NewManager

func NewManager(name string) *Manager

NewManager create event manager

func (*Manager) AddEvent

func (em *Manager) AddEvent(e Event)

AddEvent add a defined event instance to manager.

func (*Manager) AddListener

func (em *Manager) AddListener(name string, listener Listener, priority ...int)

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) AsyncFire

func (em *Manager) AsyncFire(e Event)

AsyncFire async fire event by 'go' keywords

func (*Manager) AwaitFire

func (em *Manager) AwaitFire(e Event) (err error)

AwaitFire async fire event by 'go' keywords, but will wait return result

func (*Manager) Clear

func (em *Manager) Clear()

Clear alias of the Reset()

func (*Manager) Fire

func (em *Manager) Fire(name string, params M) (err error, e Event)

Fire trigger event by name. if not found listener, will return (nil, nil)

func (*Manager) FireBatch

func (em *Manager) FireBatch(es ...interface{}) (ers []error)

FireBatch fire multi event at once. Usage:

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

func (*Manager) FireEvent

func (em *Manager) FireEvent(e Event) (err error)

FireEvent fire event by given Event instance

func (*Manager) GetEvent

func (em *Manager) GetEvent(name string) (e Event, ok bool)

GetEvent get a defined event instance by name

func (*Manager) HasEvent

func (em *Manager) HasEvent(name string) bool

HasEvent has event check

func (*Manager) HasListeners

func (em *Manager) HasListeners(name string) bool

HasListeners has listeners for the event name.

func (*Manager) Listen

func (em *Manager) Listen(name string, listener Listener, priority ...int)

Listen alias of the On()

func (*Manager) ListenedNames

func (em *Manager) ListenedNames() map[string]int

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

func (em *Manager) ListenersCount(name string) int

ListenersCount get listeners number for the event name.

func (*Manager) MustFire

func (em *Manager) MustFire(name string, params M) Event

MustFire fire event by name. will panic on error

func (*Manager) MustTrigger

func (em *Manager) MustTrigger(name string, params M) Event

MustTrigger alias of the method MustFire()

func (*Manager) On

func (em *Manager) On(name string, listener Listener, priority ...int)

On register a event handler/listener. can setting priority.

Usage:

On("evt0", listener)
On("evt0", listener, High)

func (*Manager) RemoveEvent

func (em *Manager) RemoveEvent(name string)

RemoveEvent delete Event by name

func (*Manager) RemoveEvents

func (em *Manager) RemoveEvents()

RemoveEvents remove all registered events

func (*Manager) RemoveListener

func (em *Manager) RemoveListener(name string, listener Listener)

RemoveListener remove a given listener, you can limit event name.

Usage:

RemoveListener("", listener)
RemoveListener("name", listener) // limit event name.

func (*Manager) RemoveListeners

func (em *Manager) RemoveListeners(name string)

RemoveListeners remove listeners by given name

func (*Manager) Reset

func (em *Manager) Reset()

Reset the manager, clear all data.

func (*Manager) Subscribe

func (em *Manager) Subscribe(sbr Subscriber)

Subscribe alias of the AddSubscriber()

func (*Manager) Trigger

func (em *Manager) Trigger(name string, params M) (error, Event)

Trigger alias of the method Fire()

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.

Jump to

Keyboard shortcuts

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