Skip to content

Commit

Permalink
Refactor SpinifyImpl to use Future instead of FutureOr in callback types
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Jun 13, 2024
1 parent 09401a6 commit 9e122d8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 60 deletions.
4 changes: 2 additions & 2 deletions lib/src/model/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,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();

/// Log level for logger
extension type const SpinifyLogLevel._(int level) implements int {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/model/subscription_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,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 @@ -22,7 +22,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
107 changes: 52 additions & 55 deletions lib/src/subscription_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,52 +114,78 @@ abstract base class SpinifySubscriptionBase implements SpinifySubscription {
assert(_state.isUnsubscribed,
'Subscription "$channel" is not unsubscribed before closing');
}
}

@internal
final class SpinifyClientSubscriptionImpl extends SpinifySubscriptionBase
implements SpinifyClientSubscription {
SpinifyClientSubscriptionImpl({
required super.client,
required super.channel,
required this.config,
}) : super(
recoverable: config.recoverable,
epoch: config.since?.epoch ?? '',
offset: config.since?.offset ?? fixnum.Int64.ZERO,
);

@override
final SpinifySubscriptionConfig config;
Future<void> ready() async {
if (_state.isSubscribed) return;
if (_stateController.isClosed)
throw SpinifySubscriptionException(
channel: channel,
message: 'Subscription is closed permanently',
);
if (!_state.isSubscribing)
throw SpinifySubscriptionException(
channel: channel,
message: 'Subscription is not in subscribing state',
);
final state = await _stateController.stream
.firstWhere((state) => !state.isSubscribing);
if (!state.isSubscribed)
throw SpinifySubscriptionException(
channel: channel,
message: 'Subscription failed to subscribe',
);
}

@override
Future<SpinifyHistory> history({
int? limit,
SpinifyStreamPosition? since,
bool? reverse,
}) {
throw UnimplementedError();
}) async {
await ready().timeout(_client.config.timeout);
return _client.history(
channel,
limit: limit,
since: since,
reverse: reverse,
);
}

@override
Future<SpinifyPresence> presence() {
throw UnimplementedError();
Future<SpinifyPresence> presence() async {
await ready().timeout(_client.config.timeout);
return _client.presence(channel);
}

@override
Future<SpinifyPresenceStats> presenceStats() {
throw UnimplementedError();
Future<SpinifyPresenceStats> presenceStats() async {
await ready().timeout(_client.config.timeout);
return _client.presenceStats(channel);
}

@override
Future<void> publish(List<int> data) {
throw UnimplementedError();
Future<void> publish(List<int> data) async {
await ready().timeout(_client.config.timeout);
return _client.publish(channel, data);
}
}

@internal
final class SpinifyClientSubscriptionImpl extends SpinifySubscriptionBase
implements SpinifyClientSubscription {
SpinifyClientSubscriptionImpl({
required super.client,
required super.channel,
required this.config,
}) : super(
recoverable: config.recoverable,
epoch: config.since?.epoch ?? '',
offset: config.since?.offset ?? fixnum.Int64.ZERO,
);

@override
FutureOr<void> ready() {
throw UnimplementedError();
}
final SpinifySubscriptionConfig config;

@override
Future<void> subscribe() {
Expand Down Expand Up @@ -189,33 +215,4 @@ final class SpinifyServerSubscriptionImpl extends SpinifySubscriptionBase
@override
SpinifyChannelEvents<SpinifyChannelEvent> get stream =>
_client.stream.filter(channel: channel);

@override
Future<SpinifyHistory> history({
int? limit,
SpinifyStreamPosition? since,
bool? reverse,
}) {
throw UnimplementedError();
}

@override
Future<SpinifyPresence> presence() {
throw UnimplementedError();
}

@override
Future<SpinifyPresenceStats> presenceStats() {
throw UnimplementedError();
}

@override
Future<void> publish(List<int> data) {
throw UnimplementedError();
}

@override
FutureOr<void> ready() {
throw UnimplementedError();
}
}
2 changes: 1 addition & 1 deletion lib/src/subscription_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ abstract interface 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

0 comments on commit 9e122d8

Please sign in to comment.