hook

package
v0.900.9 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StopPropagation = errors.New("Event hook propagation stopped")

Functions

This section is empty.

Types

type Handler

type Handler[T any] func(e T) error

Handler defines a hook handler function.

type Hook

type Hook[T any] struct {
	// contains filtered or unexported fields
}

Hook defines a concurrent safe structure for handling event hooks (aka. callbacks propagation).

func (*Hook[T]) Add

func (h *Hook[T]) Add(fn Handler[T]) string

Add registers a new handler to the hook by appending it to the existing queue.

Returns an autogenerated hook id that could be used later to remove the hook with Hook.Remove(id).

func (*Hook[T]) PreAdd

func (h *Hook[T]) PreAdd(fn Handler[T]) string

PreAdd registers a new handler to the hook by prepending it to the existing queue.

Returns an autogenerated hook id that could be used later to remove the hook with Hook.Remove(id).

func (*Hook[T]) Remove

func (h *Hook[T]) Remove(id string)

Remove removes a single hook handler by its id.

func (*Hook[T]) RemoveAll

func (h *Hook[T]) RemoveAll()

RemoveAll removes all registered handlers.

func (*Hook[T]) Trigger

func (h *Hook[T]) Trigger(data T, oneOffHandlers ...Handler[T]) error

Trigger executes all registered hook handlers one by one with the specified `data` as an argument.

Optionally, this method allows also to register additional one off handlers that will be temporary appended to the handlers queue.

The execution stops when: - hook.StopPropagation is returned in one of the handlers - any non-nil error is returned in one of the handlers

type 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).

func 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.

func (*TaggedHook[T]) Add

func (h *TaggedHook[T]) Add(fn Handler[T]) string

Add registers a new handler to the hook by appending it to the existing queue.

The fn handler will be called only if the event data tags satisfy h.CanTriggerOn.

func (*TaggedHook[T]) CanTriggerOn

func (h *TaggedHook[T]) CanTriggerOn(tags []string) bool

CanTriggerOn checks if the current TaggedHook can be triggered with the provided event data tags.

func (*TaggedHook[T]) PreAdd

func (h *TaggedHook[T]) PreAdd(fn Handler[T]) string

PreAdd registers a new handler to the hook by prepending it to the existing queue.

The fn handler will be called only if the event data tags satisfy h.CanTriggerOn.

type Tagger

type Tagger interface {
	Tags() []string
}

Tagger defines an interface for event data structs that support tags/groups/categories/etc. Usually used together with TaggedHook.

Jump to

Keyboard shortcuts

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