Skip to content

throttleTime repeats the event if there was only one in the window #817

Description

@alexeyinkin
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:

1

Actual:

1
1

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions