MCPcopy Create free account
hub / github.com/AlexInLog/ReactivePlusPlus / main

Function main

src/examples/rpp/doxygen/debounce.cpp:8–39  ·  view source on GitHub ↗

* @example debounce.cpp **/

Source from the content-addressed store, hash-verified

6 * @example debounce.cpp
7 **/
8int main()
9{
10 //! [debounce]
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::debounce(std::chrono::milliseconds{700}, rpp::schedulers::current_thread{})
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 // > Sent value 2 at 1000
29 // >>> new value 2 at 1700
30 // > Sent value 5 at 2500
31 // > Sent value 6 at 3000
32 // >>> new value 6 at 3700
33 // > Sent value 9 at 4500
34 // > Sent value 10 at 5000
35 // >>> new value 10 at 5000
36 // >>> completed at 5000
37 //! [debounce]
38 return 0;
39}

Callers

nothing calls this directly

Calls 7

nowFunction · 0.85
justFunction · 0.85
flat_mapFunction · 0.85
delayFunction · 0.85
filterFunction · 0.85
debounceFunction · 0.85
subscribeFunction · 0.85

Tested by

no test coverage detected