vcom/internal/tui/tracker.go
vrubelroman 40b8722d58 feat(tui): App skeleton with mode machine, async navigation and effects (6.1.5)
The new UI's root model: Screen x viewer focus x native selection x
filter edit x modal stack, one inputOwner, keys resolved through the
registry, an action table that features extend from register_*.go.
Directory reads and previews run in Cmds with tickets: Update never
blocks (N-P1-1), stale results are dropped (U-P1-13), failed navigation
keeps the panel and a vanished directory climbs up (U-P1-8). q closes
layers before asking to quit (U-P1-16), PgDn pages (U-P1-6), the theme
list previews live and saves with config.SetKey. Mouse mode is
reconciled after every Update; View only reads state.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 09:11:39 +03:00

95 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tui
import "context"
// ReqKind is a kind of async request.
type ReqKind uint8
const (
ReqList ReqKind = iota
ReqPreview
ReqPreviewDebounce
ReqDirSize
ReqMount
ReqConnect
ReqFeature // features (6.2–6.6) use ReqKey.Extra to tell their requests apart
)
// ReqKey identifies a request slot: a newer request with the same key
// supersedes the older one.
type ReqKey struct {
Kind ReqKind
Pane int
Extra string
}
// Ticket travels with every async message. Update accepts a message only if
// its ticket is still current (design §7.7, U-P1-13).
type Ticket struct {
Key ReqKey
Gen uint64
}
type slot struct {
gen uint64
cancel context.CancelFunc
live bool
}
// Tracker hands out tickets. UI goroutine only.
type Tracker struct {
gen uint64
slots map[ReqKey]*slot
}
// Begin starts a request for k: the previous one with the same key is
// cancelled and its ticket becomes stale.
func (t *Tracker) Begin(k ReqKey) (Ticket, context.Context) {
if t.slots == nil {
t.slots = map[ReqKey]*slot{}
}
if s := t.slots[k]; s != nil && s.cancel != nil {
s.cancel()
}
t.gen++
ctx, cancel := context.WithCancel(context.Background())
t.slots[k] = &slot{gen: t.gen, cancel: cancel, live: true}
return Ticket{Key: k, Gen: t.gen}, ctx
}
// Accept reports whether tk is the current ticket of its key; if so the
// request is finished (its context released) and the slot freed.
func (t *Tracker) Accept(tk Ticket) bool {
s := t.slots[tk.Key]
if s == nil || !s.live || s.gen != tk.Gen {
return false
}
s.live = false
s.cancel()
delete(t.slots, tk.Key)
return true
}
// Pending reports whether a request for k is in flight.
func (t *Tracker) Pending(k ReqKey) bool {
s := t.slots[k]
return s != nil && s.live
}
// Cancel cancels the request for k (its ticket becomes stale).
func (t *Tracker) Cancel(k ReqKey) {
if s := t.slots[k]; s != nil {
s.cancel()
delete(t.slots, k)
}
}
// CancelAll cancels every request of kind.
func (t *Tracker) CancelAll(kind ReqKind) {
for k, s := range t.slots {
if k.Kind == kind {
s.cancel()
delete(t.slots, k)
}
}
}