IMAP Folder speedup tuning

This commit is contained in:
2026-11-09 20:11:31 +02:00
parent d0b26db9ee
commit b14209a5ae
14 changed files with 216 additions and 18 deletions
+36 -4
View File
@@ -11,6 +11,8 @@ const mailFeatureState = {
total: 0,
syncPromise: null,
syncQueued: false,
syncMailboxKey: null,
displayedMailboxKey: null,
refreshPending: false,
folderBeforeSearch: null,
customFolders: [],
@@ -648,9 +650,14 @@ function setupDraftComposer() {
}
async function syncMailbox(showToast, options = {}) {
const isAndroid = window.daybreakMail?.platform === 'android';
const mailboxKey = JSON.stringify([mailFeatureState.connectedAccount?.id, rendererState.activeFolder]);
if (mailFeatureState.syncPromise) {
mailFeatureState.syncQueued = true;
return mailFeatureState.syncPromise;
if (!isAndroid || mailFeatureState.syncMailboxKey === mailboxKey) {
mailFeatureState.syncQueued = true;
return mailFeatureState.syncPromise;
}
mailFeatureState.syncQueued = false;
}
if (!mailFeatureState.connectedAccount || !window.daybreakMail) {
openMailAccount();
@@ -658,11 +665,26 @@ async function syncMailbox(showToast, options = {}) {
}
const refresh = selectMailUi('#refresh');
if (refresh) refresh.disabled = true;
const request = {};
mailFeatureState.syncRequest = request;
mailFeatureState.syncMailboxKey = mailboxKey;
const sync = (async () => {
let showingCachedMessages = false;
try {
if (isAndroid && mailFeatureState.displayedMailboxKey !== mailboxKey) {
mailFeatureState.displayedMailboxKey = mailboxKey;
rendererState.messages.splice(0, rendererState.messages.length);
rendererState.activeMessage = null;
rendererState.selectedIds.clear();
document.body.classList.remove('reader-open');
selectMailUi('#reader-pane').innerHTML = appShell.emptyReader();
appShell.renderList();
const status = selectMailUi('#mail-status');
if (status) status.textContent = 'Loading folder…';
}
if (rendererState.activeFolder === 'Drafts') {
const result = await window.daybreakMail.listDrafts();
if (isAndroid && mailFeatureState.syncRequest !== request) return;
renderDrafts(result.drafts || []);
if (showToast) toastMailUi(`${result.drafts.length} saved drafts`);
return;
@@ -671,19 +693,27 @@ async function syncMailbox(showToast, options = {}) {
const accountId = mailFeatureState.connectedAccount.id;
const offset = options.offset ?? (options.append ? mailFeatureState.pageOffset + rendererState.messages.length : mailFeatureState.pageOffset);
const filters = mailSearchFilters();
const isCurrentMailbox = () => rendererState.activeFolder === folder && mailFeatureState.connectedAccount?.id === accountId;
const isCurrentMailbox = () => rendererState.activeFolder === folder && mailFeatureState.connectedAccount?.id === accountId &&
(!isAndroid || mailFeatureState.syncRequest === request);
if (window.daybreakMail.getCachedFolder && !options.append && offset === 0 && !rendererState.messages.length) {
try {
const cached = await window.daybreakMail.getCachedFolder(folder, offset, filters);
if (cached && isCurrentMailbox()) {
rendererState.messages.splice(0, rendererState.messages.length, ...cached.messages);
rendererState.activeMessage = null;
mailFeatureState.pageOffset = offset;
mailFeatureState.total = cached.total;
const pagination = selectMailUi('#pagination');
if (pagination) pagination.textContent = cached.total ? `${cached.start}–${cached.end} of ${cached.total} saved messages` : '0 saved messages';
const loadMore = selectMailUi('#load-more');
if (loadMore) loadMore.classList.toggle('hidden', cached.messages.length >= cached.total);
appShell.renderList();
showingCachedMessages = true;
const status = selectMailUi('#mail-status');
if (status) status.textContent = 'Showing saved messages — syncing';
}
} catch (error) {
if (isAndroid && mailFeatureState.syncRequest !== request) return;
console.error('Could not load cached messages:', error);
}
}
@@ -734,7 +764,7 @@ async function syncMailbox(showToast, options = {}) {
}
toastMailUi(error.message || 'Could not sync the inbox');
} finally {
if (refresh) refresh.disabled = false;
if (refresh && (!isAndroid || mailFeatureState.syncRequest === request)) refresh.disabled = false;
}
})();
@@ -742,7 +772,9 @@ async function syncMailbox(showToast, options = {}) {
try {
await sync;
} finally {
if (mailFeatureState.syncPromise !== sync) return;
mailFeatureState.syncPromise = null;
mailFeatureState.syncMailboxKey = null;
if (mailFeatureState.syncQueued) {
mailFeatureState.syncQueued = false;
void syncMailbox(false);