Skip to content

Commit ef67955

Browse files
authored
fix(vscode): should throttle project rescan (#2359)
1 parent 917bca2 commit ef67955

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

packages/vscode/src/projects.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { LoadedSlidevData } from '@slidev/parser/fs'
2+
import type { Uri } from 'vscode'
23
import { existsSync } from 'node:fs'
34
import { basename, dirname } from 'node:path'
45
import { slash } from '@antfu/utils'
@@ -74,6 +75,27 @@ export function useProjects() {
7475
const fsWatcher = workspace.createFileSystemWatcher('**/*.md')
7576
onScopeDispose(() => fsWatcher.dispose())
7677

78+
let throttleTimeout: NodeJS.Timeout | null = null
79+
let scheduledRescan = false
80+
async function onFsChange(uri: Uri) {
81+
if (uri.toString().includes('node_modules'))
82+
return
83+
if (throttleTimeout) {
84+
scheduledRescan = true
85+
}
86+
else {
87+
throttleTimeout = setTimeout(async () => {
88+
if (scheduledRescan) {
89+
scheduledRescan = false
90+
await rescanProjects()
91+
}
92+
throttleTimeout = null
93+
}, 500)
94+
scheduledRescan = false
95+
await rescanProjects()
96+
}
97+
}
98+
7799
fsWatcher.onDidChange(async (uri) => {
78100
const path = slash(uri.fsPath)
79101
logger.info(`File ${path} changed.`)
@@ -101,8 +123,8 @@ export function useProjects() {
101123
effects.map(effect => effect())
102124
logger.info(`All affected Slidev projects updated in ${Date.now() - startMs}ms.`)
103125
})
104-
fsWatcher.onDidCreate(rescanProjects)
105-
fsWatcher.onDidDelete(rescanProjects)
126+
fsWatcher.onDidCreate(onFsChange)
127+
fsWatcher.onDidDelete(onFsChange)
106128

107129
watch(include, rescanProjects)
108130
}

0 commit comments

Comments
 (0)