Skip to content

Commit

Permalink
fix: count uploaded files when upload reaches 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrvis committed Nov 23, 2023
1 parent eeb535b commit 2653c52
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/core/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,15 @@ class BatchService extends Service {
items: StackCreateItem[],
options: BatchStackCreateOptions = {}
): Promise<BatchStackCreateResponse> {

const size = items.reduce((sum, stack) => {
return sum + stack.file.size;
}, 0);
let progress = 0;
let uploadedStacksCount = 0;
const perFileProgress = new Map();
const uploadedFiles = new Set();

if (options.processingCountHook) {
options.processingCountHook(uploadedStacksCount);
}
Expand All @@ -119,10 +122,17 @@ class BatchService extends Service {

if (options.progressHook) {
const onProgress = options.progressHook
const stackProgressHook = (_: number, binaryProgress: number, progressId: string) => {
const stackProgressHook = (percentageProgress: number, binaryProgress: number, progressId: string) => {
progress += binaryProgress - (perFileProgress.get(progressId) || 0)
perFileProgress.set(progressId, binaryProgress);
onProgress(Math.min(100, Math.round(progress / size * 100)));
if (percentageProgress === 100 && !uploadedFiles.has(progressId)) {
uploadedFiles.add(progressId);
uploadedStacksCount += 1;
if (options.processingCountHook) {
options.processingCountHook(uploadedStacksCount);
}
}
}
options.progressHook = stackProgressHook;
}
Expand Down Expand Up @@ -174,11 +184,6 @@ class BatchService extends Service {
};
const id = await service.uploadState(state, service.vault.cacheOnly);

uploadedStacksCount += 1;
if (options.processingCountHook) {
options.processingCountHook(uploadedStacksCount);
}

postTxQ.add(() => postTx({
vaultId: service.vaultId,
input: { function: service.function, data: id, parentId: createOptions.parentId },
Expand Down

0 comments on commit 2653c52

Please sign in to comment.