Documentation ¶
Overview ¶
Package handler handles incoming Gateway events. It reflects the function's first argument and caches that for use in each event.
Performance ¶
Each call to the event would take 156 ns/op for roughly each handler. Scaling that up to 100 handlers is multiplying 156 ns by 100, which gives 15600 ns, or 0.0156 ms.
BenchmarkReflect-8 7260909 156 ns/op
Usage ¶
Handler's usage is similar to discordgo, in that AddHandler expects a function with only one argument. The only argument must be a pointer to one of the events, or an interface{} which would accept all events.
AddHandler would panic if the handler is invalid.
s.AddHandler(func(m *gateway.MessageCreateEvent) { log.Println(m.Author.Username, "said", m.Content) })
Index ¶
- type Handler
- func (h *Handler) AddHandler(handler interface{}) (rm func())
- func (h *Handler) AddHandlerCheck(handler interface{}) (rm func(), err error)
- func (h *Handler) Call(ev interface{})
- func (h *Handler) ChanFor(fn func(interface{}) bool) (out <-chan interface{}, cancel func())
- func (h *Handler) WaitFor(ctx context.Context, fn func(interface{}) bool) interface{}
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler struct { // Synchronous controls whether to spawn each event handler in its own // goroutine. Default false (meaning goroutines are spawned). Synchronous bool // contains filtered or unexported fields }
func (*Handler) AddHandler ¶
func (h *Handler) AddHandler(handler interface{}) (rm func())
AddHandler adds the handler, returning a function that would remove this handler when called.
func (*Handler) AddHandlerCheck ¶
AddHandlerCheck adds the handler, but safe-guards reflect panics with a recoverer, returning the error.