timetravel

package
v4.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 1 Imported by: 0

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

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

func New[T any](parseCapacity int, parseInitial T) *History[T]

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]) CanRedo

func (parseH *History[T]) CanRedo() bool

CanRedo reports whether there is a later snapshot to step forward to.

func (*History[T]) CanUndo

func (parseH *History[T]) CanUndo() bool

CanUndo reports whether there is an earlier snapshot to step back to.

func (*History[T]) Current

func (parseH *History[T]) Current() T

Current returns the state at the cursor.

func (*History[T]) Cursor

func (parseH *History[T]) Cursor() int

Cursor returns the current position in history.

func (*History[T]) Labels

func (parseH *History[T]) Labels() []string

Labels returns the snapshot labels in order — the timeline the devtools scrubber renders.

func (*History[T]) Len

func (parseH *History[T]) Len() int

Len returns the number of retained snapshots.

func (*History[T]) Record

func (parseH *History[T]) Record(parseLabel string, parseState T)

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

func (parseH *History[T]) Redo() (T, bool)

Redo steps forward one snapshot and returns it; ok is false at the end of history.

func (*History[T]) ScrubTo

func (parseH *History[T]) ScrubTo(parseIndex int) (T, bool)

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

func (parseH *History[T]) Snapshots() []Snapshot[T]

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.

func (*History[T]) Undo

func (parseH *History[T]) Undo() (T, bool)

Undo steps back one snapshot and returns it; ok is false at the start of history.

type Snapshot

type Snapshot[T any] struct {
	Label string
	State T
}

Snapshot is one recorded point in history: a human-facing Label and the state value.

Directories

Path Synopsis
Package devpanel renders the time-travel devtools UI as a GoWebComponents component — the framework dogfooding its own engine.
Package devpanel renders the time-travel devtools UI as a GoWebComponents component — the framework dogfooding its own engine.

Jump to

Keyboard shortcuts

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