Skip to content

Commit

Permalink
Refactor SpinifyImpl to improve handling of server subscriptions in S…
Browse files Browse the repository at this point in the history
…pinifySubscriptionMixin
  • Loading branch information
PlugFox committed Jun 13, 2024
1 parent 16a38e8 commit c1e58a2
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 18 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ dependencies:
## Features and Roadmap
Connection related features
- ✅ Connect to a server
- ✅ Setting client configuration
- ✅ Automatic reconnect with backoff algorithm
Expand Down
3 changes: 3 additions & 0 deletions example/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'package:spinify/spinify.dart';

void main() => Spinify();
5 changes: 3 additions & 2 deletions lib/src.old/client/config.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:meta/meta.dart';

import '../model/pubspec.yaml.g.dart';

/// Token used for authentication
Expand All @@ -17,15 +18,15 @@ typedef SpinifyToken = String;
///
/// {@category Client}
/// {@category Entity}
typedef SpinifyTokenCallback = FutureOr<SpinifyToken?> Function();
typedef SpinifyTokenCallback = Future<SpinifyToken?> Function();

/// Callback to get initial connection payload data.
///
/// If method returns null then no payload will be sent at connect time.
///
/// {@category Client}
/// {@category Entity}
typedef SpinifyConnectionPayloadCallback = FutureOr<List<int>?> Function();
typedef SpinifyConnectionPayloadCallback = Future<List<int>?> Function();

/// {@template spinify_config}
/// Spinify client common options.
Expand Down
4 changes: 2 additions & 2 deletions lib/src.old/client/spinify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ base mixin SpinifyConnectionMixin
}

@override
FutureOr<void> ready() async {
Future<void> ready() async {
try {
switch (state) {
case SpinifyState$Disconnected _:
Expand Down Expand Up @@ -767,7 +767,7 @@ base mixin SpinifyQueueMixin on SpinifyBase {
_eventQueue.push<void>('publish', () => super.publish(channel, data));

/* @override
FutureOr<void> ready() => _eventQueue.push<void>('ready', super.ready); */
Future<void> ready() => _eventQueue.push<void>('ready', super.ready); */

@override
Future<SpinifyPresence> presence(String channel) =>
Expand Down
2 changes: 1 addition & 1 deletion lib/src.old/client/spinify_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract interface class ISpinify

/// Ready resolves when client successfully connected.
/// Throws exceptions if called not in connecting or connected state.
FutureOr<void> ready();
Future<void> ready();

/// Disconnect from the server.
Future<void> disconnect([
Expand Down
2 changes: 1 addition & 1 deletion lib/src.old/subscription/client_subscription_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ base mixin SpinifyClientSubscriptionSubscribeMixin

/// Await for subscription to be ready.
@override
FutureOr<void> ready() async {
Future<void> ready() async {
try {
switch (state) {
case SpinifySubscriptionState$Unsubscribed _:
Expand Down
4 changes: 2 additions & 2 deletions lib/src.old/subscription/server_subscription_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ base mixin SpinifyServerSubscriptionReadyMixin
on SpinifyServerSubscriptionBase, SpinifyServerSubscriptionErrorsMixin {
/// Await for subscription to be ready.
@override
FutureOr<void> ready() async {
Future<void> ready() async {
try {
switch (state) {
case SpinifySubscriptionState$Unsubscribed _:
Expand Down Expand Up @@ -409,7 +409,7 @@ base mixin SpinifyServerSubscriptionQueueMixin
final SpinifyEventQueue _eventQueue = SpinifyEventQueue();

@override
FutureOr<void> ready() => _eventQueue.push<void>(
Future<void> ready() => _eventQueue.push<void>(
'ready',
super.ready,
);
Expand Down
2 changes: 1 addition & 1 deletion lib/src.old/subscription/subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ sealed class SpinifySubscription {
/// Await for subscription to be ready.
/// Ready resolves when subscription successfully subscribed.
/// Throws exceptions if called not in subscribing or subscribed state.
FutureOr<void> ready();
Future<void> ready();

/// Publish data to current Subscription channel
Future<void> publish(List<int> data);
Expand Down
4 changes: 2 additions & 2 deletions lib/src.old/subscription/subscription_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typedef SpinifySubscriptionToken = String;
/// If method returns null then subscription will be established without token.
/// {@category Subscription}
/// {@category Entity}
typedef SpinifySubscriptionTokenCallback = FutureOr<SpinifySubscriptionToken?>
typedef SpinifySubscriptionTokenCallback = Future<SpinifySubscriptionToken?>
Function();

/// Callback to set subscription payload data.
Expand All @@ -21,7 +21,7 @@ typedef SpinifySubscriptionTokenCallback = FutureOr<SpinifySubscriptionToken?>
/// {@category Subscription}
/// {@category Entity}
typedef SpinifySubscribePayloadCallback = FutureOr<List<int>?> Function();
typedef SpinifySubscribePayloadCallback = Future<List<int>?> Function();

/// {@template subscription_config}
/// Subscription common options
Expand Down
10 changes: 5 additions & 5 deletions lib/src.old/util/event_queue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class SpinifyEventQueue {
bool _isClosed = false;

/// Push it at the end of the queue.
Future<T> push<T>(String id, FutureOr<T> Function() fn) {
Future<T> push<T>(String id, Future<T> Function() fn) {
final task = SpinifyTask<T>(id, fn);
_queue.add(task);
_exec();
Expand All @@ -24,7 +24,7 @@ final class SpinifyEventQueue {
/// Mark the queue as closed.
/// The queue will be processed until it's empty.
/// But all new and current events will be rejected with [WSClientClosed].
FutureOr<void> close() async {
Future<void> close() async {
_isClosed = true;
await _processing;
}
Expand Down Expand Up @@ -64,7 +64,7 @@ final class SpinifyEventQueue {
/// Task for the [SpinifyEventQueue].
class SpinifyTask<T> {
/// Create a new instance of [SpinifyTask].
SpinifyTask(this.id, FutureOr<T> Function() fn)
SpinifyTask(this.id, Future<T> Function() fn)
: _fn = fn,
_completer = Completer<T>();

Expand All @@ -73,13 +73,13 @@ class SpinifyTask<T> {
/// Unique identifier for the task.
final String id;

final FutureOr<T> Function() _fn;
final Future<T> Function() _fn;

/// Future of the task.
Future<T> get future => _completer.future;

/// Execute the task.
FutureOr<T> call() async {
Future<T> call() async {
final result = await _fn();
if (!_completer.isCompleted) {
_completer.complete(result);
Expand Down

0 comments on commit c1e58a2

Please sign in to comment.