refresh method

Future<String?> refresh(
  1. String? rejected,
  2. CancellationToken caller
)

Implementation

Future<String?> refresh(String? rejected, CancellationToken caller) async {
  _lifetime.throwIfCanceled();
  final current = await caller.race(provider.getToken());
  if (current != rejected) return current;
  final task = _refreshing ??=
      Future<String?>.sync(() => provider.refreshToken(_lifetime));
  try {
    return await caller.race(task);
  } finally {
    // Clear only when the shared work completes, not when one waiter cancels.
    unawaited(task.then<void>((_) {
      if (identical(_refreshing, task)) _refreshing = null;
    }, onError: (Object e, StackTrace s) {
      if (identical(_refreshing, task)) _refreshing = null;
    }));
  }
}