shoc-frontend-new/src/domain/work-orders/utils/run-sequential-uploads.ts

25 lines
650 B
TypeScript
Raw Normal View History

/**
* Runs file uploads one-by-one while holding an aggregate pending flag true
* until every file has settled (success or failure). Avoids relying on a single
* TanStack Mutation `isPending`, which can flip false when a later small file
* finishes before an earlier large one.
*/
export async function runSequentialUploads<TFile>(
files: readonly TFile[],
upload: (file: TFile) => Promise<unknown>,
setPending: (pending: boolean) => void,
): Promise<void> {
if (files.length === 0) {
return;
}
setPending(true);
try {
for (const file of files) {
await upload(file);
}
} finally {
setPending(false);
}
}