getAvailableTokens method
Gets the number of tokens currently available.
This method calculates the current token count based on the stored value plus any tokens that would have been refilled since the last check. The returned value is capped at maxTokens.
Implementation
Future<double> getAvailableTokens() async {
final now = DateTime.now();
final tokens = await _tokenCount.getOrFallback(maxTokens.toDouble());
final last = await _lastRefill.getOrFallback(now);
final elapsedMs = now.difference(last).inMilliseconds;
final refillRatePerMs = maxTokens / refillDuration.inMilliseconds;
final refilledTokens = tokens + (elapsedMs * refillRatePerMs);
return min(maxTokens.toDouble(), refilledTokens);
}