Skip to content

fix: preserve paginated emails during background refresh#235

Merged
andrinoff merged 3 commits intofloatpane:masterfrom
arclayto:fix/stale-email-cache
Mar 4, 2026
Merged

fix: preserve paginated emails during background refresh#235
andrinoff merged 3 commits intofloatpane:masterfrom
arclayto:fix/stale-email-cache

Conversation

@arclayto
Copy link
Contributor

@arclayto arclayto commented Mar 4, 2026

Summary

  • Fix race condition where EmailsRefreshedMsg overwrites m.emailsByAcct and m.emails, discarding emails loaded via pagination

Root Cause

When the user scrolls down to paginate, EmailsAppendedMsg adds new emails to both the inbox's internal list and m.emails. However, the background refresh (EmailsRefreshedMsg) races with pagination and replaces m.emailsByAcct entirely with only the initial batch, wiping paginated emails from m.emails. The inbox list still displays them, but pressing Enter fails because the email can no longer be found in m.emails.

Fix

EmailsRefreshedMsg now merges refreshed emails with existing paginated emails instead of replacing them. The same fix is applied to the sent mailbox path.

Test plan

  • Scroll past ~50 emails to trigger pagination, open a paginated email — works
  • Press r to refresh while paginated emails are loaded — paginated emails preserved
  • Open emails from initial batch — still works
  • Tested on both Gmail and Proton Bridge (custom IMAP)
  • go test ./... passes

Fixes #234

arclayto added 2 commits March 3, 2026 17:42
EmailsRefreshedMsg was overwriting m.emailsByAcct and rebuilding
m.emails from scratch, discarding any emails loaded via pagination.
This caused a race where paginated emails appeared in the inbox list
but could not be opened because they no longer existed in the main
model's email store.

Merge refreshed emails with existing paginated emails instead of
replacing them. Also remove an unnecessary getEmailByUIDAndAccount
lookup in ViewEmailMsg that silently blocked opening emails not yet
in the store, and drop the unused email parameter from
fetchEmailBodyCmd.
Keep the pre-fetch check to avoid unnecessary IMAP server calls
for emails not in the local store. The merge fix in
EmailsRefreshedMsg ensures paginated emails are preserved, so
this guard now works correctly.
@arclayto arclayto requested a review from andrinoff as a code owner March 4, 2026 00:05
@github-actions github-actions bot added the bug Something isn't working label Mar 4, 2026
Copy link
Member

@andrinoff andrinoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an issue, that when you open an email that was loaded, it closes after opening

Three interconnected issues caused the email view to auto-close when
opening an email that was loaded via pagination:

1. The inbox's EmailsRefreshedMsg handler overwrote its internal email
   data with the raw (unmerged) refresh response, discarding paginated
   emails. Since main.go already merges the data and pushes it via
   SetEmails, the inbox handler now only clears the refreshing flag.

2. EmailsRefreshedMsg was forwarded to m.current twice: once at the top
   of Update (line 110) and again explicitly. This second call could
   target the wrong component or overwrite correctly merged data. Fixed
   by calling m.inbox.Update() directly to clear the refreshing state.

3. The EmailsFetchedMsg handler unconditionally set m.current = m.inbox,
   which would close the email view if a pagination fetch with offset 0
   completed while the user was reading an email. Fixed by only switching
   to inbox from a loading screen, not from an active email view.

Signed-off-by: drew <me@andrinoff.com>
@andrinoff andrinoff self-requested a review March 4, 2026 05:34
@andrinoff andrinoff merged commit 27b7b1f into floatpane:master Mar 4, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Paginated emails cannot be opened after background refresh

2 participants