Run upload chunks at the same time (max 3)

This commit is contained in:
LittleSheep 2024-08-21 09:33:34 +08:00
parent aa94dfcfe0
commit 498bb0e5fb

View File

@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:collection';
import 'dart:typed_data';
import 'package:cross_file/cross_file.dart';
@ -199,7 +200,12 @@ class AttachmentUploaderController extends GetxController {
final filename = basename(file.path);
final chunks = holder.meta.fileChunks ?? {};
var currentTask = 0;
final queue = Queue<Future<void>>();
final activeTasks = <Future<void>>[];
for (final entry in chunks.entries) {
queue.add(() async {
final beginCursor = entry.value * holder.chunkSize;
final endCursor = (entry.value + 1) * holder.chunkSize;
final data = Uint8List.fromList(await file
@ -218,6 +224,20 @@ class AttachmentUploaderController extends GetxController {
currentTask++;
onProgress(currentTask / chunks.length);
onData(holder);
}());
}
while (queue.isNotEmpty || activeTasks.isNotEmpty) {
while (activeTasks.length < 3 && queue.isNotEmpty) {
final task = queue.removeFirst();
activeTasks.add(task);
task.then((_) => activeTasks.remove(task));
}
if (activeTasks.isNotEmpty) {
await Future.any(activeTasks);
}
}
return holder.meta;