Skip to content

Commit

Permalink
[optimize] simplify Async Generator of Repository model
Browse files Browse the repository at this point in the history
[optimize] update Upstream packages
  • Loading branch information
TechQuery committed Jun 19, 2024
1 parent 46dbd2c commit eec8b88
Show file tree
Hide file tree
Showing 5 changed files with 469 additions and 458 deletions.
13 changes: 5 additions & 8 deletions components/Git/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ export const GitListLayout: FC<{ defaultData: GitRepository[] }> = ({
defaultData,
}) => (
<Row as="ul" className="list-unstyled g-4" xs={1} md={2} xl={3}>
{defaultData.map(
item =>
!!(item.description && item.topics?.length) && (
<Col as="li" key={item.id}>
<GitCard className="h-100 shadow-sm" {...item} />
</Col>
),
)}
{defaultData.map(item => (
<Col as="li" key={item.id}>
<GitCard className="h-100 shadow-sm" {...item} />
</Col>
))}
</Row>
);
80 changes: 33 additions & 47 deletions models/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { observable } from 'mobx';
import {
githubClient,
GitRepository,
Organization,
RepositoryFilter,
RepositoryModel,
} from 'mobx-github';
import { parseCookie } from 'mobx-i18n';
import { Stream } from 'mobx-restful';
import { buildURLData, mergeStream } from 'web-utility';

import { API_Host, isServer } from './Base';

Expand All @@ -26,9 +25,9 @@ githubClient.use(({ request }, next) => {
return next();
});

type Repository = Omit<GitRepository, keyof RepositoryModel['relation']>;

export class GitRepositoryModel extends Stream<GitRepository>(RepositoryModel) {
export class GitRepositoryModel extends Stream<GitRepository, RepositoryFilter>(
RepositoryModel,
) {
client = githubClient;

organizations = ['idea2app', 'IdeaMall', 'EasyWebApp'];
Expand All @@ -42,50 +41,37 @@ export class GitRepositoryModel extends Stream<GitRepository>(RepositoryModel) {
));
}

async *getRepository(organization: string) {
const per_page = this.pageSize;

this.totalCount ||= 0;
this.totalCount += await this.getRepositoryCount(organization);

for (let page = 1, count = 0; count <= this.totalCount; page++) {
const { body: list } = await this.client.get<Repository[]>(
`orgs/${organization}/repos?${buildURLData({
type: 'public',
sort: 'pushed',
page,
per_page,
})}`,
);
if (!list?.length) break;

count += list!.length;

const pageData = await Promise.all(
list!.map(async ({ full_name, ...item }) => ({
...item,
full_name,
// @ts-ignore
...(await this.getOneRelation(full_name, ['languages'])),
})),
);
yield* pageData as GitRepository[];
}
}
async *openStream(filter: RepositoryFilter) {
const { loadPage } = RepositoryModel.prototype;
var count = 0;

async getRepositoryCount(organization: string) {
const { body } = await this.client.get<Organization>(
`orgs/${organization}`,
);
return body!.public_repos;
}
for (const name of this.organizations) {
this.baseURI = `orgs/${name}/repos`;

openStream() {
return mergeStream(
...this.organizations.map(organization =>
this.getRepository.bind(this, organization),
),
);
for (let i = 1; ; i++) {
const { pageData, totalCount } = await loadPage.call(
this,
i,
this.pageSize,
filter,
);
const list = pageData.filter(
({ description, topics, fork, archived }) =>
description?.trim() && topics?.[0] && !fork && !archived,
);
const droppedCount = pageData.length - list.length;

if (!pageData[0]) break;

if (i === 1) count += totalCount;
if (droppedCount) count -= droppedCount;

yield* list;

if (pageData.length < this.pageSize) break;
}
}
this.totalCount = count;
}
}

Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
"description": "React project scaffold based on TypeScript, Next.js, Bootstrap & Workbox.",
"private": true,
"dependencies": {
"@sentry/nextjs": "^8.7.0",
"@sentry/nextjs": "^8.9.2",
"classnames": "^2.5.1",
"file-type": "^19.0.0",
"idea-react": "^2.0.0-rc.2",
"koajax": "^1.1.2",
"less": "^4.2.0",
"less-loader": "^12.2.0",
"mobx": "^6.12.3",
"mobx-github": "^0.3.1",
"mobx": "^6.12.4",
"mobx-github": "^0.3.2",
"mobx-i18n": "^0.5.0",
"mobx-lark": "^2.0.0-rc.1",
"mobx-react": "^9.1.1",
"mobx-restful": "^0.7.0-rc.0",
"mobx-restful-table": "^2.0.0-rc.1",
"next": "^14.2.3",
"next": "^14.2.4",
"next-pwa": "~5.6.0",
"next-ssr-middleware": "^0.8.1",
"next-with-less": "^3.0.1",
Expand All @@ -27,21 +27,21 @@
"react-dom": "^18.3.1",
"react-marked-renderer": "^2.0.1",
"web-utility": "^4.4.0",
"webpack": "^5.91.0"
"webpack": "^5.92.0"
},
"devDependencies": {
"@babel/plugin-proposal-decorators": "^7.24.7",
"@babel/plugin-transform-typescript": "^7.24.7",
"@babel/preset-react": "^7.24.7",
"@types/node": "^18.19.34",
"@types/node": "^18.19.36",
"@types/react": "^18.3.3",
"eslint": "^8.57.0",
"eslint-config-next": "^14.2.3",
"eslint-config-next": "^14.2.4",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-simple-import-sort": "^12.1.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.5",
"prettier": "^3.3.1",
"lint-staged": "^15.2.7",
"prettier": "^3.3.2",
"typescript": "~5.4.5"
},
"prettier": {
Expand Down
2 changes: 1 addition & 1 deletion pages/open-source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const getServerSideProps = compose(
errorLogger,
translator(i18n),
async () => {
const list = await new GitRepositoryModel('idea2app').getList({}, 1);
const list = await new GitRepositoryModel('idea2app').getList();

return { props: { list } };
},
Expand Down
Loading

1 comment on commit eec8b88

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for idea2app ready!

✅ Preview
https://idea2app-gbjrcvkcc-techquerys-projects.vercel.app

Built with commit eec8b88.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.