Skip to content

Commit 3e68675

Browse files
authored
Merge pull request #4 from superlistapp/fix/dev-11330-investigate-why-mac-app-opens-two-websocket-connections-in
[DEV-11330] Double-check whether connection is possible after _buildMountPoint
2 parents 658661b + 7854342 commit 3e68675

File tree

4 files changed

+249
-89
lines changed

4 files changed

+249
-89
lines changed

lib/src/socket.dart

+23-6
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,19 @@ class PhoenixSocket {
170170
/// Whether the underlying socket is connected of not.
171171
bool get isConnected => _ws != null && _socketState == SocketState.connected;
172172

173+
bool get _isConnectingOrConnected =>
174+
_ws != null &&
175+
(_socketState == SocketState.connected ||
176+
_socketState == SocketState.connecting);
177+
178+
void _checkNotDisposed() {
179+
if (_disposed) {
180+
throw StateError('PhoenixSocket cannot connect after being disposed.');
181+
}
182+
}
183+
173184
void _connect(Completer<PhoenixSocket?> completer) async {
174-
if (_ws != null &&
175-
(_socketState == SocketState.connected ||
176-
_socketState == SocketState.connecting)) {
185+
if (_isConnectingOrConnected) {
177186
_logger.warning(
178187
'Calling connect() on already connected or connecting socket.');
179188
completer.complete(this);
@@ -182,12 +191,20 @@ class PhoenixSocket {
182191

183192
_shouldReconnect = true;
184193

185-
if (_disposed) {
186-
throw StateError('PhoenixSocket cannot connect after being disposed.');
187-
}
194+
_checkNotDisposed();
188195

189196
_mountPoint = await _buildMountPoint(_endpoint, _options);
190197

198+
// Double-check in case something changed during the async call above.
199+
if (_isConnectingOrConnected) {
200+
_logger.warning(
201+
'Calling connect() on already connected or connecting socket.');
202+
completer.complete(this);
203+
return;
204+
}
205+
206+
_checkNotDisposed();
207+
191208
// workaround to check the existing bearer token
192209
final token = _mountPoint.queryParameters["token"];
193210

test/mocks.dart

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export 'mocks.mocks.dart';
1111
MockSpec<PhoenixSocket>(),
1212
MockSpec<WebSocketChannel>(),
1313
MockSpec<WebSocketSink>(),
14+
MockSpec<PhoenixSocketOptions>(),
1415
],
1516
)
1617
class MockTest extends Mock {

0 commit comments

Comments
 (0)