in lib/src/rate_limit.dart [136:172]
Stream<T> _throttleTrailing(Duration duration) {
Timer? timer;
T? pending;
var hasPending = false;
var isDone = false;
return transformByHandlers(onData: (data, sink) {
void onTimer() {
if (hasPending) {
sink.add(pending as T);
if (isDone) {
sink.close();
} else {
timer = Timer(duration, onTimer);
hasPending = false;
pending = null;
}
} else {
timer = null;
}
}
if (timer == null) {
sink.add(data);
timer = Timer(duration, onTimer);
} else {
hasPending = true;
pending = data;
}
}, onDone: (sink) {
isDone = true;
if (hasPending) return; // Will be closed by timer.
sink.close();
timer?.cancel();
timer = null;
});
}