poll method

  1. @override
Future<AuthDeviceAuthorizationPollResult> poll(
  1. String deviceCodeHash, {
  2. DateTime? now,
})
override

Polls a record using UTC time and updates its last-poll timestamp.

Polling before the current interval returns slowDown and adds five seconds to that interval. Terminal and expired states are returned without reopening the request.

Implementation

@override
Future<AuthDeviceAuthorizationPollResult> poll(
  String deviceCodeHash, {
  DateTime? now,
}) async {
  final current = (now ?? DateTime.now()).toUtc();
  final key = deviceCodeHash.trim();
  final existing = _records[key];
  if (existing == null) {
    return const AuthDeviceAuthorizationPollResult(
      AuthDeviceAuthorizationPollStatus.invalid,
    );
  }
  if (existing.isExpired(now: current)) {
    return AuthDeviceAuthorizationPollResult(
      AuthDeviceAuthorizationPollStatus.expired,
      existing,
    );
  }
  if (existing.status == AuthDeviceAuthorizationStatus.denied) {
    return AuthDeviceAuthorizationPollResult(
      AuthDeviceAuthorizationPollStatus.denied,
      existing,
    );
  }
  if (existing.status == AuthDeviceAuthorizationStatus.consumed) {
    return AuthDeviceAuthorizationPollResult(
      AuthDeviceAuthorizationPollStatus.consumed,
      existing,
    );
  }
  final previousPoll = existing.lastPolledAt;
  if (previousPoll != null &&
      current.difference(previousPoll) < existing.interval) {
    final slowed = existing.copyWith(
      interval: existing.interval + const Duration(seconds: 5),
    );
    _records[key] = slowed;
    return AuthDeviceAuthorizationPollResult(
      AuthDeviceAuthorizationPollStatus.slowDown,
      slowed,
    );
  }
  final updated = existing.copyWith(lastPolledAt: current);
  _records[key] = updated;
  return AuthDeviceAuthorizationPollResult(
    updated.status == AuthDeviceAuthorizationStatus.approved
        ? AuthDeviceAuthorizationPollStatus.approved
        : AuthDeviceAuthorizationPollStatus.pending,
    updated,
  );
}