-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
future_extensions.dart
615 lines (578 loc) · 17.8 KB
/
future_extensions.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
part of dart.async;
@Since("3.0")
extension FutureIterable<T> on Iterable<Future<T>> {
/// Waits for futures in parallel.
///
/// Waits for all the futures in this iterable.
/// Returns a list of the resulting values,
/// in the same order as the futures which created them,
/// if all futures are successful.
///
/// Similar to [Future.wait], but reports errors using a
/// [ParallelWaitError], which allows the caller to
/// handle errors and dispose successful results if necessary.
///
/// The returned future is completed when all the futures have completed.
/// If any of the futures do not complete, nor does the returned future.
///
/// If any future completes with an error,
/// the returned future completes with a [ParallelWaitError].
/// The [ParallelWaitError.values] is a list of the values for
/// successful futures and `null` for futures with errors.
/// The [ParallelWaitError.errors] is a list of the same length,
/// with `null` values for the successful futures
/// and an [AsyncError] with the error for futures
/// which completed with an error.
Future<List<T>> get wait {
var results = [for (var f in this) _FutureResult<T>(f)];
if (results.isEmpty) return Future<List<T>>.value(<T>[]);
final c = Completer<List<T>>.sync();
_FutureResult._waitAll(results, (errors) {
if (errors == 0) {
c.complete([for (var r in results) r.value]);
} else {
var errorList = [for (var r in results) r.errorOrNull];
c.completeError(ParallelWaitError<List<T?>, List<AsyncError?>>(
[for (var r in results) r.valueOrNull], errorList,
errorCount: errors, defaultError: errorList.firstWhere(_notNull)));
}
});
return c.future;
}
}
bool _notNull(Object? object) => object != null;
/// Parallel operations on a record of futures.
///
/// {@template record-parallel-wait}
/// Waits for futures in parallel.
///
/// Waits for all the futures in this record.
/// Returns a record of the values, if all futures are successful.
///
/// The returned future is completed when all the futures have completed.
/// If any of the futures do not complete, nor does the returned future.
///
/// If some futures complete with an error,
/// the returned future completes with a [ParallelWaitError].
/// The [ParallelWaitError.values] is a record of the values of
/// successful futures, and `null` for futures with errors.
/// The [ParallelWaitError.errors] is a record of the same shape,
/// with `null` values for the successful futures
/// and an [AsyncError] with the error of futures
/// which completed with an error.
/// {@endtemplate}
@Since("3.0")
extension FutureRecord2<T1, T2> on (Future<T1>, Future<T2>) {
/// {@macro record-parallel-wait}
Future<(T1, T2)> get wait {
final c = Completer<(T1, T2)>.sync();
final v1 = _FutureResult<T1>($1);
final v2 = _FutureResult<T2>($2);
_FutureResult._waitAll([v1, v2], (int errors) {
if (errors == 0) {
c.complete((v1.value, v2.value));
} else {
c.completeError(ParallelWaitError(
(v1.valueOrNull, v2.valueOrNull),
(v1.errorOrNull, v2.errorOrNull),
errorCount: errors,
defaultError: v1.errorOrNull ?? v2.errorOrNull
));
}
});
return c.future;
}
}
/// Parallel operations on a record of futures.
@Since("3.0")
extension FutureRecord3<T1, T2, T3> on (Future<T1>, Future<T2>, Future<T3>) {
/// {@macro record-parallel-wait}
Future<(T1, T2, T3)> get wait {
final c = Completer<(T1, T2, T3)>.sync();
final v1 = _FutureResult<T1>($1);
final v2 = _FutureResult<T2>($2);
final v3 = _FutureResult<T3>($3);
_FutureResult._waitAll([v1, v2, v3], (int errors) {
if (errors == 0) {
c.complete((v1.value, v2.value, v3.value));
} else {
c.completeError(ParallelWaitError(
(v1.valueOrNull, v2.valueOrNull, v3.valueOrNull),
(v1.errorOrNull, v2.errorOrNull, v3.errorOrNull),
errorCount: errors,
defaultError: v1.errorOrNull ?? v2.errorOrNull ?? v3.errorOrNull,
));
}
});
return c.future;
}
}
/// Parallel operations on a record of futures.
@Since("3.0")
extension FutureRecord4<T1, T2, T3, T4> on (
Future<T1>,
Future<T2>,
Future<T3>,
Future<T4>
) {
/// {@macro record-parallel-wait}
Future<(T1, T2, T3, T4)> get wait {
final c = Completer<(T1, T2, T3, T4)>.sync();
final v1 = _FutureResult<T1>($1);
final v2 = _FutureResult<T2>($2);
final v3 = _FutureResult<T3>($3);
final v4 = _FutureResult<T4>($4);
_FutureResult._waitAll([v1, v2, v3, v4], (int errors) {
if (errors == 0) {
c.complete((v1.value, v2.value, v3.value, v4.value));
} else {
c.completeError(ParallelWaitError(
(v1.valueOrNull, v2.valueOrNull, v3.valueOrNull, v4.valueOrNull),
(v1.errorOrNull, v2.errorOrNull, v3.errorOrNull, v4.errorOrNull),
errorCount: errors,
defaultError: v1.errorOrNull ??
v2.errorOrNull ??
v3.errorOrNull ??
v4.errorOrNull,
));
}
});
return c.future;
}
}
/// Parallel operations on a record of futures.
@Since("3.0")
extension FutureRecord5<T1, T2, T3, T4, T5> on (
Future<T1>,
Future<T2>,
Future<T3>,
Future<T4>,
Future<T5>
) {
/// {@macro record-parallel-wait}
Future<(T1, T2, T3, T4, T5)> get wait {
final c = Completer<(T1, T2, T3, T4, T5)>.sync();
final v1 = _FutureResult<T1>($1);
final v2 = _FutureResult<T2>($2);
final v3 = _FutureResult<T3>($3);
final v4 = _FutureResult<T4>($4);
final v5 = _FutureResult<T5>($5);
_FutureResult._waitAll([v1, v2, v3, v4, v5], (int errors) {
if (errors == 0) {
c.complete((v1.value, v2.value, v3.value, v4.value, v5.value));
} else {
c.completeError(ParallelWaitError(
(
v1.valueOrNull,
v2.valueOrNull,
v3.valueOrNull,
v4.valueOrNull,
v5.valueOrNull
),
(
v1.errorOrNull,
v2.errorOrNull,
v3.errorOrNull,
v4.errorOrNull,
v5.errorOrNull
),
errorCount: errors,
defaultError: v1.errorOrNull ??
v2.errorOrNull ??
v3.errorOrNull ??
v4.errorOrNull ??
v5.errorOrNull
));
}
});
return c.future;
}
}
/// Parallel operations on a record of futures.
@Since("3.0")
extension FutureRecord6<T1, T2, T3, T4, T5, T6> on (
Future<T1>,
Future<T2>,
Future<T3>,
Future<T4>,
Future<T5>,
Future<T6>
) {
/// {@macro record-parallel-wait}
Future<(T1, T2, T3, T4, T5, T6)> get wait {
final c = Completer<(T1, T2, T3, T4, T5, T6)>.sync();
final v1 = _FutureResult<T1>($1);
final v2 = _FutureResult<T2>($2);
final v3 = _FutureResult<T3>($3);
final v4 = _FutureResult<T4>($4);
final v5 = _FutureResult<T5>($5);
final v6 = _FutureResult<T6>($6);
_FutureResult._waitAll([v1, v2, v3, v4, v5, v6], (int errors) {
if (errors == 0) {
c.complete(
(v1.value, v2.value, v3.value, v4.value, v5.value, v6.value));
} else {
c.completeError(ParallelWaitError(
(
v1.valueOrNull,
v2.valueOrNull,
v3.valueOrNull,
v4.valueOrNull,
v5.valueOrNull,
v6.valueOrNull
),
(
v1.errorOrNull,
v2.errorOrNull,
v3.errorOrNull,
v4.errorOrNull,
v5.errorOrNull,
v6.errorOrNull
),
errorCount: errors,
defaultError: v1.errorOrNull ??
v2.errorOrNull ??
v3.errorOrNull ??
v4.errorOrNull ??
v5.errorOrNull ??
v6.errorOrNull
));
}
});
return c.future;
}
}
/// Parallel operations on a record of futures.
@Since("3.0")
extension FutureRecord7<T1, T2, T3, T4, T5, T6, T7> on (
Future<T1>,
Future<T2>,
Future<T3>,
Future<T4>,
Future<T5>,
Future<T6>,
Future<T7>
) {
/// {@macro record-parallel-wait}
Future<(T1, T2, T3, T4, T5, T6, T7)> get wait {
final c = Completer<(T1, T2, T3, T4, T5, T6, T7)>.sync();
final v1 = _FutureResult<T1>($1);
final v2 = _FutureResult<T2>($2);
final v3 = _FutureResult<T3>($3);
final v4 = _FutureResult<T4>($4);
final v5 = _FutureResult<T5>($5);
final v6 = _FutureResult<T6>($6);
final v7 = _FutureResult<T7>($7);
_FutureResult._waitAll([v1, v2, v3, v4, v5, v6, v7], (int errors) {
if (errors == 0) {
c.complete((
v1.value,
v2.value,
v3.value,
v4.value,
v5.value,
v6.value,
v7.value
));
} else {
c.completeError(ParallelWaitError(
(
v1.valueOrNull,
v2.valueOrNull,
v3.valueOrNull,
v4.valueOrNull,
v5.valueOrNull,
v6.valueOrNull,
v7.valueOrNull
),
(
v1.errorOrNull,
v2.errorOrNull,
v3.errorOrNull,
v4.errorOrNull,
v5.errorOrNull,
v6.errorOrNull,
v7.errorOrNull
),
errorCount: errors,
defaultError: v1.errorOrNull ??
v2.errorOrNull ??
v3.errorOrNull ??
v4.errorOrNull ??
v5.errorOrNull ??
v6.errorOrNull ??
v7.errorOrNull
));
}
});
return c.future;
}
}
/// Parallel operations on a record of futures.
@Since("3.0")
extension FutureRecord8<T1, T2, T3, T4, T5, T6, T7, T8> on (
Future<T1>,
Future<T2>,
Future<T3>,
Future<T4>,
Future<T5>,
Future<T6>,
Future<T7>,
Future<T8>
) {
/// {@macro record-parallel-wait}
Future<(T1, T2, T3, T4, T5, T6, T7, T8)> get wait {
final c = Completer<(T1, T2, T3, T4, T5, T6, T7, T8)>.sync();
final v1 = _FutureResult<T1>($1);
final v2 = _FutureResult<T2>($2);
final v3 = _FutureResult<T3>($3);
final v4 = _FutureResult<T4>($4);
final v5 = _FutureResult<T5>($5);
final v6 = _FutureResult<T6>($6);
final v7 = _FutureResult<T7>($7);
final v8 = _FutureResult<T8>($8);
_FutureResult._waitAll([v1, v2, v3, v4, v5, v6, v7, v8], (int errors) {
if (errors == 0) {
c.complete((
v1.value,
v2.value,
v3.value,
v4.value,
v5.value,
v6.value,
v7.value,
v8.value
));
} else {
c.completeError(ParallelWaitError(
(
v1.valueOrNull,
v2.valueOrNull,
v3.valueOrNull,
v4.valueOrNull,
v5.valueOrNull,
v6.valueOrNull,
v7.valueOrNull,
v8.valueOrNull
),
(
v1.errorOrNull,
v2.errorOrNull,
v3.errorOrNull,
v4.errorOrNull,
v5.errorOrNull,
v6.errorOrNull,
v7.errorOrNull,
v8.errorOrNull
),
errorCount: errors,
defaultError: v1.errorOrNull ??
v2.errorOrNull ??
v3.errorOrNull ??
v4.errorOrNull ??
v5.errorOrNull ??
v6.errorOrNull ??
v7.errorOrNull ??
v8.errorOrNull
));
}
});
return c.future;
}
}
/// Parallel operations on a record of futures.
@Since("3.0")
extension FutureRecord9<T1, T2, T3, T4, T5, T6, T7, T8, T9> on (
Future<T1>,
Future<T2>,
Future<T3>,
Future<T4>,
Future<T5>,
Future<T6>,
Future<T7>,
Future<T8>,
Future<T9>
) {
/// {@macro record-parallel-wait}
Future<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> get wait {
final c = Completer<(T1, T2, T3, T4, T5, T6, T7, T8, T9)>.sync();
final v1 = _FutureResult<T1>($1);
final v2 = _FutureResult<T2>($2);
final v3 = _FutureResult<T3>($3);
final v4 = _FutureResult<T4>($4);
final v5 = _FutureResult<T5>($5);
final v6 = _FutureResult<T6>($6);
final v7 = _FutureResult<T7>($7);
final v8 = _FutureResult<T8>($8);
final v9 = _FutureResult<T9>($9);
_FutureResult._waitAll([v1, v2, v3, v4, v5, v6, v7, v8, v9], (int errors) {
if (errors == 0) {
c.complete((
v1.value,
v2.value,
v3.value,
v4.value,
v5.value,
v6.value,
v7.value,
v8.value,
v9.value
));
} else {
c.completeError(ParallelWaitError(
(
v1.valueOrNull,
v2.valueOrNull,
v3.valueOrNull,
v4.valueOrNull,
v5.valueOrNull,
v6.valueOrNull,
v7.valueOrNull,
v8.valueOrNull,
v9.valueOrNull
),
(
v1.errorOrNull,
v2.errorOrNull,
v3.errorOrNull,
v4.errorOrNull,
v5.errorOrNull,
v6.errorOrNull,
v7.errorOrNull,
v8.errorOrNull,
v9.errorOrNull
),
errorCount: errors,
defaultError: v1.errorOrNull ??
v2.errorOrNull ??
v3.errorOrNull ??
v4.errorOrNull ??
v5.errorOrNull ??
v6.errorOrNull ??
v7.errorOrNull ??
v8.errorOrNull ??
v9.errorOrNull
));
}
});
return c.future;
}
}
/// Error thrown when waiting for multiple futures, when some have errors.
///
/// The [V] and [E] types will have the same basic shape as the
/// original collection of futures that was waited on.
///
/// For example, if the original awaited futures were a record
/// `(Future<T1>, ..., Future<Tn>)`,
/// the type `V` will be `(T1?, ..., Tn?)` which allows keeping the
/// values of futures that completed with a value,
/// and `E` will be `(AsyncError?, ..., AsyncError?)`, also with *n*
/// fields, which can contain the errors for the futures which completed
/// with an error.
///
/// Waiting for a list or iterable of futures should provide
/// a list of nullable values and errors of the same length.
@Since("3.0")
class ParallelWaitError<V, E> extends Error {
/// Values of successful futures.
///
/// Has the same shape as the original collection of futures,
/// with values for each successful future and `null` values
/// for each failing future.
final V values;
/// Errors of failing futures.
///
/// Has the same shape as the original collection of futures,
/// with errors, typically [AsyncError], for each failing
/// future and `null` values for each successful future.
final E errors;
/// An error which, if present, is included in the [toString] output.
///
/// If the default error has a stack trace, it's also reported by the
/// [stackTrace] getter, instead of where this [ParallelWaitError] was thrown.
final AsyncError? _defaultError;
/// Number of errors, if available.
final int? _errorCount;
/// Creates error with the provided [values] and [errors].
///
/// If [defaultError] is provided, its [AsyncError.error] is used in
/// the [toString] of this parallel error, and its [AsyncError.stackTrace]
/// is returned by [stackTrace].
///
/// If [errorCount] is provided, and it's greater than one,
/// the number is reported in the [toString].
ParallelWaitError(this.values, this.errors,
{@Since("3.4") int? errorCount, @Since("3.4") AsyncError? defaultError})
: _defaultError = defaultError,
_errorCount = errorCount;
String toString() {
if (_defaultError == null) {
if (_errorCount == null || _errorCount <= 1) {
return "ParallelWaitError";
}
return "ParallelWaitError($_errorCount errors)";
}
return "ParallelWaitError${_errorCount != null && _errorCount > 1 //
? "($_errorCount errors)" : ""}: ${_defaultError.error}";
}
StackTrace? get stackTrace => _defaultError?.stackTrace ?? super.stackTrace;
}
/// The result of a future, when it has completed.
///
/// Stores a value result in [value] and an error result in [error].
/// Then calls [onReady] with a 0 argument for a value, and 1 for an error.
///
/// The [onReady] callback must be set synchronously,
/// before the future has a chance to complete.
///
/// Abstracted into a class of its own in order to reuse code.
class _FutureResult<T> {
// Consider integrating directly into `_Future` as a `_FutureListener`
// to avoid creating as many function tear-offs.
/// The value or `null`.
///
/// Set when the future completes with a value.
T? valueOrNull;
/// Set when the future completes with an error or value.
AsyncError? errorOrNull;
void Function(int errors) onReady = _noop;
_FutureResult(Future<T> future) {
future.then(_onValue, onError: _onError);
}
/// The value.
///
/// Should only be used when the future is known to have completed with
/// a value.
T get value => valueOrNull ?? valueOrNull as T;
void _onValue(T value) {
valueOrNull = value;
onReady(0);
}
void _onError(Object error, StackTrace stack) {
this.errorOrNull = AsyncError(error, stack);
onReady(1);
}
/// Waits for a number of [_FutureResult]s to all have completed.
///
/// List must not be empty.
static void _waitAll(
List<_FutureResult> results, void Function(int) whenReady) {
assert(results.isNotEmpty);
var ready = 0;
var errors = 0;
void onReady(int error) {
errors += error;
if (++ready == results.length) {
whenReady(errors);
}
}
for (var r in results) {
r.onReady = onReady;
}
}
static void _noop(_) {}
}