Skip to content

Commit c3bb418

Browse files
committed
perf(diagnostics): use incremental updates with debounce
1 parent c89008a commit c3bb418

4 files changed

Lines changed: 37 additions & 26 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
"module-replacements": "^2.11.0",
116116
"nano-staged": "^0.9.0",
117117
"ofetch": "^2.0.0-alpha.3",
118+
"perfect-debounce": "^2.1.0",
118119
"reactive-vscode": "^0.4.1",
119120
"tsdown": "^0.20.1",
120121
"typescript": "^5.9.3",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/providers/diagnostics/index.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Diagnostic, TextDocument } from 'vscode'
55
import { basename } from 'node:path'
66
import { config, logger } from '#state'
77
import { getPackageInfo } from '#utils/api/package'
8+
import { debounce } from 'perfect-debounce'
89
import { computed, useActiveTextEditor, useDocumentText, watch } from 'reactive-vscode'
910
import { languages } from 'vscode'
1011
import { displayName } from '../../generated-meta'
@@ -44,35 +45,35 @@ export function registerDiagnosticCollection(mapping: Record<string, Extractor |
4445
const dependencies = extractor.getDependenciesInfo(root)
4546
const diagnostics: Diagnostic[] = []
4647

47-
await Promise.all(
48-
dependencies.map(async (dep) => {
49-
try {
50-
const pkg = await getPackageInfo(dep.name)
51-
if (!pkg)
52-
return
48+
const flush = debounce(() => {
49+
diagnosticCollection.set(document.uri, [...diagnostics])
50+
}, 100)
5351

54-
for (const rule of enabledRules.value) {
55-
const diagnostic = await rule(dep, pkg)
52+
dependencies.forEach(async (dep) => {
53+
try {
54+
const pkg = await getPackageInfo(dep.name)
55+
if (!pkg)
56+
return
5657

57-
if (diagnostic) {
58-
diagnostics.push({
59-
source: displayName,
60-
message: diagnostic.message,
61-
severity: diagnostic.severity,
62-
code: diagnostic.code,
63-
range: extractor.getNodeRange(document, diagnostic.node),
64-
})
65-
}
66-
}
67-
} catch (err) {
68-
logger.warn(`Failed to check ${dep.name}: ${err}`)
69-
}
70-
}),
71-
)
58+
enabledRules.value.forEach(async (rule) => {
59+
const diagnostic = await rule(dep, pkg)
60+
61+
if (diagnostic) {
62+
diagnostics.push({
63+
source: displayName,
64+
message: diagnostic.message,
65+
severity: diagnostic.severity,
66+
code: diagnostic.code,
67+
range: extractor.getNodeRange(document, diagnostic.node),
68+
})
7269

73-
if (diagnostics.length > 0)
74-
logger.info(`${diagnostics.length} diagnostic collected in ${document.fileName}.`)
75-
diagnosticCollection.set(document.uri, diagnostics)
70+
flush()
71+
}
72+
})
73+
} catch (err) {
74+
logger.warn(`Failed to check ${dep.name}: ${err}`)
75+
}
76+
})
7677
}
7778

7879
watch(activeDocumentText, async () => {

tsdown.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default defineConfig({
1616
'yaml',
1717
'ofetch',
1818
'fast-npm-meta',
19+
'perfect-debounce',
1920
],
2021
minify: 'dce-only',
2122
})

0 commit comments

Comments
 (0)