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

hooks

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package hooks provides WordPress-style actions, filters and events.

Contracts:

  • Priority: lower prio runs first; equal prio runs in registration order.
  • Do passes the payload to actions as-is (typed) and joins their errors.
  • Apply threads v through filters; on the first error it stops and returns the last good value with the error.
  • Emit marshals the payload to JSON and delivers it to the actions registered under the event name as a json.RawMessage, both when it runs async (via the "hooks.event" job when a queue is attached) and when it falls back to sync delivery. Emit never returns an error.

Index

Constants

View Source
const EventJobKind = "hooks.event"

EventJobKind is the job kind used to deliver Emit events asynchronously.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionFunc

type ActionFunc func(ctx context.Context, payload any) error

ActionFunc handles an action or event.

type FilterFunc

type FilterFunc func(ctx context.Context, v any) (any, error)

FilterFunc transforms a value in a filter chain.

type Registry

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

Registry holds actions and filters by name. It is safe for concurrent use.

func New

func New() *Registry

New returns an empty registry.

func (*Registry) Action

func (r *Registry) Action(name string, prio int, fn ActionFunc) Token

Action registers fn under name with priority prio and returns a token for Remove.

func (*Registry) Apply

func (r *Registry) Apply(ctx context.Context, name string, v any) (any, error)

Apply runs the filter chain registered under name over v.

func (*Registry) AttachQueue

func (r *Registry) AttachQueue(q *jobs.Queue)

AttachQueue makes Emit asynchronous and registers the "hooks.event" job handler on q.

func (*Registry) Do

func (r *Registry) Do(ctx context.Context, name string, payload any) error

Do runs every action registered under name synchronously and returns their errors joined with errors.Join (nil when all succeed).

func (*Registry) Emit

func (r *Registry) Emit(ctx context.Context, name string, payload any)

Emit publishes an event. With a queue attached it is persisted as a "hooks.event" job and delivered by a worker (retried on error); otherwise, or if enqueueing fails, it is delivered synchronously. Errors are logged.

Emit enqueues on its own pool connection: call it after a db.InTx transaction commits, never inside one (it would stall on the lock).

func (*Registry) Filter

func (r *Registry) Filter(name string, prio int, fn FilterFunc) Token

Filter registers fn under name with priority prio and returns a token for Remove.

func (*Registry) Names

func (r *Registry) Names() []string

Names returns every action and filter name, sorted and de-duplicated.

func (*Registry) Remove

func (r *Registry) Remove(t Token) bool

Remove unregisters the action or filter identified by t and reports whether it was registered. In-flight Do/Apply calls that already copied the chain still run it; later calls do not.

type Token

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

Token identifies one Action or Filter registration; pass it to Remove on the registry that issued it (ids are per registry, so another registry may hold an unrelated registration with the same token). The zero Token identifies nothing.

func (Token) IsZero

func (t Token) IsZero() bool

IsZero reports whether t is the zero Token.

Jump to

Keyboard shortcuts

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