unsubscribeByPrefix method
Unsubscribe from all subscription listeners starting with the specified topic prefix.
This method is no-op if there are no active subscriptions with the specified topic prefix.
The related sse connection will be autoclosed if after the unsubscribe operation there are no active subscriptions left.
Implementation
Future<void> unsubscribeByPrefix(String topicPrefix) async {
final beforeLength = _subscriptions.length;
// remove matching subscriptions
_subscriptions.removeWhere((key, func) {
// "?" so that it can be used as end delimiter for the prefix
return "$key?".startsWith(topicPrefix);
});
// no changes
if (beforeLength == _subscriptions.length) {
return;
}
// no other subscriptions -> close the sse connection
if (!_hasNonEmptyTopic()) {
return _disconnect();
}
// otherwise - notify the server about the subscription changes
if (_clientId.isNotEmpty) {
return _submitSubscriptions();
}
}