Persist edits immediately so a fast app-kill loses no state
Reproduced on device: open a file, scroll, long-press-highlight a word, then recents-wipe the app ~1s after the highlight. The session file held the file but lost the selection and cursor: the 1s rate-limit had not elapsed since the previous save, so the final changes never flushed before the process died (a recents-wipe is a kill, not a clean shutdown, so Shutdown's final save never runs). Two changes in saveSessionIfChanged: - Urgent path: save immediately when the file changes or a selection appears/vanishes. These are low-frequency, high-value changes (the user just opened a file or highlighted/cleared text), and they are exactly what 'where I left off' means. - sessionSaveInterval 1s -> 250ms: bounds how stale scroll/cursor can be at an unlucky kill. The file is a few hundred bytes, so a few small writes a second during active scrolling is negligible. TestRestore_UrgentSaveOnSelection drives the kill race: a selection made right after the file-open save must land in the saver without waiting out the interval (fails with the urgent path disabled).
This commit is contained in:
@@ -30,8 +30,12 @@ import (
|
||||
)
|
||||
|
||||
// sessionSaveInterval rate-limits the periodic session save. The file is
|
||||
// tiny (a few hundred bytes), so one write a second at most is negligible.
|
||||
const sessionSaveInterval = time.Second
|
||||
// tiny (a few hundred bytes), so a few writes a second at most is
|
||||
// negligible. The window bounds how stale the persisted state can be if
|
||||
// the process is killed (recents-wipe) right after a change: at 1s, a
|
||||
// kill less than a second after the last edit lost the cursor, selection
|
||||
// and final scroll. See saveSessionIfChanged for the urgent path.
|
||||
const sessionSaveInterval = 250 * time.Millisecond
|
||||
|
||||
// SessionState is the relaunch snapshot (see the file doc above). All
|
||||
// positions are absolute file byte offsets; Scroll is in Dp. It is a plain
|
||||
@@ -101,13 +105,21 @@ func (l *Logic) saveSessionIfChanged() {
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
if now.Sub(l.lastSessionSave) < sessionSaveInterval {
|
||||
return
|
||||
}
|
||||
s := l.SnapshotSession()
|
||||
if s == l.lastSession {
|
||||
return
|
||||
}
|
||||
// Save immediately when the file changes or a selection appears or
|
||||
// vanishes — low-frequency, high-value changes (the user just opened a
|
||||
// file or highlighted/cleared text). Everything else (scroll ticks,
|
||||
// cursor moves, selection-handle drags) is high-frequency and stays
|
||||
// rate-limited; a kill within the window then loses at most a fraction
|
||||
// of a second of motion, not the edit state.
|
||||
urgent := s.File != l.lastSession.File ||
|
||||
(s.SelStart >= 0) != (l.lastSession.SelStart >= 0)
|
||||
if !urgent && now.Sub(l.lastSessionSave) < sessionSaveInterval {
|
||||
return
|
||||
}
|
||||
l.sessionSaver(s)
|
||||
l.lastSession = s
|
||||
l.lastSessionSave = now
|
||||
|
||||
Reference in New Issue
Block a user