diff options
Diffstat (limited to 'Rx/v2/examples/doxygen/create.cpp')
-rw-r--r-- | Rx/v2/examples/doxygen/create.cpp | 51 |
1 files changed, 42 insertions, 9 deletions
diff --git a/Rx/v2/examples/doxygen/create.cpp b/Rx/v2/examples/doxygen/create.cpp index a9fa347..f8d024f 100644 --- a/Rx/v2/examples/doxygen/create.cpp +++ b/Rx/v2/examples/doxygen/create.cpp @@ -5,15 +5,17 @@ SCENARIO("Create sample"){ printf("//! [Create sample]\n"); - auto subscriber = rxcpp::make_subscriber<int>( - [&](int v){printf("OnNext: %d\n", v);}, - [](){printf("OnCompleted\n");}); - rxcpp::observable<>::create<int>( - [](const rxcpp::subscriber<int>& s){ + auto ints = rxcpp::observable<>::create<int>( + [](rxcpp::subscriber<int> s){ s.on_next(1); s.on_next(2); s.on_completed(); - }).subscribe(subscriber); + }); + + ints. + subscribe( + [](int v){printf("OnNext: %d\n", v);}, + [](){printf("OnCompleted\n");}); printf("//! [Create sample]\n"); } @@ -31,7 +33,7 @@ SCENARIO("Create bad code"){ printf("OnCompleted\n"); }); rxcpp::observable<>::create<int>( - [](const rxcpp::subscriber<int>& s){ + [](rxcpp::subscriber<int> s){ for (int i = 0; i < 5; ++i) { s.on_next(i); printf("Just sent: OnNext(%d)\n", i); @@ -56,10 +58,10 @@ SCENARIO("Create good code"){ printf("OnCompleted\n"); }); rxcpp::observable<>::create<int>( - [](const rxcpp::subscriber<int>& s){ + [](rxcpp::subscriber<int> s){ for (int i = 0; i < 5; ++i) { if (!s.is_subscribed()) // Stop emitting if nobody is listening - return; + break; s.on_next(i); printf("Just sent: OnNext(%d)\n", i); } @@ -68,3 +70,34 @@ SCENARIO("Create good code"){ }).subscribe(subscriber); printf("//! [Create good code]\n"); } + +SCENARIO("Create great code"){ + printf("//! [Create great code]\n"); + auto ints = rxcpp::observable<>::create<int>( + [](rxcpp::subscriber<int> s){ + for (int i = 0; i < 5; ++i) { + if (!s.is_subscribed()) // Stop emitting if nobody is listening + break; + s.on_next(i); + printf("Just sent: OnNext(%d)\n", i); + } + s.on_completed(); + printf("Just sent: OnCompleted()\n"); + }); + ints. + take(2). + 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("//! [Create great code]\n"); +} |