hook
import hook "ganigeorgiev.com/pocketbase/pocketbase/tools/hook"
Source: tools/hook.
Types
Event
type Event struct {
// Extra holds any dynamic data that could be passed between event handlers.
Extra map[string]any
// contains filtered or unexported fields
}
Event implements Resolver and it is intended to be used as a base Hook event that you can embed in your custom typed event structs.
Example:
type CustomEvent struct {
hook.Event
SomeField int
}
Event.Next
func (e *Event) Next() error
Next calls the next hook handler.
Handler
type Handler[T Resolver] struct {
// Func defines the handler function to execute.
//
// Note that users need to call e.Next() in order to proceed with
// the execution of the hook chain.
Func func(T) error
// Id is the unique identifier of the handler.
//
// It could be used later to remove the handler from a hook via [Hook.Remove].
//
// If missing, an autogenerated value will be assigned when adding
// the handler to a hook.
Id string
// Priority allows changing the default exec priority of the handler within a hook.
//
// If 0, the handler will be executed in the same order it was registered.
Priority int
}
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.
Hook
type Hook[T Resolver] struct {
// contains filtered or unexported fields
}
Hook defines a generic concurrent safe structure for managing event hooks.
When using custom event it must embed the base hook.Event.
Example:
type CustomEvent struct {
hook.Event
SomeField int
}
h := Hook[*CustomEvent]{}
h.BindFunc(func(e *CustomEvent) error {
println(e.SomeField)
return e.Next()
})
h.Trigger(&CustomEvent{ SomeField: 123 })
Hook.Bind
func (h *Hook[T]) Bind(handler *Handler[T]) string
Bind registers the provided handler to the current hooks queue.
If handler.Id is empty it is updated with autogenerated value.
If a handler from the current hook list has Id matching handler.Id then the old handler is replaced with the new one.
Hook.BindFunc
func (h *Hook[T]) BindFunc(fn func(e T) error) string
BindFunc is similar to Bind but registers a new handler from just the provided function.
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.
Hook.Length
func (h *Hook[T]) Length() int
Length returns to total number of registered hook handlers.
Hook.Trigger
func (h *Hook[T]) Trigger(event T, oneOffHandlerFuncs ...func(T) error) error
Trigger executes all registered hook handlers one by one with the specified event as an argument.
Optionally, this method allows also to register additional one off handler funcs that will be temporary appended to the handlers queue.
NB! Each hook handler must call event.Next() in order the hook chain to proceed.
Hook.Unbind
func (h *Hook[T]) Unbind(idsToRemove ...string)
Unbind removes one or many hook handler by their id.
Hook.UnbindAll
func (h *Hook[T]) UnbindAll()
UnbindAll removes all registered handlers.
Resolver
type Resolver interface {
// Next triggers the next handler in the hook's chain (if any).
Next() error
// contains filtered or unexported methods
}
Resolver defines a common interface for a Hook event (see Event).
TaggedHook
type TaggedHook[T Tagger] struct {
// contains filtered or unexported fields
}
TaggedHook defines a proxy hook which register handlers that are triggered only if the TaggedHook.tags are empty or includes at least one of the event data tag(s).
NewTaggedHook
func NewTaggedHook[T Tagger](hook *Hook[T], tags ...string) *TaggedHook[T]
NewTaggedHook creates a new TaggedHook with the provided main hook and optional tags.
TaggedHook.Bind
func (h *TaggedHook[T]) Bind(handler *Handler[T]) string
Bind registers the provided handler to the current hooks queue.
It is similar to Hook.Bind with the difference that the handler function is invoked only if the event data tags satisfy h.CanTriggerOn.
TaggedHook.BindFunc
func (h *TaggedHook[T]) BindFunc(fn func(e T) error) string
BindFunc registers a new handler with the specified function.
It is similar to Hook.Bind with the difference that the handler function is invoked only if the event data tags satisfy h.CanTriggerOn.
TaggedHook.CanTriggerOn
func (h *TaggedHook[T]) CanTriggerOn(tagsToCheck []string) bool
CanTriggerOn checks if the current TaggedHook can be triggered with the provided event data tags.
It returns always true if the hook doesn't have any tags.
Tagger
type Tagger interface {
Resolver
Tags() []string
}
Tagger defines an interface for event data structs that support tags/groups/categories/etc. Usually used together with TaggedHook.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.