1 #include "rxcpp/rx.hpp" 2 namespace rxu=rxcpp::util; 3 4 #include "rxcpp/rx-test.hpp" 5 #include "catch.hpp" 6 7 #include <sstream> 8 9 SCENARIO("merge_delay_error sample"){ 10 printf("//! [merge_delay_error sample]\n"); __anon87c160b80102(int) 11 auto o1 = rxcpp::observable<>::timer(std::chrono::milliseconds(15)).map([](int) {return 1;}); 12 auto o2 = rxcpp::observable<>::error<int>(std::runtime_error("Error from source\n")); __anon87c160b80202(int) 13 auto o3 = rxcpp::observable<>::timer(std::chrono::milliseconds(5)).map([](int) {return 3;}); 14 auto values = o1.merge_delay_error(o2, o3); 15 values. 16 subscribe( __anon87c160b80302(int v)17 [](int v){printf("OnNext: %d\n", v);}, __anon87c160b80402(std::exception_ptr eptr) 18 [](std::exception_ptr eptr) { printf("OnError %s\n", rxu::what(eptr).c_str()); }, __anon87c160b80502()19 [](){printf("OnCompleted\n");}); 20 printf("//! [merge_delay_error sample]\n"); 21 } 22 23 SCENARIO("implicit merge_delay_error sample"){ 24 printf("//! [implicit merge_delay_error sample]\n"); __anon87c160b80602(int) 25 auto o1 = rxcpp::observable<>::timer(std::chrono::milliseconds(15)).map([](int) {return 1;}); 26 auto o2 = rxcpp::observable<>::error<int>(std::runtime_error("Error from source\n")); __anon87c160b80702(int) 27 auto o3 = rxcpp::observable<>::timer(std::chrono::milliseconds(5)).map([](int) {return 3;}); 28 auto base = rxcpp::observable<>::from(o1.as_dynamic(), o2, o3); 29 auto values = base.merge(); 30 values. 31 subscribe( __anon87c160b80802(int v)32 [](int v){printf("OnNext: %d\n", v);}, __anon87c160b80902(std::exception_ptr eptr) 33 [](std::exception_ptr eptr) { printf("OnError %s\n", rxu::what(eptr).c_str()); }, __anon87c160b80a02()34 [](){printf("OnCompleted\n");}); 35 printf("//! [implicit merge_delay_error sample]\n"); 36 } 37 38 #include "main.hpp" 39 40 SCENARIO("threaded merge_delay_error sample"){ 41 printf("//! [threaded merge_delay_error sample]\n"); 42 printf("[thread %s] Start task\n", get_pid().c_str()); __anon87c160b80b02(long) 43 auto o1 = rxcpp::observable<>::timer(std::chrono::milliseconds(10)).map([](long) -> long { 44 printf("[thread %s] Timer1 fired\n", get_pid().c_str()); 45 return 1; 46 }); __anon87c160b80c02(long) 47 auto o2 = rxcpp::observable<>::timer(std::chrono::milliseconds(20)).flat_map([](long) -> rxcpp::observable<long> { 48 std::stringstream ss; 49 ss << "[thread " << get_pid().c_str() << "] Timer2 failed\n"; 50 printf("%s\n", ss.str().c_str()); 51 ss.str(std::string()); 52 ss << "(Error from thread: " << get_pid().c_str() << ")\n"; 53 return rxcpp::observable<>::error<long>(std::runtime_error(ss.str())); 54 }); __anon87c160b80d02(long) 55 auto o3 = rxcpp::observable<>::timer(std::chrono::milliseconds(30)).map([](long) -> long { 56 printf("[thread %s] Timer3 fired\n", get_pid().c_str()); 57 return 3; 58 }); 59 auto values = o1.merge(rxcpp::observe_on_new_thread(), o2, o3); 60 values. 61 as_blocking(). 62 subscribe( __anon87c160b80e02(int v)63 [](int v){printf("[thread %s] OnNext: %d\n", get_pid().c_str(), v);}, __anon87c160b80f02(std::exception_ptr eptr) 64 [](std::exception_ptr eptr) { printf("[thread %s] OnError %s\n", get_pid().c_str(), rxu::what(eptr).c_str()); }, __anon87c160b81002()65 [](){printf("[thread %s] OnCompleted\n", get_pid().c_str());}); 66 printf("[thread %s] Finish task\n", get_pid().c_str()); 67 printf("//! [threaded merge_delay_error sample]\n"); 68 } 69 70 SCENARIO("threaded implicit merge_delay_error sample"){ 71 printf("//! [threaded implicit merge_delay_error sample]\n"); 72 printf("[thread %s] Start task\n", get_pid().c_str()); __anon87c160b81102(long) 73 auto o1 = rxcpp::observable<>::timer(std::chrono::milliseconds(10)).map([](long) -> long { 74 printf("[thread %s] Timer1 fired\n", get_pid().c_str()); 75 return 1; 76 }); __anon87c160b81202(long) 77 auto o2 = rxcpp::observable<>::timer(std::chrono::milliseconds(20)).flat_map([](long) -> rxcpp::observable<long> { 78 std::stringstream ss; 79 ss << "[thread " << get_pid().c_str() << "] Timer2 failed\n"; 80 printf("%s\n", ss.str().c_str()); 81 ss.str(std::string()); 82 ss << "(Error from thread: " << get_pid().c_str() << ")\n"; 83 return rxcpp::observable<>::error<long>(std::runtime_error(ss.str())); 84 }); __anon87c160b81302(long) 85 auto o3 = rxcpp::observable<>::timer(std::chrono::milliseconds(30)).map([](long) -> long { 86 printf("[thread %s] Timer3 fired\n", get_pid().c_str()); 87 return 3; 88 }); 89 auto base = rxcpp::observable<>::from(o1.as_dynamic(), o2, o3); 90 auto values = base.merge(rxcpp::observe_on_new_thread()); 91 values. 92 as_blocking(). 93 subscribe( __anon87c160b81402(long v)94 [](long v){printf("[thread %s] OnNext: %ld\n", get_pid().c_str(), v);}, __anon87c160b81502(std::exception_ptr eptr) 95 [](std::exception_ptr eptr) { printf("[thread %s] OnError %s\n", get_pid().c_str(), rxu::what(eptr).c_str()); }, __anon87c160b81602()96 [](){printf("[thread %s] OnCompleted\n", get_pid().c_str());}); 97 printf("[thread %s] Finish task\n", get_pid().c_str()); 98 printf("//! [threaded implicit merge_delay_error sample]\n"); 99 } 100