21 lines
571 B
Dart
21 lines
571 B
Dart
|
|
import 'package:meta/meta.dart';
|
||
|
|
|
||
|
|
/// Runs an action once for everyone who asks: a caller while it runs awaits
|
||
|
|
/// the same run, and one after it is done gets it done; only a run that
|
||
|
|
/// failed is run again by the next caller.
|
||
|
|
@internal
|
||
|
|
class Once {
|
||
|
|
Future<void>? _run;
|
||
|
|
|
||
|
|
/// Runs [action] unless it ran or runs already, and completes with that
|
||
|
|
/// run.
|
||
|
|
Future<void> run(Future<void> Function() action) async {
|
||
|
|
final run = _run ??= action();
|
||
|
|
try {
|
||
|
|
await run;
|
||
|
|
} catch (_) {
|
||
|
|
if (identical(_run, run)) _run = null;
|
||
|
|
rethrow;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|