Documentation
¶
Overview ¶
Package timetravel is the snapshot step-back replay engine behind GoWebComponents' time-travel devtools (C4) and undo/redo (FB6) — one engine, two consumers. It records immutable state snapshots over time and lets you step back (undo), step forward (redo), or scrub to any point, with a bounded ring so history never grows without limit.
It is pure and deterministic: it owns no clock, no DOM, and no runtime, so it unit-tests trivially and the devtools panel is just a thin view over Labels()/ScrubTo. State is held by value, so snapshots are immutable as long as T is (use value types or copy on Record).
Index ¶
- type History
- func (parseH *History[T]) CanRedo() bool
- func (parseH *History[T]) CanUndo() bool
- func (parseH *History[T]) Current() T
- func (parseH *History[T]) Cursor() int
- func (parseH *History[T]) Labels() []string
- func (parseH *History[T]) Len() int
- func (parseH *History[T]) Record(parseLabel string, parseState T)
- func (parseH *History[T]) Redo() (T, bool)
- func (parseH *History[T]) ScrubTo(parseIndex int) (T, bool)
- func (parseH *History[T]) Snapshots() []Snapshot[T]
- func (parseH *History[T]) Undo() (T, bool)
- type Snapshot
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type History ¶
type History[T any] struct { // contains filtered or unexported fields }
History is a bounded, navigable timeline of state snapshots. Safe for concurrent use.
func New ¶
New creates a history seeded with an initial snapshot. capacity bounds the number of retained snapshots (the oldest are evicted past it); a capacity <= 0 means unbounded.
func (*History[T]) Current ¶
func (parseH *History[T]) Current() T
Current returns the state at the cursor.
func (*History[T]) Labels ¶
Labels returns the snapshot labels in order — the timeline the devtools scrubber renders.
func (*History[T]) Record ¶
Record appends a new snapshot as the current state. If the cursor is not at the end (because of prior Undo calls), the redo branch ahead of it is discarded first — the standard undo/redo rule — then the oldest snapshots are evicted to honor capacity.
func (*History[T]) Redo ¶
Redo steps forward one snapshot and returns it; ok is false at the end of history.
func (*History[T]) ScrubTo ¶
ScrubTo jumps the cursor to an arbitrary index (the scrubber/timeline in the devtools panel), returning that state; ok is false if the index is out of range.
func (*History[T]) Snapshots ¶
Snapshots returns a copy of every retained snapshot (label + state) in order, WITHOUT moving the cursor — so a devtools panel or undo-list UI can read the whole timeline at once instead of looping ScrubTo (which mutates the cursor). The returned slice is a copy; mutating it is safe.