unsubscribe method
Unsubscribe from all subscription listeners with the specified topic.
If topic
is not set, then this method will unsubscribe
from all active subscriptions.
This method is no-op if there are no active subscriptions.
The related sse connection will be autoclosed if after the unsubscribe operation there are no active subscriptions left.
Implementation
Future<void> unsubscribe([String topic = ""]) async {
var needToSubmit = false;
if (topic.isEmpty) {
// remove all subscriptions
_subscriptions.clear();
} else {
final subs = _getSubscriptionsByTopic(topic);
for (final key in subs.keys) {
_subscriptions.remove(key);
needToSubmit = true;
}
}
// no other subscriptions -> close the sse connection
if (!_hasNonEmptyTopic()) {
return _disconnect();
}
// otherwise - notify the server about the subscription changes
if (_clientId.isNotEmpty && needToSubmit) {
return _submitSubscriptions();
}
}