Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEV-11330] Double-check whether connection is possible after _buildMountPoint #4

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions lib/src/socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,19 @@ class PhoenixSocket {
/// Whether the underlying socket is connected of not.
bool get isConnected => _ws != null && _socketState == SocketState.connected;

bool get _isConnectingOrConnected =>
_ws != null &&
(_socketState == SocketState.connected ||
_socketState == SocketState.connecting);

void _checkNotDisposed() {
if (_disposed) {
throw StateError('PhoenixSocket cannot connect after being disposed.');
}
}

void _connect(Completer<PhoenixSocket?> completer) async {
if (_ws != null &&
(_socketState == SocketState.connected ||
_socketState == SocketState.connecting)) {
if (_isConnectingOrConnected) {
_logger.warning(
'Calling connect() on already connected or connecting socket.');
completer.complete(this);
Expand All @@ -182,12 +191,20 @@ class PhoenixSocket {

_shouldReconnect = true;

if (_disposed) {
throw StateError('PhoenixSocket cannot connect after being disposed.');
}
_checkNotDisposed();

_mountPoint = await _buildMountPoint(_endpoint, _options);

// Double-check in case something changed during the async call above.
if (_isConnectingOrConnected) {
_logger.warning(
'Calling connect() on already connected or connecting socket.');
completer.complete(this);
return;
}

_checkNotDisposed();

// workaround to check the existing bearer token
final token = _mountPoint.queryParameters["token"];

Expand Down
Loading