summaryrefslogtreecommitdiff
path: root/Rx/v2/examples/doxygen/timestamp.cpp
blob: 2c394d35e6660e7d0632f76adaad9bba9b1411a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "rxcpp/rx.hpp"

#include "rxcpp/rx-test.hpp"
#include "catch.hpp"

SCENARIO("timestamp sample") {
    printf("//! [timestamp sample]\n");

    typedef rxcpp::schedulers::scheduler::clock_type::time_point time_point;

    using namespace std::chrono;
    auto values = rxcpp::observable<>::interval(milliseconds(100))
            .timestamp()
            .take(3);
    time_point start = rxcpp::identity_current_thread().now();
    values.
        subscribe(
            [&](std::pair<long, time_point> v) { 
                long long int ms = duration_cast<milliseconds>(v.second - start).count();
                printf("OnNext: %ld @%lldms\n", v.first, ms); 
            },
            [](std::exception_ptr ep) {
                try {
                    std::rethrow_exception(ep);
                } catch (const std::exception& ex) {
                    printf("OnError: %s\n", ex.what());
                }
            },
            []() { printf("OnCompleted\n"); });
    printf("//! [timestamp sample]\n");
}

SCENARIO("timestamp operator syntax sample") {
    using namespace rxcpp;
    using namespace rxcpp::sources;
    using namespace rxcpp::operators;
    using namespace std::chrono;

    typedef rxcpp::schedulers::scheduler::clock_type::time_point time_point;

    printf("//! [timestamp operator syntax sample]\n");
    auto values = interval(milliseconds(100))
                  | timestamp()
                  | take(3);
    time_point start = rxcpp::identity_current_thread().now();
    values.
            subscribe(
            [&](std::pair<long, time_point> v) { 
                long long int ms = duration_cast<milliseconds>(v.second - start).count();
                printf("OnNext: %ld @%lldms\n", v.first, ms); 
            },
            [](std::exception_ptr ep) {
                try {
                    std::rethrow_exception(ep);
                } catch (const std::exception& ex) {
                    printf("OnError: %s\n", ex.what());
                }
            },
            []() { printf("OnCompleted\n"); });
    printf("//! [timestamp operator syntax sample]\n");
}