ReactiveX / RxCpp

Reactive Extensions for C++
Apache License 2.0
3.03k stars 390 forks source link

Throttle missing #501

Open wagcampbell opened 5 years ago

wagcampbell commented 5 years ago

The current implementation is missing the throttle operator.

The closest I could find is the debounce operator. However, one difference is that the debounce operator will emit the last item in a series of items while throttle will emit the first.

Is there an equivalent operator or combination that will produce the same result?

kirkshoop commented 5 years ago

yes, throttle should be added.

it may be possible to use window_trigger, first, debounce to get the first item instead of the last. something like this:

auto throttle = [](auto time){
  return [time](auto in){
    auto s = in.publish().ref_count();
    return s.
      window_trigger(s.debounce(time)).
      transform([](auto w){return w.first();})).
      switch_on_next();
  };
};
shtaif commented 5 years ago

Very nice!

I wonder how one could make such custom throttle operator pipable as the other built-in operators?

auto sub = rxcpp::observable<>::interval(std::chrono::steady_clock::now(), std::chrono::milliseconds(100), rxcpp::observe_on_new_thread())
    | throttle(std::chrono::milliseconds(400))
    | subscribe<long>([](...) { /* ... */ });

Thanks! :)