hook

package
v0.0.0-...-89d1b08 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2022 License: MIT Imports: 2 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(data 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])

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

func (*Hook[T]) PreAdd

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

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

func (*Hook[T]) Reset

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

Reset 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

Jump to

Keyboard shortcuts

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