From 855ec37935f29f896f61f542722cfeceba97ff9c Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 15:09:02 +0100 Subject: [PATCH] fix(mail-ingest): read the inbox again when it changes while a read is on its way A reload asked for during a read was dropped and the older answer overwrote the newer count, and an entry decided meanwhile came back with it. --- client/src/store/mailIngestStore.test.ts | 34 +++++++++++++++++++++++- client/src/store/mailIngestStore.ts | 34 +++++++++++++++++------- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/client/src/store/mailIngestStore.test.ts b/client/src/store/mailIngestStore.test.ts index 0100fdac0..ac6d1291b 100644 --- a/client/src/store/mailIngestStore.test.ts +++ b/client/src/store/mailIngestStore.test.ts @@ -1,4 +1,4 @@ -// FE-STORE-MAILINGEST-001 to FE-STORE-MAILINGEST-006 +// FE-STORE-MAILINGEST-001 to FE-STORE-MAILINGEST-008 import { beforeEach, describe, expect, it, vi } from 'vitest'; vi.mock('../api/mailIngest', () => ({ @@ -104,6 +104,38 @@ describe('mailIngestStore', () => { expect(state.loadingInbox).toBe(false); }); + it('FE-STORE-MAILINGEST-007: a reload asked for while a read is on its way runs once that read is in', async () => { + let answer: (value: unknown) => void = () => undefined; + vi.mocked(mailIngestApi.inbox) + .mockImplementationOnce(() => new Promise((resolve) => (answer = resolve)) as never) + .mockResolvedValueOnce({ entries: [entry(1), entry(2), entry(3)], pending: 3, next_before: null }); + const stop = watchMailInbox(); + const reading = useMailIngestStore.getState().loadInbox(); + listeners.forEach((fn) => fn({ type: 'mailInbox:changed', pending: 3 })); + answer({ entries: [entry(1), entry(2)], pending: 2, next_before: null }); + await reading; + const state = useMailIngestStore.getState(); + expect(state.entries.map((e) => e.id)).toEqual([1, 2, 3]); + expect(state.pending).toBe(3); + expect(mailIngestApi.inbox).toHaveBeenCalledTimes(2); + stop(); + }); + + it('FE-STORE-MAILINGEST-008: an entry decided while a read is on its way does not come back with it', async () => { + useMailIngestStore.setState({ entries: [entry(1), entry(2)] as never, pending: 2 }); + let answer: (value: unknown) => void = () => undefined; + vi.mocked(mailIngestApi.inbox) + .mockImplementationOnce(() => new Promise((resolve) => (answer = resolve)) as never) + .mockResolvedValueOnce({ entries: [entry(2)], pending: 1, next_before: null }); + vi.mocked(mailIngestApi.act).mockResolvedValue({ candidate: candidate(1, 'discarded') }); + const reading = useMailIngestStore.getState().loadInbox(); + await useMailIngestStore.getState().act(1, 'discard'); + answer({ entries: [entry(1), entry(2)], pending: 2, next_before: null }); + await reading; + expect(useMailIngestStore.getState().entries.map((e) => e.id)).toEqual([2]); + expect(useMailIngestStore.getState().pending).toBe(1); + }); + it('FE-STORE-MAILINGEST-005: the server inbox events move the count, read the list again and reload the settings', async () => { vi.mocked(mailIngestApi.inbox).mockResolvedValue({ entries: [entry(3)], pending: 1, next_before: null }); vi.mocked(mailIngestApi.settings).mockResolvedValue({ nope: true }); diff --git a/client/src/store/mailIngestStore.ts b/client/src/store/mailIngestStore.ts index de97a8996..bdcdfbed5 100644 --- a/client/src/store/mailIngestStore.ts +++ b/client/src/store/mailIngestStore.ts @@ -35,6 +35,8 @@ const EMPTY = { settings: null, entries: [], nextBefore: null, pending: 0, loadi export const useMailIngestStore = create((set, get) => { // Bumped on sign-out, so an answer still on its way for the account before lands nowhere. let account = 0; + // Set when the inbox changed while a read was on its way: that answer is older, so the read runs again. + let stale = false; return { ...EMPTY, @@ -63,18 +65,30 @@ export const useMailIngestStore = create((set, get) => { }, loadInbox: async (more = false) => { - if (isEffectivelyOffline() || get().loadingInbox) return; + if (isEffectivelyOffline()) return; + if (get().loadingInbox) { + if (!more) stale = true; + return; + } const asked = account; set({ loadingInbox: true }); try { - const before = more ? (get().nextBefore ?? undefined) : undefined; - const { entries, pending, next_before } = await mailIngestRepo.inbox(before); - if (asked !== account) return; - set((state) => ({ - entries: more ? [...state.entries, ...entries] : entries, - pending, - nextBefore: next_before, - })); + let page = more; + do { + stale = false; + const before = page ? (get().nextBefore ?? undefined) : undefined; + const { entries, pending, next_before } = await mailIngestRepo.inbox(before); + if (asked !== account) return; + if (stale) { + page = false; + continue; + } + set((state) => ({ + entries: page ? [...state.entries, ...entries] : entries, + pending, + nextBefore: next_before, + })); + } while (stale); } catch { // The inbox stays as it was; the next open reads it again. } finally { @@ -86,6 +100,7 @@ export const useMailIngestStore = create((set, get) => { const asked = account; const candidate = await mailIngestRepo.act(candidateId, action, body); if (asked !== account) return candidate; + if (get().loadingInbox) stale = true; set((state) => { const stillOpen = OPEN.has(candidate.resolution); const entries = state.entries.flatMap((entry) => @@ -99,6 +114,7 @@ export const useMailIngestStore = create((set, get) => { reset: () => { account += 1; + stale = false; set(EMPTY); }, };