Skip to content

Commit

Permalink
Add option for calling event handlers sync or async (bwmarrin#416)
Browse files Browse the repository at this point in the history
* Add option for calling event handlers sync or async

* Small doc update
  • Loading branch information
jogramming authored and iopred committed Jul 30, 2017
1 parent b64eabe commit fc44ab7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 10 additions & 2 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,20 @@ func (s *Session) removeEventHandlerInstance(t string, ehi *eventHandlerInstance
// Handles calling permanent and once handlers for an event type.
func (s *Session) handle(t string, i interface{}) {
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 {
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
}
Expand Down
4 changes: 4 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ type Session struct {
// active guilds and the members of the guilds.
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.

// Whether the Data Websocket is ready
Expand Down

0 comments on commit fc44ab7

Please sign in to comment.