Giter Site home page Giter Site logo

Comments (7)

therealkenc avatar therealkenc commented on May 24, 2024 1

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.

victimsnino avatar victimsnino commented on May 24, 2024 1

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.

victimsnino avatar victimsnino commented on May 24, 2024 1

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.

victimsnino avatar victimsnino commented on May 24, 2024

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.

therealkenc avatar therealkenc commented on May 24, 2024

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.

victimsnino avatar victimsnino commented on May 24, 2024

Yeah, looks interesting.. have to check it later

from reactiveplusplus.

victimsnino avatar victimsnino commented on May 24, 2024

Made attemp to fix.

from reactiveplusplus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.