Help us shape the future of Go! Take the 2026 Go Developer Survey

hooks

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package hooks is the host-side engine behind the SDK's typed actions and filters (see pkg/sdk/hooks.go).

Actions are fire-and-forget: every callback runs in its own goroutine with panic recovery and a timeout derived from the server lifecycle context (the analytics dispatchListeners model). Filters run synchronously in ascending priority order; a failing filter leaves the value unchanged.

Index

Constants

View Source
const DefaultActionTimeout = 10 * time.Second

DefaultActionTimeout bounds each action callback.

Variables

This section is empty.

Functions

func OnAction

func OnAction[T any](h Registrar, a sdk.Action[T], priority int, fn func(context.Context, T))

OnAction registers fn for action a. A payload of an unexpected type is skipped.

func OnFilter

func OnFilter[T any](h Registrar, f sdk.Filter[T], priority int, fn func(context.Context, T) (T, error))

OnFilter registers fn for filter f. A value of an unexpected type is passed through unchanged.

Types

type Engine

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

Engine stores hook registrations and dispatches them.

func New

func New(l *slog.Logger) *Engine

New returns an empty engine. A nil logger logs to slog.Default().

func (*Engine) AddAction

func (e *Engine) AddAction(name string, priority int, fn func(ctx context.Context, payload any))

AddAction implements Registrar for host-owned (untagged) registrations.

func (*Engine) AddFilter

func (e *Engine) AddFilter(name string, priority int, fn func(ctx context.Context, v any) (any, error))

AddFilter implements Registrar for host-owned (untagged) registrations.

func (*Engine) Apply

func (e *Engine) Apply(ctx context.Context, name string, v any) any

Apply runs the filter chain for name over v and returns the result. A filter that errors, panics or changes the value's type is logged and skipped; the value stays as it was before that filter.

func (*Engine) ApplyStrict

func (e *Engine) ApplyStrict(ctx context.Context, name string, v any) (any, error)

ApplyStrict is Apply, except a *sdk.Reject RETURNED by a filter (matched with errors.As) stops the chain and is returned to the caller (used by entity.before_save to reject a save). Any other error, and panics, are logged and skipped like in Apply: the filter's change is dropped.

func (*Engine) Fire

func (e *Engine) Fire(caller context.Context, name string, payload any)

Fire starts every callback registered for name, in ascending priority, each in its own goroutine, and returns immediately. Callbacks do not inherit the caller's cancellation: they run under the lifecycle context with a timeout, but see the caller's context values.

func (*Engine) For

func (e *Engine) For(plugin string) Registrar

For returns a registrar that tags registrations with the plugin name so log lines identify the offender.

func (*Engine) HasAction

func (e *Engine) HasAction(name string) bool

HasAction reports whether any callback is registered for the action.

func (*Engine) HasFilter

func (e *Engine) HasFilter(name string) bool

HasFilter reports whether any callback is registered for the filter.

func (*Engine) RemovePlugin

func (e *Engine) RemovePlugin(plugin string)

RemovePlugin drops every action and filter registered under the plugin name. The host calls it when a plugin's Wire fails so the half-wired plugin does not keep running callbacks.

func (*Engine) SetActionTimeout

func (e *Engine) SetActionTimeout(d time.Duration)

SetActionTimeout overrides the per-callback action timeout.

func (*Engine) SetLifecycleContext

func (e *Engine) SetLifecycleContext(ctx context.Context)

SetLifecycleContext sets the parent context of action callbacks; cancelling it (server shutdown) cancels in-flight callbacks.

func (*Engine) Wait

func (e *Engine) Wait()

Wait blocks until every in-flight action callback has finished. Used by tests and graceful shutdown.

type Registrar

type Registrar interface {
	AddAction(name string, priority int, fn func(ctx context.Context, payload any))
	AddFilter(name string, priority int, fn func(ctx context.Context, v any) (any, error))
}

Registrar is the untyped hook registration surface. Lower priority runs first; ties run in registration order.

Jump to

Keyboard shortcuts

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