Browser: keep the file list live (re-read on change, re-sort, no idle frames)

The browser list was cached: files added/removed/renamed, or whose mtime
changed, did not show up (and did not re-sort) until the user navigated
away and back in. In particular, opening a file and returning showed a
stale list — a new file created elsewhere only appeared after navigating
up and back down.

The browser now re-reads the current directory while it is on screen:

- A short (~1 s) timer re-reads the visible directory so external changes
  (including inbound syncs) appear live, re-sorted in the current sort
  mode. A landing re-index repopulates the visible+prefetch pages
  synchronously (no flicker), preserves the scroll offset, and re-filters
  an active search without a scroll jump.
- Returning to the browser from the editor triggers one immediate re-read
  so the list is current the moment you land back.
- Bounded to one read per directory: a refresh while a read of the same
  directory is in flight coalesces, and a stale result for a directory the
  user has since left is dropped rather than clobbering the current view.
- A failed background refresh keeps the last good view (the next tick
  retries); a failed navigation still recovers to the previous directory.
- Change detection: a refresh that finds the directory unchanged rebuilds
  nothing and emits no frame, preserving the event-driven emission
  contract (TestNoFramesWhileIdle_Browser) while the browser idles.

Verified on-device: external add while sitting in the browser appears and
re-sorts to the top within ~1 s; an external add+remove made while in the
editor is reflected immediately on return; an external delete drops the
row within ~1 s.
This commit is contained in:
gmp
2026-09-02 18:23:16 -04:00
parent 5e40378072
commit 16740b0de0
7 changed files with 454 additions and 45 deletions
+70 -4
View File
@@ -93,6 +93,12 @@ type Logic struct {
done chan struct{}
exitWg sync.WaitGroup
saveTimer *time.Timer // auto-save debounce timer; non-nil while pending
// browserRefresh* keep the visible browser directory fresh while the
// browser page is on screen (external adds/removes/renames/mtime changes,
// incl. inbound syncs). The timer goroutine only sends a token; the owner
// does the re-read. All touched only on the owner goroutine.
browserRefreshChan chan struct{} // periodic browser re-read (timer -> owner)
browserRefreshTimer *time.Timer // browser refresh timer; non-nil while pending
lastEmit time.Time // time of the last frame emission (profiler cadence)
debugCmdC chan string // one-shot debug commands from the cmd-file poller; nil = disabled
@@ -183,6 +189,7 @@ func NewLogic(mfs pool.FileSystem, path string, openfunc func(string)) *Logic {
openFileChan: make(chan string),
retryChan: make(chan string, 1), // Buffered channel
autosaveChan: make(chan struct{}),
browserRefreshChan: make(chan struct{}),
flushSession: make(chan struct{}, 1),
inspectChan: make(chan *inspectReq),
writeInFlight: make(map[string]int),
@@ -260,8 +267,10 @@ func (l *Logic) Run() {
l.exitWg.Add(1)
defer l.exitWg.Done()
// Dispatch initial directory index build on startup
l.workerPool.Dispatch(pool.NewBuildIndexTask(l.state.Browser.CurrentPath, l.mockFS))
// Dispatch the initial directory index build on startup, routed through the
// manager so the in-flight marker is set (a later refresh of the same
// directory coalesces, and a late result is dropped once we navigate).
l.browserManager.Load(l.state.Browser.CurrentPath)
// Relaunch restoration (spec §7): re-open the last file if a session
// was handed in before Run (BeginRestore). The browser index is still
@@ -362,6 +371,11 @@ func (l *Logic) Run() {
// the owner snapshots content and dispatches the write.
l.saveTimer = nil
l.requestSave(l.state.Editor.Filename)
case <-l.browserRefreshChan:
// Periodic browser-refresh tick. The timer goroutine only sent a
// token; the owner re-reads the current directory (bounded) and
// re-arms. No-op while the editor is on screen.
l.doBrowserRefresh()
case p := <-l.state.pasteChan:
// Clipboard content arrived from the main goroutine: insert it
// (replacing any live selection, per the selection-aware edit rule).
@@ -411,6 +425,14 @@ func (l *Logic) emitFrame() {
// Relaunch snapshot (spec §7): persist when the state has changed and
// the rate limit elapsed (tiny JSON file, see session.go).
l.saveSessionIfChanged()
// While the browser is on screen, keep its directory fresh so external
// adds/removes/renames/mtime changes (incl. inbound syncs) appear live, in
// the current sort order. Arming here covers every entry onto the browser
// page (startup, back from the editor, sort toggle) and re-arms after each
// refresh tick; the timer goroutine only sends a token.
if l.state.page == BrowserPage {
l.armBrowserRefresh()
}
elems := l.state.layout(l.browserManager)
// Relaunch restore (spec §7): land the armed restore scroll AFTER the
// layout pass above (it refreshed MaxScroll for the current viewport and
@@ -655,10 +677,52 @@ func (l *Logic) markDirty() {
})
}
// browserRefreshInterval is how often the visible browser directory is re-read
// while the browser page is on screen, so external changes (files added,
// removed, renamed, or mtime-changed — including inbound syncs) appear live, in
// the current sort order. One directory read per interval is a single ReadDir
// plus a per-entry stat; the manager's in-flight marker keeps it to one read at
// a time, so a slow read simply coalesces the next tick.
const browserRefreshInterval = time.Second
// doBrowserRefresh runs on the owner goroutine when the refresh timer fires:
// re-read the current directory (bounded to one in flight) and re-arm. It is a
// no-op while the editor is on screen — there is no list to keep fresh there.
func (l *Logic) doBrowserRefresh() {
l.browserRefreshTimer = nil
if l.state.page != BrowserPage {
return
}
l.browserManager.Refresh(l.state.Browser.CurrentPath)
l.armBrowserRefresh()
}
// armBrowserRefresh starts (or leaves running) the periodic browser-refresh
// timer. Idempotent: at most one timer at a time. The timer goroutine only
// sends a token on browserRefreshChan; the owner does the re-read. The send is
// non-blocking so the timer goroutine can never block, even after the logic
// loop has exited (the token is then simply dropped). Owner-goroutine-only.
func (l *Logic) armBrowserRefresh() {
if l.browserRefreshTimer != nil {
return
}
l.browserRefreshTimer = time.AfterFunc(browserRefreshInterval, func() {
select {
case l.browserRefreshChan <- struct{}{}:
default:
}
})
}
// handleWorkerResult processes results from the worker pool.
func (l *Logic) handleWorkerResult(res pool.Result) {
// A browser result re-emits only when it actually changed the view; a
// periodic refresh of an unchanged directory is dropped here, keeping
// emission event-driven while the browser idles. Other results always
// re-emit.
emit := true
if res.IsBrowserResult() {
l.browserManager.HandleResult(res)
emit = l.browserManager.HandleResult(res)
} else if res.TaskType == pool.TypeReadFile {
if res.Success {
if content, ok := res.Data.([]byte); ok {
@@ -824,7 +888,9 @@ func (l *Logic) handleWorkerResult(res pool.Result) {
l.state.Editor.applySearchResult(res)
}
}
l.emitFrame()
if emit {
l.emitFrame()
}
}
// State returns the current state.
+6
View File
@@ -686,6 +686,12 @@ func HandleBrowserScroll(data any) {
func GoToBrowser(data any) {
if TheLogic != nil {
TheLogic.FlushAll()
// Re-read the current directory now (don't wait for the first periodic
// tick) so the list is current the moment we land back: files may have
// been added/removed/renamed, or an inbound sync may have changed them,
// while we were in the editor. The periodic refresh keeps it fresh
// afterwards (see Logic.armBrowserRefresh).
TheLogic.browserManager.Refresh(TheLogic.state.Browser.CurrentPath)
}
TheState.Editor.findClose()
TheState.page = BrowserPage