Skip to content

Commit

Permalink
chore: using genericity for pages aggregators (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpb06 authored Mar 18, 2024
1 parent b5f72ef commit 7771d35
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Effect } from 'effect';

import { EffectResultSuccess } from '../../../../types/effect.types';
import { arrayRange } from '../../../../util/array-range.util';
import { defaultConcurrency } from '../constants/default-concurrency.constant';
import { getAllPages } from '../generic/get-all-pages.effect';
import { getPullRequestReviewsPage } from '../paging/get-pull-request-reviews-page';

export interface GetPullRequestReviewsArgs {
Expand All @@ -21,22 +20,7 @@ const getPage = (args: GetPullRequestReviewsArgs) => (page: number) =>
export const getPullRequestReviews = (args: GetPullRequestReviewsArgs) =>
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)];
}),
);
})(getAllPages(getPage, args));

export type PullRequestReviewsResult = EffectResultSuccess<
typeof getPullRequestReviews
Expand Down
22 changes: 4 additions & 18 deletions src/layer/github/implementation/aggregators/get-repo-issues.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Effect } from 'effect';

import { EffectResultSuccess } from '../../../../types/effect.types';
import { arrayRange } from '../../../../util/array-range.util';
import { defaultConcurrency } from '../constants/default-concurrency.constant';
import { getAllPages } from '../generic/get-all-pages.effect';
import { getRepoIssuesPage } from '../paging/get-repo-issues-page';

export interface GetRepoIssuesArgs {
Expand All @@ -18,21 +17,8 @@ const getPage = (args: GetRepoIssuesArgs) => (page: number) =>
});

export const getRepoIssues = (args: GetRepoIssuesArgs) =>
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)];
}),
);
Effect.withSpan(__filename, {
attributes: { ...args },
})(getAllPages(getPage, args));

export type RepoIssuesResult = EffectResultSuccess<typeof getRepoIssues>;
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Effect } from 'effect';

import { EffectResultSuccess } from '../../../../types/effect.types';
import { arrayRange } from '../../../../util/array-range.util';
import { defaultConcurrency } from '../constants/default-concurrency.constant';
import { getAllPages } from '../generic/get-all-pages.effect';
import { getRepoPullRequestsPage } from '../paging/get-repo-pull-requests-page';

export interface GetRepoPullRequestsArgs {
Expand All @@ -18,22 +17,9 @@ const getPage = (args: GetRepoPullRequestsArgs) => (page: number) =>
});

export const getRepoPullRequests = (args: GetRepoPullRequestsArgs) =>
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)];
}),
);
Effect.withSpan(__filename, {
attributes: { ...args },
})(getAllPages(getPage, args));

export type RepoPullRequestsResult = EffectResultSuccess<
typeof getRepoPullRequests
Expand Down
22 changes: 4 additions & 18 deletions src/layer/github/implementation/aggregators/get-repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { Effect } from 'effect';
import { match } from 'ts-pattern';

import { EffectResultSuccess } from '../../../../types/effect.types';
import { arrayRange } from '../../../../util/array-range.util';
import { defaultConcurrency } from '../constants/default-concurrency.constant';
import { getAllPages } from '../generic/get-all-pages.effect';
import { getOrgReposPage } from '../paging/get-org-repos-page';
import { getUserReposPage } from '../paging/get-user-repos-page';

Expand Down Expand Up @@ -32,21 +31,8 @@ const getPage =
.exhaustive();

export const getRepositories = (args: GetRepositoriesArgs) =>
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)];
}),
);
Effect.withSpan(__filename, {
attributes: { ...args },
})(getAllPages(getPage, args));

export type RepositoriesResult = EffectResultSuccess<typeof getRepositories>;
20 changes: 2 additions & 18 deletions src/layer/github/implementation/aggregators/get-user-events.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Effect } from 'effect';

import { EffectResultSuccess } from '../../../..';
import { arrayRange } from '../../../../util/array-range.util';
import { defaultConcurrency } from '../constants/default-concurrency.constant';
import { getAllPages } from '../generic/get-all-pages.effect';
import { getUserEventsPage } from '../paging/get-user-events-page';

export interface GetUserEventsArgs {
Expand All @@ -19,21 +18,6 @@ const getPage = (args: GetUserEventsArgs) => (page: number) =>
export const getUserEvents = (args: GetUserEventsArgs) =>
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)];
}),
);
})(getAllPages(getPage, args));

export type UserEventsResult = EffectResultSuccess<typeof getUserEvents>;

This file was deleted.

48 changes: 48 additions & 0 deletions src/layer/github/implementation/generic/get-all-pages.effect.ts
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;
}),
);
2 changes: 0 additions & 2 deletions src/layer/octokit.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,3 @@ export const OctokitLayer = {
}),
}),
};

OctokitLayer.repo({ owner: 'jpb06', name: 'effect' }).pull(1).reviews();

0 comments on commit 7771d35

Please sign in to comment.