Streams are equivalent to the observed .Throttle?

Are there threads equivalent to Observable.Throttle ? If not, is there a reasonably elegant way to achieve this effect?

+4
source share
3 answers

rate_limit package provides throttling and debouncing of threads.

+2
source

At the moment, there is no such method in streams. A request for a raise was requested, you can send a question 8492 .

However, you can do this with the where method. In the following example, I defined the ThrottleFilter class to ignore events for a given duration:

 import 'dart:async'; class ThrottleFilter<T> { DateTime lastEventDateTime = null; final Duration duration; ThrottleFilter(this.duration); bool call(T e) { final now = new DateTime.now(); if (lastEventDateTime == null || now.difference(lastEventDateTime) > duration) { lastEventDateTime = now; return true; } return false; } } main() { final sc = new StreamController<int>(); final stream = sc.stream; // filter stream with ThrottleFilter stream.where(new ThrottleFilter<int>(const Duration(seconds: 10)).call) .listen(print); // send ints to stream every second, but ThrottleFilter will give only one int // every 10 sec. int i = 0; new Timer.repeating(const Duration(seconds:1), (t) { sc.add(i++); }); } 
+3
source

The next version is closer to Observable.Throttle:

 class Throttle extends StreamEventTransformer { final duration; Timer lastTimer; Throttle(millis) : duration = new Duration(milliseconds : millis); void handleData(event, EventSink<int> sink) { if(lastTimer != null){ lastTimer.cancel(); } lastTimer = new Timer(duration, () => sink.add(event)); } } main(){ //... stream.transform(new Throttle(500)).listen((_) => print(_)); //.. } 
+1
source

All Articles