/** * Rebuilds stored raw proxies from their originals. * * A decode is CPU bound and holds a LibRaw worker for seconds, so the work runs * one photo at a time behind a snapshot: a request that blocked for an hour * would time out, and decoding in parallel would starve the editor. The job * itself keeps progress in memory; `onProgress` lets the caller persist it so a * restart can pick the run up where it stopped. */ /** Photos that have a proxy worth regenerating: camera raws that still exist. */ export function previewRebuildTargets(photos) { return photos.filter((photo) => photo && photo.raw === true && !photo.deletedAt); } /** * Runs `regenerate(photo)` for every target and reports progress. * * A photo that fails is counted and recorded, and the run continues: one * unreadable original must not strand the rest of the library. */ export function startPreviewRebuild({ targets, regenerate, now = Date.now, onProgress }) { const job = { state: 'running', total: targets.length, done: 0, failed: 0, sidecars: 0, startedAt: now(), finishedAt: null, errors: [], }; const finished = (async () => { for (const [index, photo] of targets.entries()) { try { const result = await regenerate(photo); if (result?.sidecar) job.sidecars += 1; } catch (err) { job.failed += 1; if (job.errors.length < 5) { const label = photo?.name || photo?.id || 'photo'; job.errors.push(`${label}: ${err instanceof Error ? err.message : String(err)}`); } } job.done += 1; onProgress?.({ ...job, errors: [...job.errors] }, targets.slice(index + 1)); } job.state = 'done'; job.finishedAt = now(); })(); return { snapshot: () => ({ ...job, errors: [...job.errors] }), finished }; }