Skip to content

Commit

Permalink
Refactor SpinifyCommandMixin error handling and connection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed May 12, 2024
1 parent e054dac commit 476a70e
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/src/spinify_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ base mixin SpinifyCommandMixin on SpinifyBase {
assert(command.id > -1, 'Command ID should be greater or equal to 0');
assert(_replies[command.id] == null, 'Command ID should be unique');
assert(_transport != null, 'Transport is not connected');
assert(state.isConnected, 'State is not connected');
assert(!state.isClosed, 'State is closed');
final completer = _replies[command.id] = Completer<T>();
await _sendCommandAsync(command);
return await completer.future.timeout(config.timeout);
Expand All @@ -135,7 +135,7 @@ base mixin SpinifyCommandMixin on SpinifyBase {
Future<void> _sendCommandAsync(SpinifyCommand command) async {
assert(command.id > -1, 'Command ID should be greater or equal to 0');
assert(_transport != null, 'Transport is not connected');
assert(state.isConnected, 'State is not connected');
assert(!state.isClosed, 'State is closed');
await _transport?.send(command);
}

Expand Down Expand Up @@ -166,9 +166,11 @@ base mixin SpinifyConnectionMixin

@override
Future<void> connect(String url) async {
if (state.url == url) return;
final completer = _readyCompleter ??= Completer<void>();
await disconnect();
try {
// Disconnect previous transport if exists.
_transport?.disconnect(1000, 'Reconnecting').ignore();
_setState(SpinifyState$Connecting(url: url));

// Create new transport.
_transport = await _createTransport(url, config.headers)
Expand All @@ -181,7 +183,6 @@ base mixin SpinifyConnectionMixin
final reply = await _sendCommand<SpinifyConnectResult>(request);
_setState(SpinifyState$Connected(
url: url,
timestamp: DateTime.now(),
client: reply.client,
version: reply.version,
expires: reply.expires,
Expand All @@ -194,10 +195,11 @@ base mixin SpinifyConnectionMixin
));

// Notify ready.
_readyCompleter?.complete();
if (!completer.isCompleted) completer.complete();
_readyCompleter = null;
} on Object catch (error, stackTrace) {
_readyCompleter?.completeError(error, stackTrace);
if (!completer.isCompleted) completer.completeError(error, stackTrace);
_readyCompleter = null;
rethrow;
}
}
Expand Down

0 comments on commit 476a70e

Please sign in to comment.