Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1694)

Unified Diff: lib/src/transformers/debounce.dart

Issue 1648963002: Add reactive-inspired stream transformers: Base URL: https://github.com/dart-lang/async@master
Patch Set: Restructure failes and add more tests. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/src/transformers/debounce.dart
diff --git a/lib/src/transformers/debounce.dart b/lib/src/transformers/debounce.dart
new file mode 100644
index 0000000000000000000000000000000000000000..6f8d612ec0c44ede18e5b64ac99e0295b80e4c70
--- /dev/null
+++ b/lib/src/transformers/debounce.dart
@@ -0,0 +1,47 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import "dart:async";
+import "throttle.dart";
nweiz 2016/03/01 02:10:04 Nit: newline between these two imports.
Lasse Reichstein Nielsen 2016/03/01 16:51:17 Acknowledged.
+
+/// Drops any event that happens shortly after another event.
+///
+/// If an event happens within a certain [Duration] of the previous event,
+/// the new event is dropped from the result stream.
+/// The dropped event will still cause following events to be dropped
nweiz 2016/03/01 02:10:04 Nit: "will still cause" -> "still causes"
Lasse Reichstein Nielsen 2016/03/01 16:51:17 Acknowledged.
+/// until the entire duration has passed without any events.
+///
+/// This differs from [Throttle] which only restarts its timer at events
+/// that aren't dropped.
+///
+/// Any error in the source stream will be forwarded to the transformed stream
+/// and will terminate the transformation.
+class Debounce<S> implements StreamTransformer<S, S> {
+ final Duration _interval;
+ final Function _createStopwatch;
+
+ /// Drops events sooner than [interval] after any other event.
+ ///
+ /// A [createStopwatch] function can be provided to introduce a non-standard
+ /// [Stopwatch] implementation, e.g., for testing
nweiz 2016/03/01 02:10:03 Nit: "testing" -> "testing."
Lasse Reichstein Nielsen 2016/03/01 16:51:16 Acknowledged.
+ /// It defaults to using [StopWatch].
nweiz 2016/03/01 02:10:04 Nit: "StopWatch" -> "Stopwatch"
Lasse Reichstein Nielsen 2016/03/01 16:51:16 Acknowledged.
+ Debounce(Duration interval, {Stopwatch createStopwatch()})
nweiz 2016/03/01 02:10:04 Up to you, but I'd just use the clock package here
Lasse Reichstein Nielsen 2016/03/01 16:51:17 I don't really like introducing an abstraction lay
+ : _interval = interval,
+ _createStopwatch = createStopwatch;
+
+ Stream<S> bind(Stream<S> stream) async* {
+ var interval = Duration.ZERO; // Avoid dropping the first event.
+ var stopwatch =
+ (_createStopwatch == null) ? new Stopwatch() : _createStopwatch();
+ stopwatch.start() ;
+ await for (var source in stream) {
+ if (stopwatch.elapsed >= interval) {
+ yield source;
+ interval = _interval;
+ }
+ stopwatch.reset();
+ }
+ stopwatch.stop();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698