Skip to content

Commit 69c6d8c

Browse files
committed
fix(watchman): Parallelize Watchman calls in crawler again
This is a follow up to #5615 where it made all async watchman commands serialized when converting them from promises to `async` functions. Existing Watchman crawler tests should pass, maybe slightly faster.
1 parent 497be76 commit 69c6d8c

1 file changed

Lines changed: 39 additions & 35 deletions

File tree

packages/jest-haste-map/src/crawlers/watchman.js

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,48 +51,52 @@ module.exports = async function watchmanCrawl(
5151

5252
try {
5353
const watchmanRoots = new Map();
54-
for (const root of roots) {
55-
const response = await cmd('watch-project', root);
56-
const existing = watchmanRoots.get(response.watch);
57-
// A root can only be filtered if it was never seen with a relative_path before
58-
const canBeFiltered = !existing || existing.length > 0;
54+
await Promise.all(
55+
roots.map(async root => {
56+
const response = await cmd('watch-project', root);
57+
const existing = watchmanRoots.get(response.watch);
58+
// A root can only be filtered if it was never seen with a relative_path before
59+
const canBeFiltered = !existing || existing.length > 0;
5960

60-
if (canBeFiltered) {
61-
if (response.relative_path) {
62-
watchmanRoots.set(
63-
response.watch,
64-
(existing || []).concat(response.relative_path),
65-
);
66-
} else {
67-
// Make the filter directories an empty array to signal that this root
68-
// was already seen and needs to be watched for all files/directories
69-
watchmanRoots.set(response.watch, []);
61+
if (canBeFiltered) {
62+
if (response.relative_path) {
63+
watchmanRoots.set(
64+
response.watch,
65+
(existing || []).concat(response.relative_path),
66+
);
67+
} else {
68+
// Make the filter directories an empty array to signal that this root
69+
// was already seen and needs to be watched for all files/directories
70+
watchmanRoots.set(response.watch, []);
71+
}
7072
}
71-
}
72-
}
73+
}),
74+
);
7375

7476
let shouldReset = false;
7577
const watchmanFileResults = new Map();
76-
for (const [root, directoryFilters] of watchmanRoots) {
77-
const expression = Array.from(defaultWatchExpression);
78-
if (directoryFilters.length > 0) {
79-
expression.push([
80-
'anyof',
81-
...directoryFilters.map(dir => ['dirname', dir]),
82-
]);
83-
}
84-
const fields = ['name', 'exists', 'mtime_ms'];
78+
await Promise.all(
79+
Array.from(watchmanRoots).map(async ([root, directoryFilters]) => {
80+
const expression = Array.from(defaultWatchExpression);
81+
if (directoryFilters.length > 0) {
82+
expression.push([
83+
'anyof',
84+
...directoryFilters.map(dir => ['dirname', dir]),
85+
]);
86+
}
87+
const fields = ['name', 'exists', 'mtime_ms'];
8588

86-
const query = clocks[root]
87-
? // Use the `since` generator if we have a clock available
88-
{expression, fields, since: clocks[root]}
89-
: // Otherwise use the `suffix` generator
90-
{expression, fields, suffix: extensions};
89+
const query = clocks[root]
90+
? // Use the `since` generator if we have a clock available
91+
{expression, fields, since: clocks[root]}
92+
: // Otherwise use the `suffix` generator
93+
{expression, fields, suffix: extensions};
9194

92-
const response = await cmd('query', root, query);
93-
shouldReset = shouldReset || response.is_fresh_instance;
94-
watchmanFileResults.set(root, response);
95-
}
95+
const response = await cmd('query', root, query);
96+
shouldReset = shouldReset || response.is_fresh_instance;
97+
watchmanFileResults.set(root, response);
98+
}),
99+
);
96100

97101
// Reset the file map if watchman was restarted and sends us a list of files.
98102
if (shouldReset) {

0 commit comments

Comments
 (0)