Skip to content

Commit

Permalink
Refactor code to remove @internal annotations and update imports
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed May 3, 2024
1 parent 6881c4a commit a9ebfba
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 24 deletions.
1 change: 1 addition & 0 deletions lib/spinify.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
library spinify;

export 'src/model/state.dart';
export 'src/spinify_impl.dart' show Spinify;
16 changes: 8 additions & 8 deletions lib/src/event_bus.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:collection';
import 'dart:developer' as dev;

//import 'dart:developer' as dev;

import 'package:meta/meta.dart';

Expand Down Expand Up @@ -115,27 +116,26 @@ final class SpinifyEventBus$Bucket {
Future<void> _processEvents() async {
if (_processing) return;
_processing = true;
dev.Timeline.instantSync('$_debugLabel _processEvents() start');
//dev.Timeline.instantSync('$_debugLabel _processEvents() start');
log.fine('$_debugLabel start processing events');
while (_queue.isNotEmpty) {
var task = _queue.removeFirst();
final event = task.event;
log.fine('$_debugLabel processing "$event"');
try {
await _notifySubscribers(event, task.data);
task.completer.complete(null);
//dev.Timeline.instantSync('$_debugLabel event "$event" processed');
//log.fine('$_debugLabel event "$event" processed');
//dev.Timeline.instantSync('$_debugLabel $event');
log.fine('$_debugLabel $event');
} on Object catch (error, stackTrace) {
final reason = '$_debugLabel error processing event "$event"';
dev.Timeline.instantSync(reason);
final reason = '$_debugLabel $event error';
//dev.Timeline.instantSync(reason);
log.warning(error, stackTrace, reason);
task.completer.completeError(error, stackTrace);
}
}
_processing = false;
log.fine('$_debugLabel end processing events');
dev.Timeline.instantSync('$_debugLabel _processEvents() end');
//dev.Timeline.instantSync('$_debugLabel _processEvents() end');
}

/// Notify the subscribers
Expand Down
14 changes: 14 additions & 0 deletions lib/src/model/events.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:meta/meta.dart';

/// Spinify client events.
@internal
abstract interface class ClientEvents {
static const String prefix = 'client';
static const String init = '${prefix}_init';
static const String close = '${prefix}_close';
static const String connecting = '${prefix}_connecting';
static const String connected = '${prefix}_connected';
static const String disconnecting = '${prefix}_disconnecting';
static const String disconnected = '${prefix}_disconnected';
static const String stateChanged = '${prefix}_state_changed';
}
86 changes: 70 additions & 16 deletions lib/src/spinify_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:meta/meta.dart';
import '../src.old/subscription/subscription.dart';
import 'event_bus.dart';
import 'model/config.dart';
import 'model/events.dart';
import 'model/history.dart';
import 'model/metrics.dart';
import 'model/presence.dart';
Expand All @@ -16,20 +17,12 @@ import 'model/stream_position.dart';
import 'model/subscription_config.dart';
import 'spinify_interface.dart';

/// Spinify client events.
@internal
abstract interface class ClientEvents {
static const String prefix = 'client';
static const String init = '${prefix}_init';
static const String close = '${prefix}_close';
}

/// Base class for Spinify client.
abstract base class SpinifyBase implements ISpinify {
/// Create a new Spinify client.
SpinifyBase(this.config) : id = _idCounter++ {
_bucket = SpinifyEventBus.instance.registerClient(this);
_init();
_initClient();
}

/// Unique client ID counter for Spinify clients.
Expand All @@ -51,14 +44,14 @@ abstract base class SpinifyBase implements ISpinify {
late final SpinifyEventBus$Bucket _bucket;

@mustCallSuper
void _init() {
void _initClient() {
_bucket
..pushEvent(ClientEvents.init)
..subscribe(ClientEvents.close, _onClose);
..subscribe(ClientEvents.close, _spinifyBase$OnClose);
}

@mustCallSuper
Future<void> _onClose(_) async {
Future<void> _spinifyBase$OnClose(_) async {
_isClosed = true;
SpinifyEventBus.instance.unregisterClient(this);
}
Expand All @@ -83,14 +76,60 @@ abstract base class SpinifyBase implements ISpinify {
String toString() => 'Spinify{}';
}

/// Base mixin for Spinify client state management.
base mixin SpinifyStateMixin on SpinifyBase {
@override
SpinifyState get state => throw UnimplementedError();
SpinifyState get state => _state;
SpinifyState _state = SpinifyState$Disconnected();

@override
late final SpinifyStatesStream states =
SpinifyStatesStream(_statesController.stream);

@nonVirtual
final StreamController<SpinifyState> _statesController =
StreamController<SpinifyState>.broadcast();

@override
SpinifyStatesStream get states => throw UnimplementedError();
@mustCallSuper
void _initClient() {
_bucket
..subscribe(ClientEvents.disconnected, _spinifyStateMixin$OnDisconnected)
..subscribe(ClientEvents.connecting, _spinifyStateMixin$OnConnecting)
..subscribe(ClientEvents.connected, _spinifyStateMixin$OnConnectedState);
super._initClient();
}

@nonVirtual
void _changeState(SpinifyState state) {
_statesController.add(_state = state);
_bucket.pushEvent(ClientEvents.stateChanged, state);
}

@mustCallSuper
Future<void> _spinifyStateMixin$OnDisconnected(Object? data) async {
_changeState(data as SpinifyState$Disconnected);
}

@mustCallSuper
Future<void> _spinifyStateMixin$OnConnecting(Object? data) async {
_changeState(data as SpinifyState$Connecting);
}

@mustCallSuper
Future<void> _spinifyStateMixin$OnConnectedState(Object? data) async {
_changeState(data as SpinifyState$Connected);
}

@override
@mustCallSuper
Future<void> close() async {
await super.close();
_changeState(SpinifyState$Closed());
}
}

/// Base mixin for Spinify client connection management (connect & disconnect).
base mixin SpinifyConnectionMixin on SpinifyBase {
@override
Future<void> connect(String url) {
Expand All @@ -103,18 +142,27 @@ base mixin SpinifyConnectionMixin on SpinifyBase {
}

@override
Future<void> disconnect([int code = 0, String reason = 'Disconnect called']) {
throw UnimplementedError();
Future<void> disconnect(
[int code = 0, String reason = 'Disconnect called']) async {
// ...
}

@override
Future<void> close() async {
await disconnect();
await super.close();
}
}

/// Base mixin for Spinify client message sending.
base mixin SpinifySendMixin on SpinifyBase {
@override
Future<void> send(List<int> data) {
throw UnimplementedError();
}
}

/// Base mixin for Spinify client subscription management.
base mixin SpinifyClientSubscriptionMixin on SpinifyBase {
@override
({
Expand All @@ -139,15 +187,18 @@ base mixin SpinifyClientSubscriptionMixin on SpinifyBase {
}
}

/// Base mixin for Spinify server subscription management.
base mixin SpinifyServerSubscriptionMixin on SpinifyBase {}

/// Base mixin for Spinify client publications management.
base mixin SpinifyPublicationsMixin on SpinifyBase {
@override
Future<void> publish(String channel, List<int> data) {
throw UnimplementedError();
}
}

/// Base mixin for Spinify client presence management.
base mixin SpinifyPresenceMixin on SpinifyBase {
@override
Future<SpinifyPresence> presence(String channel) {
Expand All @@ -160,6 +211,7 @@ base mixin SpinifyPresenceMixin on SpinifyBase {
}
}

/// Base mixin for Spinify client history management.
base mixin SpinifyHistoryMixin on SpinifyBase {
@override
Future<SpinifyHistory> history(String channel,
Expand All @@ -168,13 +220,15 @@ base mixin SpinifyHistoryMixin on SpinifyBase {
}
}

/// Base mixin for Spinify client RPC management.
base mixin SpinifyRPCMixin on SpinifyBase {
@override
Future<List<int>> rpc(String method, List<int> data) {
throw UnimplementedError();
}
}

/// Base mixin for Spinify client metrics management.
base mixin SpinifyMetricsMixin on SpinifyBase {
@override
SpinifyMetrics get metrics => throw UnimplementedError();
Expand Down
2 changes: 2 additions & 0 deletions test/unit/spinify_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ void main() {
test('Create_and_close_client', () async {
final client = Spinify();
expect(client.isClosed, isFalse);
expect(client.state, isA<SpinifyState$Disconnected>());
await client.close();
expect(client.isClosed, isTrue);
expect(client.state, isA<SpinifyState$Closed>());
});

test('Create_and_close_multiple_clients', () async {
Expand Down

0 comments on commit a9ebfba

Please sign in to comment.