diff --git a/packages/vscode/src/projects.ts b/packages/vscode/src/projects.ts index 0e6c8b2dc2..65c3465e2c 100644 --- a/packages/vscode/src/projects.ts +++ b/packages/vscode/src/projects.ts @@ -1,4 +1,5 @@ import type { LoadedSlidevData } from '@slidev/parser/fs' +import type { Uri } from 'vscode' import { existsSync } from 'node:fs' import { basename, dirname } from 'node:path' import { slash } from '@antfu/utils' @@ -74,6 +75,27 @@ export function useProjects() { const fsWatcher = workspace.createFileSystemWatcher('**/*.md') onScopeDispose(() => fsWatcher.dispose()) + let throttleTimeout: NodeJS.Timeout | null = null + let scheduledRescan = false + async function onFsChange(uri: Uri) { + if (uri.toString().includes('node_modules')) + return + if (throttleTimeout) { + scheduledRescan = true + } + else { + throttleTimeout = setTimeout(async () => { + if (scheduledRescan) { + scheduledRescan = false + await rescanProjects() + } + throttleTimeout = null + }, 500) + scheduledRescan = false + await rescanProjects() + } + } + fsWatcher.onDidChange(async (uri) => { const path = slash(uri.fsPath) logger.info(`File ${path} changed.`) @@ -101,8 +123,8 @@ export function useProjects() { effects.map(effect => effect()) logger.info(`All affected Slidev projects updated in ${Date.now() - startMs}ms.`) }) - fsWatcher.onDidCreate(rescanProjects) - fsWatcher.onDidDelete(rescanProjects) + fsWatcher.onDidCreate(onFsChange) + fsWatcher.onDidDelete(onFsChange) watch(include, rescanProjects) }