Skip to content

Commit

Permalink
Refactor SpinifyImpl to handle nullable data in subscription state
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Jun 13, 2024
1 parent c6a1b86 commit e5b1593
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/src/model/channel_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ final class SpinifySubscribe extends SpinifyChannelEvent {
final bool positioned;

/// Data attached to subscription.
final List<int> data;
final List<int>? data;

@override
bool get isConnect => false;
Expand Down Expand Up @@ -519,7 +519,7 @@ final class SpinifyConnect extends SpinifyChannelEvent {
final String? node;

/// Payload of connected push.
final List<int> data;
final List<int>? data;

@override
bool get isConnect => true;
Expand Down
5 changes: 5 additions & 0 deletions lib/src/model/subscription_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ sealed class SpinifySubscriptionState extends _$SpinifySubscriptionStateBase {
/// Subscribed
/// {@macro subscription_state}
factory SpinifySubscriptionState.subscribed({
required List<int>? data,
DateTime? timestamp,
}) = SpinifySubscriptionState$Subscribed;
}
Expand Down Expand Up @@ -142,9 +143,13 @@ final class SpinifySubscriptionState$Subscribed
extends SpinifySubscriptionState {
/// {@macro subscription_state}
SpinifySubscriptionState$Subscribed({
required this.data,
DateTime? timestamp,
}) : super(timestamp: timestamp ?? DateTime.now());

/// Data
final List<int>? data;

@override
String get type => 'subscribed';

Expand Down
16 changes: 8 additions & 8 deletions lib/src/protobuf/protobuf_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ final class ProtobufReplyDecoder extends Converter<pb.Reply, SpinifyReply> {
event = SpinifyPublication(
timestamp: DateTime.now(),
channel: channel,
data: push.pub.data,
data: push.pub.hasData() ? push.pub.data : const <int>[],
info: _decodeClientInfo(push.pub.info),
offset: push.pub.offset,
offset: push.pub.hasOffset() ? push.pub.offset : null,
tags: push.pub.tags,
);
} else if (push.hasJoin()) {
Expand Down Expand Up @@ -254,7 +254,7 @@ final class ProtobufReplyDecoder extends Converter<pb.Reply, SpinifyReply> {
timestamp: DateTime.now(),
channel: channel,
recoverable: push.subscribe.recoverable,
data: push.subscribe.data,
data: push.subscribe.hasData() ? push.subscribe.data : null,
positioned: push.subscribe.positioned,
since: (
offset: push.subscribe.offset,
Expand Down Expand Up @@ -289,7 +289,7 @@ final class ProtobufReplyDecoder extends Converter<pb.Reply, SpinifyReply> {
version: push.connect.version,
expires: expBool,
ttl: ttlDT,
data: push.connect.data,
data: push.connect.hasData() ? push.connect.data : null,
node: push.connect.node,
pingInterval: pingInterval,
sendPong: push.connect.pong == true,
Expand Down Expand Up @@ -390,7 +390,7 @@ final class ProtobufReplyDecoder extends Converter<pb.Reply, SpinifyReply> {
// have the "channel" field - I should fill it in manually
// by copying the channel from the SubscribeRequest
channel: '',
data: pub.data,
data: pub.hasData() ? pub.data : const <int>[],
info: _decodeClientInfo(pub.info),
offset: pub.offset,
tags: pub.tags,
Expand Down Expand Up @@ -435,7 +435,7 @@ final class ProtobufReplyDecoder extends Converter<pb.Reply, SpinifyReply> {
version: connect.version,
expires: expBool,
ttl: ttlDT,
data: connect.data,
data: connect.hasData() ? connect.data : null,
subs: switch (connect.subs) {
Map<String, pb.SubscribeResult> map when map.isNotEmpty =>
<String, SpinifySubscribeResult>{
Expand Down Expand Up @@ -494,7 +494,7 @@ final class ProtobufReplyDecoder extends Converter<pb.Reply, SpinifyReply> {
// have the "channel" field - I should fill it in manually
// by copying the channel from the SubscribeRequest
channel: '',
data: pub.data,
data: pub.hasData() ? pub.data : const <int>[],
info: _decodeClientInfo(pub.info),
offset: pub.offset,
tags: pub.tags,
Expand All @@ -506,7 +506,7 @@ final class ProtobufReplyDecoder extends Converter<pb.Reply, SpinifyReply> {
return SpinifyRPCResult(
id: id,
timestamp: now,
data: rpc.data,
data: rpc.hasData() ? rpc.data : const <int>[],
);
} else if (reply.hasRefresh()) {
final refresh = reply.refresh;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/spinify_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ base mixin SpinifySubscriptionMixin on SpinifyBase, SpinifyCommandMixin {
..epoch = event.since.epoch
..offset = event.since.offset
..onEvent(event)
..setState(SpinifySubscriptionState.subscribed());
..setState(SpinifySubscriptionState.subscribed(data: event.data));
} else if (event is SpinifyUnsubscribe) {
// Remove server subscription from the registry on unsubscribe event.
_serverSubscriptionRegistry.remove(event.channel)
Expand Down Expand Up @@ -492,7 +492,7 @@ base mixin SpinifySubscriptionMixin on SpinifyBase, SpinifyCommandMixin {
..recoverable = value.recoverable
..epoch = value.since.epoch
..offset = value.since.offset
..setState(SpinifySubscriptionState.subscribed());
..setState(SpinifySubscriptionState.subscribed(data: value.data));
// Notify about new publications.
for (var publication in value.publications) {
// If publication has wrong channel, fix it.
Expand Down

0 comments on commit e5b1593

Please sign in to comment.