listenStreamWithInteractionCompleter<T> function
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;
}