getOrCreateSubscriptionController method

StreamController<SubscriptionMessage> getOrCreateSubscriptionController(
  1. String subscription, [
  2. FutureOr<void> onCancel(
    1. String subscription
    )?
])

Implementation

StreamController<SubscriptionMessage> getOrCreateSubscriptionController(
    String subscription,
    [FutureOr<void> Function(String subscription)? onCancel]) {
  if (subscriptions.containsKey(subscription)) {
    return subscriptions[subscription]!;
  } else {
    final controller =
        StreamController<SubscriptionMessage>.broadcast(onCancel: () async {
      if (onCancel != null) {
        await onCancel(subscription);
      }
      if (subscriptions.containsKey(subscription)) {
        subscriptions.remove(subscription);
      }
    });
    subscriptions[subscription] = controller;
    return controller;
  }
}