forked from ReactiveX/RxCpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
operators: Add ref_count(other) operator overload.
The existing `connectable_observable.ref_count()` operator calls connect on the source when it's subscribed to. Generalize this by allowing an optional parameter `other`, i.e. `observable.ref_count(connectable_observable other)` to be used as the connect target. Useful for implementing diamond graphs while retaining composability: ``` A / \ B C \ / D | E auto A = ... | publish(); auto B = A | ...; auto C = A | ...; auto D = B | merge(C) | ref_count(A); auto E = D | ...; E | subscribe(...); ``` Resolves: ReactiveX#484
- Loading branch information
Showing
7 changed files
with
393 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "rxcpp/rx.hpp" | ||
|
||
#include "rxcpp/rx-test.hpp" | ||
#include "catch.hpp" | ||
|
||
#include <array> | ||
|
||
SCENARIO("ref_count other diamond sample"){ | ||
printf("//! [ref_count other diamond sample]\n"); | ||
|
||
/* | ||
* Implements the following diamond graph chain with publish+ref_count without using threads. | ||
* This version is composable because it does not use connect explicitly. | ||
* | ||
* Values | ||
* / \ | ||
* *2 *100 | ||
* \ / | ||
* Merge | ||
* | | ||
* RefCount | ||
*/ | ||
|
||
std::array<double, 5> a={{1.0, 2.0, 3.0, 4.0, 5.0}}; | ||
auto values = rxcpp::observable<>::iterate(a) | ||
// The root of the chain is only subscribed to once. | ||
.tap([](double v) { printf("[0] OnNext: %lf\n", v); }) | ||
.publish(); | ||
|
||
auto values_to_long = values.map([](double v) { return (long) v; }); | ||
|
||
// Left side multiplies by 2. | ||
auto left = values_to_long.map( | ||
[](long v) -> long {printf("[1] OnNext: %ld -> %ld\n", v, v*2); return v * 2L;} ); | ||
|
||
// Right side multiplies by 100. | ||
auto right = values_to_long.map( | ||
[](long v) -> long {printf("[2] OnNext: %ld -> %ld\n", v, v*100); return v * 100L; }); | ||
|
||
// Merge the left,right sides together. | ||
// The items are emitted interleaved ... [left1, right1, left2, right2, left3, right3, ...]. | ||
auto merged = left.merge(right); | ||
|
||
// When this value is subscribed to, it calls connect on values. | ||
auto connect_on_subscribe = merged.ref_count(values); | ||
|
||
// This immediately starts emitting all values and blocks until they are completed. | ||
connect_on_subscribe.subscribe( | ||
[](long v) { printf("[3] OnNext: %ld\n", v); }, | ||
[&]() { printf("[3] OnCompleted:\n"); }); | ||
|
||
printf("//! [ref_count other diamond sample]\n"); | ||
} | ||
|
||
// see also examples/doxygen/publish.cpp for non-ref_count diamonds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.