Clean up ordering of internal handlers. (#285)
This commit is contained in:
parent
c5a94de19c
commit
c352d7016c
3 changed files with 29 additions and 13 deletions
30
discord.go
30
discord.go
|
@ -207,6 +207,8 @@ func (s *Session) handle(event interface{}) {
|
||||||
|
|
||||||
handlerParameters := []reflect.Value{reflect.ValueOf(s), reflect.ValueOf(event)}
|
handlerParameters := []reflect.Value{reflect.ValueOf(s), reflect.ValueOf(event)}
|
||||||
|
|
||||||
|
s.onInterface(event)
|
||||||
|
|
||||||
if handlers, ok := s.handlers[nil]; ok {
|
if handlers, ok := s.handlers[nil]; ok {
|
||||||
for _, handler := range handlers {
|
for _, handler := range handlers {
|
||||||
go handler.Call(handlerParameters)
|
go handler.Call(handlerParameters)
|
||||||
|
@ -226,24 +228,32 @@ func (s *Session) initialize() {
|
||||||
s.log(LogInformational, "called")
|
s.log(LogInformational, "called")
|
||||||
|
|
||||||
s.handlersMu.Lock()
|
s.handlersMu.Lock()
|
||||||
|
defer s.handlersMu.Unlock()
|
||||||
|
|
||||||
if s.handlers != nil {
|
if s.handlers != nil {
|
||||||
s.handlersMu.Unlock()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.handlers = map[interface{}][]reflect.Value{}
|
s.handlers = map[interface{}][]reflect.Value{}
|
||||||
s.handlersMu.Unlock()
|
}
|
||||||
|
|
||||||
s.AddHandler(s.onReady)
|
// onInterface handles all internal events and routes them to the appropriate internal handler.
|
||||||
s.AddHandler(s.onResumed)
|
func (s *Session) onInterface(i interface{}) {
|
||||||
s.AddHandler(s.onVoiceServerUpdate)
|
switch t := i.(type) {
|
||||||
s.AddHandler(s.onVoiceStateUpdate)
|
case *Ready:
|
||||||
s.AddHandler(s.State.onReady)
|
s.onReady(t)
|
||||||
s.AddHandler(s.State.onInterface)
|
case *Resumed:
|
||||||
|
s.onResumed(t)
|
||||||
|
case *VoiceServerUpdate:
|
||||||
|
go s.onVoiceServerUpdate(t)
|
||||||
|
case *VoiceStateUpdate:
|
||||||
|
go s.onVoiceStateUpdate(t)
|
||||||
|
}
|
||||||
|
s.State.onInterface(s, i)
|
||||||
}
|
}
|
||||||
|
|
||||||
// onReady handles the ready event.
|
// onReady handles the ready event.
|
||||||
func (s *Session) onReady(se *Session, r *Ready) {
|
func (s *Session) onReady(r *Ready) {
|
||||||
|
|
||||||
// Store the SessionID within the Session struct.
|
// Store the SessionID within the Session struct.
|
||||||
s.sessionID = r.SessionID
|
s.sessionID = r.SessionID
|
||||||
|
@ -253,7 +263,7 @@ func (s *Session) onReady(se *Session, r *Ready) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// onResumed handles the resumed event.
|
// onResumed handles the resumed event.
|
||||||
func (s *Session) onResumed(se *Session, r *Resumed) {
|
func (s *Session) onResumed(r *Resumed) {
|
||||||
|
|
||||||
// Start the heartbeat to keep the connection alive.
|
// Start the heartbeat to keep the connection alive.
|
||||||
go s.heartbeat(s.wsConn, s.listening, r.HeartbeatInterval)
|
go s.heartbeat(s.wsConn, s.listening, r.HeartbeatInterval)
|
||||||
|
|
6
state.go
6
state.go
|
@ -639,6 +639,12 @@ func (s *State) onInterface(se *Session, i interface{}) (err error) {
|
||||||
if s == nil {
|
if s == nil {
|
||||||
return ErrNilState
|
return ErrNilState
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r, ok := i.(*Ready)
|
||||||
|
if ok {
|
||||||
|
return s.onReady(se, r)
|
||||||
|
}
|
||||||
|
|
||||||
if !se.StateEnabled {
|
if !se.StateEnabled {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
6
wsapi.go
6
wsapi.go
|
@ -509,7 +509,7 @@ func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (voice *Voi
|
||||||
}
|
}
|
||||||
|
|
||||||
// onVoiceStateUpdate handles Voice State Update events on the data websocket.
|
// onVoiceStateUpdate handles Voice State Update events on the data websocket.
|
||||||
func (s *Session) onVoiceStateUpdate(se *Session, st *VoiceStateUpdate) {
|
func (s *Session) onVoiceStateUpdate(st *VoiceStateUpdate) {
|
||||||
|
|
||||||
// If we don't have a connection for the channel, don't bother
|
// If we don't have a connection for the channel, don't bother
|
||||||
if st.ChannelID == "" {
|
if st.ChannelID == "" {
|
||||||
|
@ -523,7 +523,7 @@ func (s *Session) onVoiceStateUpdate(se *Session, st *VoiceStateUpdate) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// We only care about events that are about us.
|
// We only care about events that are about us.
|
||||||
if se.State.User.ID != st.UserID {
|
if s.State.User.ID != st.UserID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -537,7 +537,7 @@ func (s *Session) onVoiceStateUpdate(se *Session, st *VoiceStateUpdate) {
|
||||||
// This is also fired if the Guild's voice region changes while connected
|
// This is also fired if the Guild's voice region changes while connected
|
||||||
// to a voice channel. In that case, need to re-establish connection to
|
// to a voice channel. In that case, need to re-establish connection to
|
||||||
// the new region endpoint.
|
// the new region endpoint.
|
||||||
func (s *Session) onVoiceServerUpdate(se *Session, st *VoiceServerUpdate) {
|
func (s *Session) onVoiceServerUpdate(st *VoiceServerUpdate) {
|
||||||
|
|
||||||
s.log(LogInformational, "called")
|
s.log(LogInformational, "called")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue