@@ -29,8 +29,10 @@ class CancelableOperation<T> {
29
29
///
30
30
/// Calling this constructor is equivalent to creating a
31
31
/// [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
+ }) =>
34
36
(CancelableCompleter <T >(onCancel: onCancel)..complete (result)).operation;
35
37
36
38
/// Creates a [CancelableOperation] which completes to [value] .
@@ -51,7 +53,8 @@ class CancelableOperation<T> {
51
53
/// subscription will be canceled (unlike
52
54
/// `CancelableOperation.fromFuture(subscription.asFuture())` ).
53
55
static CancelableOperation <void > fromSubscription (
54
- StreamSubscription <void > subscription) {
56
+ StreamSubscription <void > subscription,
57
+ ) {
55
58
var completer = CancelableCompleter <void >(onCancel: subscription.cancel);
56
59
subscription.onDone (completer.complete);
57
60
subscription.onError ((Object error, StackTrace stackTrace) {
@@ -70,7 +73,8 @@ class CancelableOperation<T> {
70
73
/// new operation is cancelled, all the [operations] are cancelled as
71
74
/// well.
72
75
static CancelableOperation <T > race <T >(
73
- Iterable <CancelableOperation <T >> operations) {
76
+ Iterable <CancelableOperation <T >> operations,
77
+ ) {
74
78
operations = operations.toList ();
75
79
if (operations.isEmpty) {
76
80
throw ArgumentError ('May not be empty' , 'operations' );
@@ -83,20 +87,25 @@ class CancelableOperation<T> {
83
87
done = true ;
84
88
return Future .wait ([
85
89
for (var operation in operations)
86
- if (! operation.isCanceled) operation.cancel ()
90
+ if (! operation.isCanceled) operation.cancel (),
87
91
]);
88
92
}
89
93
90
94
var completer = CancelableCompleter <T >(onCancel: cancelAll);
91
95
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
+ );
100
109
}
101
110
102
111
return completer.operation;
@@ -114,16 +123,21 @@ class CancelableOperation<T> {
114
123
/// This is like `value.asStream()` , but if a subscription to the stream is
115
124
/// canceled, this operation is as well.
116
125
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
+ );
127
141
return controller.stream;
128
142
}
129
143
@@ -172,24 +186,28 @@ class CancelableOperation<T> {
172
186
/// operation is canceled as well. Pass `false` if there are multiple
173
187
/// listeners on this operation and canceling the [onValue] , [onError] , and
174
188
/// [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
+ );
193
211
194
212
/// Creates a new cancelable operation to be completed when this operation
195
213
/// completes normally or as an error, or is cancelled.
@@ -222,13 +240,15 @@ class CancelableOperation<T> {
222
240
/// listeners on this operation and canceling the [onValue] , [onError] , and
223
241
/// [onCancel] callbacks should not cancel the other listeners.
224
242
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
+ }) {
230
249
final completer = CancelableCompleter <R >(
231
- onCancel: propagateCancel ? _cancelIfNotCanceled : null );
250
+ onCancel: propagateCancel ? _cancelIfNotCanceled : null ,
251
+ );
232
252
233
253
// if `_completer._inner` completes before `completer` is cancelled
234
254
// call `onValue` or `onError` with the result, and complete `completer`
@@ -246,25 +266,29 @@ class CancelableOperation<T> {
246
266
// completes before `completer` is cancelled,
247
267
// then cancel `cancelCompleter`. (Cancelling twice is safe.)
248
268
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
+ );
268
292
final cancelForwarder = _CancelForwarder <R >(completer, onCancel);
269
293
if (_completer.isCanceled) {
270
294
cancelForwarder._forward ();
@@ -430,11 +454,14 @@ class CancelableCompleter<T> {
430
454
return ;
431
455
}
432
456
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
+ );
438
465
}
439
466
440
467
/// Makes this [CancelableCompleter.operation] complete with the same result
@@ -443,23 +470,30 @@ class CancelableCompleter<T> {
443
470
/// If [propagateCancel] is `true` (the default), and the [operation] of this
444
471
/// completer is canceled before [result] completes, then [result] is also
445
472
/// canceled.
446
- void completeOperation (CancelableOperation <T > result,
447
- {bool propagateCancel = true }) {
473
+ void completeOperation (
474
+ CancelableOperation <T > result, {
475
+ bool propagateCancel = true ,
476
+ }) {
448
477
if (! _mayComplete) throw StateError ('Already completed' );
449
478
_mayComplete = false ;
450
479
if (isCanceled) {
451
480
if (propagateCancel) result.cancel ();
452
481
result.value.ignore ();
453
482
return ;
454
483
}
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
+ );
463
497
if (propagateCancel) {
464
498
_cancelCompleter? .future.whenComplete (result.cancel);
465
499
}
@@ -519,7 +553,7 @@ class CancelableCompleter<T> {
519
553
final isFuture = toReturn is Future ;
520
554
final cancelFutures = < Future <Object ?>> [
521
555
if (isFuture) toReturn,
522
- ...? _cancelForwarders? .map (_forward).nonNulls
556
+ ...? _cancelForwarders? .map (_forward).nonNulls,
523
557
];
524
558
final results = (isFuture && cancelFutures.length == 1 )
525
559
? [await toReturn]
0 commit comments