Add option for calling event handlers sync or async (#416)

* Add option for calling event handlers sync or async

* Small doc update
This commit is contained in:
Jonas is my name 2017-07-30 02:32:20 +02:00 committed by Chris Rhodes
parent 42d1f62e58
commit b4faca0f46
2 changed files with 14 additions and 2 deletions

View file

@ -156,12 +156,20 @@ func (s *Session) removeEventHandlerInstance(t string, ehi *eventHandlerInstance
// Handles calling permanent and once handlers for an event type. // Handles calling permanent and once handlers for an event type.
func (s *Session) handle(t string, i interface{}) { func (s *Session) handle(t string, i interface{}) {
for _, eh := range s.handlers[t] { for _, eh := range s.handlers[t] {
go eh.eventHandler.Handle(s, i) if s.SyncEvents {
eh.eventHandler.Handle(s, i)
} else {
go eh.eventHandler.Handle(s, i)
}
} }
if len(s.onceHandlers[t]) > 0 { if len(s.onceHandlers[t]) > 0 {
for _, eh := range s.onceHandlers[t] { for _, eh := range s.onceHandlers[t] {
go eh.eventHandler.Handle(s, i) if s.SyncEvents {
eh.eventHandler.Handle(s, i)
} else {
go eh.eventHandler.Handle(s, i)
}
} }
s.onceHandlers[t] = nil s.onceHandlers[t] = nil
} }

View file

@ -50,6 +50,10 @@ type Session struct {
// active guilds and the members of the guilds. // active guilds and the members of the guilds.
StateEnabled bool StateEnabled bool
// Whether or not to call event handlers synchronously.
// e.g false = launch event handlers in their own goroutines.
SyncEvents bool
// Exposed but should not be modified by User. // Exposed but should not be modified by User.
// Whether the Data Websocket is ready // Whether the Data Websocket is ready