|
1 | 1 | import type { LoadedSlidevData } from '@slidev/parser/fs' |
| 2 | +import type { Uri } from 'vscode' |
2 | 3 | import { existsSync } from 'node:fs' |
3 | 4 | import { basename, dirname } from 'node:path' |
4 | 5 | import { slash } from '@antfu/utils' |
@@ -74,6 +75,27 @@ export function useProjects() { |
74 | 75 | const fsWatcher = workspace.createFileSystemWatcher('**/*.md') |
75 | 76 | onScopeDispose(() => fsWatcher.dispose()) |
76 | 77 |
|
| 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 | + |
77 | 99 | fsWatcher.onDidChange(async (uri) => { |
78 | 100 | const path = slash(uri.fsPath) |
79 | 101 | logger.info(`File ${path} changed.`) |
@@ -101,8 +123,8 @@ export function useProjects() { |
101 | 123 | effects.map(effect => effect()) |
102 | 124 | logger.info(`All affected Slidev projects updated in ${Date.now() - startMs}ms.`) |
103 | 125 | }) |
104 | | - fsWatcher.onDidCreate(rescanProjects) |
105 | | - fsWatcher.onDidDelete(rescanProjects) |
| 126 | + fsWatcher.onDidCreate(onFsChange) |
| 127 | + fsWatcher.onDidDelete(onFsChange) |
106 | 128 |
|
107 | 129 | watch(include, rescanProjects) |
108 | 130 | } |
|
0 commit comments