debounce method
Implementation
Stream<T> debounce(Duration duration) {
StreamController<T>? controller;
Timer? timer;
StreamSubscription<T>? subscription;
controller = StreamController<T>(
onListen: () {
subscription = listen(
(event) {
timer?.cancel();
timer = Timer(duration, () => controller!.add(event));
},
onError: controller!.addError,
onDone: () {
timer?.cancel();
controller!.close();
},
);
},
onCancel: () {
timer?.cancel();
subscription?.cancel();
},
);
return controller.stream;
}