handler

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2020 License: ISC Imports: 5 Imported by: 0

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

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 New

func New() *Handler

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

func (h *Handler) AddHandlerCheck(handler interface{}) (rm func(), err error)

AddHandlerCheck adds the handler, but safe-guards reflect panics with a recoverer, returning the error.

func (*Handler) Call

func (h *Handler) Call(ev interface{})

func (*Handler) ChanFor

func (h *Handler) ChanFor(fn func(interface{}) bool) (out <-chan interface{}, cancel func())

ChanFor returns a channel that would receive all incoming events that match the callback given. The cancel() function removes the handler and drops all hanging goroutines.

func (*Handler) WaitFor

func (h *Handler) WaitFor(ctx context.Context, fn func(interface{}) bool) interface{}

WaitFor blocks until there's an event. It's advised to use ChanFor instead, as WaitFor may skip some events if it's not ran fast enough after the event arrived.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL