Documentation
¶
Overview ¶
Package event contains an implementation of an event bus
Index ¶
- Constants
- type ArgMap
- type BaseEvent
- type Event
- type Handler
- type HandlerFunc
- type HandlerList
- type Manager
- func (m *Manager) Attach(name string, f HandlerFunc, priority int) int
- func (m *Manager) AttachMany(f HandlerFunc, priority int, names ...string) int
- func (m *Manager) AttachMultiShot(name string, f HandlerFunc, priority, count int) int
- func (m *Manager) AttachOneShot(name string, f HandlerFunc, priority int) int
- func (m *Manager) Detach(id int) bool
- func (m *Manager) Dispatch(event Event)
- func (m *Manager) HasEvent(name string) bool
- func (m *Manager) WaitFor(name string) Event
- func (m *Manager) WaitForChan(name string) <-chan Event
- type Map
- type SimpleEvent
Constants ¶
const ( PriHighest = 16 PriHigh = 32 PriNorm = 48 PriLow = 64 PriLowest = 80 )
Priority levels
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArgMap ¶
type ArgMap map[string]interface{}
ArgMap is a map of string to interface, it exists as a type alias for ease of use
type BaseEvent ¶
type BaseEvent struct { Name_ string //nolint:golint // Needs to be exported AND needs to have a getter // contains filtered or unexported fields }
BaseEvent implements parts of the Event interface in order to reduce boilerplate. Its expected that BaseEvent is embedded in simple implementations of Event
func (*BaseEvent) IsCancelled ¶
IsCancelled returns the cancellation state of the BaseEvent
func (*BaseEvent) SetCancelled ¶
SetCancelled sets the cancellation state of the BaseEvent
type Event ¶
type Event interface { // Name refers to the Name of the specific event this Event represents. It should be based on the data within the Event Name() string // IsCancelled returns whether or not this Event has been Cancelled IsCancelled() bool // SetCancelled sets the cancelled state of the Event SetCancelled(bool) bool }
Event represents any event that can be fired over the event bus
type Handler ¶
type Handler struct { Func HandlerFunc // The callback that this Handler refers to Priority int // The priority of this callback, lower is higher ID int // The ID of this callback }
Handler represents an event handler
type HandlerList ¶
type HandlerList []Handler
HandlerList is a slice of handlers with functions added to allow the slice to be sorted
func (HandlerList) Len ¶
func (h HandlerList) Len() int
func (HandlerList) Less ¶
func (h HandlerList) Less(i, j int) bool
func (HandlerList) Swap ¶
func (h HandlerList) Swap(i, j int)
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager is an event bus. It allows you to hook callbacks onto string based event names, and fire them later. Use of Manager objects from multiple goroutines is permitted
func (*Manager) Attach ¶
func (m *Manager) Attach(name string, f HandlerFunc, priority int) int
Attach adds an event and a callback to the Manager, the returned int is an ID for the attached callback, and can be used to detach a callback later
func (*Manager) AttachMany ¶
func (m *Manager) AttachMany(f HandlerFunc, priority int, names ...string) int
AttachMany attaches the given function to all events specified by names
func (*Manager) AttachMultiShot ¶
func (m *Manager) AttachMultiShot(name string, f HandlerFunc, priority, count int) int
AttachMultiShot attaches the given callback for count number of hook dispatches, once the count is reached, the callback will be detached
func (*Manager) AttachOneShot ¶
func (m *Manager) AttachOneShot(name string, f HandlerFunc, priority int) int
AttachOneShot attaches the function provided to the Manager for one hook, after which the handler will be detached
func (*Manager) Detach ¶
Detach removes a given ID from the event Manager. If the event is not found, Detach returns False
func (*Manager) Dispatch ¶
Dispatch fires an event down the event bus under the name attached to the given event. If the name does not exist, it is silently ignored
func (*Manager) HasEvent ¶
HasEvent returns whether or not the given string exists as an event on this Manager
func (*Manager) WaitFor ¶
WaitFor is like WaitForChan but instead of returning a channel, it blocks until the event is fired, and will return the Event object used to fire the event
func (*Manager) WaitForChan ¶
WaitForChan returns a channel that will receive exactly one dispatch of the named event. The Event object is sent over the channel. The channel has a buffer, to prevent blocking of the event's dispatch
type Map ¶
type Map map[string]HandlerList
Map is a map of string to HandlerList, it exists as a type alias for ease of use
type SimpleEvent ¶
type SimpleEvent struct {
*BaseEvent
}
SimpleEvent is a basic Event implementation, its useful to provide a notification but not pass any data
func NewSimpleEvent ¶
func NewSimpleEvent(name string) *SimpleEvent
NewSimpleEvent creates a new SimpleEvent and sets its name and Argmap to the provided values