refreshSingleFlight method

Future<TokenPair?> refreshSingleFlight(
  1. RefreshExecutor executor
)

Runs a refresh operation while ensuring only one refresh is in flight.

Concurrent callers await the same refresh future. When executor succeeds, the new tokens are saved with rememberMe: true.

Implementation

Future<TokenPair?> refreshSingleFlight(RefreshExecutor executor) {
  _refreshFuture ??= load().then((current) {
    if (current == null) return null;
    return executor(current);
  }).then((pair) async {
    if (pair != null) {
      await save(pair, rememberMe: true);
    }
    return pair;
  }).whenComplete(() {
    _refreshFuture = null;
  });

  return _refreshFuture!;
}