improved performance and battery usage on older devices
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import '../model/account_data.dart';
|
||||
|
||||
/// Joins concurrent calls into the one already running for the same account
|
||||
/// session. A run left over from a signed-out session never blocks the next
|
||||
/// account's first call.
|
||||
class SessionSingleFlight {
|
||||
Future<void>? _running;
|
||||
int? _epoch;
|
||||
|
||||
Future<void> run(Future<void> Function() action) {
|
||||
final epoch = AccountData().sessionEpoch;
|
||||
final running = _running;
|
||||
if (running != null && _epoch == epoch) return running;
|
||||
_epoch = epoch;
|
||||
late final Future<void> current;
|
||||
current = action().whenComplete(() {
|
||||
if (identical(_running, current)) _running = null;
|
||||
});
|
||||
return _running = current;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user