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"); __anon404e2e960102(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")); __anon404e2e960202(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( __anon404e2e960302(int v)17 [](int v){printf("OnNext: %d\n", v);}, __anon404e2e960402(std::exception_ptr eptr) 18 [](std::exception_ptr eptr) { printf("OnError %s\n", rxu::what(eptr).c_str()); }, __anon404e2e960502()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"); __anon404e2e960602(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")); __anon404e2e960702(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( __anon404e2e960802(int v)32 [](int v){printf("OnNext: %d\n", v);}, __anon404e2e960902(std::exception_ptr eptr) 33 [](std::exception_ptr eptr) { printf("OnError %s\n", rxu::what(eptr).c_str()); }, __anon404e2e960a02()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()); __anon404e2e960b02(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 }); __anon404e2e960c02(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 }); __anon404e2e960d02(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( __anon404e2e960e02(int v)63 [](int v){printf("[thread %s] OnNext: %d\n", get_pid().c_str(), v);}, __anon404e2e960f02(std::exception_ptr eptr) 64 [](std::exception_ptr eptr) { printf("[thread %s] OnError %s\n", get_pid().c_str(), rxu::what(eptr).c_str()); }, __anon404e2e961002()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()); __anon404e2e961102(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 }); __anon404e2e961202(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 }); __anon404e2e961302(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( __anon404e2e961402(long v)94 [](long v){printf("[thread %s] OnNext: %ld\n", get_pid().c_str(), v);}, __anon404e2e961502(std::exception_ptr eptr) 95 [](std::exception_ptr eptr) { printf("[thread %s] OnError %s\n", get_pid().c_str(), rxu::what(eptr).c_str()); }, __anon404e2e961602()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