dispose method

Future<void> dispose()

Disposes of the manager and all resources.

Implementation

Future<void> dispose() async {
  if (_isDisposed) {
    return;
  }

  _isDisposed = true;

  // Cancel stream subscriptions first
  await _stateSubscription?.cancel();
  await _eventSubscription?.cancel();
  _stateSubscription = null;
  _eventSubscription = null;

  // Cancel reconnection timer
  _reconnectionTimer?.cancel();
  _reconnectionTimer = null;

  // Disconnect and dispose connection
  await _connection?.dispose();
  _connection = null;

  // Close controllers last
  if (!_stateController.isClosed) {
    await _stateController.close();
  }
  if (!_eventController.isClosed) {
    await _eventController.close();
  }
}