handler

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2020 License: Apache-2.0 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())

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 added in v0.1.1

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

func (*Handler) WaitFor added in v0.0.9

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

Jump to

Keyboard shortcuts

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