Skip to content

Commit

Permalink
Using promiseRaceTo in newComponentCategory in author checklist
Browse files Browse the repository at this point in the history
  • Loading branch information
sebryu committed Nov 3, 2023
1 parent d29b1d7 commit 86074ed
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import github from '@actions/github';
import Category from './Category';
import CONST from '../../../../libs/CONST';
import GithubUtils from '../../../../libs/GithubUtils';
import promiseRaceTo from '../../../../libs/promiseRaceTo';

type SuperClassType = {superClass: {name?: string; object: {name: string}; property: {name: string}} | null; name: string};

Expand Down Expand Up @@ -80,22 +81,17 @@ async function detectReactComponentInFile(filename: string): Promise<boolean | u
}
}

function filterFiles({filename, status}: {filename: string; status: string}): boolean {
if (status !== 'added') {
return false;
}
return filename.endsWith('.js') || filename.endsWith('.jsx') || filename.endsWith('.ts') || filename.endsWith('.tsx');
}

async function detect(changedFiles: Array<{filename: string; status: string}>): Promise<boolean> {
const filteredFiles = changedFiles.filter(filterFiles);
for (const file of filteredFiles) {
const result = await detectReactComponentInFile(file.filename);
if (result) {
return true; // If the check is true, exit directly
}
const filteredFiles = changedFiles.filter(({filename, status}) => status === 'added' && (filename.endsWith('.js') || filename.endsWith('.ts') || filename.endsWith('.tsx')));
try {
await promiseRaceTo(
filteredFiles.map(({filename}) => detectReactComponentInFile(filename)),
true,
);
return true;
} catch (err) {
return false;
}
return false;
}

const newComponentCategory: Category = {
Expand Down
51 changes: 38 additions & 13 deletions .github/actions/javascript/authorChecklist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21489,6 +21489,7 @@ const traverse_1 = __nccwpck_require__(1380);
const github_1 = __nccwpck_require__(5438);
const CONST_1 = __nccwpck_require__(4097);
const GithubUtils_1 = __nccwpck_require__(7999);
const promiseRaceTo_1 = __nccwpck_require__(8235);
const items = [
"I verified that similar component doesn't exist in the codebase",
'I verified that all props are defined accurately and each prop has a `/** comment above it */`',
Expand Down Expand Up @@ -21557,21 +21558,15 @@ async function detectReactComponentInFile(filename) {
console.error('An unknown error occurred with the GitHub API: ', error, params);
}
}
function filterFiles({ filename, status }) {
if (status !== 'added') {
return false;
}
return filename.endsWith('.js') || filename.endsWith('.jsx') || filename.endsWith('.ts') || filename.endsWith('.tsx');
}
async function detect(changedFiles) {
const filteredFiles = changedFiles.filter(filterFiles);
for (const file of filteredFiles) {
const result = await detectReactComponentInFile(file.filename);
if (result) {
return true; // If the check is true, exit directly
}
const filteredFiles = changedFiles.filter(({ filename, status }) => status === 'added' && (filename.endsWith('.js') || filename.endsWith('.ts') || filename.endsWith('.tsx')));
try {
await (0, promiseRaceTo_1.default)(filteredFiles.map(({ filename }) => detectReactComponentInFile(filename)), true);
return true;
}
catch (err) {
return false;
}
return false;
}
const newComponentCategory = {
detect,
Expand All @@ -21580,6 +21575,36 @@ const newComponentCategory = {
exports["default"] = newComponentCategory;


/***/ }),

/***/ 8235:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
/**
* Like Promise.race, except it resolves with the first promise that resolves to the desired value.
* If no promise resolves to the desired value, it rejects.
*/
function promiseRaceTo(promises, desiredValue) {
return new Promise((resolve, reject) => {
for (const p of promises) {
Promise.resolve(p)
.then((res) => {
if (res !== desiredValue) {
return;
}
resolve(res);
})
.catch(() => { });
}
Promise.allSettled(promises).then(() => reject());
});
}
exports["default"] = promiseRaceTo;


/***/ }),

/***/ 9491:
Expand Down

0 comments on commit 86074ed

Please sign in to comment.