gavalink/event.go

184 lines
5.3 KiB
Go
Raw Normal View History

2018-08-24 01:24:20 +00:00
package gavalink
// EventHandler is an interface for Lavalink events.
2018-08-24 01:24:20 +00:00
type EventHandler interface {
// Type returns the type of event this handler belongs to.
Type() string
// Handle is called whenever an event of Type() happens.
// It is the receivers responsibility to type assert that the interface
// is the expected struct.
Handle(*Player, interface{})
}
// EventInterfaceProvider is an interface for providing empty interfaces for
// Lavalink events.
type EventInterfaceProvider interface {
// Type is the type of event this handler belongs to.
Type() string
// New returns a new instance of the struct this event handler handles.
// This is called once per event.
// The struct is provided to all handlers of the same Type().
New() interface{}
}
// interfaceEventType is the event handler type for interface{} events.
const interfaceEventType = "__INTERFACE__"
// interfaceEventHandler is an event handler for interface{} events.
type interfaceEventHandler func(*Player, interface{})
// Type returns the event type for interface{} events.
func (eh interfaceEventHandler) Type() string {
return interfaceEventType
}
// Handle is the handler for an interface{} event.
func (eh interfaceEventHandler) Handle(p *Player, i interface{}) {
eh(p, i)
2018-08-24 01:24:20 +00:00
}
var registeredInterfaceProviders = map[string]EventInterfaceProvider{}
2018-08-24 01:24:20 +00:00
// registerInterfaceProvider registers a provider so that Gavalink can
// access it's New() method.
func registerInterfaceProvider(eh EventInterfaceProvider) {
if _, ok := registeredInterfaceProviders[eh.Type()]; ok {
return
// XXX:
// if we should error here, we need to do something with it.
// fmt.Errorf("event %s already registered", eh.Type())
}
registeredInterfaceProviders[eh.Type()] = eh
return
2018-08-24 01:24:20 +00:00
}
// eventHandlerInstance is a wrapper around an event handler, as functions
// cannot be compared directly.
type eventHandlerInstance struct {
eventHandler EventHandler
2018-08-24 01:24:20 +00:00
}
// addEventHandler adds an event handler that will be fired anytime
// the Lavalink event matching eventHandler.Type() fires.
func (l *Lavalink) addEventHandler(eventHandler EventHandler) func() {
l.handlersMu.Lock()
defer l.handlersMu.Unlock()
if l.handlers == nil {
l.handlers = map[string][]*eventHandlerInstance{}
}
ehi := &eventHandlerInstance{eventHandler}
l.handlers[eventHandler.Type()] = append(l.handlers[eventHandler.Type()], ehi)
return func() {
l.removeEventHandlerInstance(eventHandler.Type(), ehi)
}
}
// addEventHandler adds an event handler that will be fired the next time
// the Lavalink event matching eventHandler.Type() fires.
func (l *Lavalink) addEventHandlerOnce(eventHandler EventHandler) func() {
l.handlersMu.Lock()
defer l.handlersMu.Unlock()
if l.onceHandlers == nil {
l.onceHandlers = map[string][]*eventHandlerInstance{}
}
ehi := &eventHandlerInstance{eventHandler}
l.onceHandlers[eventHandler.Type()] = append(l.onceHandlers[eventHandler.Type()], ehi)
return func() {
l.removeEventHandlerInstance(eventHandler.Type(), ehi)
}
2018-08-24 01:24:20 +00:00
}
2019-02-04 00:31:53 +00:00
// AddHandler allows you to add an event handler that will be fired anytime
// the Lavalink event that matches the function fires.
// The first parameter is a *Session, and the second parameter is a pointer
// to a struct corresponding to the event for which you want to listen.
//
// eg:
// Player.AddHandler(func(s *gavalink.Player, m *gavalink.TrackStart) {
// })
//
// or:
// Player.AddHandler(func(s *gavalink.Player, m *gavalink.TrackEnd) {
// })
//
//
// The return value of this method is a function, that when called will remove the
// event handler.
func (l *Lavalink) AddHandler(handler interface{}) func() {
eh := handlerForInterface(handler)
if eh == nil {
return func() {}
}
return l.addEventHandler(eh)
}
// AddHandlerOnce allows you to add an event handler that will be fired the next time
// the Lavalink event that matches the function fires.
// See AddHandler for more details.
func (l *Lavalink) AddHandlerOnce(handler interface{}) func() {
eh := handlerForInterface(handler)
if eh == nil {
return func() {}
}
return l.addEventHandlerOnce(eh)
}
// removeEventHandler instance removes an event handler instance.
func (l *Lavalink) removeEventHandlerInstance(t string, ehi *eventHandlerInstance) {
l.handlersMu.Lock()
defer l.handlersMu.Unlock()
handlers := l.handlers[t]
for i := range handlers {
if handlers[i] == ehi {
l.handlers[t] = append(handlers[:i], handlers[i+1:]...)
}
}
onceHandlers := l.onceHandlers[t]
for i := range onceHandlers {
if onceHandlers[i] == ehi {
l.onceHandlers[t] = append(onceHandlers[:i], handlers[i+1:]...)
}
}
}
// Handles calling permanent and once handlers for an event type.
func (l *Lavalink) handle(p *Player, t string, i interface{}) {
for _, eh := range l.handlers[t] {
go eh.eventHandler.Handle(p, i)
}
if len(l.onceHandlers[t]) > 0 {
for _, eh := range l.onceHandlers[t] {
go eh.eventHandler.Handle(p, i)
}
l.onceHandlers[t] = nil
}
}
// Handles an event type by calling internal methods, firing handlers and firing the
// interface{} event.
func (l *Lavalink) handleEvent(p *Player, t string, i interface{}) {
l.handlersMu.RLock()
defer l.handlersMu.RUnlock()
// Then they are dispatched to anyone handling interface{} events.
l.handle(p, interfaceEventType, i)
// Finally they are dispatched to any typed handlers.
l.handle(p, t, i)
2019-02-04 00:31:53 +00:00
}