Skip to content

Commit

Permalink
Avoid using spread operator in getDocumentStream function
Browse files Browse the repository at this point in the history
  • Loading branch information
sugat009 committed Jul 31, 2024
1 parent 9015353 commit 8be7b70
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion shared-libs/cht-datasource/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const getDatasource = (ctx: DataContext) => {
* @throws Error if the provided skip is `< 0`
*/
getPage: (personType: string, limit = 100, skip = 0) => ctx.bind(Person.v1.getPage)(
Qualifier.byContactType(personType), limit, skip
{ personType: Qualifier.byContactType(personType), limit, skip }
),

/**
Expand Down
20 changes: 12 additions & 8 deletions shared-libs/cht-datasource/src/libs/data-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,24 @@ export const adapt = <T>(
return remote(context);
};

interface PaginationArgs {
limit: number;
skip: number;
}

type FetchFunctionArgs<TFields extends Record<string, unknown>> = PaginationArgs & TFields;

/** @internal */
export const getDocumentStream = async function* <T, Args extends unknown[]>(
fetchFunction: (...args: Args) => Promise<T[]>,
fetchFunctionArgs: {
limit: number;
skip: number;
} & Record<string, unknown>
): AsyncGenerator<unknown, void> {
export const getDocumentStream = async function* <T, TFields extends Record<string, unknown>>(
fetchFunction: (args: FetchFunctionArgs<TFields>) => Promise<T[]>,
fetchFunctionArgs: FetchFunctionArgs<TFields>
): AsyncGenerator<T, void> {
const { limit } = fetchFunctionArgs;
let { skip } = fetchFunctionArgs;
const hasMoreResults = () => skip && skip % limit === 0;

do {
const docs = await fetchFunction(...(Object.values(fetchFunctionArgs) as Args));
const docs = await fetchFunction(fetchFunctionArgs);

for (const doc of docs) {
yield doc;
Expand Down
8 changes: 6 additions & 2 deletions shared-libs/cht-datasource/src/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ export namespace v1 {
* @throws Error if the provided `limit` value is `<=0`
* @throws Error if the provided `skip` value is `<0`
*/
const curriedFn = async (personType: ContactTypeQualifier, limit = 100, skip = 0): Promise<T> => {
const curriedFn = async ({personType, limit = 100, skip = 0}: {
personType: ContactTypeQualifier;
limit?: number;
skip?: number;
}): Promise<T> => {
assertTypeQualifier(personType);
assertLimitAndSkip(limit, skip);

Expand Down Expand Up @@ -133,7 +137,7 @@ export namespace v1 {
const getPage = context.bind(v1.getPage);
const limit = 100;
const skip = 0;
yield* getDocumentStream(getPage, { personType, limit, skip }) as AsyncGenerator<v1.Person, void>;
yield* getDocumentStream(getPage, { personType, limit, skip });
};
};
}
2 changes: 1 addition & 1 deletion shared-libs/cht-datasource/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('CHT Script API - getDatasource', () => {

expect(returnedPeople).to.equal(expectedPeople);
expect(dataContextBind.calledOnceWithExactly(Person.v1.getPage)).to.be.true;
expect(personGetPage.calledOnceWithExactly(personTypeQualifier, limit, skip)).to.be.true;
expect(personGetPage.calledOnceWithExactly({ personType: personTypeQualifier, limit, skip })).to.be.true;
expect(byContactType.calledOnceWithExactly(personType)).to.be.true;
});

Expand Down
6 changes: 3 additions & 3 deletions shared-libs/cht-datasource/test/libs/data-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('context lib', () => {
}

expect(results).to.deep.equal(mockDocs);
expect(fetchFunctionStub.calledOnceWithExactly(...Object.values(args))).to.be.true;
expect(fetchFunctionStub.calledOnceWithExactly(args)).to.be.true;
});

it('should handle multiple pages', async () => {
Expand All @@ -159,8 +159,8 @@ describe('context lib', () => {

expect(results).to.deep.equal([...mockDocs1, ...mockDocs2]);
expect(fetchFunctionStub.callCount).to.equal(2);
expect(fetchFunctionStub.firstCall.args).to.deep.equal([2, 0]);
expect(fetchFunctionStub.secondCall.args).to.deep.equal([2, 0]);
expect(fetchFunctionStub.firstCall.args).to.deep.equal([{ limit: 2, skip: 0 }]);
expect(fetchFunctionStub.secondCall.args).to.deep.equal([{ limit: 2, skip: 0 }]);
});

it('should handle empty result', async () => {
Expand Down
13 changes: 8 additions & 5 deletions shared-libs/cht-datasource/test/person.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('person', () => {
isContactTypeQualifier.returns(true);
getPage.resolves(people);

const result = await Person.v1.getPage(dataContext)(personTypeQualifier, limit, skip);
const result = await Person.v1.getPage(dataContext)({ personType: personTypeQualifier, limit, skip });

expect(result).to.equal(people);
expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
Expand All @@ -169,7 +169,7 @@ describe('person', () => {
it('throws an error if the qualifier is invalid', async () => {
isContactTypeQualifier.returns(false);

await expect(Person.v1.getPage(dataContext)(invalidQualifier))
await expect(Person.v1.getPage(dataContext)({personType: invalidQualifier}))
.to.be.rejectedWith(`Invalid type [${JSON.stringify(invalidQualifier)}].`);

expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
Expand All @@ -182,7 +182,7 @@ describe('person', () => {
isContactTypeQualifier.returns(true);
getPage.resolves(people);

await expect(Person.v1.getPage(dataContext)(personTypeQualifier, invalidLimit, skip))
await expect(Person.v1.getPage(dataContext)({ personType: personTypeQualifier, limit: invalidLimit, skip }))
.to.be.rejectedWith(`limit must be a positive number`);

expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
Expand All @@ -195,8 +195,11 @@ describe('person', () => {
isContactTypeQualifier.returns(true);
getPage.resolves(people);

await expect(Person.v1.getPage(dataContext)(personTypeQualifier, limit, invalidSkip))
.to.be.rejectedWith(`skip must be a non-negative number`);
await expect(Person.v1.getPage(dataContext)({
personType: personTypeQualifier,
limit: limit,
skip: invalidSkip
})).to.be.rejectedWith(`skip must be a non-negative number`);

expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
expect(adapt.calledOnceWithExactly(dataContext, Local.Person.v1.getPage, Remote.Person.v1.getPage)).to.be.true;
Expand Down

0 comments on commit 8be7b70

Please sign in to comment.