streamdeck-obs-replay/obsws/client_events.go

50 lines
1.1 KiB
Go
Raw Permalink Normal View History

2020-01-15 04:32:53 +00:00
package obsws
import (
"errors"
"log"
)
// ErrUnknownEventType is returned when a handler is added for an unknown event.
var ErrUnknownEventType = errors.New("unknown event type")
// AddEventHandler adds a handler function for a given event type.
func (c *Client) AddEventHandler(eventType string, handler func(Event)) error {
if eventMap[eventType] == nil {
return ErrUnknownEventType
}
c.handlers[eventType] = handler
return nil
}
// RemoveEventHandler removes the handler for a given event type.
func (c *Client) RemoveEventHandler(eventType string) {
delete(c.handlers, eventType)
}
// handleEvent runs an event's handler if it exists.
func (c *Client) handleEvent(m map[string]interface{}) {
t := m["update-type"].(string)
log.Println("Handle", t)
eventFn, ok := eventMap[t]
if !ok {
Logger.Println("unknown event type:", m["update-type"])
return
}
event := eventFn()
handler, ok := c.handlers[t]
if !ok {
return
}
if err := mapToStruct(m, event); err != nil {
Logger.Println("event handler failed:", err)
return
}
go handler(derefEvent(event))
}