uploadBlobs method

Future<List<BlobUploadResult?>> uploadBlobs(
  1. List<BlobFile> files
)

Uploads attachments with bounded concurrency.

The first failure surfaces to the caller; results that landed before it keep their position in the returned list.

Implementation

Future<List<BlobUploadResult?>> uploadBlobs(List<BlobFile> files) async {
  if (files.isEmpty) return <BlobUploadResult?>[];
  final out = List<BlobUploadResult?>.filled(files.length, null);
  final errors = List<Object?>.filled(files.length, null);
  final concurrency = config.uploadConcurrency < 1
      ? 1
      : config.uploadConcurrency;

  var next = 0;
  Future<void> worker() async {
    while (true) {
      final index = next++;
      if (index >= files.length) return;
      try {
        out[index] = await uploadBlob(files[index]);
      } on Object catch (error) {
        errors[index] = error;
      }
    }
  }

  await Future.wait(
    List<Future<void>>.generate(
      concurrency < files.length ? concurrency : files.length,
      (_) => worker(),
    ),
  );
  for (final error in errors) {
    if (error != null) throw error;
  }
  return out;
}