* @example throttle.cpp **/
| 6 | * @example throttle.cpp |
| 7 | **/ |
| 8 | int main() |
| 9 | { |
| 10 | //! [throttle] |
| 11 | auto start = rpp::schedulers::clock_type::now(); |
| 12 | rpp::source::just(rpp::schedulers::current_thread{}, 1, 2, 5, 6, 9, 10) |
| 13 | | rpp::operators::flat_map([](int v) { |
| 14 | return rpp::source::just(v) | rpp::operators::delay(std::chrono::milliseconds(500) * v, rpp::schedulers::current_thread{}); |
| 15 | }) |
| 16 | | rpp::operators::filter([&](int v) { |
| 17 | std::cout << "> Sent value " << v << " at " << std::chrono::duration_cast<std::chrono::milliseconds>(rpp::schedulers::clock_type::now() - start).count() << std::endl; |
| 18 | return true; |
| 19 | }) |
| 20 | | rpp::operators::throttle(std::chrono::milliseconds{700}) |
| 21 | | rpp::operators::subscribe([&](int v) { std::cout << ">>> new value " << v << " at " << std::chrono::duration_cast<std::chrono::milliseconds>(rpp::schedulers::clock_type::now() - start).count() << std::endl; }, |
| 22 | [](const std::exception_ptr&) {}, |
| 23 | [&]() { std::cout << ">>> completed at " << std::chrono::duration_cast<std::chrono::milliseconds>(rpp::schedulers::clock_type::now() - start).count() << std::endl; }); |
| 24 | |
| 25 | |
| 26 | // Output: |
| 27 | // > Sent value 1 at 500 |
| 28 | // >>> new value 1 at 500 |
| 29 | // > Sent value 2 at 1000 |
| 30 | // > Sent value 5 at 2500 |
| 31 | // >>> new value 5 at 2500 |
| 32 | // > Sent value 6 at 3000 |
| 33 | // > Sent value 9 at 4500 |
| 34 | // >>> new value 9 at 4500 |
| 35 | // > Sent value 10 at 5000 |
| 36 | // >>> completed at 5000 |
| 37 | //! [throttle] |
| 38 | return 0; |
| 39 | } |