1 #include "rxcpp/rx.hpp" 2 3 #include "rxcpp/rx-test.hpp" 4 #include "catch.hpp" 5 6 SCENARIO("window count sample"){ 7 printf("//! [window count sample]\n"); 8 int counter = 0; 9 auto values = rxcpp::observable<>::range(1, 5).window(2); 10 values. 11 subscribe( __anon9b472a290102(rxcpp::observable<int> v)12 [&counter](rxcpp::observable<int> v){ 13 int id = counter++; 14 printf("[window %d] Create window\n", id); 15 v.subscribe( 16 [id](int v){printf("[window %d] OnNext: %d\n", id, v);}, 17 [id](){printf("[window %d] OnCompleted\n", id);}); 18 }); 19 printf("//! [window count sample]\n"); 20 } 21 22 SCENARIO("window count+skip sample"){ 23 printf("//! [window count+skip sample]\n"); 24 int counter = 0; 25 auto values = rxcpp::observable<>::range(1, 7).window(2, 3); 26 values. 27 subscribe( __anon9b472a290402(rxcpp::observable<int> v)28 [&counter](rxcpp::observable<int> v){ 29 int id = counter++; 30 printf("[window %d] Create window\n", id); 31 v.subscribe( 32 [id](int v){printf("[window %d] OnNext: %d\n", id, v);}, 33 [id](){printf("[window %d] OnCompleted\n", id);}); 34 }); 35 printf("//! [window count+skip sample]\n"); 36 } 37 38 SCENARIO("window period+skip+coordination sample"){ 39 printf("//! [window period+skip+coordination sample]\n"); 40 int counter = 0; 41 auto period = std::chrono::milliseconds(4); 42 auto skip = std::chrono::milliseconds(6); 43 auto values = rxcpp::observable<>::interval(std::chrono::steady_clock::now() + std::chrono::milliseconds(1), std::chrono::milliseconds(2)). 44 take(7). 45 window_with_time(period, skip, rxcpp::observe_on_new_thread()); 46 values. 47 as_blocking(). 48 subscribe( __anon9b472a290702(rxcpp::observable<long> v)49 [&counter](rxcpp::observable<long> v){ 50 int id = counter++; 51 printf("[window %d] Create window\n", id); 52 v.subscribe( 53 [id](long v){printf("[window %d] OnNext: %ld\n", id, v);}, 54 [id](){printf("[window %d] OnCompleted\n", id);}); 55 }); 56 printf("//! [window period+skip+coordination sample]\n"); 57 } 58 59 SCENARIO("window period+skip sample"){ 60 printf("//! [window period+skip sample]\n"); 61 int counter = 0; 62 auto period = std::chrono::milliseconds(4); 63 auto skip = std::chrono::milliseconds(6); 64 auto values = rxcpp::observable<>::interval(std::chrono::steady_clock::now() + std::chrono::milliseconds(1), std::chrono::milliseconds(2)). 65 take(7). 66 window_with_time(period, skip); 67 values. 68 subscribe( __anon9b472a290a02(rxcpp::observable<long> v)69 [&counter](rxcpp::observable<long> v){ 70 int id = counter++; 71 printf("[window %d] Create window\n", id); 72 v.subscribe( 73 [id](long v){printf("[window %d] OnNext: %ld\n", id, v);}, 74 [id](){printf("[window %d] OnCompleted\n", id);}); 75 }); 76 printf("//! [window period+skip sample]\n"); 77 } 78 79 SCENARIO("window period+skip overlapping sample"){ 80 printf("//! [window period+skip overlapping sample]\n"); 81 int counter = 0; 82 auto period = std::chrono::milliseconds(6); 83 auto skip = std::chrono::milliseconds(4); 84 auto values = rxcpp::observable<>::interval(std::chrono::steady_clock::now() + std::chrono::milliseconds(1), std::chrono::milliseconds(2)). 85 take(7). 86 window_with_time(period, skip); 87 values. 88 subscribe( __anon9b472a290d02(rxcpp::observable<long> v)89 [&counter](rxcpp::observable<long> v){ 90 int id = counter++; 91 printf("[window %d] Create window\n", id); 92 v.subscribe( 93 [id](long v){printf("[window %d] OnNext: %ld\n", id, v);}, 94 [id](){printf("[window %d] OnCompleted\n", id);}); 95 }); 96 printf("//! [window period+skip overlapping sample]\n"); 97 } 98 99 SCENARIO("window period+skip empty sample"){ 100 printf("//! [window period+skip empty sample]\n"); 101 int counter = 0; 102 auto period = std::chrono::milliseconds(2); 103 auto skip = std::chrono::milliseconds(4); 104 auto values = rxcpp::observable<>::timer(std::chrono::milliseconds(10)). 105 window_with_time(period, skip); 106 values. 107 subscribe( __anon9b472a291002(rxcpp::observable<long> v)108 [&counter](rxcpp::observable<long> v){ 109 int id = counter++; 110 printf("[window %d] Create window\n", id); 111 v.subscribe( 112 [id](long v){printf("[window %d] OnNext: %ld\n", id, v);}, 113 [id](){printf("[window %d] OnCompleted\n", id);}); 114 }); 115 printf("//! [window period+skip empty sample]\n"); 116 } 117 118 SCENARIO("window period+coordination sample"){ 119 printf("//! [window period+coordination sample]\n"); 120 int counter = 0; 121 auto values = rxcpp::observable<>::interval(std::chrono::steady_clock::now() + std::chrono::milliseconds(1), std::chrono::milliseconds(2)). 122 take(7). 123 window_with_time(std::chrono::milliseconds(4), rxcpp::observe_on_new_thread()); 124 values. 125 as_blocking(). 126 subscribe( __anon9b472a291302(rxcpp::observable<long> v)127 [&counter](rxcpp::observable<long> v){ 128 int id = counter++; 129 printf("[window %d] Create window\n", id); 130 v.subscribe( 131 [id](long v){printf("[window %d] OnNext: %ld\n", id, v);}, 132 [id](){printf("[window %d] OnCompleted\n", id);}); 133 }); 134 printf("//! [window period+coordination sample]\n"); 135 } 136 137 SCENARIO("window period sample"){ 138 printf("//! [window period sample]\n"); 139 int counter = 0; 140 auto values = rxcpp::observable<>::interval(std::chrono::steady_clock::now() + std::chrono::milliseconds(1), std::chrono::milliseconds(2)). 141 take(7). 142 window_with_time(std::chrono::milliseconds(4)); 143 values. 144 subscribe( __anon9b472a291602(rxcpp::observable<long> v)145 [&counter](rxcpp::observable<long> v){ 146 int id = counter++; 147 printf("[window %d] Create window\n", id); 148 v.subscribe( 149 [id](long v){printf("[window %d] OnNext: %ld\n", id, v);}, 150 [id](){printf("[window %d] OnCompleted\n", id);}); 151 }); 152 printf("//! [window period sample]\n"); 153 } 154 155 SCENARIO("window period+count+coordination sample"){ 156 printf("//! [window period+count+coordination sample]\n"); 157 int counter = 0; 158 auto int1 = rxcpp::observable<>::range(1L, 3L); 159 auto int2 = rxcpp::observable<>::timer(std::chrono::milliseconds(50)); 160 auto values = int1. 161 concat(int2). 162 window_with_time_or_count(std::chrono::milliseconds(20), 2, rxcpp::observe_on_event_loop()); 163 values. 164 as_blocking(). 165 subscribe( __anon9b472a291902(rxcpp::observable<long> v)166 [&counter](rxcpp::observable<long> v){ 167 int id = counter++; 168 printf("[window %d] Create window\n", id); 169 v.subscribe( 170 [id](long v){printf("[window %d] OnNext: %ld\n", id, v);}, 171 [id](){printf("[window %d] OnCompleted\n", id);}); 172 }); 173 printf("//! [window period+count+coordination sample]\n"); 174 } 175 176 SCENARIO("window period+count sample"){ 177 printf("//! [window period+count sample]\n"); 178 int counter = 0; 179 auto int1 = rxcpp::observable<>::range(1L, 3L); 180 auto int2 = rxcpp::observable<>::timer(std::chrono::milliseconds(50)); 181 auto values = int1. 182 concat(int2). 183 window_with_time_or_count(std::chrono::milliseconds(20), 2); 184 values. 185 subscribe( __anon9b472a291c02(rxcpp::observable<long> v)186 [&counter](rxcpp::observable<long> v){ 187 int id = counter++; 188 printf("[window %d] Create window\n", id); 189 v.subscribe( 190 [id](long v){printf("[window %d] OnNext: %ld\n", id, v);}, 191 [id](){printf("[window %d] OnCompleted\n", id);}); 192 }); 193 printf("//! [window period+count sample]\n"); 194 } 195 196 SCENARIO("window toggle+coordination sample"){ 197 printf("//! [window toggle+coordination sample]\n"); 198 int counter = 0; 199 auto values = rxcpp::observable<>::interval(std::chrono::steady_clock::now() + std::chrono::milliseconds(1), std::chrono::milliseconds(2)). 200 take(7). 201 window_toggle( 202 rxcpp::observable<>::interval(std::chrono::milliseconds(4)), __anon9b472a291f02(long)203 [](long){ 204 return rxcpp::observable<>::interval(std::chrono::milliseconds(4)).skip(1); 205 }, 206 rxcpp::observe_on_new_thread()); 207 values. 208 as_blocking(). 209 subscribe( __anon9b472a292002(rxcpp::observable<long> v)210 [&counter](rxcpp::observable<long> v){ 211 int id = counter++; 212 printf("[window %d] Create window\n", id); 213 v.subscribe( 214 [id](long v){printf("[window %d] OnNext: %ld\n", id, v);}, 215 [id](){printf("[window %d] OnCompleted\n", id);}); 216 }); 217 printf("//! [window toggle+coordination sample]\n"); 218 } 219 220 SCENARIO("window toggle sample"){ 221 printf("//! [window toggle sample]\n"); 222 int counter = 0; 223 auto values = rxcpp::observable<>::interval(std::chrono::steady_clock::now() + std::chrono::milliseconds(1), std::chrono::milliseconds(2)). 224 take(7). 225 window_toggle( 226 rxcpp::observable<>::interval(std::chrono::milliseconds(4)), __anon9b472a292302(long)227 [](long){ 228 return rxcpp::observable<>::interval(std::chrono::milliseconds(4)).skip(1); 229 }); 230 values. 231 subscribe( __anon9b472a292402(rxcpp::observable<long> v)232 [&counter](rxcpp::observable<long> v){ 233 int id = counter++; 234 printf("[window %d] Create window\n", id); 235 v.subscribe( 236 [id](long v){printf("[window %d] OnNext: %ld\n", id, v);}, 237 [id](){printf("[window %d] OnCompleted\n", id);}); 238 }); 239 printf("//! [window toggle sample]\n"); 240 } 241