Skip to content

Commit da317fd

Browse files
Add concurrency limiting utility function to files module
Co-authored-by: shannon.anahata <[email protected]>
1 parent 22b8b41 commit da317fd

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/files.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {readdir} from 'fs/promises';
22
import path from 'path';
3+
import pLimit from 'p-limit';
34

45
/**
56
* @returns Array of file paths
@@ -10,4 +11,18 @@ const getAllFilesRecursively = async (folder: string): Promise<string[]> => {
1011
.map(dirent => path.join(dirent.parentPath || dirent.path, dirent.name));
1112
};
1213

14+
/**
15+
* Utility function to limit concurrency of async operations
16+
* @param fn - The async function to limit
17+
* @param options - Options including concurrency limit
18+
* @returns A limited version of the function
19+
*/
20+
export function limitFunction<T extends (...args: any[]) => Promise<any>>(
21+
fn: T,
22+
options: {concurrency: number}
23+
): T {
24+
const limit = pLimit(options.concurrency);
25+
return ((...args: Parameters<T>) => limit(() => fn(...args))) as T;
26+
}
27+
1328
export default getAllFilesRecursively;

src/mdx.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
createBrotliCompress,
1818
createBrotliDecompress,
1919
} from 'node:zlib';
20-
import {limitFunction} from 'p-limit';
2120
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
2221
import rehypePresetMinify from 'rehype-preset-minify';
2322
import rehypePrismDiff from 'rehype-prism-diff';
@@ -28,7 +27,7 @@ import remarkMdxImages from 'remark-mdx-images';
2827
import getAppRegistry from './build/appRegistry';
2928
import getPackageRegistry from './build/packageRegistry';
3029
import {apiCategories} from './build/resolveOpenAPI';
31-
import getAllFilesRecursively from './files';
30+
import getAllFilesRecursively, {limitFunction} from './files';
3231
import remarkDefList from './mdx-deflist';
3332
import rehypeOnboardingLines from './rehype-onboarding-lines';
3433
import rehypeSlug from './rehype-slug.js';

0 commit comments

Comments
 (0)