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) CallDirect(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.
GuildCreateEvents and GuildDeleteEvents will not be called on interface{} events. Instead their situation-specific version will be fired, as they provides more information about the context of the event: GuildReadyEvent, GuildAvailableEvent, GuildJoinEvent, GuildUnavailableEvent or GuildLeaveEvent Listening to directly to GuildCreateEvent or GuildDeleteEvent will still work, however.
func (*Handler) AddHandlerCheck ¶
AddHandlerCheck adds the handler, but safe-guards reflect panics with a recoverer, returning the error.
GuildCreateEvents and GuildDeleteEvents will not be called on interface{} events. Instead their situation-specific version will be fired, as they provides more information about the context of the event: GuildReadyEvent, GuildAvailableEvent, GuildJoinEvent, GuildUnavailableEvent or GuildLeaveEvent Listening to directly to GuildCreateEvent or GuildDeleteEvent will still work, however.
func (*Handler) CallDirect ¶ added in v0.9.3
func (h *Handler) CallDirect(ev interface{})
CallDirect is the same as Call, but only calls those event handlers that listen for this specific event, i.e. that aren't interface handlers.