( tasks: FileTask[], operation: "upload" | "download", concurrency: number, onProgress?: (progress: SyncProgress) => void, )
| 223 | } |
| 224 | |
| 225 | async function runFileTasks( |
| 226 | tasks: FileTask[], |
| 227 | operation: "upload" | "download", |
| 228 | concurrency: number, |
| 229 | onProgress?: (progress: SyncProgress) => void, |
| 230 | ): Promise<boolean[]> { |
| 231 | let completed = 0; |
| 232 | const total = tasks.length; |
| 233 | const operationLabel = operation === "upload" ? "Uploading" : "Downloading"; |
| 234 | const taskLoadedBytes = new Map<number, number>(); |
| 235 | const taskTotalBytes = new Map<number, number>(); |
| 236 | for (let i = 0; i < tasks.length; i++) { |
| 237 | const size = tasks[i].sizeBytes; |
| 238 | if (isPositiveFiniteNumber(size)) { |
| 239 | taskTotalBytes.set(i, size); |
| 240 | taskLoadedBytes.set(i, 0); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | const getTotalTransferBytes = () => { |
| 245 | if (taskTotalBytes.size !== tasks.length) return undefined; |
| 246 | const totalBytes = Array.from(taskTotalBytes.values()).reduce((sum, bytes) => sum + bytes, 0); |
| 247 | return totalBytes > 0 ? totalBytes : undefined; |
| 248 | }; |
| 249 | |
| 250 | const getTotalCurrentBytes = () => |
| 251 | Array.from(taskLoadedBytes.values()).reduce((sum, bytes) => sum + bytes, 0); |
| 252 | |
| 253 | const tasksWithProgress = tasks.map((task, index) => async () => { |
| 254 | const fileNumber = index + 1; |
| 255 | const emitProgress = (currentBytes?: number, totalBytes?: number) => { |
| 256 | if (isPositiveFiniteNumber(totalBytes)) { |
| 257 | taskTotalBytes.set(index, totalBytes); |
| 258 | } |
| 259 | if (currentBytes !== undefined) { |
| 260 | const knownTotal = taskTotalBytes.get(index); |
| 261 | const boundedBytes = knownTotal |
| 262 | ? Math.max(0, Math.min(currentBytes, knownTotal)) |
| 263 | : Math.max(0, currentBytes); |
| 264 | taskLoadedBytes.set(index, boundedBytes); |
| 265 | } |
| 266 | const totalTransferBytes = getTotalTransferBytes(); |
| 267 | onProgress?.({ |
| 268 | phase: "files", |
| 269 | operation, |
| 270 | currentFile: task.label, |
| 271 | completedFiles: completed, |
| 272 | totalFiles: total, |
| 273 | ...(currentBytes !== undefined ? { currentBytes } : {}), |
| 274 | ...(totalBytes !== undefined ? { totalBytes } : {}), |
| 275 | ...(taskLoadedBytes.size > 0 ? { totalCurrentBytes: getTotalCurrentBytes() } : {}), |
| 276 | ...(totalTransferBytes !== undefined ? { totalTransferBytes } : {}), |
| 277 | message: `${operationLabel} file ${fileNumber}/${total}...`, |
| 278 | }); |
| 279 | }; |
| 280 | |
| 281 | emitProgress(); |
| 282 | const result = await task.run((loaded, taskTotal) => { |
no test coverage detected