Skip to content

Commit 34dbfbd

Browse files
committed
实现增量索引功能,新增needReindexAtom以管理需要重新索引的文件列表。更新ScanControls组件以处理文件变化并添加到重新索引列表,优化增量扫描逻辑。更新国际化文本以反映新的索引功能,确保多语言支持的准确性和一致性。
1 parent 0ea714d commit 34dbfbd

6 files changed

Lines changed: 628 additions & 38 deletions

File tree

src/components/ScanControls.tsx

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
previousScanAtom,
99
changeReportAtom,
1010
scanStatusAtom,
11+
needReindexAtom,
1112
errorMessageAtom,
1213
isMonitoringAtom,
1314
monitorIntervalAtom,
@@ -43,6 +44,7 @@ export default function ScanControls() {
4344
const [showAllFiles, setShowAllFiles] = useAtom(showAllFilesAtom);
4445
const [readmeContent, setReadmeContent] = useAtom(readmeContentAtom);
4546
const [_, setDockerfiles] = useAtom(dockerfilesAtom);
47+
const [needReindex, setNeedReindex] = useAtom(needReindexAtom);
4648

4749
const [isDownloading, setIsDownloading] = useState(false);
4850
const [isUsingObserver, setIsUsingObserver] = useState(false);
@@ -135,6 +137,12 @@ export default function ScanControls() {
135137
setScanProgress(0); // 重置进度为0
136138
setScanCompleted(false); // 重置完成状态
137139

140+
// 检查是否存在需要重新索引的文件
141+
console.log(
142+
`扫描前检查:needReindex包含 ${needReindex.length} 个文件`,
143+
needReindex
144+
);
145+
138146
// 尝试读取README.md文件
139147
try {
140148
const readmeHandle = await directoryHandle.getFileHandle("README.md", {
@@ -243,16 +251,33 @@ export default function ScanControls() {
243251
// 文件变化处理函数
244252
const handleFileChange = async (isObserverChange: boolean) => {
245253
// 如果正在扫描中,跳过
246-
if (scanStatus === "scanning") return;
254+
if (scanStatus === "scanning") {
255+
console.log("正在扫描中,忽略文件变化通知");
256+
return;
257+
}
247258

248259
console.log(
249260
`${t("scanControls.changeDetected")},由${
250261
isObserverChange ? "FileSystemObserver" : "轮询"
251262
}触发`
252263
);
253264

265+
// 检查是否有需要重新索引的文件
266+
const currentNeedReindex = needReindex.length > 0;
267+
if (currentNeedReindex) {
268+
console.log(
269+
`有 ${needReindex.length} 个文件需要重新索引,将执行增量扫描`
270+
);
271+
} else {
272+
// 如果没有检测到特定文件变更,则添加一个标记以确保执行增量扫描
273+
console.log("没有检测到具体文件变更,添加通配符到重新索引列表");
274+
addToReindexList("__CHANGED__:observer_triggered");
275+
}
276+
254277
// 执行扫描并更新UI
278+
console.log("开始执行文件扫描...");
255279
await handleScan();
280+
console.log("文件扫描完成");
256281

257282
// 强制重新初始化文件观察器,确保新文件夹被监控
258283
if (isMonitoring && directoryHandle && isObserverChange) {
@@ -295,6 +320,8 @@ export default function ScanControls() {
295320
setCurrentScan(null);
296321
setPreviousScan(null);
297322
setChangeReport(null);
323+
// 清空需要重新索引的文件列表
324+
setNeedReindex([]);
298325
// 执行扫描
299326
setTimeout(() => {
300327
handleScan();
@@ -371,6 +398,18 @@ export default function ScanControls() {
371398
setShowVectorizeModal(false);
372399
};
373400

401+
// 当有文件发生变化时,将其添加到需要重新索引的列表中
402+
const addToReindexList = (filePath: string) => {
403+
console.log(`将文件添加到重新索引列表: ${filePath}`);
404+
// 检查文件是否已在列表中
405+
setNeedReindex((prev) => {
406+
if (prev.includes(filePath)) {
407+
return prev;
408+
}
409+
return [...prev, filePath];
410+
});
411+
};
412+
374413
// 如果没有目录句柄,不显示控制器
375414
if (!directoryHandle) return null;
376415

src/lib/fileObserver.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,7 @@ export async function observeDirectoryChanges(
199199
observedPaths.add(rootId);
200200
console.log(`已开始观察根目录: ${dirHandle.name}`);
201201

202-
// 获取并观察所有子目录(使用优化后的函数)
203-
console.log("正在扫描所有子目录...");
204-
const startTime = performance.now();
205202
const subDirectories = await getAllSubdirectories(dirHandle);
206-
const scanTime = performance.now() - startTime;
207-
console.log(
208-
`扫描完成,找到 ${
209-
subDirectories.length
210-
} 个子目录,耗时 ${scanTime.toFixed(2)}ms`
211-
);
212203

213204
if (subDirectories.length > 0) {
214205
console.log(`正在观察根目录及${subDirectories.length}个子目录的变化`);

0 commit comments

Comments
 (0)