livewatch

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package livewatch keeps an open pane in step with state it does not own.

Three panes have the same defect for the same reason. The td issue card, the document preview and the diff view each read something external once, when they open, and then render that snapshot until the user reopens them. An agent adds a child issue, writes a markdown file, or lands a commit, and the pane the user is watching quietly stops being true. Watching work happen is most of why those panes exist, so a snapshot defeats them.

The fix is one shape, applied three times:

observe a cheap external change signal
  -> coalesce a burst of them into one
  -> re-read, but only one re-read at a time
  -> apply only if the result actually differs

This package owns the two halves of that shape that are worth sharing, and deliberately owns nothing else.

PathWatcher is the observe-and-coalesce half: an fsnotify registration over a named set of files and directories, with a quiet period so a burst of writes lands as one signal, a latency cap so a continuously busy target still reports, and a Stop that gives every descriptor back. It watches only the paths it is given. None of the callers here walk a tree.

Refresher is the decide half, and it holds no timers, descriptors or view state at all — just the four booleans every host was otherwise going to re-derive. It answers: should a re-read start now, is another one owed, and is this result different from what is already on screen. That last question is the one that matters most. All three tickets independently described the same symptom — a pane that visibly flashes every time an unrelated write touches the store it reads from — so the no-change gate lives here rather than in any one surface.

Hosts bind through a thin per-surface file, because each surface preserves different UI state across the re-read: the issue card keeps scroll, cursor and hover; the document keeps its scroll offset; the diff keeps the selected file by path rather than by index, since a positional cursor silently re-points at a different file when the list above it changes.

The lifecycle around those bindings — start a watcher lazily, adopt it across a project switch, re-arm the one-shot listener, hand every descriptor back — is identical for every kind and belongs to github.com/marcus/sidecar/internal/livepanes, which is also where "watch only what is on screen, and re-read a pane that comes back into view" is decided once for every surface.

Startup

Nothing here runs on the startup path. A PathWatcher is created when a pane opens, from inside a tea.Cmd, never from a plugin's Init or before Start returns. A pane that is never opened costs nothing.

Cost

A PathWatcher costs no processes and no wakeups while its targets are idle; fsnotify blocks. That is the whole reason to prefer it to a timer. Where a signal genuinely cannot be observed from the filesystem, poll — but say what the interval buys, and make it proportional to how often the thing really changes.

Index

Constants

View Source
const (
	// DefaultQuiet is how long the filesystem has to go quiet before a batch of
	// changes is reported. Agents and editors write in bursts — a tool that
	// rewrites a file three times in as many milliseconds should cost one
	// re-read, not three.
	DefaultQuiet = 150 * time.Millisecond

	// DefaultMaxLatency caps how long a change can sit unreported while writes
	// keep arriving, so a target that never goes quiet still refreshes. Without
	// it, a file being appended to continuously would never report at all.
	DefaultMaxLatency = time.Second
)

Variables

This section is empty.

Functions

func Fingerprint

func Fingerprint(v any) string

Fingerprint reduces a value to a short string that changes when the value does.

It is a change detector, not a checksum: collisions would show as a missed refresh, never as corruption, and 64 bits of FNV over the JSON encoding makes that vanishingly unlikely for the sizes involved. JSON is the encoding because it is stable for the plain data structs these panes carry and because it ignores unexported render caches, which change constantly and mean nothing to the user.

A value that cannot be encoded yields a fingerprint that never repeats, so a caller that cannot be compared repaints rather than showing stale content.

func FingerprintString

func FingerprintString(s string) string

FingerprintString reduces raw text to a change-detecting fingerprint. Use it for content already in hand — file bodies, diff output — where re-encoding through JSON would only add work.

func Listen

func Listen(w *PathWatcher, msg tea.Msg) tea.Cmd

Listen returns a command that blocks until w reports a change, then emits msg.

It is one-shot by design, matching how Bubble Tea commands work: the host re-arms it when it handles msg. Returning nil once the watcher stops ends the chain cleanly, so a stopped watcher never leaves a goroutine parked on a receive.

func Release

func Release(w *PathWatcher) tea.Cmd

Release returns a command that stops w.

Stop blocks until the watcher's goroutine has finished, so it must not run on the update goroutine where it would stall the UI. Hosts use this from teardown paths; a host already off the update loop can call Stop directly.

func Start

func Start(cfg Config, targets []Target, wrap func(*PathWatcher, error) tea.Msg) tea.Cmd

Start returns a command that creates a watcher and points it at targets.

The work happens inside the command, off the update goroutine, because creating a watcher opens a descriptor and registering targets touches the filesystem — neither belongs on the path that paints the first frame. Hosts call this when a pane opens, never from Init or Start.

wrap turns the outcome into the host's own message. A host must adopt the watcher it is handed or stop it: a pane that closed while its watcher was being created still has to give the descriptors back.

Types

type Config

type Config struct {
	// Quiet is the settle time before a batch reports. Zero means DefaultQuiet.
	Quiet time.Duration
	// MaxLatency caps time-to-report under continuous writes. Zero means
	// DefaultMaxLatency.
	MaxLatency time.Duration
	// Ignore, when set, drops paths before they are matched against targets.
	// Callers use it to filter the scratch files editors leave beside the file
	// the user is actually looking at.
	Ignore func(path string) bool
}

