hooks

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 5 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunWithTimeout added in v1.0.0

func RunWithTimeout(ctx context.Context, timeout time.Duration, fn func(ctx context.Context) error) error

RunWithTimeout runs an action with a context deadline.

Example:

err := hooks.RunWithTimeout(ctx, time.Second, func() error { ... })

Types

type Discovery

type Discovery struct {
	// contains filtered or unexported fields
}

Discovery finds lifecycle methods on structs via reflection.

Example:

d := hooks.NewDiscovery()
methods := d.Discover(myService, "OnStart")

func NewDiscovery

func NewDiscovery() *Discovery

NewDiscovery creates a hook discovery instance with caching.

func (*Discovery) Call

func (d *Discovery) Call(v any, name string, args ...any) ([]any, error)

Call invokes a method by name with optional arguments.

func (*Discovery) CallWithContext

func (d *Discovery) CallWithContext(ctx context.Context, v any, name string, args ...any) ([]any, error)

CallWithContext invokes a method with context as first argument.

func (*Discovery) Discover

func (d *Discovery) Discover(v any, prefix string) []MethodInfo

Discover finds all methods on v with the given prefix.

Returns:

A list of discovered matching methods.

func (*Discovery) DiscoverAll

func (d *Discovery) DiscoverAll(v any, prefixes ...string) map[string][]MethodInfo

DiscoverAll finds methods matching any of the given prefixes.

func (*Discovery) HasMethod

func (d *Discovery) HasMethod(v any, name string) bool

HasMethod checks if a specific method exists.

type HookFunc

type HookFunc func(ctx context.Context, key string, args []any) error

HookFunc is a function called at a lifecycle event.

type MethodInfo

type MethodInfo struct {
	Name   string
	Suffix string
	Method reflect.Method
	Value  reflect.Value
}

MethodInfo holds information about a discovered method.

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner manages execution of lifecycle hooks with before/after patterns.

func NewRunner

func NewRunner() *Runner

NewRunner creates a hook runner with a shared discovery instance.

func (*Runner) After

func (r *Runner) After(key string, fn HookFunc)

After registers a function to be called after a specific event.

func (*Runner) AfterAll

func (r *Runner) AfterAll(fn HookFunc)

AfterAll registers a function to be called after any event.

func (*Runner) Before

func (r *Runner) Before(key string, fn HookFunc)

Before registers a function to be called before a specific event.

func (*Runner) BeforeAll

func (r *Runner) BeforeAll(fn HookFunc)

BeforeAll registers a function to be called before any event.

func (*Runner) Clear

func (r *Runner) Clear()

Clear removes all registered hooks.

func (*Runner) Discovery

func (r *Runner) Discovery() *Discovery

Discovery returns the underlying discovery instance.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, key string, action func() error, args ...any) error

Run executes before hooks, the action, and after hooks.

func (*Runner) RunParallel added in v1.0.0

func (r *Runner) RunParallel(ctx context.Context, key string, args ...any) error

RunParallel executes hooks concurrently. Each hook runs in its own goroutine. If any hook returns an error, cancellation is propagated via context.

Example:

err := r.RunParallel(ctx, "my-event", args)

Jump to

Keyboard shortcuts

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