import 'dart:async';
import 'package:rxdart/rxdart.dart';
Future<void> main() async {
final controller = StreamController();
controller.stream
.throttleTime(Duration(seconds: 1), leading: true, trailing: true)
.listen((e) {
print(e);
});
controller.add(1);
await Future.delayed(Duration(seconds: 2));
}
Expected:
Actual:
The doc is wrong because it says "instead" and contradicts the first line:
|
/// Emits a value from the source [Stream], then ignores subsequent source values |
|
/// for a duration, then repeats this process. |
|
/// |
|
/// If leading is true, then the first item in each window is emitted. |
|
/// If [trailing] is true, then the last item is emitted instead. |
|
/// |
|
/// ### Example |
|
/// |
|
/// Stream.fromIterable([1, 2, 3]) |
|
/// .throttleTime(Duration(seconds: 1)); |
|
Stream<T> throttleTime(Duration duration, |
But I don't see a reason for the documented behavior. By allowing both leading and trailing to be true, the interface is misleading. If trailing was meant to suppress the leading, it had to be an enum with leading and trailing as values.
leading must take priority and emit the first event right away. And then trailing should emit the last event only if something else came after that.
If this is not what this transformer is supposed to do, we need another one that actually does that because it's a common need.
Expected:
Actual:
The doc is wrong because it says "instead" and contradicts the first line:
rxdart/packages/rxdart/lib/src/transformers/backpressure/throttle.dart
Lines 62 to 72 in d1f3ba6
But I don't see a reason for the documented behavior. By allowing both
leadingandtrailingto be true, the interface is misleading. Iftrailingwas meant to suppress the leading, it had to be an enum withleadingandtrailingas values.leadingmust take priority and emit the first event right away. And thentrailingshould emit the last event only if something else came after that.If this is not what this transformer is supposed to do, we need another one that actually does that because it's a common need.