Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions packages/convex/convex/feedPagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ function makeFetchPage(tables: Record<string, any[]>) {
const acceptMain = (c: any) => !c.subagent;

// Drain the feed to the end, asserting per-page invariants as we go.
async function drain(tables: Record<string, any[]>, opts?: { limit?: number; startCursor?: string | null; maxPages?: number }) {
async function drain(
tables: Record<string, any[]>,
opts?: { limit?: number; startCursor?: string | null; maxPages?: number; perMemberFetch?: number; perMemberWant?: number },
) {
const limit = opts?.limit ?? 5;
let cursor: string | null = opts?.startCursor ?? null;
const pages: any[][] = [];
Expand All @@ -42,8 +45,8 @@ async function drain(tables: Record<string, any[]>, opts?: { limit?: number; sta
limit,
fetchPage: makeFetchPage(tables),
accept: acceptMain,
perMemberFetch: 4,
perMemberWant: 3,
perMemberFetch: opts?.perMemberFetch ?? 4,
perMemberWant: opts?.perMemberWant ?? 3,
maxBatches: 2,
});
pages.push(page.rows);
Expand Down Expand Up @@ -97,6 +100,26 @@ describe("paginateTeamFeed", () => {
expect(seen.size).toBe(6);
});

test("a member whose rows fit one batch keeps those past its share for the next page", async () => {
// One fetch reads all five rows and the page takes three: the other two lead the next page.
const tables = { a: member([{ t: 50 }, { t: 40 }, { t: 30 }, { t: 20 }, { t: 10 }], "a") };
const { seen } = await drain(tables, { limit: 3, perMemberFetch: 10, perMemberWant: 3 });
expect(seen.size).toBe(5);
});

test("a team with no member to read ends at once", async () => {
const page = await paginateTeamFeed({
memberIds: [],
cursor: null,
limit: 5,
fetchPage: makeFetchPage({}),
accept: acceptMain,
perMemberFetch: 4,
perMemberWant: 3,
});
expect(page).toEqual({ rows: [], nextCursor: null });
});

test("legacy numeric cursor is honored as a shared bound and upgrades to composite", async () => {
const tables = {
a: member([{ t: 100 }, { t: 80 }, { t: 60 }], "a"),
Expand Down
10 changes: 8 additions & 2 deletions packages/convex/convex/feedPagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ export async function batchScanConversations(opts: {
exhausted = true;
break;
}
// A scan that reaches `want` mid-batch has not examined the rest of it,
// so a short batch is the end of the index only when read to its end.
let examined = 0;
for (const c of batch) {
examined++;
if (accept(c)) {
rows.push(c);
if (rows.length >= want) break;
}
}
cursor = batch[batch.length - 1].updated_at;
cursor = batch[examined - 1].updated_at;
oldestSeen = cursor;
if (batch.length < batchSize) {
if (examined === batch.length && batch.length < batchSize) {
exhausted = true;
break;
}
Expand Down Expand Up @@ -107,6 +111,8 @@ export async function paginateTeamFeed(opts: {
maxBatches?: number;
}): Promise<{ rows: any[]; nextCursor: string | null }> {
const { memberIds, limit, fetchPage, accept, perMemberFetch, perMemberWant, maxBatches = 4 } = opts;
// No member to read (everyone hidden) is the end, not an empty page to repeat.
if (memberIds.length === 0) return { rows: [], nextCursor: null };
const { legacy, members } = parseFeedCursor(opts.cursor);
const boundOf = (id: string): MemberBound => (members ? members[id] : legacy ?? undefined);

Expand Down
Loading