prefetchFastModeStatus method

Future<void> prefetchFastModeStatus()

Prefetch fast mode status from the API.

Implementation

Future<void> prefetchFastModeStatus() async {
  // Skip network requests if nonessential traffic is disabled.
  if (_config.isEssentialTrafficOnly()) return;
  if (!isFastModeEnabled) return;

  if (_inflightPrefetch != null) {
    return _inflightPrefetch;
  }

  // Service key OAuth sessions lack user:profile scope.
  final apiKey = _config.getApiKey();
  final oauthTokens = _config.getOAuthTokens();
  final hasUsableOAuth =
      oauthTokens != null &&
      oauthTokens['accessToken'] != null &&
      _config.hasProfileScope();
  if (!hasUsableOAuth && apiKey == null) {
    final isAnt = _config.getUserType() == 'ant';
    final cachedEnabled =
        _config.getGlobalConfig()['penguinModeOrgEnabled'] == true;
    _orgStatus = isAnt || cachedEnabled
        ? const FastModeOrgEnabled()
        : const FastModeOrgDisabled(
            reason: FastModeDisabledReason.preference,
          );
    return;
  }

  final now = DateTime.now().millisecondsSinceEpoch;
  if (now - _lastPrefetchAt < prefetchMinIntervalMs) return;
  _lastPrefetchAt = now;

  _inflightPrefetch = _doFetch(apiKey, oauthTokens);
  return _inflightPrefetch;
}