Browse Source

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

* Add option for calling event handlers sync or async

* Small doc update
Jonas is my name 7 years ago
parent
commit
b4faca0f46
2 changed files with 14 additions and 2 deletions
  1. 10 2
      event.go
  2. 4 0
      structs.go

+ 10 - 2
event.go

@@ -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
 	}

+ 4 - 0
structs.go

@@ -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