@@ -170,10 +170,19 @@ class PhoenixSocket {
170
170
/// Whether the underlying socket is connected of not.
171
171
bool get isConnected => _ws != null && _socketState == SocketState .connected;
172
172
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
+
173
184
void _connect (Completer <PhoenixSocket ?> completer) async {
174
- if (_ws != null &&
175
- (_socketState == SocketState .connected ||
176
- _socketState == SocketState .connecting)) {
185
+ if (_isConnectingOrConnected) {
177
186
_logger.warning (
178
187
'Calling connect() on already connected or connecting socket.' );
179
188
completer.complete (this );
@@ -182,12 +191,20 @@ class PhoenixSocket {
182
191
183
192
_shouldReconnect = true ;
184
193
185
- if (_disposed) {
186
- throw StateError ('PhoenixSocket cannot connect after being disposed.' );
187
- }
194
+ _checkNotDisposed ();
188
195
189
196
_mountPoint = await _buildMountPoint (_endpoint, _options);
190
197
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
+
191
208
// workaround to check the existing bearer token
192
209
final token = _mountPoint.queryParameters["token" ];
193
210
0 commit comments