Skip to content

Commit

Permalink
Refactor getDocumentStream w.r.t. the cursor based pagination in getP…
Browse files Browse the repository at this point in the history
…ageByType
  • Loading branch information
sugat009 committed Aug 7, 2024
1 parent 15b6e39 commit 1d62657
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
14 changes: 5 additions & 9 deletions shared-libs/cht-datasource/src/libs/data-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hasField, isRecord } from './core';
import { hasField, isRecord, Page } from './core';
import { isLocalDataContext, LocalDataContext } from '../local/libs/data-context';
import { assertRemoteDataContext, isRemoteDataContext, RemoteDataContext } from '../remote/libs/data-context';

Expand Down Expand Up @@ -42,24 +42,20 @@ export const adapt = <T>(

/** @internal */
export const getDocumentStream = async function* <S, T>(
fetchFunction: (args: S, l: number, s: number) => Promise<T[]>,
fetchFunction: (args: S, l: number, s: number) => Promise<Page<T>>,
fetchFunctionArgs: S
): AsyncGenerator<T, void> {
const limit = 100;
let skip = 0;
// TODO: found a bug in this function where if limit is 1
// then it will always return true but right now since limit
// is hardcoded to 100, it won't but if limit is made to be
// dynamic then, change this implementation
const hasMoreResults = () => skip && skip % limit === 0;
const hasMoreResults = () => skip !== -1;

do {
const docs = await fetchFunction(fetchFunctionArgs, limit, skip);

for (const doc of docs) {
for (const doc of docs.data) {
yield doc;
}

skip += docs.length;
skip = Number(docs.cursor);
} while (hasMoreResults());
};
11 changes: 7 additions & 4 deletions shared-libs/cht-datasource/test/libs/data-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ describe('context lib', () => {

it('yields document one by one', async () => {
const mockDocs = [{ id: 1 }, { id: 2 }, { id: 3 }];
const mockPage = { data: mockDocs, cursor: '-1' };
const extraArg = 'value';
fetchFunctionStub.resolves(mockDocs);
fetchFunctionStub.resolves(mockPage);

const generator = getDocumentStream(fetchFunctionStub, extraArg);

Expand All @@ -148,11 +149,13 @@ describe('context lib', () => {
it('should handle multiple pages', async () => {
const mockDoc = { id: 1 };
const mockDocs1 = Array.from({ length: 100 }, () => ({ ...mockDoc }));
const mockPage1 = { data: mockDocs1, cursor: '100' };
const mockDocs2 = [{ id: 101 }];
const mockPage2 = { data: mockDocs2, cursor: '-1' };
const extraArg = 'value';

fetchFunctionStub.onFirstCall().resolves(mockDocs1);
fetchFunctionStub.onSecondCall().resolves(mockDocs2);
fetchFunctionStub.onFirstCall().resolves(mockPage1);
fetchFunctionStub.onSecondCall().resolves(mockPage2);

const generator = getDocumentStream(fetchFunctionStub, extraArg);

Expand All @@ -168,7 +171,7 @@ describe('context lib', () => {
});

it('should handle empty result', async () => {
fetchFunctionStub.resolves([]);
fetchFunctionStub.resolves({ data: [], cursor: '-1' });

const generator = getDocumentStream(fetchFunctionStub, { limit: 10, skip: 0 });

Expand Down

0 comments on commit 1d62657

Please sign in to comment.