summaryrefslogtreecommitdiff
path: root/Rx/v2/examples/doxygen/tap.cpp
blob: ad9d4e460e4511110ee47e41c6ea1f62ba67556b (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
#include "rxcpp/rx.hpp"
namespace rxu=rxcpp::util;

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

SCENARIO("tap sample"){
    printf("//! [tap sample]\n");
    auto values = rxcpp::observable<>::range(1, 3).
        tap(
			[](int v){printf("Tap -       OnNext: %d\n", v);},
            [](){printf("Tap -       OnCompleted\n");});
    values.
        subscribe(
            [](int v){printf("Subscribe - OnNext: %d\n", v);},
            [](){printf("Subscribe - OnCompleted\n");});
    printf("//! [tap sample]\n");
}

SCENARIO("error tap sample"){
    printf("//! [error tap sample]\n");
    auto values = rxcpp::observable<>::range(1, 3).
        concat(rxcpp::observable<>::error<int>(std::runtime_error("Error from source"))).
        tap(
            [](int v){printf("Tap -       OnNext: %d\n", v);},
            [](std::exception_ptr ep){
                printf("Tap -       OnError: %s\n", rxu::what(ep).c_str());
            },
            [](){printf("Tap -       OnCompleted\n");});
    values.
        subscribe(
            [](int v){printf("Subscribe - OnNext: %d\n", v);},
            [](std::exception_ptr ep){
                printf("Subscribe - OnError: %s\n", rxu::what(ep).c_str());
            },
            [](){printf("Subscribe - OnCompleted\n");});
    printf("//! [error tap sample]\n");
}