2020-01-17 05:27:57 +00:00
|
|
|
// Package handler handles incoming Gateway events. It reflects the function's
|
|
|
|
// first argument and caches that for use in each event.
|
|
|
|
//
|
|
|
|
// Performance
|
|
|
|
//
|
2020-07-15 07:48:50 +00:00
|
|
|
// Each call to the event would take 167 ns/op for roughly each handler. Scaling
|
|
|
|
// that up to 100 handlers is roughly the same as multiplying 167 ns by 100,
|
|
|
|
// which gives 16700 ns or 0.0167 ms.
|
2020-01-17 05:27:57 +00:00
|
|
|
//
|
2020-07-15 07:48:50 +00:00
|
|
|
// BenchmarkReflect-8 7260909 167 ns/op
|
2020-01-17 05:27:57 +00:00
|
|
|
//
|
|
|
|
// Usage
|
|
|
|
//
|
2020-07-15 07:48:50 +00:00
|
|
|
// Handler's usage is mostly similar to Discordgo, in that AddHandler expects a
|
|
|
|
// function with only one argument or an event channel. For more information,
|
|
|
|
// refer to AddHandler.
|
2020-01-17 05:17:46 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2020-01-23 03:52:07 +00:00
|
|
|
"context"
|
2020-01-17 05:17:46 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
// Synchronous controls whether to spawn each event handler in its own
|
|
|
|
// goroutine. Default false (meaning goroutines are spawned).
|
|
|
|
Synchronous bool
|
|
|
|
|
|
|
|
handlers map[uint64]handler
|
2020-01-25 05:24:33 +00:00
|
|
|
horders []uint64
|
2020-01-17 05:17:46 +00:00
|
|
|
hserial uint64
|
2020-02-05 04:30:17 +00:00
|
|
|
hmutex sync.RWMutex
|
2020-01-17 05:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Handler {
|
|
|
|
return &Handler{
|
|
|
|
handlers: map[uint64]handler{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 06:57:50 +00:00
|
|
|
// Call calls all handlers with the given event. This is an internal method; use
|
|
|
|
// with care.
|
2020-01-17 05:17:46 +00:00
|
|
|
func (h *Handler) Call(ev interface{}) {
|
|
|
|
var evV = reflect.ValueOf(ev)
|
|
|
|
var evT = evV.Type()
|
|
|
|
|
2020-02-05 04:30:17 +00:00
|
|
|
h.hmutex.RLock()
|
|
|
|
defer h.hmutex.RUnlock()
|
2020-01-17 05:17:46 +00:00
|
|
|
|
2020-01-25 05:24:33 +00:00
|
|
|
for _, order := range h.horders {
|
|
|
|
handler, ok := h.handlers[order]
|
|
|
|
if !ok {
|
|
|
|
// This shouldn't ever happen, but we're adding this just in case.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-17 05:17:46 +00:00
|
|
|
if handler.not(evT) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.Synchronous {
|
|
|
|
handler.call(evV)
|
|
|
|
} else {
|
|
|
|
go handler.call(evV)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 20:47:15 +00:00
|
|
|
// 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.
|
|
|
|
func (h *Handler) CallDirect(ev interface{}) {
|
|
|
|
var evV = reflect.ValueOf(ev)
|
|
|
|
var evT = evV.Type()
|
|
|
|
|
|
|
|
h.hmutex.RLock()
|
|
|
|
defer h.hmutex.RUnlock()
|
|
|
|
|
|
|
|
for _, order := range h.horders {
|
|
|
|
handler, ok := h.handlers[order]
|
|
|
|
if !ok {
|
|
|
|
// This shouldn't ever happen, but we're adding this just in case.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if evT != handler.event {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.Synchronous {
|
|
|
|
handler.call(evV)
|
|
|
|
} else {
|
|
|
|
go handler.call(evV)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 02:51:24 +00:00
|
|
|
// 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.
|
|
|
|
func (h *Handler) WaitFor(ctx context.Context, fn func(interface{}) bool) interface{} {
|
2020-01-23 03:52:07 +00:00
|
|
|
var result = make(chan interface{})
|
|
|
|
|
|
|
|
cancel := h.AddHandler(func(v interface{}) {
|
|
|
|
if fn(v) {
|
|
|
|
result <- v
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case r := <-result:
|
|
|
|
return r
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 02:51:24 +00:00
|
|
|
// 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.
|
2020-07-15 06:57:50 +00:00
|
|
|
//
|
|
|
|
// This method is more intended to be used as a filter. For a persistent event
|
|
|
|
// channel, consider adding it directly as a handler with AddHandler.
|
2020-03-04 02:51:24 +00:00
|
|
|
func (h *Handler) ChanFor(fn func(interface{}) bool) (out <-chan interface{}, cancel func()) {
|
|
|
|
result := make(chan interface{})
|
|
|
|
closer := make(chan struct{})
|
2020-02-05 04:30:17 +00:00
|
|
|
|
2020-03-04 02:51:24 +00:00
|
|
|
removeHandler := h.AddHandler(func(v interface{}) {
|
2020-02-05 04:30:17 +00:00
|
|
|
if fn(v) {
|
2020-03-04 02:51:24 +00:00
|
|
|
select {
|
|
|
|
case result <- v:
|
|
|
|
case <-closer:
|
|
|
|
}
|
2020-02-05 04:30:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-04-09 00:48:36 +00:00
|
|
|
// Only allow cancel to be called once.
|
|
|
|
var once sync.Once
|
2020-03-04 02:51:24 +00:00
|
|
|
cancel = func() {
|
2020-04-09 00:48:36 +00:00
|
|
|
once.Do(func() {
|
|
|
|
removeHandler()
|
|
|
|
close(closer)
|
|
|
|
})
|
2020-03-04 02:51:24 +00:00
|
|
|
}
|
|
|
|
out = result
|
2020-02-05 04:30:17 +00:00
|
|
|
|
2020-03-04 02:51:24 +00:00
|
|
|
return
|
2020-02-05 04:30:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 20:26:00 +00:00
|
|
|
// AddHandler adds the handler, returning a function that would remove this
|
2020-07-15 06:57:50 +00:00
|
|
|
// handler when called. A handler type is either a single-argument no-return
|
|
|
|
// function or a channel.
|
|
|
|
//
|
|
|
|
// Function
|
|
|
|
//
|
|
|
|
// A handler can be a function with a single argument that is the expected event
|
|
|
|
// type. It must not have any returns or any other number of arguments.
|
|
|
|
//
|
2020-07-15 07:05:35 +00:00
|
|
|
// // An example of a valid function handler.
|
|
|
|
// h.AddHandler(func(*gateway.MessageCreateEvent) {})
|
|
|
|
//
|
2020-07-15 06:57:50 +00:00
|
|
|
// Channel
|
2020-06-06 20:47:15 +00:00
|
|
|
//
|
2020-07-15 06:57:50 +00:00
|
|
|
// A handler can also be a channel. The underlying type that the channel wraps
|
|
|
|
// around will be the event type. As such, the type rules are the same as
|
|
|
|
// function handlers.
|
|
|
|
//
|
|
|
|
// Keep in mind that the user must NOT close the channel. In fact, the channel
|
|
|
|
// should not be closed at all. The caller function WILL PANIC if the channel is
|
|
|
|
// closed!
|
2020-07-15 07:05:35 +00:00
|
|
|
//
|
|
|
|
// // An example of a valid channel handler.
|
|
|
|
// ch := make(chan *gateway.MessageCreateEvent)
|
|
|
|
// h.AddHandler(ch)
|
|
|
|
//
|
2020-01-17 05:17:46 +00:00
|
|
|
func (h *Handler) AddHandler(handler interface{}) (rm func()) {
|
|
|
|
rm, err := h.addHandler(handler)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return rm
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddHandlerCheck adds the handler, but safe-guards reflect panics with a
|
2020-07-15 06:57:50 +00:00
|
|
|
// recoverer, returning the error. Refer to AddHandler for more information.
|
2020-01-17 05:17:46 +00:00
|
|
|
func (h *Handler) AddHandlerCheck(handler interface{}) (rm func(), err error) {
|
|
|
|
// Reflect would actually panic if anything goes wrong, so this is just in
|
|
|
|
// case.
|
|
|
|
defer func() {
|
|
|
|
if rec := recover(); rec != nil {
|
|
|
|
if recErr, ok := rec.(error); ok {
|
|
|
|
err = recErr
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("%v", rec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return h.addHandler(handler)
|
|
|
|
}
|
|
|
|
|
2020-02-05 04:30:17 +00:00
|
|
|
func (h *Handler) addHandler(fn interface{}) (rm func(), err error) {
|
2020-01-17 05:17:46 +00:00
|
|
|
// Reflect the handler
|
2020-07-15 06:57:50 +00:00
|
|
|
r, err := newHandler(fn)
|
2020-01-17 05:17:46 +00:00
|
|
|
if err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return nil, errors.Wrap(err, "handler reflect failed")
|
2020-01-17 05:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.hmutex.Lock()
|
|
|
|
defer h.hmutex.Unlock()
|
|
|
|
|
2020-01-25 05:24:33 +00:00
|
|
|
// Get the current counter value and increment the counter:
|
2020-01-17 05:17:46 +00:00
|
|
|
serial := h.hserial
|
|
|
|
h.hserial++
|
|
|
|
|
2020-02-05 04:30:17 +00:00
|
|
|
// Create a map if there's none:
|
|
|
|
if h.handlers == nil {
|
|
|
|
h.handlers = map[uint64]handler{}
|
|
|
|
}
|
|
|
|
|
2020-01-25 05:24:33 +00:00
|
|
|
// Use the serial for the map:
|
2020-01-17 05:17:46 +00:00
|
|
|
h.handlers[serial] = *r
|
|
|
|
|
2020-01-25 05:24:33 +00:00
|
|
|
// Append the serial into the list of keys:
|
|
|
|
h.horders = append(h.horders, serial)
|
|
|
|
|
2020-01-17 05:17:46 +00:00
|
|
|
return func() {
|
|
|
|
h.hmutex.Lock()
|
|
|
|
defer h.hmutex.Unlock()
|
|
|
|
|
2020-01-25 05:24:33 +00:00
|
|
|
// Delete the handler from the map:
|
2020-01-17 05:17:46 +00:00
|
|
|
delete(h.handlers, serial)
|
2020-01-25 05:24:33 +00:00
|
|
|
|
|
|
|
// Delete the key from the orders slice:
|
|
|
|
for i, order := range h.horders {
|
|
|
|
if order == serial {
|
|
|
|
h.horders = append(h.horders[:i], h.horders[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-01-17 05:17:46 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type handler struct {
|
2020-07-15 06:57:50 +00:00
|
|
|
event reflect.Type // underlying type; arg0 or chan underlying type
|
2020-01-17 05:17:46 +00:00
|
|
|
callback reflect.Value
|
2020-07-15 06:57:50 +00:00
|
|
|
isChan bool
|
2020-01-17 05:17:46 +00:00
|
|
|
isIface bool
|
|
|
|
}
|
|
|
|
|
2020-07-15 06:57:50 +00:00
|
|
|
// newHandler reflects either a channel or a function into a handler. A function
|
|
|
|
// must only have a single argument being the event and no return, and a channel
|
|
|
|
// must have the event type as the underlying type.
|
|
|
|
func newHandler(unknown interface{}) (*handler, error) {
|
|
|
|
fnV := reflect.ValueOf(unknown)
|
2020-01-17 05:17:46 +00:00
|
|
|
fnT := fnV.Type()
|
|
|
|
|
2020-07-15 06:57:50 +00:00
|
|
|
// underlying event type
|
|
|
|
var argT reflect.Type
|
|
|
|
var isch bool
|
2020-01-17 05:17:46 +00:00
|
|
|
|
2020-07-15 06:57:50 +00:00
|
|
|
switch fnT.Kind() {
|
|
|
|
case reflect.Func:
|
|
|
|
if fnT.NumIn() != 1 {
|
|
|
|
return nil, errors.New("function can only accept 1 event as argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fnT.NumOut() > 0 {
|
|
|
|
return nil, errors.New("function can't accept returns")
|
|
|
|
}
|
2020-01-17 05:17:46 +00:00
|
|
|
|
2020-07-15 06:57:50 +00:00
|
|
|
argT = fnT.In(0)
|
|
|
|
|
|
|
|
case reflect.Chan:
|
|
|
|
argT = fnT.Elem()
|
|
|
|
isch = true
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.New("given interface is not a function or channel")
|
2020-02-05 04:30:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 06:57:50 +00:00
|
|
|
var kind = argT.Kind()
|
2020-01-17 05:17:46 +00:00
|
|
|
|
|
|
|
// Accept either pointer type or interface{} type
|
|
|
|
if kind != reflect.Ptr && kind != reflect.Interface {
|
|
|
|
return nil, errors.New("first argument is not pointer")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &handler{
|
|
|
|
event: argT,
|
|
|
|
callback: fnV,
|
2020-07-15 06:57:50 +00:00
|
|
|
isChan: isch,
|
2020-01-17 05:17:46 +00:00
|
|
|
isIface: kind == reflect.Interface,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h handler) not(event reflect.Type) bool {
|
|
|
|
if h.isIface {
|
|
|
|
return !event.Implements(h.event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.event != event
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h handler) call(event reflect.Value) {
|
2020-07-15 06:57:50 +00:00
|
|
|
if h.isChan {
|
|
|
|
h.callback.Send(event)
|
|
|
|
} else {
|
|
|
|
h.callback.Call([]reflect.Value{event})
|
|
|
|
}
|
2020-01-17 05:17:46 +00:00
|
|
|
}
|