feat: stabilize mailbox status query options and combine callback to avoid repeated aggregation

This commit is contained in:
Matthew Meszaros
2026-10-14 03:18:30 +00:00
parent ecdbf5f757
commit 3101006cc2
2 changed files with 127 additions and 17 deletions
@@ -0,0 +1,104 @@
import type { PropsWithChildren } from "react";
import { act, renderHook, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { beforeEach, describe, expect, it, vi } from "vitest";
import useAccountStatuses from "@/lib/api/hooks/app/analytics/useAccountStatuses";
import getAccountStatuses from "@/lib/api/client/app/analytics/getAccountStatuses";
import type AccountStatus from "@/lib/api/models/app/analytics/AccountStatus";
vi.mock("@/lib/api/client/app/analytics/getAccountStatuses", () => ({ default: vi.fn() }));
function harness() {
const client = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: Infinity, staleTime: Infinity } } });
const wrapper = ({ children }: PropsWithChildren) => <QueryClientProvider client={client}>{children}</QueryClientProvider>;
return { client, wrapper };
}
function status(id: string): AccountStatus {
return { id } as AccountStatus;
}
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(getAccountStatuses).mockImplementation(async (ids) => ids.map(status));
});
describe("account status aggregation", () => {
it("keeps sorted, bounded keys without mutating the input or refetching on reorder", async () => {
const { client, wrapper } = harness();
const ids = Array.from({ length: 205 }, (_, i) => String(204 - i).padStart(3, "0"));
const original = [...ids];
const { result, rerender, unmount } = renderHook(({ ids }) => useAccountStatuses(ids), { wrapper, initialProps: { ids } });
await waitFor(() => expect(result.current.data).toHaveLength(205));
expect(ids).toEqual(original);
expect(vi.mocked(getAccountStatuses).mock.calls.map(([chunk]) => chunk.length)).toEqual([100, 100, 5]);
expect(result.current.data.map((s) => s.id)).toEqual([...ids].sort());
expect(getAccountStatuses).toHaveBeenCalledTimes(3);
const first = result.current;
rerender({ ids: [...ids].reverse() });
expect(result.current).toBe(first);
expect(getAccountStatuses).toHaveBeenCalledTimes(3);
expect(client.getQueryCache().getAll().every((q) => q.queryKey.slice(0, 3).join("/") === "analytics/accounts/list")).toBe(true);
unmount(); client.clear();
});
it("skips per-row aggregation on unchanged renders and recomputes for realtime data", async () => {
const { client, wrapper } = harness();
const ids = Array.from({ length: 200 }, (_, i) => String(i));
const { result, rerender, unmount } = renderHook(() => useAccountStatuses(ids), { wrapper });
await waitFor(() => expect(result.current.data).toHaveLength(200));
const first = result.current;
const query = client.getQueryCache().getAll()[0];
// Every aggregation maps each chunk into observations.
const observationMaps = client.getQueryCache().getAll().map((q) => vi.spyOn(q.state.data as AccountStatus[], "map"));
const start = performance.now();
for (let i = 0; i < 200; i++) rerender();
console.info(JSON.stringify({ fixture: "200 statuses; two 100-row chunks; 200 unchanged renders", observationMaps: observationMaps.reduce((sum, spy) => sum + spy.mock.calls.length, 0), elapsedMs: performance.now() - start }));
expect(result.current).toBe(first);
for (const spy of observationMaps) {
expect(spy).not.toHaveBeenCalled();
spy.mockRestore();
}
act(() => client.setQueryData(query.queryKey, [status("updated")]));
await waitFor(() => expect(result.current.data[0].id).toBe("updated"));
expect(result.current.observations[0]).toMatchObject({ status: status("updated"), current: true });
unmount(); client.clear();
});
it("preserves enabled gating, empty selection and source-revision freshness", async () => {
const { client, wrapper } = harness();
const { result, rerender, unmount } = renderHook(
({ ids, enabled, revision }) => useAccountStatuses(ids, { enabled, sourceRevision: revision }),
{ wrapper, initialProps: { ids: [] as string[], enabled: false, revision: 0 } },
);
expect(result.current.data).toEqual([]);
expect(getAccountStatuses).not.toHaveBeenCalled();
rerender({ ids: ["a"], enabled: false, revision: 0 });
expect(getAccountStatuses).not.toHaveBeenCalled();
rerender({ ids: ["a"], enabled: true, revision: 0 });
await waitFor(() => expect(result.current.data).toEqual([status("a")]));
rerender({ ids: ["b"], enabled: true, revision: 1 });
await waitFor(() => expect(result.current.data).toEqual([status("b")]));
expect(getAccountStatuses).toHaveBeenCalledTimes(2);
expect(client.getQueryCache().getAll().some((q) => q.queryKey[4] === 1)).toBe(true);
unmount(); client.clear();
});
it("marks retained observations non-current during refetch and on refetch failure", async () => {
const { client, wrapper } = harness();
const ids = ["a"];
const { result, unmount } = renderHook(() => useAccountStatuses(ids), { wrapper });
await waitFor(() => expect(result.current.observations[0]?.current).toBe(true));
const observedAt = result.current.observations[0].observedAt;
let reject!: (reason: Error) => void;
vi.mocked(getAccountStatuses).mockImplementationOnce(() => new Promise((_resolve, rej) => { reject = rej; }));
act(() => { void client.invalidateQueries({ queryKey: ["analytics", "accounts"] }); });
await waitFor(() => expect(result.current.isFetching).toBe(true));
expect(result.current.observations[0]).toMatchObject({ observedAt, current: false });
act(() => reject(new Error("refetch failed")));
await waitFor(() => expect(result.current.isError).toBe(true));
expect(result.current.data).toEqual([status("a")]);
expect(result.current.observations[0]).toMatchObject({ observedAt, current: false });
unmount(); client.clear();
});
});
@@ -1,4 +1,5 @@
import { useQueries } from "@tanstack/react-query";
import { useMemo } from "react";
import { useQueries, type UseQueryResult } from "@tanstack/react-query";
import getAccountStatuses from "@/lib/api/client/app/analytics/getAccountStatuses";
import type AccountStatus from "@/lib/api/models/app/analytics/AccountStatus";
@@ -22,22 +23,27 @@ export default function useAccountStatuses(emailIds: string[], options?: { enabl
// Sort so a chunk's id set (and its query key) is stable regardless of the
// order the mailbox list arrived in; adding a mailbox only refetches the
// chunk it lands in.
const chunks = chunk([...emailIds].sort());
return useQueries({
queries: chunks.map((ids) => ({
queryKey: ["analytics", "accounts", "list", ids, options?.sourceRevision ?? 0],
const enabled = options?.enabled ?? true;
const sourceRevision = options?.sourceRevision ?? 0;
const queries = useMemo(
() => chunk([...emailIds].sort()).map((ids) => ({
queryKey: ["analytics", "accounts", "list", ids, sourceRevision],
queryFn: ({ signal }: { signal: AbortSignal }) => getAccountStatuses(ids, signal),
enabled: ids.length > 0 && (options?.enabled ?? true),
enabled: ids.length > 0 && enabled,
})),
// react-query memoizes this by result reference, so the merged array is
// stable across renders until a chunk actually updates.
combine: (results) => ({
data: results.flatMap((r) => (r.data ?? []) as AccountStatus[]),
observations: results.flatMap((r) => (r.data ?? []).map((status) => ({ status, observedAt: r.dataUpdatedAt, current: r.isSuccess && !r.isFetching }))),
isLoading: results.some((r) => r.isLoading),
isFetching: results.some((r) => r.isFetching),
isError: results.some((r) => r.isError),
}),
});
[emailIds, enabled, sourceRevision],
);
return useQueries({ queries, combine: combineAccountStatuses });
}
// TanStack reuses the aggregation only while both results and this callback are stable.
function combineAccountStatuses(results: UseQueryResult<AccountStatus[], Error>[]) {
return {
data: results.flatMap((r) => r.data ?? []),
observations: results.flatMap((r) => (r.data ?? []).map((status) => ({ status, observedAt: r.dataUpdatedAt, current: r.isSuccess && !r.isFetching }))),
isLoading: results.some((r) => r.isLoading),
isFetching: results.some((r) => r.isFetching),
isError: results.some((r) => r.isError),
};
}