Skip to content

Commit

Permalink
feat: inject whole result array instead of just lastItem for fetchApi…
Browse files Browse the repository at this point in the history
…AllPages' UntilFn
  • Loading branch information
vfurmane committed May 24, 2024
1 parent 5de594f commit fa4c06f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { FetchUntilFunctionItems } from '../../../ft-api/ft-api.service.js';
import { lastItemIsAlreadyCached } from './last-item-is-already-cached.js';

export function prefetchAll42EventsUntil(latestEvent: FindEventsResponseDto[number]) {
return ({ lastItem }: FetchUntilFunctionItems<FindEventsResponseDto[number]>) =>
lastItemIsAlreadyCached(latestEvent, lastItem);
return ({ result }: FetchUntilFunctionItems<FindEventsResponseDto[number]>) => {
if (result.length === 0) {
return true;
}
const lastItem = result[result.length - 1];
return lastItemIsAlreadyCached(latestEvent, lastItem);
};
}
4 changes: 2 additions & 2 deletions src/ft-api/ft-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { getDataFromResponseOrThrow } from '../common/utils/get-data-from-respon
export type RequestWithSchemas<T> = RequestInit & { schema: FetchSchemas<Array<T>> };
export type FetchUntilFunctionItems<T> = {
iteration: number;
lastItem?: T;
result: Array<T>;
};
export type FetchUntilFunction<T> = (items: FetchUntilFunctionItems<T>) => boolean;

Expand Down Expand Up @@ -121,7 +121,7 @@ export class FtApiService {
} while (
route !== null &&
iteration < this.apiPaginationMaxDepth &&
(untilFn === undefined || !untilFn({ iteration, lastItem: result[result.length - 1] }))
(untilFn === undefined || !untilFn({ iteration, result }))
);
return result;
}
Expand Down

0 comments on commit fa4c06f

Please sign in to comment.