Files
magpie/internal/gui/assets/plugins/usage
yetone 2620dc319f gui: Routing's countdowns, Usage's refresh look and the lanes' read tick only while in sight (#3185)
#3185 reports magpie's Energy Impact while nobody uses it. In the real
app (sandbox HOME, main window open on Providers, panel hidden) the two
WebKit WebContent processes woke ~5.9 and ~6.0 times a second. A census
of every setInterval in WebKit over 30s idle found three that fired from
anywhere, drawing nothing:
- routing/client.js's 1s countdown tick (30 in 30s),
- usage/page.js's 1s look at the refresh period (30 in 30s),
- app.js's refreshLanes every 2s (15 in 30s), which returned at once
  with no lane pill in sight.
Each now runs only while its page (or a lane pill) is in sight and stops
itself otherwise: Routing's starts again from start() when the page is
seen, Usage's from its view's hidden attribute, visibilitychange and
focus, the lanes' from laneText drawing a pill and visibilitychange.

Measured on the built app (go build -tags production, sandbox HOME,
`magpie app`, 90s warm-up, then 120s of proc_pid_rusage wakeups):
- before (e5dd86965): WebContent 5.93 and 6.01 /s, main 6.69 /s
- after: WebContent 1.61 and 0.93 /s, main 7.70 /s
The main process is unchanged: its threads are Go's (sample + per-thread
CPU: ~10ms CPU in 30s, ~0.03%), and a Go trace of the new build shows
~2.6 goroutine starts/s, mostly the minute loops lined up at :10 and :40
and the 2-min forced GC. That is left to its own change.

Test: tests/idle-ticks.test.cjs counts each interval's ticks by script
and period under Playwright's clock: on Providers none of the three
ticks in 10s; on Usage its look ticks and Routing's doesn't; on Routing
the reverse. Red on e5dd86965 in both engines ("plugins/routing/
client.js ticked out of sight, 10 !== 0"), green here.

Run at this head (rebased on origin/main e5dd86965):
- go test -count=1 -timeout 45m ./internal/gui: ok (budgets included;
  app.js 1,326,086 under 1,326,105, core scripts under 1,542,418)
- GUI, both engines: idle-ticks, i18n-keys, lane-limit, usage-refresh,
  quota-curve, quota-holds, sess-agent-strip, usage-head-steady,
  click-scroll, routing-time, routing-time-went, routing-reset-deadline,
  routing-auto-reset, usage-card-refresh, usage-refresh-flight,
  usage-loops, routing-idle-frame, routing-hidden-stage,
  panel-routing-hidden, routing-js, routing-sessions,
  usage-focus-steady: all pass.
- routing-steady (2 red) and routing-live-patch (6 red) fail the same
  on origin/main e5dd86965 run alone: #3412 and #3637.
Not tried: the window minimised or closed (WebKit's own throttling), and
Linux/Windows webviews; the change is plain JS timers.
2026-10-26 14:59:13 +08:00
..