listenStreamWithInteractionCompleter<T> function

InteractionCompleter listenStreamWithInteractionCompleter<T>(
  1. Stream<T> stream,
  2. Duration triggerDelay,
  3. void onData(
    1. T? event
    )
)

Listen stream, calling onData only after triggerDelay duration.

onData is only called when stream event is triggered and stays without any new event for triggerDelay duration.

Implementation

InteractionCompleter listenStreamWithInteractionCompleter<T>(
    Stream<T> stream, Duration triggerDelay, void Function(T? event) onData) {
  var lastEvent = [];

  var interactionCompleter = InteractionCompleter(
      'listenStreamWithInteractionCompleter[$stream]',
      triggerDelay: triggerDelay, functionToTrigger: () {
    var event = lastEvent.isNotEmpty ? lastEvent.first : null;
    onData(event is T ? event : null);
  });

  stream.listen((Object? event) {
    lastEvent.clear();
    lastEvent.add(event);
    interactionCompleter.interact();
  });

  return interactionCompleter;
}