@@ -29,8 +29,10 @@ class CancelableOperation<T> {
2929 ///
3030 /// Calling this constructor is equivalent to creating a
3131 /// [CancelableCompleter] and completing it with [result] .
32- factory CancelableOperation .fromFuture (Future <T > result,
33- {FutureOr Function ()? onCancel}) =>
32+ factory CancelableOperation .fromFuture (
33+ Future <T > result, {
34+ FutureOr Function ()? onCancel,
35+ }) =>
3436 (CancelableCompleter <T >(onCancel: onCancel)..complete (result)).operation;
3537
3638 /// Creates a [CancelableOperation] which completes to [value] .
@@ -51,7 +53,8 @@ class CancelableOperation<T> {
5153 /// subscription will be canceled (unlike
5254 /// `CancelableOperation.fromFuture(subscription.asFuture())` ).
5355 static CancelableOperation <void > fromSubscription (
54- StreamSubscription <void > subscription) {
56+ StreamSubscription <void > subscription,
57+ ) {
5558 var completer = CancelableCompleter <void >(onCancel: subscription.cancel);
5659 subscription.onDone (completer.complete);
5760 subscription.onError ((Object error, StackTrace stackTrace) {
@@ -70,7 +73,8 @@ class CancelableOperation<T> {
7073 /// new operation is cancelled, all the [operations] are cancelled as
7174 /// well.
7275 static CancelableOperation <T > race <T >(
73- Iterable <CancelableOperation <T >> operations) {
76+ Iterable <CancelableOperation <T >> operations,
77+ ) {
7478 operations = operations.toList ();
7579 if (operations.isEmpty) {
7680 throw ArgumentError ('May not be empty' , 'operations' );
@@ -83,20 +87,25 @@ class CancelableOperation<T> {
8387 done = true ;
8488 return Future .wait ([
8589 for (var operation in operations)
86- if (! operation.isCanceled) operation.cancel ()
90+ if (! operation.isCanceled) operation.cancel (),
8791 ]);
8892 }
8993
9094 var completer = CancelableCompleter <T >(onCancel: cancelAll);
9195 for (var operation in operations) {
92- operation.then ((value) {
93- if (! done) cancelAll ().whenComplete (() => completer.complete (value));
94- }, onError: (error, stackTrace) {
95- if (! done) {
96- cancelAll ()
97- .whenComplete (() => completer.completeError (error, stackTrace));
98- }
99- }, propagateCancel: false );
96+ operation.then (
97+ (value) {
98+ if (! done) cancelAll ().whenComplete (() => completer.complete (value));
99+ },
100+ onError: (error, stackTrace) {
101+ if (! done) {
102+ cancelAll ().whenComplete (
103+ () => completer.completeError (error, stackTrace),
104+ );
105+ }
106+ },
107+ propagateCancel: false ,
108+ );
100109 }
101110
102111 return completer.operation;
@@ -114,16 +123,21 @@ class CancelableOperation<T> {
114123 /// This is like `value.asStream()` , but if a subscription to the stream is
115124 /// canceled, this operation is as well.
116125 Stream <T > asStream () {
117- var controller =
118- StreamController <T >(sync : true , onCancel: _completer._cancel);
119-
120- _completer._inner? .future.then ((value) {
121- controller.add (value);
122- controller.close ();
123- }, onError: (Object error, StackTrace stackTrace) {
124- controller.addError (error, stackTrace);
125- controller.close ();
126- });
126+ var controller = StreamController <T >(
127+ sync : true ,
128+ onCancel: _completer._cancel,
129+ );
130+
131+ _completer._inner? .future.then (
132+ (value) {
133+ controller.add (value);
134+ controller.close ();
135+ },
136+ onError: (Object error, StackTrace stackTrace) {
137+ controller.addError (error, stackTrace);
138+ controller.close ();
139+ },
140+ );
127141 return controller.stream;
128142 }
129143
@@ -172,24 +186,28 @@ class CancelableOperation<T> {
172186 /// operation is canceled as well. Pass `false` if there are multiple
173187 /// listeners on this operation and canceling the [onValue] , [onError] , and
174188 /// [onCancel] callbacks should not cancel the other listeners.
175- CancelableOperation <R > then <R >(FutureOr <R > Function (T ) onValue,
176- {FutureOr <R > Function (Object , StackTrace )? onError,
177- FutureOr <R > Function ()? onCancel,
178- bool propagateCancel = true }) =>
179- thenOperation <R >((value, completer) {
180- completer.complete (onValue (value));
181- },
182- onError: onError == null
183- ? null
184- : (error, stackTrace, completer) {
185- completer.complete (onError (error, stackTrace));
186- },
187- onCancel: onCancel == null
188- ? null
189- : (completer) {
190- completer.complete (onCancel ());
191- },
192- propagateCancel: propagateCancel);
189+ CancelableOperation <R > then <R >(
190+ FutureOr <R > Function (T ) onValue, {
191+ FutureOr <R > Function (Object , StackTrace )? onError,
192+ FutureOr <R > Function ()? onCancel,
193+ bool propagateCancel = true ,
194+ }) =>
195+ thenOperation <R >(
196+ (value, completer) {
197+ completer.complete (onValue (value));
198+ },
199+ onError: onError == null
200+ ? null
201+ : (error, stackTrace, completer) {
202+ completer.complete (onError (error, stackTrace));
203+ },
204+ onCancel: onCancel == null
205+ ? null
206+ : (completer) {
207+ completer.complete (onCancel ());
208+ },
209+ propagateCancel: propagateCancel,
210+ );
193211
194212 /// Creates a new cancelable operation to be completed when this operation
195213 /// completes normally or as an error, or is cancelled.
@@ -222,13 +240,15 @@ class CancelableOperation<T> {
222240 /// listeners on this operation and canceling the [onValue] , [onError] , and
223241 /// [onCancel] callbacks should not cancel the other listeners.
224242 CancelableOperation <R > thenOperation <R >(
225- FutureOr <void > Function (T , CancelableCompleter <R >) onValue,
226- {FutureOr <void > Function (Object , StackTrace , CancelableCompleter <R >)?
227- onError,
228- FutureOr <void > Function (CancelableCompleter <R >)? onCancel,
229- bool propagateCancel = true }) {
243+ FutureOr <void > Function (T , CancelableCompleter <R >) onValue, {
244+ FutureOr <void > Function (Object , StackTrace , CancelableCompleter <R >)?
245+ onError,
246+ FutureOr <void > Function (CancelableCompleter <R >)? onCancel,
247+ bool propagateCancel = true ,
248+ }) {
230249 final completer = CancelableCompleter <R >(
231- onCancel: propagateCancel ? _cancelIfNotCanceled : null );
250+ onCancel: propagateCancel ? _cancelIfNotCanceled : null ,
251+ );
232252
233253 // if `_completer._inner` completes before `completer` is cancelled
234254 // call `onValue` or `onError` with the result, and complete `completer`
@@ -246,25 +266,29 @@ class CancelableOperation<T> {
246266 // completes before `completer` is cancelled,
247267 // then cancel `cancelCompleter`. (Cancelling twice is safe.)
248268
249- _completer._inner? .future.then <void >((value) async {
250- if (completer.isCanceled) return ;
251- try {
252- await onValue (value, completer);
253- } catch (error, stack) {
254- completer.completeError (error, stack);
255- }
256- },
257- onError: onError == null
258- ? completer.completeError // Is ignored if already cancelled.
259- : (Object error, StackTrace stack) async {
260- if (completer.isCanceled) return ;
261- try {
262- await onError (error, stack, completer);
263- } catch (error2, stack2) {
264- completer.completeErrorIfPending (
265- error2, identical (error, error2) ? stack : stack2);
266- }
267- });
269+ _completer._inner? .future.then <void >(
270+ (value) async {
271+ if (completer.isCanceled) return ;
272+ try {
273+ await onValue (value, completer);
274+ } catch (error, stack) {
275+ completer.completeError (error, stack);
276+ }
277+ },
278+ onError: onError == null
279+ ? completer.completeError // Is ignored if already cancelled.
280+ : (Object error, StackTrace stack) async {
281+ if (completer.isCanceled) return ;
282+ try {
283+ await onError (error, stack, completer);
284+ } catch (error2, stack2) {
285+ completer.completeErrorIfPending (
286+ error2,
287+ identical (error, error2) ? stack : stack2,
288+ );
289+ }
290+ },
291+ );
268292 final cancelForwarder = _CancelForwarder <R >(completer, onCancel);
269293 if (_completer.isCanceled) {
270294 cancelForwarder._forward ();
@@ -430,11 +454,14 @@ class CancelableCompleter<T> {
430454 return ;
431455 }
432456
433- value.then ((result) {
434- _completeNow ()? .complete (result);
435- }, onError: (Object error, StackTrace stackTrace) {
436- _completeNow ()? .completeError (error, stackTrace);
437- });
457+ value.then (
458+ (result) {
459+ _completeNow ()? .complete (result);
460+ },
461+ onError: (Object error, StackTrace stackTrace) {
462+ _completeNow ()? .completeError (error, stackTrace);
463+ },
464+ );
438465 }
439466
440467 /// Makes this [CancelableCompleter.operation] complete with the same result
@@ -443,23 +470,30 @@ class CancelableCompleter<T> {
443470 /// If [propagateCancel] is `true` (the default), and the [operation] of this
444471 /// completer is canceled before [result] completes, then [result] is also
445472 /// canceled.
446- void completeOperation (CancelableOperation <T > result,
447- {bool propagateCancel = true }) {
473+ void completeOperation (
474+ CancelableOperation <T > result, {
475+ bool propagateCancel = true ,
476+ }) {
448477 if (! _mayComplete) throw StateError ('Already completed' );
449478 _mayComplete = false ;
450479 if (isCanceled) {
451480 if (propagateCancel) result.cancel ();
452481 result.value.ignore ();
453482 return ;
454483 }
455- result.then <void >((value) {
456- _inner? .complete (
457- value); // _inner is set to null if this.operation is cancelled.
458- }, onError: (error, stack) {
459- _inner? .completeError (error, stack);
460- }, onCancel: () {
461- operation.cancel ();
462- });
484+ result.then <void >(
485+ (value) {
486+ _inner? .complete (
487+ value,
488+ ); // _inner is set to null if this.operation is cancelled.
489+ },
490+ onError: (error, stack) {
491+ _inner? .completeError (error, stack);
492+ },
493+ onCancel: () {
494+ operation.cancel ();
495+ },
496+ );
463497 if (propagateCancel) {
464498 _cancelCompleter? .future.whenComplete (result.cancel);
465499 }
@@ -519,7 +553,7 @@ class CancelableCompleter<T> {
519553 final isFuture = toReturn is Future ;
520554 final cancelFutures = < Future <Object ?>> [
521555 if (isFuture) toReturn,
522- ...? _cancelForwarders? .map (_forward).nonNulls
556+ ...? _cancelForwarders? .map (_forward).nonNulls,
523557 ];
524558 final results = (isFuture && cancelFutures.length == 1 )
525559 ? [await toReturn]
0 commit comments