feat: keep what was loaded between runs, and start where you left off #2

Merged
keb merged 3 commits from feat/disk-cache into main 2026-10-11 09:53:33 +02:00
2 changed files with 16 additions and 9 deletions
Showing only changes of commit 9787f78dd8 - Show all commits

No files matched your search

+2 -1
View File
@@ -168,6 +168,7 @@ type Model struct {
cacheDirty bool cacheDirty bool
sourcesAt map[string]time.Time sourcesAt map[string]time.Time
placeDir string placeDir string
savedPlace string // the link last saved as the place
now func() time.Time now func() time.Time
// A pending clear of a copied value, counted down in the footer. // A pending clear of a copied value, counted down in the footer.
@@ -456,7 +457,7 @@ func (m *Model) setVersions(items []provider.Object) {
func (m Model) Init() tea.Cmd { func (m Model) Init() tea.Cmd {
cmds := []tea.Cmd{m.sourcesCmd(), m.spin.Tick, m.checkClipboard()} cmds := []tea.Cmd{m.sourcesCmd(), m.spin.Tick, m.checkClipboard()}
if m.cachePath != "" { if m.cachePath != "" || m.placeDir != "" {
cmds = append(cmds, tea.Tick(persistEvery, func(time.Time) tea.Msg { return persistTickMsg{} })) cmds = append(cmds, tea.Tick(persistEvery, func(time.Time) tea.Msg { return persistTickMsg{} }))
} }
if m.fetchOnStart { if m.fetchOnStart {
+14 -8
View File
@@ -164,15 +164,21 @@ func writeCacheFile(path string, f cacheFile) error {
return os.Rename(tmp.Name(), path) return os.Rename(tmp.Name(), path)
} }
// persistTick saves the cache in the background when it changed. // persistTick saves the cache, and where lazysecrets is, in the
// background when they changed.
func (m *Model) persistTick() tea.Cmd { func (m *Model) persistTick() tea.Cmd {
next := tea.Tick(persistEvery, func(time.Time) tea.Msg { return persistTickMsg{} }) cmds := []tea.Cmd{tea.Tick(persistEvery, func(time.Time) tea.Msg { return persistTickMsg{} })}
if !m.cacheDirty { if m.cacheDirty && m.cachePath != "" {
return next m.cacheDirty = false
f, path := m.cacheSnapshot(), m.cachePath
cmds = append(cmds, func() tea.Msg { return persistedMsg{writeCacheFile(path, f)} })
} }
m.cacheDirty = false if p, ok := m.place(); ok && m.placeDir != "" && p.Link != m.savedPlace {
f, path := m.cacheSnapshot(), m.cachePath m.savedPlace = p.Link
return tea.Batch(next, func() tea.Msg { return persistedMsg{writeCacheFile(path, f)} }) st, dir := m.store, m.placeDir
cmds = append(cmds, func() tea.Msg { return persistedMsg{st.SetPlace(dir, p)} })
}
return tea.Batch(cmds...)
} }
// Persist saves the cache and where lazysecrets was left; run it after // Persist saves the cache and where lazysecrets was left; run it after
@@ -238,6 +244,6 @@ func (m *Model) restorePlace() {
func (m *Model) handlePersisted(msg persistedMsg) { func (m *Model) handlePersisted(msg persistedMsg) {
if msg.err != nil { if msg.err != nil {
m.noteErr(fmt.Errorf("saving the cache: %w", msg.err)) m.noteErr(fmt.Errorf("saving the cache or place: %w", msg.err))
} }
} }