DEV Community

francesco agati
francesco agati

Posted on

Dart Streams with RxDart: Debounce, Throttle, and Distinct

Dart, a versatile language for building applications, offers powerful tools for managing asynchronous data streams. When coupled with rxdart, a reactive programming library for Dart, developers can leverage advanced stream handling techniques like debouncing, throttling, and distinct emission to efficiently manage data flow.

Understanding Reactive Streams

In reactive programming, streams are a fundamental concept used to handle sequences of asynchronous events or data. Each event in a stream represents a discrete piece of data. Dart provides robust support for streams through its dart:async library, and rxdart extends this capability with additional operators and utilities.

Key Concepts and Operators

Let's delve into the code snippet to explore how rxdart can enhance stream processing:

import 'dart:async';
import 'package:rxdart/rxdart.dart';

void main() {
  // Create a BehaviorSubject for the text input
  final textSubject = BehaviorSubject<String>();

  // Debounce: Emit only after a specified duration of silence
  final debouncedStream = textSubject.stream.debounceTime(Duration(milliseconds: 300));

  // Throttle: Emit at most one value per specified duration
  final throttledStream = textSubject.stream.throttleTime(Duration(milliseconds: 300));

  // Skip duplicates: Emit only if the value is different from the previous one
  final distinctStream = textSubject.stream.distinct();

  // Listen to the debounced stream
  debouncedStream.listen((value) {
    print('Debounced: $value');
  });

  // Listen to the throttled stream
  throttledStream.listen((value) {
    print('Throttled: $value');
  });

  // Listen to the distinct stream
  distinctStream.listen((value) {
    print('Distinct: $value');
  });

  // Simulate text input
  simulateTextInput(textSubject);
}

void simulateTextInput(BehaviorSubject<String> subject) {
  const inputValues = [
    'hello',
    'hello',  // Duplicate value
    'hell',
    'hello world',
    'flutter',
    'flutter',  // Duplicate value
    'dart',
  ];

  // Emit values with a slight delay between them
  var delay = 0;
  for (final value in inputValues) {
    Future.delayed(Duration(milliseconds: delay), () {
      subject.add(value);
    });
    delay += 100;
  }

  // Close the subject after all values have been emitted
  Future.delayed(Duration(milliseconds: delay + 500), () {
    subject.close();
  });
}
Enter fullscreen mode Exit fullscreen mode

Detailed Explanation

  1. BehaviorSubject: This is a special type of StreamController from rxdart that allows subscribers to access the most recently emitted item and all subsequent items of the stream.

  2. Debounce: The debounceTime operator emits an item from the source stream only after a specified duration of silence (i.e., no new items emitted). In our example, debouncedStream will print 'Debounced' values after 300 milliseconds of inactivity.

  3. Throttle: In contrast, throttleTime ensures that at most one value is emitted per specified duration, ignoring values emitted in quick succession. Here, throttledStream will print 'Throttled' values every 300 milliseconds.

  4. Distinct: The distinct operator filters out consecutive duplicate values from the stream. The distinctStream in our example will print 'Distinct' values only when the current value differs from the previous one.

Simulating Text Input

The simulateTextInput function mimics user input by emitting predefined values to textSubject. Each value is added with a slight delay, simulating realistic user interaction.

Conclusion

By utilizing rxdart operators like debounceTime, throttleTime, and distinct, Dart developers can effectively manage and manipulate asynchronous data streams. These operators empower applications to handle user input, network responses, and other asynchronous events with precision and efficiency. Understanding and leveraging reactive programming concepts and tools like rxdart opens up new possibilities for creating responsive and reactive Dart applications.

Top comments (0)