each<T> method

StreamSubscription<T> each<T>(
  1. Stream<T> stream,
  2. void fn(
    1. T item
    ), {
  3. @Deprecated('Use [uniqueEach] instead') Symbol? uniqueId,
})

Listens and iterates through stream by calling fn. The listener is disposed in the dispose function.

If you add a uniqueId, it means that whenever you call each, we will make sure that clear any listener with the same uniqueId.

Implementation

StreamSubscription<T> each<T>(Stream<T> stream, void Function(T item) fn,
    {@Deprecated('Use [uniqueEach] instead') Symbol? uniqueId}) {
  late StreamSubscription<T> ret;

  _sanity();

  if (uniqueId == null) {
    ret = ControlledStreamSubscription(
        stream.listen(fn), () => _subs.remove(ret));
    _subs.add(ret);
  } else {
    ret = ControlledStreamSubscription(
        stream.listen(fn), () => _uniqueSubs.remove(uniqueId));

    _uniqueSubs[uniqueId]?.cancel();

    _uniqueSubs[uniqueId] = ret;
  }

  return ret;
}