mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Refactor lintInput to share code between sync/async, support an async path for strings, and process files first for better concurrency.
This commit is contained in:
parent
53e5e4272e
commit
e531bd6359
2 changed files with 101 additions and 119 deletions
|
|
@ -749,62 +749,32 @@ function lintInput(options, synchronous, callback) {
|
|||
const fs = options.fs || require("fs");
|
||||
const results = newResults(ruleList);
|
||||
let done = false;
|
||||
// Linting of strings is always synchronous
|
||||
let syncItem = null;
|
||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||
function syncCallback(err, result) {
|
||||
if (err) {
|
||||
done = true;
|
||||
return callback(err);
|
||||
}
|
||||
results[syncItem] = result;
|
||||
return null;
|
||||
}
|
||||
while (!done && (syncItem = stringsKeys.shift())) {
|
||||
lintContent(
|
||||
ruleList,
|
||||
syncItem,
|
||||
strings[syncItem] || "",
|
||||
md,
|
||||
config,
|
||||
frontMatter,
|
||||
handleRuleFailures,
|
||||
noInlineConfig,
|
||||
resultVersion,
|
||||
syncCallback
|
||||
);
|
||||
}
|
||||
if (synchronous) {
|
||||
// Lint files synchronously
|
||||
while (!done && (syncItem = files.shift())) {
|
||||
lintFile(
|
||||
ruleList,
|
||||
syncItem,
|
||||
md,
|
||||
config,
|
||||
frontMatter,
|
||||
handleRuleFailures,
|
||||
noInlineConfig,
|
||||
resultVersion,
|
||||
fs,
|
||||
synchronous,
|
||||
syncCallback
|
||||
);
|
||||
}
|
||||
return done || callback(null, results);
|
||||
}
|
||||
// Lint files asynchronously
|
||||
let concurrency = 0;
|
||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||
function lintConcurrently() {
|
||||
const asyncItem = files.shift();
|
||||
function lintWorker() {
|
||||
let currentItem = null;
|
||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||
function lintWorkerCallback(err, result) {
|
||||
concurrency--;
|
||||
if (err) {
|
||||
done = true;
|
||||
return callback(err);
|
||||
}
|
||||
results[currentItem] = result;
|
||||
if (!synchronous) {
|
||||
lintWorker();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (done) {
|
||||
// Nothing to do
|
||||
} else if (asyncItem) {
|
||||
// Abort for error or nothing left to do
|
||||
} else if (files.length > 0) {
|
||||
// Lint next file
|
||||
concurrency++;
|
||||
currentItem = files.shift();
|
||||
lintFile(
|
||||
ruleList,
|
||||
asyncItem,
|
||||
currentItem,
|
||||
md,
|
||||
config,
|
||||
frontMatter,
|
||||
|
|
@ -813,34 +783,48 @@ function lintInput(options, synchronous, callback) {
|
|||
resultVersion,
|
||||
fs,
|
||||
synchronous,
|
||||
(err, result) => {
|
||||
concurrency--;
|
||||
if (err) {
|
||||
done = true;
|
||||
return callback(err);
|
||||
}
|
||||
results[asyncItem] = result;
|
||||
lintConcurrently();
|
||||
return null;
|
||||
}
|
||||
lintWorkerCallback
|
||||
);
|
||||
} else if (stringsKeys.length > 0) {
|
||||
// Lint next string
|
||||
concurrency++;
|
||||
currentItem = stringsKeys.shift();
|
||||
lintContent(
|
||||
ruleList,
|
||||
currentItem,
|
||||
strings[currentItem] || "",
|
||||
md,
|
||||
config,
|
||||
frontMatter,
|
||||
handleRuleFailures,
|
||||
noInlineConfig,
|
||||
resultVersion,
|
||||
lintWorkerCallback
|
||||
);
|
||||
} else if (concurrency === 0) {
|
||||
// Finish
|
||||
done = true;
|
||||
return callback(null, results);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// Testing on a Raspberry Pi 4 Model B with an artificial 5ms file access
|
||||
// delay suggests that a concurrency factor of 8 can eliminate the impact
|
||||
// of that delay (i.e., total time is the same as with no delay).
|
||||
lintConcurrently();
|
||||
lintConcurrently();
|
||||
lintConcurrently();
|
||||
lintConcurrently();
|
||||
lintConcurrently();
|
||||
lintConcurrently();
|
||||
lintConcurrently();
|
||||
lintConcurrently();
|
||||
if (synchronous) {
|
||||
while (!done) {
|
||||
lintWorker();
|
||||
}
|
||||
} else {
|
||||
// Testing on a Raspberry Pi 4 Model B with an artificial 5ms file access
|
||||
// delay suggests that a concurrency factor of 8 can eliminate the impact
|
||||
// of that delay (i.e., total time is the same as with no delay).
|
||||
lintWorker();
|
||||
lintWorker();
|
||||
lintWorker();
|
||||
lintWorker();
|
||||
lintWorker();
|
||||
lintWorker();
|
||||
lintWorker();
|
||||
lintWorker();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue