trigger

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package trigger gives every part of this SDK one shared vocabulary for "a condition fired, so run this": Condition, Action, and a Registry mapping a name to one of each. A leaf package: no I/O, no goroutine, no persistence, no polling loop of its own.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBlankName is Add's error when name is empty after
	// strings.TrimSpace.
	ErrBlankName = errors.New("trigger: name must not be blank")
	// ErrNilAction is Add's error for a nil Action; a trigger with
	// nothing to run has no purpose.
	ErrNilAction = errors.New("trigger: action must not be nil")
	// ErrDuplicateName is Add's error for a name already registered.
	ErrDuplicateName = errors.New("trigger: name already registered")
	// ErrUnknownName is Fire's error when name is not registered.
	ErrUnknownName = errors.New("trigger: unknown name")
	// ErrConditionNotMet is Fire's error when the named entry's
	// Condition evaluates false. Fire does not call Action in this
	// case.
	ErrConditionNotMet = errors.New("trigger: condition not met")
)

Sentinel errors for Registry operations; test with errors.Is.

Functions

This section is empty.

Types

type Action

type Action func(ctx context.Context) error

Action is the invocable a named trigger runs once its Condition is satisfied. Add rejects a nil Action.

type Condition

type Condition func(ctx context.Context) (bool, error)

Condition reports whether a named trigger's Action should run. A nil Condition passed to Add means "always ready," matching machine.Guard's own nil convention.

type Registry

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

Registry holds named triggers. The zero value is ready to use, the same as New's result. Safe for concurrent Add, Remove, and Fire; a sync.Mutex guards the map.

func New

func New() *Registry

New creates an empty Registry.

func (*Registry) Add

func (r *Registry) Add(name string, c Condition, a Action) error

Add registers c and a under name. Rejects a blank name (empty after strings.TrimSpace) with ErrBlankName, a nil a with ErrNilAction, and a duplicate name with ErrDuplicateName. A nil c is accepted; see Condition.

func (*Registry) Fire

func (r *Registry) Fire(ctx context.Context, name string) error

Fire resolves name, evaluates its Condition (a nil Condition reads as true), and, when true, calls its Action and returns that call's error. Returns ErrUnknownName when name is not registered. Returns ErrConditionNotMet when the Condition evaluates false, without calling Action. Returns a Condition evaluation error wrapped `trigger: %q: %w`, without calling Action. An Action already resolved by Fire runs to completion even if a concurrent Remove deletes the entry mid-call.

func (*Registry) Remove

func (r *Registry) Remove(name string) bool

Remove removes name from the registry. Returns whether name was present. Removing an absent name is not a fault; it returns false and changes nothing.

Jump to

Keyboard shortcuts

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