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 14, 2024
1 parent 5bebdfe commit 1d0494d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
3 changes: 1 addition & 2 deletions lib/src/model/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,9 @@ final class SpinifyPingRequest extends SpinifyCommand {
final class SpinifySendRequest extends SpinifyCommand {
/// {@macro command}
const SpinifySendRequest({
required super.id,
required super.timestamp,
required this.data,
});
}) : super(id: 0);

@override
String get type => 'SendRequest';
Expand Down
9 changes: 6 additions & 3 deletions lib/src/spinify_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ base mixin SpinifyCommandMixin on SpinifyBase {

@override
Future<void> send(List<int> data) => _sendCommandAsync(SpinifySendRequest(
id: _getNextCommandId(),
timestamp: DateTime.now(),
data: data,
));
Expand Down Expand Up @@ -311,7 +310,9 @@ base mixin SpinifyConnectionMixin
Future<void> connect(String url) async {
if (state.url == url) return;
final completer = _readyCompleter ??= Completer<void>();
await disconnect();
try {
await disconnect();
} on Object {/* ignore */}
try {
_setState(SpinifyState$Connecting(url: url));
_reconnectUrl = url;
Expand Down Expand Up @@ -489,6 +490,8 @@ base mixin SpinifyConnectionMixin
@override
Future<void> disconnect() async {
_reconnectUrl = null;
// TODO(plugfox): tear down reconnect timer
// tearDownReconnectTimer();
if (state.isDisconnected) return Future.value();
await _transport?.disconnect(1000, 'Client disconnecting');
await _onDisconnected();
Expand All @@ -498,7 +501,7 @@ base mixin SpinifyConnectionMixin
Future<void> _onDisconnected() async {
_refreshTimer?.cancel();
_transport = null;
// TODO(plugfox): reconnect
// TODO(plugfox): setup reconnect if reconnectUrl is not null
await super._onDisconnected();
}

Expand Down

0 comments on commit 1d0494d

Please sign in to comment.