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

Function main

src/examples/rpp/doxygen/delay.cpp:10–50  ·  view source on GitHub ↗

* @example delay.cpp **/

Source from the content-addressed store, hash-verified

8 * @example delay.cpp
9 **/
10int main() // NOLINT(bugprone-exception-escape)
11{
12 //! [delay]
13
14 auto start = rpp::schedulers::clock_type::now();
15
16 rpp::source::create<int>([&start](const auto& obs) {
17 for (int i = 0; i < 3; ++i)
18 {
19 auto emitting_time = rpp::schedulers::clock_type::now();
20 std::cout << "emit " << i << " in thread{" << std::this_thread::get_id() << "} duration since start " << std::chrono::duration_cast<std::chrono::seconds>(emitting_time - start).count() << "s" << std::endl;
21
22 obs.on_next(i);
23 std::this_thread::sleep_for(std::chrono::seconds{1});
24 }
25 auto emitting_time = rpp::schedulers::clock_type::now();
26 std::cout << "emit error in thread{" << std::this_thread::get_id() << "} duration since start " << std::chrono::duration_cast<std::chrono::seconds>(emitting_time - start).count() << "s" << std::endl;
27 obs.on_error({});
28 })
29 | rpp::operators::delay(std::chrono::seconds{3}, rpp::schedulers::new_thread{})
30 | rpp::operators::as_blocking()
31 | rpp::operators::subscribe([&](int v) {
32 auto observing_time = rpp::schedulers::clock_type::now();
33 std::cout << "observe " << v << " in thread{" << std::this_thread::get_id() << "} duration since start " << std::chrono::duration_cast<std::chrono::seconds>(observing_time - start).count() <<"s" << std::endl; },
34 [&](const std::exception_ptr&) {
35 auto observing_time = rpp::schedulers::clock_type::now();
36 std::cout << "observe error in thread{" << std::this_thread::get_id() << "} duration since start " << std::chrono::duration_cast<std::chrono::seconds>(observing_time - start).count() << "s" << std::endl;
37 });
38
39 // Template for output:
40 // emit 0 in thread{139855196489600} duration since start 0s
41 // emit 1 in thread{139855196489600} duration since start 1s
42 // emit 2 in thread{139855196489600} duration since start 2s
43 // observe 0 in thread{139855196485184} duration since start 3s
44 // emit error in thread{139855196489600} duration since start 3s
45 // observe 1 in thread{139855196485184} duration since start 4s
46 // observe 2 in thread{139855196485184} duration since start 5s
47 // observe error in thread{139855196485184} duration since start 6s
48 //! [delay]
49 return 0;
50}

Callers

nothing calls this directly

Calls 6

nowFunction · 0.85
delayFunction · 0.85
as_blockingFunction · 0.85
subscribeFunction · 0.85
on_nextMethod · 0.45
on_errorMethod · 0.45

Tested by

no test coverage detected