Config tunes a PathWatcher. The zero value uses the defaults.

type PathWatcher

type PathWatcher struct {
	// contains filtered or unexported fields
}

PathWatcher reports that something in a named set of paths changed.

It says only that: there is no event payload, because every consumer's next move is the same regardless of which path moved — re-read and compare. That keeps coalescing trivially correct, since any number of pending signals collapse into the one buffered slot.

A PathWatcher is safe to use from multiple goroutines. It is created lazily, when a pane opens, and must be stopped when the pane closes.

func NewPathWatcher

func NewPathWatcher(cfg Config) (*PathWatcher, error)

NewPathWatcher starts a watcher with no targets. Nothing is observed, and no descriptors beyond fsnotify's own are held, until PathWatcher.Watch is called.

This must not run on the startup path. Call it from inside a tea.Cmd when a pane opens.

func (*PathWatcher) Signals

func (w *PathWatcher) Signals() <-chan struct{}

Signals returns the channel on which coalesced change notifications arrive. It is closed once the watcher stops, so a consumer blocked on a receive is always released.

func (*PathWatcher) Stop

func (w *PathWatcher) Stop()

Stop releases every descriptor and blocks until the signal channel is closed, so a caller that has stopped a watcher can never see another signal from it.

It is idempotent. Every pane that creates a watcher must call this when it closes, navigates away, or loses a race to adopt the watcher it started.

func (*PathWatcher) Watch

func (w *PathWatcher) Watch(targets ...Target)

Watch replaces the target set, adding and removing only the fsnotify registrations that actually changed.

Passing no targets releases every descriptor while leaving the watcher usable — that is how a pane that navigated away stops observing without tearing down and rebuilding. Watch never returns an error for a target that does not exist: a file an agent has not written yet is a normal state, and the registration on its parent directory will report the create.

func (*PathWatcher) WatchedDirs

func (w *PathWatcher) WatchedDirs() []string

WatchedDirs returns the directories currently registered with fsnotify. It exists so tests can assert that closing a pane gives its descriptors back.

type Refresher

type Refresher struct {
	// contains filtered or unexported fields
}

Refresher sequences re-reads of externally-owned state for one pane.

It is deliberately tiny and deliberately not concurrent: a host owns one per pane and drives it from its own Update loop, on the goroutine that owns the view. It holds no timers, no descriptors and no view state, so it can be embedded in a pane struct that outlives any particular watcher.

The zero value is ready: nothing owed, nothing running, nothing known.

func (*Refresher) Adopt

func (r *Refresher) Adopt(fingerprint string)

Adopt records a fingerprint as already on screen without reporting it as a change. Hosts call this after the pane's initial load, so the first watcher signal is measured against what the user is actually looking at.

func (*Refresher) Begin

func (r *Refresher) Begin(suppressed bool) bool

Begin reports whether the host should issue a re-read now, claiming the single in-flight slot if so.

suppressed is the host's veto — an editor holding an unsaved buffer, a modal that owns the pane, a search the user is halfway through typing. A suppressed change is not dropped: it stays owed, so the refresh lands as soon as the veto lifts and the user never has to know a signal was deferred.

The single in-flight slot is what keeps a busy repository from stacking re-reads. Re-reading the diff costs half a dozen git subprocesses; without this, a rebase would queue one set per ref that moved.

func (*Refresher) Changed

func (r *Refresher) Changed(fingerprint string) bool

Changed reports whether fingerprint differs from the last one applied, and records it either way.

This is the no-repaint gate. The signals these panes watch are coarser than the state they render: touching any issue moves the td store's mtime, and any git command at all moves the index. Without this gate a pane repaints on every unrelated write, which is visible as a flash and is exactly what the tickets asked to avoid. With it, a re-read that produced identical state costs a comparison and stops.

func (*Refresher) Done

func (r *Refresher) Done() bool

Done releases the in-flight slot and reports whether a further change arrived while the last re-read was running, so the host can immediately Begin again.

func (*Refresher) InFlight

func (r *Refresher) InFlight() bool

InFlight reports whether a re-read is running.

func (*Refresher) Observe

func (r *Refresher) Observe()

Observe records that the underlying state may have moved.

It is the only input from the watcher side, and it is intentionally cheap and idempotent: ten signals arriving before the first re-read starts owe exactly one re-read.

func (*Refresher) Pending

func (r *Refresher) Pending() bool

Pending reports whether a re-read is owed but has not started.

func (*Refresher) Reset

func (r *Refresher) Reset()

Reset returns the Refresher to its zero state.

Hosts call this when the pane retargets — a different issue, a different file, a different diff. The next result must always be applied then, because "unchanged since last time" is meaningless once the subject changed, and a suppressed re-read owed for the previous subject must not fire against the new one.

type Target

type Target struct {
	Path string
	// Dir marks Path as a directory whose entries are all of interest, rather
	// than a single file.
	Dir bool
}

Target is one thing a PathWatcher should report changes to.

A file target reports only that exact path. A directory target reports any entry directly inside it. Callers name what they care about; nothing here walks a tree or expands a target into its children.

func Dir

func Dir(path string) Target

Dir returns a Target matching any entry directly inside a directory.

func File

func File(path string) Target

File returns a Target matching exactly one path.

Jump to

Keyboard shortcuts

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