hook

package
v0.23.4 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2024 License: MIT Imports: 4 Imported by: 42

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event added in v0.23.0

type Event struct {
	// 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
}

func (*Event) Next added in v0.23.0

func (e *Event) Next() error

Next calls the next hook handler.

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

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

func (*Hook[T]) Bind added in v0.23.0

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.

func (*Hook[T]) BindFunc added in v0.23.0

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.

func (*Hook[T]) Length added in v0.23.0

func (h *Hook[T]) Length() int

Length returns to total number of registered hook handlers.

func (*Hook[T]) 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.

func (*Hook[T]) Unbind added in v0.23.0

func (h *Hook[T]) Unbind(idsToRemove ...string)

Unbind removes one or many hook handler by their id.

func (*Hook[T]) UnbindAll added in v0.23.0

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

UnbindAll removes all registered handlers.

type Resolver added in v0.23.0

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

type TaggedHook added in v0.12.0

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

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]) Bind added in v0.23.0

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.

func (*TaggedHook[T]) BindFunc added in v0.23.0

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.

func (*TaggedHook[T]) CanTriggerOn added in v0.12.0

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 doens't have any tags.

type Tagger added in v0.12.0

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.

Jump to

Keyboard shortcuts

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