Skip to content

Commit 797bd33

Browse files
committed
Code review
1 parent 32cd9b6 commit 797bd33

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

pkgs/async/lib/src/future_group.dart

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'cancelable_operation.dart';
88

99
/// A sentinel object indicating that a member of a [FutureGroup] was canceled
1010
/// rather than completing normally.
11-
const _canceledResult = Object();
11+
final _canceledResult = Object();
1212

1313
/// A collection of futures waits until all added [Future]s complete.
1414
///
@@ -74,14 +74,19 @@ class FutureGroup<T> implements Sink<Future<T>> {
7474

7575
/// Wait for [task] to complete.
7676
@override
77-
void add(Future<T> task) =>
78-
addCancelable(CancelableOperation.fromFuture(task));
77+
void add(Future<T> task) => _add(task);
7978

8079
/// Wait for [task] to complete.
8180
///
8281
/// If [task] is canceled, it's removed from the group without adding a value
8382
/// to [future].
8483
void addCancelable(CancelableOperation<T> task) {
84+
_add(task
85+
.then((value) => value, onCancel: () => _canceledResult)
86+
.valueOrCancellation());
87+
}
88+
89+
void _add(Future<Object?> task) {
8590
if (_closed) throw StateError('The FutureGroup is closed.');
8691

8792
// Ensure that future values are put into [values] in the same order they're
@@ -91,11 +96,11 @@ class FutureGroup<T> implements Sink<Future<T>> {
9196
_values.add(null);
9297

9398
_pending++;
94-
task.valueOrCancellation().then((value) {
99+
task.then((value) {
95100
if (_completer.isCompleted) return null;
96101

97102
_pending--;
98-
_values[index] = task.isCanceled ? _canceledResult : value;
103+
_values[index] = value;
99104

100105
if (_pending != 0) return null;
101106
var onIdleController = _onIdleController;
@@ -104,9 +109,9 @@ class FutureGroup<T> implements Sink<Future<T>> {
104109
if (!_closed) return null;
105110
if (onIdleController != null) onIdleController.close();
106111
_completer.complete([
107-
for (var value in _values)
108-
if (value != _canceledResult && value is T) value
109-
]);
112+
for (var value in _values)
113+
if (value != _canceledResult) value as T
114+
]);
110115
}).catchError((Object error, StackTrace stackTrace) {
111116
if (_completer.isCompleted) return null;
112117
_completer.completeError(error, stackTrace);
@@ -120,6 +125,9 @@ class FutureGroup<T> implements Sink<Future<T>> {
120125
_closed = true;
121126
if (_pending != 0) return;
122127
if (_completer.isCompleted) return;
123-
_completer.complete(_values.whereType<T>().toList());
128+
_completer.complete([
129+
for (var value in _values)
130+
if (value != _canceledResult) value as T
131+
]);
124132
}
125133
}

0 commit comments

Comments
 (0)