-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: using genericity for pages aggregators (#8)
- Loading branch information
Showing
8 changed files
with
64 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
src/layer/github/implementation/constants/default-retry-count.constant.ts
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
src/layer/github/implementation/generic/get-all-pages.effect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Effect } from 'effect'; | ||
|
||
import { arrayRange } from '../../../../util/array-range.util'; | ||
import { defaultConcurrency } from '../constants/default-concurrency.constant'; | ||
|
||
type LinkKey = 'prev' | 'next' | 'last'; | ||
|
||
interface DataWithLinks<TData> { | ||
links: Record<LinkKey, number | undefined> | undefined; | ||
data: TData; | ||
} | ||
|
||
type GetPage<TArgs, TData, TError> = ( | ||
args: TArgs, | ||
) => (page: number) => Effect.Effect<DataWithLinks<TData>, TError>; | ||
|
||
export const getAllPages = < | ||
TError, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
TArgs extends { concurrency?: number } & Record<string, any>, | ||
TDataItem, | ||
TData extends TDataItem[], | ||
>( | ||
getPage: GetPage<TArgs, TData, TError>, | ||
args: TArgs, | ||
) => | ||
Effect.withSpan(__filename, { | ||
attributes: { ...args }, | ||
})( | ||
Effect.gen(function* (_) { | ||
const firstPage = yield* _(getPage(args)(1)); | ||
|
||
if (firstPage.links?.last === undefined) { | ||
return firstPage.data; | ||
} | ||
|
||
const pagesResults = yield* _( | ||
Effect.all(arrayRange(2, firstPage.links.last).map(getPage(args)), { | ||
concurrency: args.concurrency ?? defaultConcurrency, | ||
}), | ||
); | ||
|
||
return [ | ||
...firstPage.data, | ||
...pagesResults.flatMap((r) => r.data), | ||
] as TData; | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters