From 724ff531434af943b0a1e212f0e6c8a476bcd87d Mon Sep 17 00:00:00 2001 From: Max Metral Date: Sun, 31 Mar 2024 21:47:38 -0400 Subject: [PATCH] fix(cursor): record completion of a datasource as an empty string cursor --- src/asDataGenerator.ts | 5 +++ src/multiSourcePager.ts | 6 +++- src/queuePager.spec.ts | 78 ++++++++++++++++++++--------------------- 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/asDataGenerator.ts b/src/asDataGenerator.ts index 491c850..b886be8 100644 --- a/src/asDataGenerator.ts +++ b/src/asDataGenerator.ts @@ -8,6 +8,11 @@ export function asDataGenerator( return { async *getGenerator(cursor: string | undefined) { + if (cursor === '') { + // The special empty string cursor indicates we should not fetch any more. + return; + } + let currentCursor = cursor; let continueFetching = true; diff --git a/src/multiSourcePager.ts b/src/multiSourcePager.ts index 2d83032..6b1acc4 100644 --- a/src/multiSourcePager.ts +++ b/src/multiSourcePager.ts @@ -47,6 +47,8 @@ export async function multiSourcePager>( index, key: dataSources[index].sortKey(item.value), }); + } else { + cursors[index] = ''; } })); @@ -63,7 +65,9 @@ export async function multiSourcePager>( // Fetch next item from the generator of the data source that the last item came from const nextResult = await generators[index].next(); - if (!nextResult.done) { + if (nextResult.done) { + cursors[index] = ''; + } else { queue.push({ result: nextResult.value, index, diff --git a/src/queuePager.spec.ts b/src/queuePager.spec.ts index 02ca8e6..34846a5 100644 --- a/src/queuePager.spec.ts +++ b/src/queuePager.spec.ts @@ -65,45 +65,45 @@ describe('multi source pager', () => { const p3 = await pager.getNextResults(5); expect(p3.results).toMatchInlineSnapshot(` - [ - { - "cursor": "WyIyMDIzLTAxLTAzVDAwOjAwOjAwLjAwMFojQyIsIjIwMjMtMDEtMDJUMDA6MDA6MDAuMDAwWiNCQkIiXQ==", - "data": "C", - "type": "letter", - }, - { - "cursor": "WyIyMDIzLTAxLTA0VDAwOjAwOjAwLjAwMFojRCIsIjIwMjMtMDEtMDJUMDA6MDA6MDAuMDAwWiNCQkIiXQ==", - "data": "D", - "type": "letter", - }, - { - "cursor": "WyIyMDIzLTAxLTA1VDAwOjAwOjAwLjAwMFojRSIsIjIwMjMtMDEtMDJUMDA6MDA6MDAuMDAwWiNCQkIiXQ==", - "data": "E", - "type": "letter", - }, - { - "cursor": "WyIyMDIzLTAxLTA1VDAwOjAwOjAwLjAwMFojRSIsIjIwMjMtMDEtMDZUMDA6MDA6MDAuMDAwWiNGRiJd", - "data": "FF", - "type": "letter", - }, - { - "cursor": "WyIyMDIzLTAxLTA1VDAwOjAwOjAwLjAwMFojRSIsIjIwMjMtMDEtMDdUMDA6MDA6MDAuMDAwWiNHRyJd", - "data": "GG", - "type": "letter", - }, - ] - `); + [ + { + "cursor": "WyIyMDIzLTAxLTAzVDAwOjAwOjAwLjAwMFojQyIsIjIwMjMtMDEtMDJUMDA6MDA6MDAuMDAwWiNCQkIiXQ==", + "data": "C", + "type": "letter", + }, + { + "cursor": "WyIyMDIzLTAxLTA0VDAwOjAwOjAwLjAwMFojRCIsIjIwMjMtMDEtMDJUMDA6MDA6MDAuMDAwWiNCQkIiXQ==", + "data": "D", + "type": "letter", + }, + { + "cursor": "WyIyMDIzLTAxLTA1VDAwOjAwOjAwLjAwMFojRSIsIjIwMjMtMDEtMDJUMDA6MDA6MDAuMDAwWiNCQkIiXQ==", + "data": "E", + "type": "letter", + }, + { + "cursor": "WyIiLCIyMDIzLTAxLTA2VDAwOjAwOjAwLjAwMFojRkYiXQ==", + "data": "FF", + "type": "letter", + }, + { + "cursor": "WyIiLCIyMDIzLTAxLTA3VDAwOjAwOjAwLjAwMFojR0ciXQ==", + "data": "GG", + "type": "letter", + }, + ] + `); const p4 = await pager.getNextResults(5); expect(p4.results).toMatchInlineSnapshot(` - [ - { - "cursor": "WyIyMDIzLTAxLTA1VDAwOjAwOjAwLjAwMFojRSIsIjIwMjMtMDEtMDhUMDA6MDA6MDAuMDAwWiNISCJd", - "data": "HH", - "type": "letter", - }, - ] - `); + [ + { + "cursor": "WyIiLCIyMDIzLTAxLTA4VDAwOjAwOjAwLjAwMFojSEgiXQ==", + "data": "HH", + "type": "letter", + }, + ] + `); } }); @@ -137,17 +137,17 @@ describe('multi source pager', () => { "type": "letter", }, { - "cursor": "WyIyMDIzLTAxLTAzVDAwOjAwOjAwLjAwMFojQyIsIjIwMjMtMDEtMDJUMDA6MDA6MDAuMDAwWiNCQkIiXQ==", + "cursor": "WyIyMDIzLTAxLTAzVDAwOjAwOjAwLjAwMFojQyIsIiJd", "data": "C", "type": "letter", }, { - "cursor": "WyIyMDIzLTAxLTA0VDAwOjAwOjAwLjAwMFojRCIsIjIwMjMtMDEtMDJUMDA6MDA6MDAuMDAwWiNCQkIiXQ==", + "cursor": "WyIyMDIzLTAxLTA0VDAwOjAwOjAwLjAwMFojRCIsIiJd", "data": "D", "type": "letter", }, { - "cursor": "WyIyMDIzLTAxLTA1VDAwOjAwOjAwLjAwMFojRSIsIjIwMjMtMDEtMDJUMDA6MDA6MDAuMDAwWiNCQkIiXQ==", + "cursor": "WyIyMDIzLTAxLTA1VDAwOjAwOjAwLjAwMFojRSIsIiJd", "data": "E", "type": "letter", },