Comments (7)
Thank-you so much for taking a look. I can give you a void*
context on the api -- I was trying to make the example simpler by omitting but I think doing that obscured rather than simplified. Using a global publish_subject
is kind of like a new()
with only one slot.
Here is what I have:
https://rpp.godbolt.org/z/9MxsYhGhK
Crux:
auto create_observable() {
auto onstring = [](const void* ctx, const char* s) {
subject_from_ctx(ctx)->get_observer().on_next(s);
};
auto ondone = [](const void* ctx) {
auto subject = subject_from_ctx(ctx);
subject->get_observer().on_completed();
delete subject; // <--- this I don't love
};
auto subject = new rpp::subjects::publish_subject<const char*>();
do_async_thing(subject, onstring, ondone);
return subject->get_observable();
}
It is so "awesome" that it causes clang to crash. GCC will eat it.
The crash is bad enough, but from a pattern standpoint, creating a new subject just so I can delete it feels like I am doing it wong.
from reactiveplusplus.
In this case you can do it even much easier
https://rpp.godbolt.org/z/jcjz4K5xP
In short: we are doing raw detached copy of original observer instead of providing extra indirection via subject
But there is difference in behavior:
- you are starting
do_async_thing
for each new observer instead of "sharing" same do_async_thing for observers using same observable. - To achieve previous behavior you can add
| rpp::operators::publish() | rpp::operators::ref_count()
- actually it works in same way, but more elegant - it makes subscription to original observable during first ever subscription and then share this observable for any future subscriptions. - if you still need to start do_async_thing even before first ever subscription ,then you can call
.connect()
on result of| rpp::operators::publish
(it returns connectable observable)
So, i mean this one:
const auto connectable = observable | rpp::ops::publish();
connectable.connect(); // there actually happens subscription of "nothing" to your observable and it starts do_async_thing
// then subscribe as regular
connectable.subscribe(....);
or
const auto resulting_observable = observable | rpp::ops::publish() | rpp::ops::ref_count();
resulting_observable.subscribe(....); // this one initiates do_async_thing
resulting_observable.subscribe(....); // this one just connects to previous do_async_thing
BTW: looks like clang is crashing due to using latches in unexpected way
from reactiveplusplus.
Fixed on godbolt
https://rpp.godbolt.org/z/5jrGcq4os
(compilation results of exactly same source are cached, so, have to modify it in anyway - atleast add space at any place)
from reactiveplusplus.
Hi!
Yeah, pretty interesting task. Due to c-style function pointer is stateless (i mean, we can't pass lambda with some capture) and it doesn't provide extra user_object
(for example, void(const char* s, void* user_object)
) to be used to have some state inside, there is only one possible thing - use global variables/functions. Like this:
https://rpp.godbolt.org/z/5azdnh67h
There are 2 options, not sure which one is more appropriate for your needs. First one looks much more straightforward, second one is more "reactive" (call to do_async_thing
happens only after first ever subscription)
If it is not suitable solution, feel free to provide some additional details/requirements =)
from reactiveplusplus.
Thank you! I can work with this.
BTW: looks like clang is crashing due to using latches in unexpected way
Interesting, I did not expect that was the problem. Is capturing not allowed (or not well formed)? Using your setup:
std::string huh = "huh";
create_observable()
| rpp::ops::as_blocking()
| rpp::ops::subscribe(
[](const std::string& s) {
std::cout << "got: " << s << std::endl;
},
[=]() {
std::cout << huh << " completed" << std::endl;
});
std::cout << "and we're done" << std::endl;
This takes down clang
too, though I can't say I understand why.
from reactiveplusplus.
Yeah, looks interesting.. have to check it later
from reactiveplusplus.
Made attemp to fix.
from reactiveplusplus.
Related Issues (20)
- Add buffer_with_time operator
- Consider pushing builds to NUGet HOT 1
- RPP v2 - Implementation Status
- Two broken links HOT 1
- Feature request for "Single-Observable" HOT 2
- Create another v1 release HOT 8
- Resolve "nano sleeps" HOT 5
- Provide in-place manipulation of stateful subjects (behaviour subject) HOT 6
- Throttle operator HOT 3
- Publish subject fails compilation on GCC 13 HOT 2
- Limit memory growing for window/concat/merge operators
- Reemit from subscription HOT 3
- How to refer to observable type anyway? HOT 2
- Fix timeouting example from godbolt HOT 1
- Please update RPP in conan center HOT 3
- libuv / uvw integration HOT 10
- Add behavior_subject
- What is the best way to add support for Unreal Engine's TMulticastDelegate as an observable? HOT 4
- Add get_value method for behavior_subject
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reactiveplusplus.