unsubscribeByTopicAndListener method

Future<void> unsubscribeByTopicAndListener(
  1. String topic,
  2. SubscriptionFunc listener
)

Unsubscribe from all subscriptions matching the specified topic and listener function.

This method is no-op if there are no active subscription with the specified topic and listener.

The related SSE connection will be autoclosed if after the unsubscribe operation there are no active subscriptions left.

Implementation

Future<void> unsubscribeByTopicAndListener(
  String topic,
  SubscriptionFunc listener,
) async {
  final subs = _getSubscriptionsByTopic(topic);

  for (final key in subs.keys) {
    if (_subscriptions[key]?.isEmpty ?? true) {
      continue; // nothing to unsubscribe from
    }

    _subscriptions[key]?.removeWhere((fn) => fn == listener);
  }

  return _submitSubscriptions();
}