Skip to content

Commit

Permalink
Use p-map rather than custom concurrentForEach
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
pipex committed Jan 10, 2025
1 parent 7d64ad5 commit 77ef8e0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 52 deletions.
73 changes: 24 additions & 49 deletions lib/universe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,10 @@ import type { Stats } from 'fs';
import Contract from './contract';
import { UNIVERSE } from './types/types';

/**
* @summary Run the callback function concurrently on elements from the given iterator
* @function
* @memberof module:universe
*
* Gets up to `concurrency` elements from the given iterator and apply the asynchronous function
* concurrently using `Promise.all`.
*
* If at any point the call to the callback fails, the function will throw the error
*
* @param it - iterator of elements to go traverse
* @param callbackFn - a function to execute for each element in the iterator
* @param concurrency - number of elements to apply the function at the same time. Default to 1
*/
export async function concurrentForEach<T>(
it: IterableIterator<T>,
callbackFn: (t: T) => PromiseLike<void>,
concurrency = 1,
): Promise<void> {
const run = async () => {
const next = it.next();
if (next.value && !next.done) {
await callbackFn(next.value);
await run();
}
};
const runs = [];
for (let i = 0; i < concurrency; i++) {
runs.push(run());
}
await Promise.all(runs);
}
const pMapLoader = (async () => {
const module = await import('p-map');
return module.default;
})();

/**
* @summary recursively find all files under the directory that match the given filter
Expand Down Expand Up @@ -103,24 +75,27 @@ export class Universe extends Contract {
path.extname(filePath) === '.json' && filter(filePath, stat),
);

const universe = new Universe();
const children: Contract[] = [];
await concurrentForEach(
allFiles.values(),
async (file) => {
const contents = await fs.readFile(file, { encoding: 'utf8' });
let source = JSON.parse(contents);
// Get the result from the p-map initialization
// promise
const pMap = await pMapLoader;

if (canonicalOnly) {
// Ignore aliases
const { aliases, ...obj } = source;
source = obj;
}

children.push(...Contract.build(source));
},
10,
);
const universe = new Universe();
const children = (
await pMap(
allFiles,
async (file) => {
const contents = await fs.readFile(file, { encoding: 'utf8' });
let source = JSON.parse(contents);
if (canonicalOnly) {
// Ignore aliases
const { aliases, ...obj } = source;
source = obj;
}
return Contract.build(source);
},
{ concurrency: 10 },
)
).flat();

universe.addChildren(children);

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"json-schema": "^0.4.0",
"lodash": "^4.17.19",
"object-hash": "^1.3.1",
"p-map": "^7.0.3",
"promised-handlebars": "^2.0.1",
"semver": "^5.7.1"
},
Expand Down
9 changes: 6 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"module": "Node16",
"moduleResolution": "node16",
"outDir": "build",
"noUnusedParameters": true,
"noUnusedLocals": true,
Expand All @@ -13,5 +13,8 @@
"skipLibCheck": true,
"resolveJsonModule": true
},
"include": ["lib/**/*.ts", "typings/**/*.ts"]
"include": [
"lib/**/*.ts",
"typings/**/*.ts"
]
}

0 comments on commit 77ef8e0

Please sign in to comment.