connect method

Future<void> connect()

Implementation

Future<void> connect() async {
  if (!config.retryInitialConnect) {
    return _connectOnce();
  }
  var attempt = 0;
  while (true) {
    attempt++;
    try {
      return await _connectOnce();
    } on SpacetimeDbAuthException {
      rethrow;
    } catch (e) {
      if (attempt >= config.maxReconnectAttempts) {
        SdkLogger.e(
          'Initial connect failed after $attempt attempt(s); giving up',
        );
        _updateState(
          const FatalError(message: 'Max initial connect attempts reached'),
        );
        rethrow;
      }
      _reconnectAttempts = attempt - 1;
      final delay = _getReconnectDelay();
      SdkLogger.w(
        'Initial connect attempt $attempt failed ($e); '
        'retrying in ${delay.inSeconds}s',
      );
      _updateState(Reconnecting(attempt: attempt, nextDelay: delay));
      await Future<void>.delayed(delay);
      if (_state is FatalError) rethrow;
    }
  }
}