mapPerEvent<TOutput> method

Stream<TOutput> mapPerEvent<TOutput>(
  1. _Converter<TInput, TOutput> convert
)

Like map, but calls convert once per event, and not per subscription.

Implementation

Stream<TOutput> mapPerEvent<TOutput>(_Converter<TInput, TOutput> convert) {
  late StreamController<TOutput> controller;
  late StreamSubscription<TInput> subscription;

  void onListen() {
    subscription = listen((event) => controller.add(convert(event)),
        onError: controller.addError, onDone: controller.close);
  }

  if (isBroadcast) {
    controller = StreamController<TOutput>.broadcast(
        onListen: onListen,
        onCancel: () => subscription.cancel(),
        sync: true);
  } else {
    controller = StreamController<TOutput>(
        onListen: onListen,
        onPause: () => subscription.pause(),
        onResume: () => subscription.resume(),
        onCancel: () => subscription.cancel(),
        sync: true);
  }

  return controller.stream;
}