disconnect method

  1. @override
Future<void> disconnect({
  1. bool disconnectAllSessions = true,
})
override

Disconnects the session and pairing, if any. If there is no session, this does nothing.

Implementation

@override
Future<void> disconnect({bool disconnectAllSessions = true}) async {
  _checkInitialized();

  _status = W3MServiceStatus.initializing;
  _notify();

  if (_currentSession?.sessionService.isCoinbase == true) {
    try {
      await coinbaseService.instance.resetSession();
    } catch (_) {
      _status = W3MServiceStatus.initialized;
      _notify();
      return;
    }
  }
  if (_currentSession?.sessionService.isMagic == true) {
    await Future.delayed(Duration(milliseconds: 300));
    final disconnected = await magicService.instance.disconnect();
    if (!disconnected) {
      _status = W3MServiceStatus.initialized;
      _notify();
      return;
    }
  }

  try {
    // If we want to disconnect all sessions, loop through them and disconnect them
    if (disconnectAllSessions) {
      for (final SessionData session in _web3App.sessions.getAll()) {
        await _disconnectSession(session.pairingTopic, session.topic);
      }
    } else {
      // Disconnect the session
      await _disconnectSession(
        _currentSession?.pairingTopic,
        _currentSession?.topic,
      );
    }
    try {
      if (siweService.instance!.signOutOnDisconnect) {
        await siweService.instance!.signOut();
      }
    } catch (_) {}

    analyticsService.instance.sendEvent(DisconnectSuccessEvent());
    if (!(_currentSession?.sessionService.isWC == true)) {
      // if sessionService.isWC then _cleanSession() is being called on sessionDelete event
      return await _cleanSession();
    }
    return;
  } catch (e) {
    analyticsService.instance.sendEvent(DisconnectErrorEvent());
    _status = W3MServiceStatus.initialized;
    _notify();
  }
}