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 18, 2024
1 parent ef8f9fb commit 4c9b230
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 69 deletions.
8 changes: 7 additions & 1 deletion lib/spinify.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
library spinify;

export 'package:fixnum/fixnum.dart';

export 'src/model/channel_push.dart';
export 'src/model/client_info.dart';
export 'src/model/command.dart';
export 'src/model/config.dart';
export 'src/model/exception.dart';
export 'src/model/history.dart';
export 'src/model/jwt.dart';
export 'src/model/metric.dart' show SpinifyMetrics;
export 'src/model/metric.dart';
export 'src/model/presence_stats.dart';
export 'src/model/pushes_stream.dart';
export 'src/model/reply.dart';
export 'src/model/spinify_interface.dart';
export 'src/model/state.dart';
export 'src/model/states_stream.dart';
export 'src/model/stream_position.dart';
export 'src/model/subscription.dart';
export 'src/model/subscription_config.dart';
export 'src/model/subscription_state.dart';
export 'src/model/subscription_states_stream.dart';
export 'src/model/transport_interface.dart';
export 'src/spinify_impl.dart' show Spinify;
export 'src/transport_fake.dart';
7 changes: 0 additions & 7 deletions lib/spinify_developer.dart

This file was deleted.

46 changes: 39 additions & 7 deletions lib/src/model/metric.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ sealed class SpinifyMetrics implements Comparable<SpinifyMetrics> {
/// The current state of the client.
abstract final SpinifyState state;

/*
/// The total number of messages & size of bytes sent.
final ({BigInt count, BigInt size}) transferred;
/// The total number of bytes sent.
abstract final BigInt bytesSent;

/// The total number of bytes received.
abstract final BigInt bytesReceived;

/// The total number of messages sent.
abstract final BigInt messagesSent;

/// The total number of messages & size of bytes received.
final ({BigInt count, BigInt size}) received;
/// The total number of messages received.
abstract final BigInt messagesReceived;

/*
/// The number of subscriptions.
final ({
SpinifySubscriptionCount client,
Expand Down Expand Up @@ -97,9 +103,10 @@ sealed class SpinifyMetrics implements Comparable<SpinifyMetrics> {
String toString() => 'SpinifyMetrics{}';
}

@internal
/// {@macro metrics}
@immutable
final class SpinifyMetrics$Immutable extends SpinifyMetrics {
/// {@macro metrics}
const SpinifyMetrics$Immutable();

@override
Expand Down Expand Up @@ -134,10 +141,23 @@ final class SpinifyMetrics$Immutable extends SpinifyMetrics {

@override
DateTime? get lastDisconnectAt => throw UnimplementedError();

@override
BigInt get bytesReceived => throw UnimplementedError();

@override
BigInt get bytesSent => throw UnimplementedError();

@override
BigInt get messagesReceived => throw UnimplementedError();

@override
BigInt get messagesSent => throw UnimplementedError();
}

@internal
/// {@macro metrics}
final class SpinifyMetrics$Mutable extends SpinifyMetrics {
/// {@macro metrics}
SpinifyMetrics$Mutable();

@override
Expand Down Expand Up @@ -173,5 +193,17 @@ final class SpinifyMetrics$Mutable extends SpinifyMetrics {
@override
DateTime? lastDisconnectAt;

@override
BigInt bytesReceived = BigInt.zero;

@override
BigInt bytesSent = BigInt.zero;

@override
BigInt messagesReceived = BigInt.zero;

@override
BigInt messagesSent = BigInt.zero;

SpinifyMetrics$Immutable freeze() => const SpinifyMetrics$Immutable();
}
26 changes: 14 additions & 12 deletions lib/src/model/transport_interface.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import 'command.dart';
import 'config.dart';
import 'metric.dart';
import 'reply.dart';

/// Create a Spinify transport
/// (e.g. WebSocket or gRPC with JSON or Protocol Buffers).
typedef SpinifyTransportBuilder = Future<ISpinifyTransport> Function(
typedef SpinifyTransportBuilder = Future<ISpinifyTransport> Function({
/// URL for the connection
String url,
required String url,

/// Spinify client configuration
SpinifyConfig config,
);
required SpinifyConfig config,

/// Metrics
required SpinifyMetrics$Mutable metrics,

/// Callback for reply messages
required void Function(SpinifyReply reply) onReply,

/// Callback for disconnect event
required void Function() onDisconnect,
});

/// Spinify transport interface.
abstract interface class ISpinifyTransport {
/// Send command to the server.
Future<void> send(SpinifyCommand command);

/// Set handler for [SpinifyReply] messages.
// ignore: avoid_setters_without_getters
set onReply(void Function(SpinifyReply reply) handler);

/// Set handler for connection close event.
// ignore: avoid_setters_without_getters
set onDisconnect(void Function() handler);

/// Disconnect from the server.
/// Client if not needed anymore.
Future<void> disconnect([int? code, String? reason]);
Expand Down
12 changes: 9 additions & 3 deletions lib/src/spinify_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,15 @@ base mixin SpinifyConnectionMixin
_setState(SpinifyState$Connecting(url: _metrics.reconnectUrl = url));

// Create new transport.
_transport = await _createTransport(url, config)
..onReply = _onReply
..onDisconnect = () => _onDisconnected().ignore();
_transport = await _createTransport(
url: url,
config: config,
metrics: _metrics,
onReply: _onReply,
onDisconnect: _onDisconnected,
);
// ..onReply = _onReply
// ..onDisconnect = () => _onDisconnected().ignore();

// Prepare connect request.
final SpinifyConnectRequest request;
Expand Down
38 changes: 29 additions & 9 deletions lib/src/transport_fake.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
// ignore_for_file: avoid_setters_without_getters

import 'dart:async';

import 'package:fixnum/fixnum.dart';

import 'model/command.dart';
import 'model/config.dart';
import 'model/metric.dart';
import 'model/reply.dart';
import 'model/transport_interface.dart';

/// Create a fake Spinify transport.
Future<ISpinifyTransport> $createFakeSpinifyTransport(
String url,
Map<String, String> headers,
) async {
final transport = SpinifyTransportFake();
Future<ISpinifyTransport> $createFakeSpinifyTransport({
/// URL for the connection
required String url,

/// Spinify client configuration
required SpinifyConfig config,

/// Metrics
required SpinifyMetrics$Mutable metrics,

/// Callback for reply messages
required void Function(SpinifyReply reply) onReply,

/// Callback for disconnect event
required void Function() onDisconnect,
}) async {
final transport = SpinifyTransportFake()
..metrics = metrics
..onReply = onReply
..onDisconnect = onDisconnect;
await transport._connect(url);
return transport;
}
Expand Down Expand Up @@ -165,13 +184,14 @@ class SpinifyTransportFake implements ISpinifyTransport {
},
);

@override
// ignore: avoid_setters_without_getters
/// Metrics
late SpinifyMetrics$Mutable metrics;

/// Callback for reply messages
set onReply(void Function(SpinifyReply reply) handler) => _onReply = handler;
void Function(SpinifyReply reply)? _onReply;

@override
// ignore: avoid_setters_without_getters
/// Callback for disconnect event
set onDisconnect(void Function() handler) => _onDisconnect = handler;
void Function()? _onDisconnect;

Expand Down
22 changes: 18 additions & 4 deletions lib/src/transport_ws_pb_js.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import 'package:meta/meta.dart';

import 'model/config.dart';
import 'model/metric.dart';
import 'model/reply.dart';
import 'model/transport_interface.dart';

/// Create a WebSocket Protocol Buffers transport.
@internal
Future<ISpinifyTransport> $create$WS$PB$Transport(
String url,
SpinifyConfig config,
) =>
Future<ISpinifyTransport> $create$WS$PB$Transport({
/// URL for the connection
required String url,

/// Spinify client configuration
required SpinifyConfig config,

/// Metrics
required SpinifyMetrics$Mutable metrics,

/// Callback for reply messages
required void Function(SpinifyReply reply) onReply,

/// Callback for disconnect event
required void Function() onDisconnect,
}) =>
throw UnimplementedError();
22 changes: 18 additions & 4 deletions lib/src/transport_ws_pb_stub.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import 'package:meta/meta.dart';

import 'model/config.dart';
import 'model/metric.dart';
import 'model/reply.dart';
import 'model/transport_interface.dart';

/// Create a WebSocket Protocol Buffers transport.
@internal
Future<ISpinifyTransport> $create$WS$PB$Transport(
String url,
SpinifyConfig config,
) =>
Future<ISpinifyTransport> $create$WS$PB$Transport({
/// URL for the connection
required String url,

/// Spinify client configuration
required SpinifyConfig config,

/// Metrics
required SpinifyMetrics$Mutable metrics,

/// Callback for reply messages
required void Function(SpinifyReply reply) onReply,

/// Callback for disconnect event
required void Function() onDisconnect,
}) =>
throw UnimplementedError();
Loading

0 comments on commit 4c9b230

Please sign in to comment.