fragments

package
v0.2.4 Latest Latest
Warning

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

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

Documentation

Overview

Package fragments is the compiled source for docs/guide/fragments-and-dirty-tracking.md.

templ: version: v0.3.1020

Index

Constants

View Source
const (
	FragmentReading = "dash.reading"
	FragmentLog     = "dash.log"
)

Fragment identifiers. A patch names one of these, so changing one is a client-visible change. They match ^[A-Za-z0-9_:.-]{1,64}$ and are unique within the application; New refuses a duplicate.

Variables

This section is empty.

Functions

func Age

func Age(changedAtUnixMilli int64, at time.Time) time.Duration

Age is derived at the transition, from the event's own At stamp, and rendered as data.

A render may not read a clock: it must be a pure function of state, or two renders of the same state produce different bytes and the identical-render suppression that compares them breaks.

func Fragments

func Fragments() []live.Fragment[State]

Fragments declares the two live regions.

Dirty is optional. Nil means "re-render on every transition", which is always correct and is the right first answer; declare one when a fragment is expensive or when an unrelated event stream would otherwise re-render it.

func LogRegion

func LogRegion(s State) templ.Component

func ReadingRegion

func ReadingRegion(s State) templ.Component

Types

type History

type History struct {
	Samples []Sample
}

History is the recent readings, oldest first. It is IMMUTABLE: nothing appends in place, and a transition builds a new History and points at it.

It is behind a pointer in State because the library compares consecutive states with == to decide whether the state version moved, and a type that is not comparable is reported as changed on EVERY transition — so a no-op event bumps the version and every fragment's Dirty is asked about a change that did not happen. A slice field makes State uncomparable; one pointer field does not.

func (*History) Len

func (h *History) Len() int

Len is derived state the log fragment renders.

type Sample

type Sample struct {
	Seq   uint64
	Value int
}

Sample is one reading. It is a value, and the History holding it is never mutated after construction.

type State

type State struct {
	// Latest is the newest reading. A pointer to an immutable value, so == is
	// pointer identity: a new reading is a new pointer, and the same reading
	// folded twice is the same pointer.
	Latest *History

	// ChangedAtUnixMilli is when the reading last moved.
	//
	// It is an int64 and not a time.Time, and that is the hazard this file
	// exists to name. A time.Time read from time.Now carries a monotonic clock
	// reading as well as a wall clock, and == compares both — plus the
	// *Location pointer. Two time.Time values naming the same instant can
	// therefore compare unequal, so a Dirty function written with == over a
	// time.Time reports a change that did not happen on every transition, and
	// State stops being comparable in the way the paragraph on History
	// assumes. time.Time.Equal is the correct comparison and == is not; storing
	// the instant as an integer removes the choice.
	ChangedAtUnixMilli int64

	// Paused belongs to this session and to nothing else.
	Paused bool
}

State is one session's view.

Every field is comparable. That is what lets the Dirty functions below use ==, and it is what keeps the library's own state comparison working.

func Fold

func Fold(s State, sample Sample, atUnixMilli int64) State

Fold is the transition a new reading takes. It replaces the History rather than appending to the one it was given: a reducer must not mutate the state it was handed, which is what makes panic recovery free.

Jump to

Keyboard shortcuts

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