twitchpubsub/eventhandlers.go

111 lines
3.0 KiB
Go

package twitchpubsub
const (
viewerCountEventType = "viewcount"
streamUpEventType = "stream-up"
streamDownEventType = "stream-down"
reconnectEventType = "RECONNECT"
)
// viewerCountEventHandler is an event handler for ViewerCount events.
type viewerCountEventHandler func(*TwitchPubSub, *ViewerCount)
// Type returns the event type for ViewerCount events.
func (eh viewerCountEventHandler) Type() string {
return viewerCountEventType
}
// New returns a new instance of ViewerCount.
func (eh viewerCountEventHandler) New() interface{} {
return &ViewerCount{}
}
// Handle is the handler for ViewerCount events.
func (eh viewerCountEventHandler) Handle(s *TwitchPubSub, i interface{}) {
if t, ok := i.(*ViewerCount); ok {
eh(s, t)
}
}
// streamUpEventHandler is an event handler for StreamUp events.
type streamUpEventHandler func(*TwitchPubSub, *StreamUp)
// Type returns the event type for StreamUp events.
func (eh streamUpEventHandler) Type() string {
return streamUpEventType
}
// New returns a new instance of StreamUp.
func (eh streamUpEventHandler) New() interface{} {
return &StreamUp{}
}
// Handle is the handler for StreamUp events.
func (eh streamUpEventHandler) Handle(s *TwitchPubSub, i interface{}) {
if t, ok := i.(*StreamUp); ok {
eh(s, t)
}
}
// viewerCountEventHandler is an event handler for StreamDown events.
type streamDownEventHandler func(*TwitchPubSub, *StreamDown)
// Type returns the event type for StreamDown events.
func (eh streamDownEventHandler) Type() string {
return streamDownEventType
}
// New returns a new instance of StreamDown.
func (eh streamDownEventHandler) New() interface{} {
return &StreamDown{}
}
// Handle is the handler for StreamDown events.
func (eh streamDownEventHandler) Handle(s *TwitchPubSub, i interface{}) {
if t, ok := i.(*StreamDown); ok {
eh(s, t)
}
}
// viewerCountEventHandler is an event handler for StreamDown events.
type reconnectEventHandler func(*TwitchPubSub, *Reconnect)
// Type returns the event type for StreamDown events.
func (eh reconnectEventHandler) Type() string {
return reconnectEventType
}
// New returns a new instance of StreamDown.
func (eh reconnectEventHandler) New() interface{} {
return &Reconnect{}
}
// Handle is the handler for StreamDown events.
func (eh reconnectEventHandler) Handle(s *TwitchPubSub, i interface{}) {
if t, ok := i.(*Reconnect); ok {
eh(s, t)
}
}
func handlerForInterface(handler interface{}) EventHandler {
switch v := handler.(type) {
case func(*TwitchPubSub, interface{}):
return interfaceEventHandler(v)
case func(*TwitchPubSub, *ViewerCount):
return viewerCountEventHandler(v)
case func(*TwitchPubSub, *StreamUp):
return streamUpEventHandler(v)
case func(*TwitchPubSub, *StreamDown):
return streamDownEventHandler(v)
}
return nil
}
func init() {
registerInterfaceProvider(viewerCountEventHandler(nil))
registerInterfaceProvider(streamUpEventHandler(nil))
registerInterfaceProvider(streamDownEventHandler(nil))
registerInterfaceProvider(reconnectEventHandler(nil))
}