Skip to content

Commit

Permalink
fix(cursor): record completion of a datasource as an empty string cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
djMax committed Apr 1, 2024
1 parent 05227ac commit 724ff53
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
5 changes: 5 additions & 0 deletions src/asDataGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export function asDataGenerator<T extends ResultWithCursor>(

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;

Expand Down
6 changes: 5 additions & 1 deletion src/multiSourcePager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export async function multiSourcePager<Types extends Array<Source>>(
index,
key: dataSources[index].sortKey(item.value),
});
} else {
cursors[index] = '';
}
}));

Expand All @@ -63,7 +65,9 @@ export async function multiSourcePager<Types extends Array<Source>>(

// 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,
Expand Down
78 changes: 39 additions & 39 deletions src/queuePager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
]
`);
}
});

Expand Down Expand Up @@ -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",
},
Expand Down

0 comments on commit 724ff53

Please sign in to comment.