Beam and SourceBeam
Reactive value streams for rendering and state flow.
A Beam[T] is a read-only stream. A SourceBeam[T] is the root stream you update. During a render cycle, a Door and all children see the same value for a given Beam.
Constructors
Create a Source Beam
// Uses == for equality
NewSourceBeam[T comparable](init T) SourceBeam[T]
// Uses custom equality function
NewSourceBeamEqual[T any](init T, equal func(new T, old T) bool) SourceBeam[T]
NewSourceBeamsuppresses updates whennew == old.NewSourceBeamEqualuses the supplied equality function. Ifequalisnil, every update propagates.
Create a Derived Beam
// Uses == on derived type
NewBeam[T any, T2 comparable](source Beam[T], cast func(T) T2) Beam[T2]
// Uses custom equality function
NewBeamEqual[T any, T2 any](source Beam[T], cast func(T) T2, equal func(new T2, old T2) bool) Beam[T2]
- Derived beams transform data from another beam.
NewBeamEqualsuppresses updates when equality function returns true.
Beam API (Read-only)
Sub(ctx, onValue) bool
XSub(ctx, onValue, onCancel) (Cancel, bool)
ReadAndSub(ctx, onValue) (T, bool)
XReadAndSub(ctx, onValue, onCancel) (T, Cancel, bool)
Read(ctx) (T, bool)
AddWatcher(ctx, w Watcher[T]) (Cancel, bool)
X*variants includeonCanceland return a cancel handle.SubcallsonValueimmediately, then again on updates untilonValuereturns true, parent dynamic contauner (Door) unmounted or manually canceled.
Watcher Interface
Watcher interface for low level beam access
Interface Watcher[T any]
Defines hooks for observing and reacting to the lifecycle of a beam value stream. Implementers can perform custom logic during initialization, on each update, and when canceled.
Cancel()
Called when the watcher is terminated due to context cancellation.
Init(ctx context.Context, value *T, seq uint) bool
Called with the initial value.
seq: Sequence number of the update.Called in the same goroutine where the watcher was added.
Return value: Return
trueto stop receiving updates after this call.Update(ctx context.Context, value *T, seq uint) bool
Called for each subsequent update to the value.
seq: Increments with each update.- Return value: Return
trueto stop receiving further updates.
SourceBeam API (Writable)
Update(ctx, value T)
XUpdate(ctx, value T) <-chan error
Mutate(ctx, f func(T) T)
XMutate(ctx, f func(T) T) <-chan error
Latest() T
DisableSkipping()
UpdateandMutatesend updates if equality check indicates a change.XUpdateandXMutatesignal completion through a channel:
- Yields
nilon success or skip.
- Yields an
erroron cancellation or ended instance.
- Closes empty if equality suppressed update.
- Yields
Latestreturns current value without render synchronization.
DisableSkippingdisables coalescing; all values propagate.
Helper Components
Sub
Reactive render helper:
@doors.Sub(beam, func(v T) templ.Component { ... })
Re-renders when the beam updates.
Inject
Injects current value into the context for child components and re-renders on updates.
Design Notes
- Keep source beams minimal. Store references or identifiers, not large structs.
- Use derived beams for transformations or projections.
- Use
New*Equalwhen equality by==is not meaningful.