Skip to content

Commit 314b193

Browse files
committed
fix(worker): solve the problem that did not end the process
1 parent 6f1b020 commit 314b193

1 file changed

Lines changed: 32 additions & 90 deletions

File tree

src/services/files/files.worker.ts

Lines changed: 32 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -162,71 +162,22 @@ class FileWalker {
162162

163163
const collector = {
164164
total: 0,
165-
pending: 0,
165+
pending: 1,
166166
onComplete: (finalSize: number) => {
167167
this.events.emit('folderSizeResult', { path, size: finalSize });
168168
},
169169
};
170170

171-
this.calculateFolderSizeRecursive(path, collector);
171+
this.enqueueTask(path, ETaskOperation.getFolderSizeChild, false, collector);
172172
this.completeTask();
173173
}
174174

175-
private async calculateFolderSizeRecursive(
176-
path: string,
177-
collector: Task['sizeCollector'],
178-
): Promise<void> {
179-
if (!collector) return;
180-
181-
collector.pending += 1;
182-
this.updateProcs(1);
183-
184-
try {
185-
const entries = await readdir(path, { withFileTypes: true });
186-
187-
for (const entry of entries) {
188-
const fullPath = join(path, entry.name);
189-
190-
let stats;
191-
try {
192-
stats = await lstat(fullPath);
193-
} catch {
194-
continue; // Skip files we can't access
195-
}
196-
197-
if (stats.isSymbolicLink()) {
198-
continue;
199-
}
200-
201-
const size =
202-
typeof stats.blocks === 'number' ? stats.blocks * 512 : stats.size;
203-
collector.total += size;
204-
205-
if (stats.isDirectory()) {
206-
// Process subdirectory recursively
207-
this.calculateFolderSizeRecursive(fullPath, collector);
208-
}
209-
}
210-
} catch (error) {
211-
// Handle directory access errors gracefully
212-
console.warn(`Failed to read directory: ${path}`, error);
213-
} finally {
214-
// Always decrement pending count
215-
collector.pending -= 1;
216-
this.updateProcs(-1);
217-
218-
// Check if all work is complete
219-
if (collector.pending === 0) {
220-
collector.onComplete(collector.total);
221-
}
222-
}
223-
}
224-
225175
private async runGetFolderSizeChild(
226176
path: string,
227177
collector: Task['sizeCollector'],
228178
): Promise<void> {
229179
if (!collector) {
180+
// Should not happen with proper initiation, but safe.
230181
this.completeTask();
231182
return;
232183
}
@@ -235,47 +186,37 @@ class FileWalker {
235186

236187
try {
237188
const entries = await readdir(path, { withFileTypes: true });
189+
let currentLevelSize = 0;
238190
const directoriesToProcess: string[] = [];
239-
const promises: Promise<void>[] = [];
240-
241-
for (const entry of entries) {
242-
const fullPath = join(path, entry.name);
243-
244-
if (entry.isSymbolicLink()) {
245-
continue;
246-
}
247-
248-
if (entry.isDirectory()) {
249-
directoriesToProcess.push(fullPath);
250-
promises.push(
251-
lstat(fullPath)
252-
.then((stats) => {
253-
const size =
254-
typeof stats.blocks === 'number'
255-
? stats.blocks * 512
256-
: stats.size;
257-
collector.total += size;
258-
})
259-
.catch(() => {}),
260-
);
261-
} else if (entry.isFile()) {
262-
promises.push(
263-
lstat(fullPath)
264-
.then((stats) => {
265-
const size =
266-
typeof stats.blocks === 'number'
267-
? stats.blocks * 512
268-
: stats.size;
269-
collector.total += size;
270-
})
271-
.catch(() => {}),
272-
);
273-
}
274-
}
275-
276-
await Promise.all(promises);
277191

192+
await Promise.all(
193+
entries.map(async (entry) => {
194+
const fullPath = join(path, entry.name);
195+
try {
196+
if (entry.isSymbolicLink()) {
197+
return;
198+
}
199+
200+
const stats = await lstat(fullPath);
201+
const size =
202+
typeof stats.blocks === 'number'
203+
? stats.blocks * 512
204+
: stats.size;
205+
206+
currentLevelSize += size;
207+
208+
if (stats.isDirectory()) {
209+
directoriesToProcess.push(fullPath);
210+
}
211+
} catch {
212+
// Ignore permissions errors.
213+
}
214+
}),
215+
);
216+
217+
collector.total += currentLevelSize;
278218
collector.pending += directoriesToProcess.length;
219+
279220
for (const dirPath of directoriesToProcess) {
280221
this.enqueueTask(
281222
dirPath,
@@ -285,6 +226,7 @@ class FileWalker {
285226
);
286227
}
287228
} catch (error) {
229+
// Ignore permissions errors.
288230
} finally {
289231
collector.pending -= 1;
290232

0 commit comments

Comments
 (0)