Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 836f6f1

Browse files
optionally batch calls to getImportedFiles (#1)
1 parent a3625aa commit 836f6f1

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`dependencyTree batch size rejects a batch size less than 1 1`] = `"expected a batch size greater than 0, got '-1'"`;

__tests__/index.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,30 @@ describe('dependencyTree', () => {
473473
});
474474
});
475475
});
476+
477+
describe('batch size', () => {
478+
const dir = 'directive';
479+
beforeEach(() => {
480+
dependencyTree = new DependencyTree([fixture(dir)]);
481+
});
482+
483+
it('works with a batch size of 1', async () => {
484+
expect.hasAssertions();
485+
const result = await dependencyTree.gather({ batchSize: 1 });
486+
expect(result).toBeTruthy();
487+
});
488+
489+
it('works with a batch size of 100', async () => {
490+
expect.hasAssertions();
491+
const result = await dependencyTree.gather({ batchSize: 100 });
492+
expect(result).toBeTruthy();
493+
});
494+
495+
it('rejects a batch size less than 1', async () => {
496+
expect.hasAssertions();
497+
await expect(() =>
498+
dependencyTree.gather({ batchSize: -1 }),
499+
).rejects.toThrowErrorMatchingSnapshot();
500+
});
501+
});
476502
});

src/index.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,30 @@ export class DependencyTree {
155155
*
156156
* Built-in (Node) and package dependencies are ignored.
157157
*/
158-
public async gather(): Promise<{
158+
public async gather({
159+
batchSize = 10,
160+
}: { batchSize?: number } = {}): Promise<{
159161
missing: FileToDeps;
160162
resolved: FileToDeps;
161163
}> {
164+
if (batchSize < 1) {
165+
throw new Error(
166+
`expected a batch size greater than 0, got '${batchSize}'`,
167+
);
168+
}
169+
162170
const fileToDeps: FileToDeps = new Map();
163171
const missing: FileToDeps = new Map();
164172
const files = this.getFiles();
165173
info(`Found a total of ${files.length} source files`);
166174

167-
for (const file of files) {
175+
const getDepsForFilesTasks = files.map((file: string) => async () => {
168176
info('Scanning %s', file);
169177
const importedFiles = await this.getImportedFiles(file, missing, files);
170178
fileToDeps.set(file, importedFiles);
171-
}
179+
});
180+
181+
await runInBatches(getDepsForFilesTasks, batchSize);
172182

173183
return {
174184
missing,
@@ -294,6 +304,16 @@ export class DependencyTree {
294304
}
295305
}
296306

307+
async function runInBatches(
308+
tasks: (() => Promise<void>)[],
309+
batchSize: number,
310+
): Promise<void> {
311+
for (let i = 0; i < tasks.length; i += batchSize) {
312+
const batch = tasks.slice(i, i + batchSize);
313+
await Promise.all(batch.map((task) => task()));
314+
}
315+
}
316+
297317
/**
298318
* Directed graph. Internally represented using the edges of the graph. For example, the following
299319
* directed graph:

0 commit comments

Comments
 (0)