ReactiveX / RxCpp

Reactive Extensions for C++
Apache License 2.0
3.05k stars 395 forks source link

Question: Implement observable concept for custom types? #435

Closed jupp0r closed 6 years ago

jupp0r commented 6 years ago

Hi,

I'm writing a library that I want to return observables. However, I'd like this library to not depend on RxCpp, but rather return my own observable types that implement the observable concept. Users of my library could then choose to use rxcpp to perform operations on these value streams or choose other ways of interacting with them (provided by my custom types).

From my understanding of this library, this should be feasible, but I couldn't find any documentation of what interface an observable needs to provide, except the ones described for RxCpp3 at https://kirkshoop.github.io/cppcon2016/#34

  1. Is this a reasonable (albeit more complicated) approach?
  2. Do you consider the Observable concept/interface a public API of this library/does it change often?
  3. Can you point me to any documentation or code showing methods/types I'd have to implement?
kirkshoop commented 6 years ago

Hi!

I like this idea. It should be possible and any issues encountered I would consider important to fix.

This version of rxcpp has a fixed api for observable, observer, subscription and subscriber. The next version will be a rewrite that depends on a more recent version of the C++ standard.

Rxcpp3 is probably not the interface to target. I would recommend rxcpp v2 which is in this repo.

observable only needs a type that has a subscribe method that takes a subscriber and returns a subscription.

subscription observable::subscribe(observer);

the subscription only needs to provide the methods unsubscribe and is_subscribed

void subscription::unsubscribe(); bool subscription::is_subscribed();

the subscriber is a subscription and an observer it has the union of their methods.

the observer has three methods; on_next, on_error and on_completed

void observer::on_next(T); void observer::on_error(std::exception_ptr); void observer::on_completed();

Then there needs to be a small rxcpp specific library to stitch these new types with rxcpp. something like

rxcpp::observable<T> observable_from(observable<T>) subscriber<T> subscriber_from(rxcpp::subscriber<T>)

I hope this is enough to get you started!

jupp0r commented 6 years ago

Thanks for your answer, it looks much easier than I anticipated. I'll let you know how it goes.

It would actually be nice if rxcpp provided a templated implementation of

rxcpp::observable<T> observable_from(observable<T>) subscriber<T> subscriber_from(rxcpp::subscriber<T>)