Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/realtime_client/lib/src/realtime_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,13 @@ class RealtimeClient {
bool get isConnected => connectionState == SocketState.open;

/// Removes a subscription from the socket.
///
/// Matches on identity rather than on [RealtimeChannel.joinRef], which is
/// the empty string until a channel is subscribed and is therefore shared
/// by every channel that has not joined yet.
@internal
void remove(RealtimeChannel channel) {
channels = channels.where((c) => c.joinRef != channel.joinRef).toList();
channels = channels.where((c) => !identical(c, channel)).toList();
if (channels.isEmpty) {
log('transport', 'no channels remaining, scheduling disconnect');
_schedulePendingDisconnect();
Expand Down
26 changes: 26 additions & 0 deletions packages/realtime_client/test/socket_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,32 @@ void main() {
final foundChannel = mockedSocket.channels[0];
expect(foundChannel, channel2);
});

test('keeps the other channels when none of them have joined', () {
// Channels that have never subscribed all share the empty join ref, so
// matching on it removed every one of them at once.
final socket = RealtimeClient(socketEndpoint);
final channel1 = socket.channel('topic-1');
socket.channel('topic-2');
socket.channel('topic-3');

socket.remove(channel1);

expect(
socket.channels.map((channel) => channel.topic),
['realtime:topic-2', 'realtime:topic-3'],
);
});

test('removes only the given channel when topics are duplicated', () {
final socket = RealtimeClient(socketEndpoint);
final channel1 = socket.channel('topic');
final channel2 = socket.channel('topic');

socket.remove(channel1);

expect(socket.channels, [channel2]);
});
});

group('deferred disconnect', () {
Expand Down
Loading