Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,22 @@ extension StreamAttachmentUploaderBatch on StreamAttachmentUploader {
/// [Result] objects as each upload completes. Progress updates are provided
/// through the optional [onProgress] callback.
///
/// When [eagerError] is true, the stream throws an exception and closes
/// immediately on the first upload failure. When false (default), failed
/// uploads are emitted as [Result.failure] and processing continues.
///
/// Returns a [Stream] of [Result] objects in completion order, not input order.
Stream<Result<UploadedAttachment>> uploadBatch(
List<StreamAttachment> attachments, {
Iterable<StreamAttachment> attachments, {
OnBatchUploadProgress? onProgress,
int maxConcurrent = 5,
}) {
return Stream.fromIterable(attachments).flatMap(
bool eagerError = false,
}) async* {
// Early return for empty list
if (attachments.isEmpty) return;

// Create a stream that uploads attachments with controlled concurrency
final uploadStream = Stream.fromIterable(attachments).flatMap(
maxConcurrent: maxConcurrent,
(attachment) => Stream.fromFuture(
upload(
Expand All @@ -146,5 +155,16 @@ extension StreamAttachmentUploaderBatch on StreamAttachmentUploader {
),
),
);

// Yield results as they complete
await for (final result in uploadStream) {
// If eagerError is enabled, throw on first failure
if (result.exceptionOrNull() case final error? when eagerError) {
final stackTrace = result.stackTraceOrNull();
Error.throwWithStackTrace(error, stackTrace ?? StackTrace.current);
}

yield result;
}
}
}
2 changes: 1 addition & 1 deletion packages/stream_core/lib/src/utils/list_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ extension SortedListExtensions<T extends Object> on List<T> {
/// // Result: [Score(userId: '1', points: 150), Score(userId: '3', points: 120), Score(userId: '2', points: 80)]
/// ```
List<T> merge<K>(
List<T> other, {
Iterable<T> other, {
required K Function(T item) key,
T Function(T original, T updated)? update,
Comparator<T>? compare,
Expand Down