fix(library): fix photo count live update and optimize scan speed

This commit is contained in:
2026-10-07 21:16:10 +07:00
parent 8dd8ba9bc7
commit aa981e4e65
2 changed files with 56 additions and 44 deletions
+34 -32
View File
@@ -790,13 +790,15 @@ export async function scanFolder(
// Count the frame the moment the scan gets to it, before it knows whether the
// frame is new: the rows say how far the reading has come, not what it kept.
const count = (rel: string) => {
const cut = rel.lastIndexOf('/');
counts[folder.name] = (counts[folder.name] ?? 0) + 1;
const relNorm = rel.replace(/\\/g, '/');
const cut = relNorm.lastIndexOf('/');
const normRoot = normPath(folder.name);
counts[normRoot] = (counts[normRoot] ?? 0) + 1;
let path = '';
for (const part of (cut < 0 ? '' : rel.slice(0, cut)).split('/')) {
for (const part of (cut < 0 ? '' : relNorm.slice(0, cut)).split('/')) {
if (!part) continue;
path = path ? `${path}/${part}` : part;
const key = `${folder.name}/${path}`;
const key = `${normRoot}/${path}`;
counts[key] = (counts[key] ?? 0) + 1;
}
};
@@ -928,25 +930,31 @@ export async function scanFolder(
let preview: Uint8Array | null = null;
if (heic) {
preview = await heicThumbnail(bytes);
if (!preview) {
try {
const fullBytes = new Uint8Array(await file.arrayBuffer());
preview = await heicToJpeg(fullBytes, 320);
} catch {
preview = null;
preview = await oneRawAtATime(async () => {
let p = await heicThumbnail(bytes);
if (!p) {
try {
const fullBytes = new Uint8Array(await file.arrayBuffer());
p = await heicToJpeg(fullBytes, 320);
} catch {
p = null;
}
}
}
return p;
});
} else if (raw || isTiff) {
preview = await rawThumbnail(bytes, file.name);
if (!preview && isTiff) {
try {
const fullBytes = new Uint8Array(await file.arrayBuffer());
preview = tiffThumbnail(fullBytes);
} catch {
preview = null;
preview = await oneRawAtATime(async () => {
let p = await rawThumbnail(bytes, file.name);
if (!p && isTiff) {
try {
const fullBytes = new Uint8Array(await file.arrayBuffer());
p = tiffThumbnail(fullBytes);
} catch {
p = null;
}
}
}
return p;
});
}
const src = preview ? new Blob([preview as BlobPart], { type: 'image/jpeg' }) : (raw || heic || isTiff) ? null : file;
@@ -998,16 +1006,9 @@ export async function scanFolder(
// folder is being read while it is being read.
const at = rel.lastIndexOf('/');
progress.at = at < 0 ? '' : rel.slice(0, at);
// A frame a reload handed back that was counted already is read without
// being counted twice; the ones behind it are frames that reading found and
// never reached, and they are counted here as they are reached.
if (restored) restored--;
else {
progress.done++;
count(rel);
}
// Queue image decoding one at a time so memory usage stays low and UI stays smooth
const read = oneRawAtATime(() => readOne(handle, rel));
progress.done++;
count(rel);
const read = readOne(handle, rel);
const lane = read
.catch(() => undefined)
.finally(() => lanes.delete(lane));
@@ -1015,14 +1016,15 @@ export async function scanFolder(
if (lanes.size >= LANES) await Promise.race(lanes);
publish();
// Micro-yield to the browser main thread event loop so scanning never blocks UI rendering or user interactions
// Micro-yield to main thread every 64 files to keep scan fast while keeping UI responsive
processed++;
if (processed % 3 === 0) {
if (processed % 64 === 0) {
await new Promise((resolve) => setTimeout(resolve, 0));
}
}
await Promise.all(lanes);
await flush();
publish(true);
};
// How many of the frames in hand were counted by the reading that handed them