[#5614] removed hook.HandlerFunc[T] type
This commit is contained in:
+4
-7
@@ -7,9 +7,6 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tools/security"
|
||||
)
|
||||
|
||||
// HandlerFunc defines a hook handler function.
|
||||
type HandlerFunc[T Resolver] func(e T) error
|
||||
|
||||
// Handler defines a single Hook handler.
|
||||
// Multiple handlers can share the same id.
|
||||
// If Id is not explicitly set it will be autogenerated by Hook.Add and Hook.AddHandler.
|
||||
@@ -18,7 +15,7 @@ type Handler[T Resolver] struct {
|
||||
//
|
||||
// Note that users need to call e.Next() in order to proceed with
|
||||
// the execution of the hook chain.
|
||||
Func HandlerFunc[T]
|
||||
Func func(T) error
|
||||
|
||||
// Id is the unique identifier of the handler.
|
||||
//
|
||||
@@ -111,7 +108,7 @@ func (h *Hook[T]) Bind(handler *Handler[T]) string {
|
||||
// The registered handler is added with a default 0 priority and the id will be autogenerated.
|
||||
//
|
||||
// If you want to register a handler with custom priority or id use the [Hook.Bind] method.
|
||||
func (h *Hook[T]) BindFunc(fn HandlerFunc[T]) string {
|
||||
func (h *Hook[T]) BindFunc(fn func(T) error) string {
|
||||
return h.Bind(&Handler[T]{Func: fn})
|
||||
}
|
||||
|
||||
@@ -151,9 +148,9 @@ func (h *Hook[T]) Length() int {
|
||||
// handlers that will be temporary appended to the handlers queue.
|
||||
//
|
||||
// NB! Each hook handler must call event.Next() in order the hook chain to proceed.
|
||||
func (h *Hook[T]) Trigger(event T, oneOffHandlers ...HandlerFunc[T]) error {
|
||||
func (h *Hook[T]) Trigger(event T, oneOffHandlers ...func(T) error) error {
|
||||
h.mu.RLock()
|
||||
handlers := make([]HandlerFunc[T], 0, len(h.handlers)+len(oneOffHandlers))
|
||||
handlers := make([]func(T) error, 0, len(h.handlers)+len(oneOffHandlers))
|
||||
for _, handler := range h.handlers {
|
||||
handlers = append(handlers, handler.Func)
|
||||
}
|
||||
|
||||
@@ -124,12 +124,12 @@ func TestHookTriggerErrorPropagation(t *testing.T) {
|
||||
|
||||
scenarios := []struct {
|
||||
name string
|
||||
handlers []HandlerFunc[*Event]
|
||||
handlers []func(*Event) error
|
||||
expectedError error
|
||||
}{
|
||||
{
|
||||
"without error",
|
||||
[]HandlerFunc[*Event]{
|
||||
[]func(*Event) error{
|
||||
func(e *Event) error { return e.Next() },
|
||||
func(e *Event) error { return e.Next() },
|
||||
},
|
||||
@@ -137,7 +137,7 @@ func TestHookTriggerErrorPropagation(t *testing.T) {
|
||||
},
|
||||
{
|
||||
"with error",
|
||||
[]HandlerFunc[*Event]{
|
||||
[]func(*Event) error{
|
||||
func(e *Event) error { return e.Next() },
|
||||
func(e *Event) error { e.Next(); return err },
|
||||
func(e *Event) error { return e.Next() },
|
||||
|
||||
@@ -73,7 +73,7 @@ func (h *TaggedHook[T]) Bind(handler *Handler[T]) string {
|
||||
//
|
||||
// It is similar to [Hook.Bind] with the difference that the handler
|
||||
// function is invoked only if the event data tags satisfy h.CanTriggerOn.
|
||||
func (h *TaggedHook[T]) BindFunc(fn HandlerFunc[T]) string {
|
||||
func (h *TaggedHook[T]) BindFunc(fn func(T) error) string {
|
||||
return h.mainHook.BindFunc(func(e T) error {
|
||||
if h.CanTriggerOn(e.Tags()) {
|
||||
return fn(e)
|
||||
|
||||
+10
-10
@@ -46,7 +46,7 @@ func (group *RouterGroup[T]) Group(prefix string) *RouterGroup[T] {
|
||||
//
|
||||
// If you need to specify a named middleware (ex. so that it can be removed)
|
||||
// or middleware with custom exec prirority, use [Group.Bind] method.
|
||||
func (group *RouterGroup[T]) BindFunc(middlewareFuncs ...hook.HandlerFunc[T]) *RouterGroup[T] {
|
||||
func (group *RouterGroup[T]) BindFunc(middlewareFuncs ...func(T) error) *RouterGroup[T] {
|
||||
for _, m := range middlewareFuncs {
|
||||
group.Middlewares = append(group.Middlewares, &hook.Handler[T]{Func: m})
|
||||
}
|
||||
@@ -115,7 +115,7 @@ func (group *RouterGroup[T]) Unbind(middlewareIds ...string) *RouterGroup[T] {
|
||||
// meaning that only a top level group route could have HOST as part of the prefix.
|
||||
//
|
||||
// Returns the newly created route to allow attaching route-only middlewares.
|
||||
func (group *RouterGroup[T]) Route(method string, path string, action hook.HandlerFunc[T]) *Route[T] {
|
||||
func (group *RouterGroup[T]) Route(method string, path string, action func(T) error) *Route[T] {
|
||||
route := &Route[T]{
|
||||
Method: method,
|
||||
Path: path,
|
||||
@@ -128,42 +128,42 @@ func (group *RouterGroup[T]) Route(method string, path string, action hook.Handl
|
||||
}
|
||||
|
||||
// Any is a shorthand for [Group.AddRoute] with "" as route method (aka. matches any method).
|
||||
func (group *RouterGroup[T]) Any(path string, action hook.HandlerFunc[T]) *Route[T] {
|
||||
func (group *RouterGroup[T]) Any(path string, action func(T) error) *Route[T] {
|
||||
return group.Route("", path, action)
|
||||
}
|
||||
|
||||
// GET is a shorthand for [Group.AddRoute] with GET as route method.
|
||||
func (group *RouterGroup[T]) GET(path string, action hook.HandlerFunc[T]) *Route[T] {
|
||||
func (group *RouterGroup[T]) GET(path string, action func(T) error) *Route[T] {
|
||||
return group.Route(http.MethodGet, path, action)
|
||||
}
|
||||
|
||||
// POST is a shorthand for [Group.AddRoute] with POST as route method.
|
||||
func (group *RouterGroup[T]) POST(path string, action hook.HandlerFunc[T]) *Route[T] {
|
||||
func (group *RouterGroup[T]) POST(path string, action func(T) error) *Route[T] {
|
||||
return group.Route(http.MethodPost, path, action)
|
||||
}
|
||||
|
||||
// DELETE is a shorthand for [Group.AddRoute] with DELETE as route method.
|
||||
func (group *RouterGroup[T]) DELETE(path string, action hook.HandlerFunc[T]) *Route[T] {
|
||||
func (group *RouterGroup[T]) DELETE(path string, action func(T) error) *Route[T] {
|
||||
return group.Route(http.MethodDelete, path, action)
|
||||
}
|
||||
|
||||
// PATCH is a shorthand for [Group.AddRoute] with PATCH as route method.
|
||||
func (group *RouterGroup[T]) PATCH(path string, action hook.HandlerFunc[T]) *Route[T] {
|
||||
func (group *RouterGroup[T]) PATCH(path string, action func(T) error) *Route[T] {
|
||||
return group.Route(http.MethodPatch, path, action)
|
||||
}
|
||||
|
||||
// PUT is a shorthand for [Group.AddRoute] with PUT as route method.
|
||||
func (group *RouterGroup[T]) PUT(path string, action hook.HandlerFunc[T]) *Route[T] {
|
||||
func (group *RouterGroup[T]) PUT(path string, action func(T) error) *Route[T] {
|
||||
return group.Route(http.MethodPut, path, action)
|
||||
}
|
||||
|
||||
// HEAD is a shorthand for [Group.AddRoute] with HEAD as route method.
|
||||
func (group *RouterGroup[T]) HEAD(path string, action hook.HandlerFunc[T]) *Route[T] {
|
||||
func (group *RouterGroup[T]) HEAD(path string, action func(T) error) *Route[T] {
|
||||
return group.Route(http.MethodHead, path, action)
|
||||
}
|
||||
|
||||
// OPTIONS is a shorthand for [Group.AddRoute] with OPTIONS as route method.
|
||||
func (group *RouterGroup[T]) OPTIONS(path string, action hook.HandlerFunc[T]) *Route[T] {
|
||||
func (group *RouterGroup[T]) OPTIONS(path string, action func(T) error) *Route[T] {
|
||||
return group.Route(http.MethodOptions, path, action)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import "github.com/pocketbase/pocketbase/tools/hook"
|
||||
type Route[T hook.Resolver] struct {
|
||||
excludedMiddlewares map[string]struct{}
|
||||
|
||||
Action hook.HandlerFunc[T]
|
||||
Action func(T) error
|
||||
Method string
|
||||
Path string
|
||||
Middlewares []*hook.Handler[T]
|
||||
@@ -18,7 +18,7 @@ type Route[T hook.Resolver] struct {
|
||||
//
|
||||
// If you need to specify a named middleware (ex. so that it can be removed)
|
||||
// or middleware with custom exec prirority, use the [Bind] method.
|
||||
func (route *Route[T]) BindFunc(middlewareFuncs ...hook.HandlerFunc[T]) *Route[T] {
|
||||
func (route *Route[T]) BindFunc(middlewareFuncs ...func(T) error) *Route[T] {
|
||||
for _, m := range middlewareFuncs {
|
||||
route.Middlewares = append(route.Middlewares, &hook.Handler[T]{Func: m})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user