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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
#include "rxcpp/rx.hpp"
#include "rxcpp/rx-test.hpp"
#include "catch.hpp"
SCENARIO("subscribe by subscriber"){
printf("//! [subscribe by subscriber]\n");
auto subscriber = rxcpp::make_subscriber<int>(
[](int v){printf("OnNext: %d\n", v);},
[](){printf("OnCompleted\n");});
auto values = rxcpp::observable<>::range(1, 3);
values.subscribe(subscriber);
printf("//! [subscribe by subscriber]\n");
}
SCENARIO("subscribe by observer"){
printf("//! [subscribe by observer]\n");
auto subscriber = rxcpp::make_subscriber<int>(
[](int v){printf("OnNext: %d\n", v);},
[](){printf("OnCompleted\n");});
auto values1 = rxcpp::observable<>::range(1, 3);
auto values2 = rxcpp::observable<>::range(4, 6);
values1.subscribe(subscriber.get_observer());
values2.subscribe(subscriber.get_observer());
printf("//! [subscribe by observer]\n");
}
SCENARIO("subscribe by on_next"){
printf("//! [subscribe by on_next]\n");
auto values = rxcpp::observable<>::range(1, 3);
values.subscribe(
[](int v){printf("OnNext: %d\n", v);});
printf("//! [subscribe by on_next]\n");
}
SCENARIO("subscribe by on_next and on_error"){
printf("//! [subscribe by on_next and on_error]\n");
auto values = rxcpp::observable<>::range(1, 3).
concat(rxcpp::observable<>::error<int>(std::runtime_error("Error from source")));
values.subscribe(
[](int v){printf("OnNext: %d\n", v);},
[](std::exception_ptr ep){
try {std::rethrow_exception(ep);}
catch (const std::exception& ex) {
printf("OnError: %s\n", ex.what());
}
});
printf("//! [subscribe by on_next and on_error]\n");
}
SCENARIO("subscribe by on_next and on_completed"){
printf("//! [subscribe by on_next and on_completed]\n");
auto values = rxcpp::observable<>::range(1, 3);
values.subscribe(
[](int v){printf("OnNext: %d\n", v);},
[](){printf("OnCompleted\n");});
printf("//! [subscribe by on_next and on_completed]\n");
}
SCENARIO("subscribe by subscription, on_next, and on_completed"){
printf("//! [subscribe by subscription, on_next, and on_completed]\n");
auto subscription = rxcpp::composite_subscription();
auto values = rxcpp::observable<>::range(1, 5);
values.subscribe(
subscription,
[&subscription](int v){
printf("OnNext: %d\n", v);
if (v == 3)
subscription.unsubscribe();
},
[](){printf("OnCompleted\n");});
printf("//! [subscribe by subscription, on_next, and on_completed]\n");
}
SCENARIO("subscribe by on_next, on_error, and on_completed"){
printf("//! [subscribe by on_next, on_error, and on_completed]\n");
auto values = rxcpp::observable<>::range(1, 3).
concat(rxcpp::observable<>::error<int>(std::runtime_error("Error from source")));
values.subscribe(
[](int v){printf("OnNext: %d\n", v);},
[](std::exception_ptr ep){
try {std::rethrow_exception(ep);}
catch (const std::exception& ex) {
printf("OnError: %s\n", ex.what());
}
},
[](){printf("OnCompleted\n");});
printf("//! [subscribe by on_next, on_error, and on_completed]\n");
}
SCENARIO("subscribe unsubscribe"){
printf("//! [subscribe unsubscribe]\n");
auto values = rxcpp::observable<>::range(1, 3).
concat(rxcpp::observable<>::never<int>()).
finally([](){printf("The final action\n");});
auto subscription = values.subscribe(
[](int v){printf("OnNext: %d\n", v);},
[](){printf("OnCompleted\n");});
subscription.unsubscribe();
printf("//! [subscribe unsubscribe]\n");
}
|