settingswatch

package
v0.9.6 Latest Latest
Warning

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

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

Documentation

Overview

Package settingswatch applies a value-typed settings section to a live consumer, re-applying whenever the section changes. It is the lightweight counterpart to observers like payloadlog's Controller: those own sink/emitter lifecycle and rebuild their own resources, whereas a Watcher just reads one section and hands the resolved value to an apply callback.

Use it for knobs whose runtime effect is "call this setter with the new value" (e.g. a parser toggle). The callback is where any vendor-specific or impure coupling lives, so the watcher itself stays vendor-neutral and testable.

The watcher is event-driven: it applies the current value once at Start and then re-applies on each settings-change callback from the Source — no polling. Because the callback fires on the catalog's serial NOTIFY goroutine, the apply must be cheap (it is, for a setter); a consumer with heavy work should use payloadlog's signal-a-goroutine pattern instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Source

type Source interface {
	Setting(section string) (any, bool)
	OnSettingsChange(section string, fn func())
}

Source is a live settings provider: read a section's current value and subscribe to its changes. Satisfied by *app/catalog.Catalog.

type Watcher

type Watcher[T comparable] struct {
	// contains filtered or unexported fields
}

Watcher applies one settings section to a consumer via an apply callback, on first Start and on every subsequent change. T is the section's value type; it must be comparable so change detection is a plain ==.

func New

func New[T comparable](src Source, section string, apply func(T), log *slog.Logger) *Watcher[T]

New builds a Watcher for section. apply is called with the resolved value at Start and on every change. If apply panics it is recovered and logged so a bad value can't take down the caller (or the catalog listener goroutine the change callback runs on).

func (*Watcher[T]) Start

func (w *Watcher[T]) Start()

Start applies the current value once, then subscribes for changes. Synchronous and cheap; call it during composition after the catalog is wired. Reconcile order (apply-then-subscribe) guarantees at least one apply even if the section never changes; the mutex makes a change that races the initial apply safe.

Jump to

Keyboard shortcuts

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