addAndSetupOnCancel method

void addAndSetupOnCancel(
  1. StreamController<T> listener, {
  2. void onEmpty()?,
})

Implementation

void addAndSetupOnCancel(
  StreamController<T> listener, {
  void Function()? onEmpty,
}) {
  listener.onCancel = () {
    try {
      remove(listener);
    } catch (_) {
      // ignore
    }
    if (isEmpty) {
      try {
        onEmpty?.call();
      } catch (_) {
        // ignore
      }
    }
  };
  add(listener);
}