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 ¶
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 ¶
Action is the invocable a named trigger runs once its Condition is satisfied. Add rejects a nil Action.
type Condition ¶
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 (*Registry) Add ¶
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 ¶
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.